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