This repository has been archived by the owner on Sep 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
views.py
185 lines (146 loc) · 6.65 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import rules
from rules.contrib.views import PermissionRequiredMixin
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib import messages
from django.db import transaction
from django.http import Http404
from django.shortcuts import redirect, get_object_or_404
from django.views.generic.base import RedirectView
from django.views.generic import (
CreateView,
DetailView,
ListView,
UpdateView,
)
from events.models import Event, EventRSVP
from locations.models import Location
from .forms import CommunityUpdateForm
from events.forms import EventCreateForm
from .models import Community, CommunitySubscription
class CommunityListView(ListView):
model = Community
class CommunityCreateView(LoginRequiredMixin, PermissionRequiredMixin, CreateView):
model = Community
fields = ['name', ]
template_name = 'communities/community_create.html'
permission_required = 'community.can_create'
def form_valid(self, form):
with transaction.atomic():
out = super().form_valid(form)
CommunitySubscription.objects.create(
community=self.object,
user=self.request.user,
role='owner'
)
return out
class CommunityUpdateView(LoginRequiredMixin, PermissionRequiredMixin, UpdateView):
model = Community
template_name = 'communities/community_update.html'
permission_required = 'community.can_edit'
form_class = CommunityUpdateForm
class CommunityDetailView(DetailView):
model = Community
class CommunityEventCreateView(LoginRequiredMixin, PermissionRequiredMixin, CreateView):
model = Event
template_name = 'communities/community_event_create.html'
form_class = EventCreateForm
permission_required = 'community.can_create_event'
def get_permission_object(self):
return self.get_community()
def get_community(self):
return get_object_or_404(Community, slug=self.kwargs.get('slug'))
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['community'] = self.get_community()
return context
def form_valid(self, form):
self.object = form.save(commit=False)
self.object.community = self.get_community()
if form.data.get('location'):
self.object.location = Location.objects.get(pk=form.data.get('location'))
self.object.save()
return redirect(self.get_success_url())
class CommunitySubscribeView(LoginRequiredMixin, DetailView):
model = Community
template_name = 'communities/community_subscribe.html'
def get(self, request, *args, **kwargs):
self.object = self.get_object()
community = self.object
try:
CommunitySubscription.objects.get(user=request.user, community=community)
messages.warning(request, 'You are already subscribed to "{}"'.format(community.name))
return redirect(community)
except CommunitySubscription.DoesNotExist:
return super().get(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
self.get_object().subscribe_user(request.user)
return redirect(self.get_object())
class CommunityUnsubscribeView(LoginRequiredMixin, DetailView):
model = Community
template_name = 'communities/community_unsubscribe.html'
def get(self, request, *args, **kwargs):
self.object = self.get_object()
community = self.object
try:
user_subscription = CommunitySubscription.objects.get(
user=request.user, community=community)
except CommunitySubscription.DoesNotExist:
messages.warning(request, 'You cannot unsubscribe when you are not subscribed')
return redirect(community)
if not rules.test_rule('can_unsubscribe', request.user, user_subscription):
messages.error(request, 'You cannot unsubscribe when you are the last owner')
return redirect(community)
return super().get(request, *args, **kwargs)
@transaction.atomic
def post(self, request, *args, **kwargs):
self.object = self.get_object()
community = self.object
try:
user_subscription = CommunitySubscription.objects.get(
user=request.user, community=community)
except CommunitySubscription.DoesNotExist:
messages.warning(request, 'You cannot unsubscribe when you are not subscribed')
return redirect(community)
if not rules.test_rule('can_unsubscribe', request.user, user_subscription):
messages.error(request, 'You cannot unsubscribe when you are the last owner')
return redirect(community)
EventRSVP.objects.filter(
event__pk__in=Event.objects.upcoming().filter(community=community).values_list('pk', flat=True),
user=request.user).delete()
user_subscription.delete()
messages.success(request, 'Successfully unsubscribed from "{}"'.format(community.name))
return redirect(community)
class SubscriptionChangeRoleView(LoginRequiredMixin, PermissionRequiredMixin, DetailView):
model = CommunitySubscription
allowed_methods = ['post']
def get_permission_object(self):
return self.get_object().community
def get_permission_required(self):
role = self.kwargs.get('role')
if role == CommunitySubscription.ROLE_OWNER:
return ['community.can_set_owner']
elif role == CommunitySubscription.ROLE_ADMIN:
return ['community.can_set_admin']
elif role == CommunitySubscription.ROLE_SUBSCRIBER:
return ['community.can_set_subscriber']
else:
raise ValueError('Unknown role type {}'.format(role))
def post(self, request, *args, **kwargs):
subscription = self.get_object()
if rules.test_rule('is_last_owner', request.user, subscription):
messages.error(request, 'You cannot change your role when you are the last owner')
return redirect(subscription.community)
subscription.role = kwargs['role']
subscription.save()
return redirect(subscription.community)
class CommunityRedirectView(RedirectView):
permanent = False
pattern_name = 'community_detail'
def get_redirect_url(self, *args, **kwargs):
cname_parameter = kwargs.pop('cname')
for community in Community.objects.filter(cname__icontains=cname_parameter):
for cname in community.cname.split(' '):
if cname.lower() == cname_parameter:
kwargs['slug'] = community.slug
return super().get_redirect_url(*args, **kwargs)
raise Http404