update document summary on save/revert
[redakcja.git] / apps / organizations / views.py
1 # -*- coding: utf-8 -*-
2 #
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.
5 #
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
12
13
14 @login_required
15 def org_new(request):
16     if request.method == 'POST':
17         form = OrganizationForm(request.POST, request.FILES)
18         if form.is_valid():
19             org = form.save()
20             org.set_user_status(request.user, Membership.Status.OWNER)
21             return redirect(org.get_absolute_url())
22     else:
23         form = OrganizationForm()
24     return render(request, 'organizations/new.html', {'form': form})
25
26
27 def main(request, pk, tab='documents'):
28     try:
29         org = Organization.objects.get(pk=pk)
30     except Organization.DoesNotExist:
31         raise Http404
32
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
36
37     return render(request, 'organizations/main.html', {
38             'org': org,
39             'tab': tab,
40             'my_status': my_status,
41             'am_owner': am_owner,
42             'am_member': am_member,
43         })
44
45
46 def user_card(request, pk):
47     try:
48         user = User.objects.get(pk=pk)
49     except User.DoesNotExist:
50         raise Http404
51     card, created = UserCard.objects.get_or_create(pk=user.pk)
52     return render(request, 'organizations/user_card.html', {
53             'card': card,
54         })
55
56
57 @login_required
58 def edit(request, pk):
59     try:
60         org = Organization.objects.get(pk=pk)
61     except Organization.DoesNotExist:
62         raise Http404
63
64     if request.method == 'POST':
65         form = OrganizationForm(request.POST, request.FILES, instance=org)
66         if form.is_valid():
67             org = form.save()
68             return redirect(org.get_absolute_url())
69     else:
70         form = OrganizationForm(instance=org)
71     return render(request, 'organizations/edit.html', {'form': form})
72
73
74 @login_required
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)
79         if form.is_valid():
80             card = form.save()
81             return redirect(card.get_absolute_url())
82     else:
83         form = UserCardForm(instance=card)
84     return render(request, 'organizations/edit_user.html', {'form': form})
85
86
87 @login_required
88 def join(request, pk):
89     try:
90         org = Organization.objects.get(pk=pk)
91     except Organization.DoesNotExist:
92         raise Http404
93
94     if request.method == 'POST':
95         org.set_user_status(request.user, Membership.Status.PENDING)
96         return redirect('organizations_members', org.pk)
97
98     return render(request, 'organizations/join.html', {'org': org})
99
100
101 @login_required
102 def membership(request, pk):
103     try:
104         org = Organization.objects.get(pk=pk)
105     except Organization.DoesNotExist:
106         raise Http404
107
108     try:
109         user = User.objects.get(pk=request.POST['user'])
110     except KeyError, User.DoesNotExist:
111         pass
112     else:
113         if user != request.user:
114             action = request.POST.get('action')
115             if action:
116                 if action == 'remove':
117                     action = None
118                 org.set_user_status(user, action)
119     return redirect('organizations_members', org.pk)
120
121
122 def organizations(request):
123     return render(request, "organizations/organizations.html", {
124         'organizations': Organization.objects.all(),
125     })