-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
292 lines (254 loc) · 10.9 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import json
import os
from abc import ABC
from actstream.actions import follow, unfollow
from actstream.models import following, followers
from allauth.account.decorators import verified_email_required
from django.contrib import messages
from django.contrib.auth import logout
from django.core.exceptions import ObjectDoesNotExist
from django.core.files.storage import FileSystemStorage, DefaultStorage
from django.http import Http404, HttpResponse
from django.shortcuts import render, redirect
from actstream.actions import follow, unfollow
from config import settings
import user.forms as form
from user.models import Profile, User
from formtools.wizard.views import SessionWizardView, WizardView
JSON_CONTENT_TYPE = 'application/json'
@verified_email_required
def profile_view(request, username):
try:
profiles = Profile.objects.all()[:4]
# get user profile by username
profile = Profile.objects.get(user__username__iexact=username)
user = User.objects.get(username=username)
user_following = following(user)
user_followers = followers(user)
# if user not found raise 404 error
except ObjectDoesNotExist:
raise Http404
# context data
context = {
"user": user,
"profile": profile,
"profiles": profiles,
"following": user_following,
"followers": user_followers,
}
return render(request, "user/profile.html", context)
@verified_email_required
def edit_personal_information(request):
profile_form = form.EditPersonalInformationForm(instance=request.user)
# checked request is post?
if request.method == "POST":
# send user data to edit profile form
profile_form = form.EditPersonalInformationForm(
request.POST, instance=request.user
)
# check user profile form is valid
if profile_form.is_valid():
# if user profile is valid, save the form
profile_form.save()
# show success message when save user data
messages.success(request, "Profile saved")
# put edit profile form with context and send to template
context = {"profile_form": profile_form}
# render request and template and context
return render(
request, "user/edit_personal_information.html", context
)
# pass context
context = {"profile_form": profile_form}
return render(request, "user/edit_personal_information.html", context)
@verified_email_required
def edit_profile_view(request):
profile_form = form.EditProfileForm(instance=request.user.profile)
# check request is post?
if request.method == "POST":
# send user data to edit profile form
profile_form = form.EditProfileForm(
request.POST, request.FILES, instance=request.user.profile
)
# check user profile form is valid
if profile_form.is_valid():
# if user profile is valid, save the form
profile_form.save()
# show success message when save user data
messages.success(request, "Profile saved")
# put edit profile form with context and send to template
context = {"profile_form": profile_form}
# render request and template and context
return render(request, "user/edit_profile.html", context)
# pass context
context = {"profile_form": profile_form}
return render(request, "user/edit_profile.html", context)
def delete_profile_image(request):
if request.user.is_authenticated:
if request.method == "POST":
profile_image = request.user.profile.profile_image
try:
profile_image.file
except ValueError:
response = ""
return HttpResponse(response)
profile_image.delete()
response = {"status": "ok"}
return HttpResponse(json.dumps(response), content_type=JSON_CONTENT_TYPE)
else:
response = ""
return HttpResponse(response)
else:
return redirect("account_login")
@verified_email_required
def delete_account_view(request):
if request.method == "POST":
data = request.POST.get("delete")
if data == "yes":
user = request.user
logout(request)
user.delete()
messages.success(
request,
"Your account has been successfully deleted, We hope you will be back soon :)",
)
return redirect("home")
else:
return redirect("home")
return render(request, "user/delete_account.html")
@verified_email_required
def follow_request(request, username):
if request.method == "POST":
try:
user_whose_page_is_currently_being_viewed = User.objects.get(username=username)
profile_whose_page_is_currently_being_viewed = Profile.objects.get(user=user_whose_page_is_currently_being_viewed)
authenticated_user_profile = request.user.profile
if request.user in followers(user_whose_page_is_currently_being_viewed):
unfollow(request.user, user_whose_page_is_currently_being_viewed)
response = {"status": "unfollow"}
return HttpResponse(
json.dumps(response), content_type=JSON_CONTENT_TYPE
)
elif profile_whose_page_is_currently_being_viewed in authenticated_user_profile.follow_requests.all():
authenticated_user_profile.follow_requests.remove(profile_whose_page_is_currently_being_viewed)
response = {"status": "cancel_follow_request"}
return HttpResponse(
json.dumps(response), content_type=JSON_CONTENT_TYPE
)
else:
if profile_whose_page_is_currently_being_viewed.is_private:
authenticated_user_profile.follow_requests.add(profile_whose_page_is_currently_being_viewed)
response = {"status": "send_follow_request"}
return HttpResponse(
json.dumps(response), content_type=JSON_CONTENT_TYPE
)
else:
follow(request.user, user_whose_page_is_currently_being_viewed)
response = {"status": "followed"}
return HttpResponse(
json.dumps(response), content_type=JSON_CONTENT_TYPE
)
except ObjectDoesNotExist:
response = {"status": "not_found"}
return HttpResponse(
json.dumps(response), content_type=JSON_CONTENT_TYPE
)
@verified_email_required
def follow_request_accepted(request, username):
if request.method == "POST":
try:
user_whose_page_is_currently_being_viewed = User.objects.get(username=username) # We get the current user
profile_whose_page_is_currently_being_viewed = Profile.objects.get(
user=user_whose_page_is_currently_being_viewed
) # We get the current user profile
authenticated_user_profile = request.user.profile # We get the logged-in user
profile_whose_page_is_currently_being_viewed.follow_requests.remove(authenticated_user_profile)
follow(user_whose_page_is_currently_being_viewed, request.user)
profile_whose_page_is_currently_being_viewed.save()
response = {"status": "ok"}
return HttpResponse(
json.dumps(response), content_type=JSON_CONTENT_TYPE
)
except ObjectDoesNotExist:
response = ""
return HttpResponse(response)
response = ""
return HttpResponse(response)
@verified_email_required
def follow_request_declined(request, username):
if request.method == "POST":
try:
profile_whose_page_is_currently_being_viewed = Profile.objects.get(user__username__iexact=username)
authenticated_user_profile = request.user.profile
authenticated_user_profile.follow_requests.remove(profile_whose_page_is_currently_being_viewed)
authenticated_user_profile.save()
response = {"status": "ok"}
return HttpResponse(
json.dumps(response), content_type=JSON_CONTENT_TYPE
)
except ObjectDoesNotExist:
response = ""
return HttpResponse(response)
response = ""
return HttpResponse(response)
@verified_email_required
def follow_request_list(request):
current_user = request.user.profile
follow_requests = current_user.follow_request_by.all()
context = {"follow_requests": follow_requests}
return render(
request, "activity/follow_request/follow_request_layout.html", context
)
@verified_email_required
def followers_list(request, username):
user = User.objects.get(username=username)
user_followers = followers(user)
context = {"followers": user_followers}
return render(request, "user/followers.html", context)
@verified_email_required
def followings_list(request, username):
user = User.objects.get(username=username)
user_following = following(user)
context = {"followings": user_following}
return render(request, "user/following.html", context)
def remove_welcome_message(request):
if request.method == "POST":
profile = request.user.profile
profile.show_welcome_message = False
profile.save()
response = {"status": "ok"}
return HttpResponse(json.dumps(response), content_type="application/json")
class UserWizard(SessionWizardView):
file_storage = FileSystemStorage(location=os.path.join(settings.MEDIA_ROOT, 'temp'))
def done(self, form_list, **kwargs):
form_data = [form.cleaned_data for form in form_list]
first_name = form_data[0]["first_name"]
last_name = form_data[0]["last_name"]
biography = form_data[1]["biography"]
birthday = form_data[2]["birthday"]
job = form_data[3]["job"]
profile_image = form_data[4]["profile_image"]
current_user = self.request.user
user = User.objects.get(pk=current_user.pk)
if first_name is not None:
user.first_name = first_name
user.save()
if last_name is not None:
user.last_name = last_name
user.save()
profile = Profile.objects.get(user=user)
if biography is not None:
profile.biography = biography
profile.save()
if birthday is not None:
profile.birthday = birthday
profile.save()
if job is not None:
profile.job = job
profile.save()
if profile_image is not None:
profile.profile_image = profile_image
profile.save()
remove_welcome_message(self.request)
response = {"status": "ok"}
return HttpResponse(json.dumps(response), content_type="application/json")