1 # -*- coding: utf-8 -*-
3 # This file is part of MIL/PEER, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
6 from django.contrib.auth.decorators import login_required
7 from django.contrib.auth.models import User
8 from django.shortcuts import render, redirect
9 from django.http import Http404
10 from .forms import OrganizationForm, UserCardForm
11 from .models import Organization, Membership, UserCard
16 if request.method == 'POST':
17 form = OrganizationForm(request.POST, request.FILES)
20 org.set_user_status(request.user, Membership.Status.OWNER)
21 return redirect(org.get_absolute_url())
23 form = OrganizationForm()
24 return render(request, 'organizations/new.html', {'form': form})
27 def main(request, pk, tab='documents'):
29 org = Organization.objects.get(pk=pk)
30 except Organization.DoesNotExist:
33 my_status = org.get_user_status(request.user)
34 am_owner = my_status == Membership.Status.OWNER
35 am_member = am_owner or my_status == Membership.Status.REGULAR
37 return render(request, 'organizations/main.html', {
40 'my_status': my_status,
42 'am_member': am_member,
46 def user_card(request, pk):
48 user = User.objects.get(pk=pk)
49 except User.DoesNotExist:
51 card, created = UserCard.objects.get_or_create(pk=user.pk)
52 return render(request, 'organizations/user_card.html', {
58 def edit(request, pk):
60 org = Organization.objects.get(pk=pk)
61 except Organization.DoesNotExist:
64 if request.method == 'POST':
65 form = OrganizationForm(request.POST, request.FILES, instance=org)
68 return redirect(org.get_absolute_url())
70 form = OrganizationForm(instance=org)
71 return render(request, 'organizations/edit.html', {'form': form})
75 def user_edit(request):
76 card, created = UserCard.objects.get_or_create(pk=request.user.pk)
77 if request.method == 'POST':
78 form = UserCardForm(request.POST, request.FILES, instance=card)
81 return redirect(card.get_absolute_url())
83 form = UserCardForm(instance=card)
84 return render(request, 'organizations/edit_user.html', {'form': form})
88 def join(request, pk):
90 org = Organization.objects.get(pk=pk)
91 except Organization.DoesNotExist:
94 if request.method == 'POST':
95 org.set_user_status(request.user, Membership.Status.PENDING)
96 return redirect('organizations_members', org.pk)
98 return render(request, 'organizations/join.html', {'org': org})
102 def membership(request, pk):
104 org = Organization.objects.get(pk=pk)
105 except Organization.DoesNotExist:
109 user = User.objects.get(pk=request.POST['user'])
110 except KeyError, User.DoesNotExist:
113 if user != request.user:
114 action = request.POST.get('action')
116 if action == 'remove':
118 org.set_user_status(user, action)
119 return redirect('organizations_members', org.pk)
122 def organizations(request):
123 return render(request, "organizations/organizations.html", {
124 'organizations': Organization.objects.all(),