-
Notifications
You must be signed in to change notification settings - Fork 183
/
views.py
270 lines (211 loc) · 8.57 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.shortcuts import get_object_or_404, redirect, render
from friendship.exceptions import AlreadyExistsError
from friendship.models import Block, Follow, Friend, FriendshipRequest
try:
from django.contrib.auth import get_user_model
user_model = get_user_model()
except ImportError:
from django.contrib.auth.models import User
user_model = User
def get_friendship_context_object_name():
return getattr(settings, "FRIENDSHIP_CONTEXT_OBJECT_NAME", "user")
def get_friendship_context_object_list_name():
return getattr(settings, "FRIENDSHIP_CONTEXT_OBJECT_LIST_NAME", "users")
def view_friends(request, username, template_name="friendship/friend/user_list.html"):
"""View the friends of a user"""
user = get_object_or_404(user_model, username=username)
friends = Friend.objects.friends(user)
return render(
request,
template_name,
{
get_friendship_context_object_name(): user,
"friendship_context_object_name": get_friendship_context_object_name(),
"friends": friends,
},
)
@login_required
def friendship_add_friend(
request, to_username, template_name="friendship/friend/add.html"
):
"""Create a FriendshipRequest"""
ctx = {"to_username": to_username}
if request.method == "POST":
to_user = user_model.objects.get(username=to_username)
from_user = request.user
try:
Friend.objects.add_friend(from_user, to_user)
except AlreadyExistsError as e:
ctx["errors"] = ["%s" % e]
else:
return redirect("friendship_request_list")
return render(request, template_name, ctx)
@login_required
def friendship_accept(request, friendship_request_id):
"""Accept a friendship request"""
if request.method == "POST":
f_request = get_object_or_404(
request.user.friendship_requests_received, id=friendship_request_id
)
f_request.accept()
return redirect("friendship_view_friends", username=request.user.username)
return redirect(
"friendship_requests_detail", friendship_request_id=friendship_request_id
)
@login_required
def friendship_reject(request, friendship_request_id):
"""Reject a friendship request"""
if request.method == "POST":
f_request = get_object_or_404(
request.user.friendship_requests_received, id=friendship_request_id
)
f_request.reject()
return redirect("friendship_request_list")
return redirect(
"friendship_requests_detail", friendship_request_id=friendship_request_id
)
@login_required
def friendship_cancel(request, friendship_request_id):
"""Cancel a previously created friendship_request_id"""
if request.method == "POST":
f_request = get_object_or_404(
request.user.friendship_requests_sent, id=friendship_request_id
)
f_request.cancel()
return redirect("friendship_request_list")
return redirect(
"friendship_requests_detail", friendship_request_id=friendship_request_id
)
@login_required
def friendship_request_list(
request, template_name="friendship/friend/requests_list.html"
):
"""View unread and read friendship requests"""
friendship_requests = Friend.objects.requests(request.user)
# This shows all friendship requests in the database
# friendship_requests = FriendshipRequest.objects.filter(rejected__isnull=True)
return render(request, template_name, {"requests": friendship_requests})
@login_required
def friendship_request_list_rejected(
request, template_name="friendship/friend/requests_list.html"
):
"""View rejected friendship requests"""
# friendship_requests = Friend.objects.rejected_requests(request.user)
friendship_requests = FriendshipRequest.objects.filter(rejected__isnull=False)
return render(request, template_name, {"requests": friendship_requests})
@login_required
def friendship_requests_detail(
request, friendship_request_id, template_name="friendship/friend/request.html"
):
"""View a particular friendship request"""
f_request = get_object_or_404(FriendshipRequest, id=friendship_request_id)
return render(request, template_name, {"friendship_request": f_request})
def followers(request, username, template_name="friendship/follow/followers_list.html"):
"""List this user's followers"""
user = get_object_or_404(user_model, username=username)
followers = Follow.objects.followers(user)
return render(
request,
template_name,
{
get_friendship_context_object_name(): user,
"friendship_context_object_name": get_friendship_context_object_name(),
"followers": followers,
},
)
def following(request, username, template_name="friendship/follow/following_list.html"):
"""List who this user follows"""
user = get_object_or_404(user_model, username=username)
following = Follow.objects.following(user)
return render(
request,
template_name,
{
get_friendship_context_object_name(): user,
"friendship_context_object_name": get_friendship_context_object_name(),
"following": following,
},
)
@login_required
def follower_add(
request, followee_username, template_name="friendship/follow/add.html"
):
"""Create a following relationship"""
ctx = {"followee_username": followee_username}
if request.method == "POST":
followee = user_model.objects.get(username=followee_username)
follower = request.user
try:
Follow.objects.add_follower(follower, followee)
except AlreadyExistsError as e:
ctx["errors"] = ["%s" % e]
else:
return redirect("friendship_following", username=follower.username)
return render(request, template_name, ctx)
@login_required
def follower_remove(
request, followee_username, template_name="friendship/follow/remove.html"
):
"""Remove a following relationship"""
if request.method == "POST":
followee = user_model.objects.get(username=followee_username)
follower = request.user
Follow.objects.remove_follower(follower, followee)
return redirect("friendship_following", username=follower.username)
return render(request, template_name, {"followee_username": followee_username})
def all_users(request, template_name="friendship/user_actions.html"):
users = user_model.objects.all()
return render(
request, template_name, {get_friendship_context_object_list_name(): users}
)
def blocking(request, username, template_name="friendship/block/blockers_list.html"):
"""List this user's followers"""
user = get_object_or_404(user_model, username=username)
Block.objects.blocked(user)
return render(
request,
template_name,
{
get_friendship_context_object_name(): user,
"friendship_context_object_name": get_friendship_context_object_name(),
},
)
def blockers(request, username, template_name="friendship/block/blocking_list.html"):
"""List who this user follows"""
user = get_object_or_404(user_model, username=username)
Block.objects.blocking(user)
return render(
request,
template_name,
{
get_friendship_context_object_name(): user,
"friendship_context_object_name": get_friendship_context_object_name(),
},
)
@login_required
def block_add(request, blocked_username, template_name="friendship/block/add.html"):
"""Create a following relationship"""
ctx = {"blocked_username": blocked_username}
if request.method == "POST":
blocked = user_model.objects.get(username=blocked_username)
blocker = request.user
try:
Block.objects.add_block(blocker, blocked)
except AlreadyExistsError as e:
ctx["errors"] = ["%s" % e]
else:
return redirect("friendship_blocking", username=blocker.username)
return render(request, template_name, ctx)
@login_required
def block_remove(
request, blocked_username, template_name="friendship/block/remove.html"
):
"""Remove a following relationship"""
if request.method == "POST":
blocked = user_model.objects.get(username=blocked_username)
blocker = request.user
Block.objects.remove_block(blocker, blocked)
return redirect("friendship_blocking", username=blocker.username)
return render(request, template_name, {"blocked_username": blocked_username})