-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
851 lines (775 loc) · 33.6 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
import datetime
import html
import json
import requests
from django_countries import countries
from django.conf import settings
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from django.db import transaction
from django.db.models import Count, F, Q
from django.http import Http404, HttpResponse, HttpResponseBadRequest, HttpResponseRedirect
from django.middleware.csrf import get_token
from django.shortcuts import get_object_or_404
from django.shortcuts import redirect, render
from django.urls import reverse
from django.utils import timezone
from django.utils.decorators import method_decorator
from django.utils.http import url_has_allowed_host_and_scheme, urlencode
from django.views import View
from django.views.decorators.cache import cache_control, never_cache
from rest_framework import permissions, mixins, viewsets, status
from rest_framework.decorators import action
from rest_framework.exceptions import APIException, PermissionDenied, NotFound
from rest_framework.permissions import BasePermission, IsAuthenticated, IsAdminUser
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.views import APIView
from urllib.parse import urlparse
from habrasanta.celery import send_email, send_notification
from habrasanta.serializers import (
AsyncResultSerializer,
BanRecordSerializer,
EventSerializer,
MessageBulkSerializer,
MessageSerializer,
ParticipationSerializer,
SeasonSerializer,
TestEMailSerializer,
TestNotificationSerializer,
UserInfoSerializer,
UserSerializer,
)
from habrasanta.utils import fetch_habr_profile
from habrasanta.models import Event, Message, Participation, Season, User
class GenericAPIError(APIException):
"""
Override APIException to avoid status code 5xx.
"""
status_code = status.HTTP_418_IM_A_TEAPOT
class SeasonViewSet(viewsets.ReadOnlyModelViewSet):
serializer_class = SeasonSerializer
queryset = Season.objects.all()
def get_permissions(self):
if self.action == "create":
return [IsAdminUser()]
return super().get_permissions()
def create(self, request):
"""
Creates a new season.
The user calling this method must be an admin.
"""
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
season = serializer.save()
Event.objects.create(
typ=Event.SEASON_CREATED,
sub=request.user,
season=season,
)
return Response(serializer.data)
@action(detail=False)
@method_decorator(cache_control(public=True))
def latest(self, request):
"""
Returns the latest season or 404 if there are no seasons yet.
"""
try:
season = Season.objects.latest()
except Season.DoesNotExist:
raise NotFound()
serializer = self.get_serializer(season)
return Response(serializer.data)
@action(
detail=True,
serializer_class=ParticipationSerializer,
permission_classes=[IsAuthenticated],
)
@method_decorator(cache_control(private=True))
def participation(self, request, pk):
"""
Returns the participation of the current user in the given season
or an error, if the user is not participating.
"""
season = self.get_object()
participation = self.get_participation(season)
serializer = self.get_serializer(participation)
return Response(serializer.data)
@participation.mapping.post
def create_participation(self, request, pk):
"""
Enrolls the current user to the given season.
An error occurs if:
- The registration to this season has closed.
- The user is not qualified for participation in our club (does the user have enough karma?)
- The user is already enrolled for this season (is another tab open?)
"""
season = self.get_object()
self.check_season_active(season)
if not season.is_registration_open:
raise GenericAPIError("Регистрация на этот сезон уже невозможна", "the_die_is_cast")
if not request.user.can_participate:
raise PermissionDenied("Вы не можете участвовать в нашем клубе", "unqualified")
if Participation.objects.filter(user=self.request.user, season=season).exists():
raise GenericAPIError("Вы уже зарегистрированы на этот сезон", "dual_participation")
serializer = self.serializer_class(data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save(season=season, user=request.user)
season.member_count += 1
season.save()
Event.objects.create(
typ=Event.ENROLLED,
sub=request.user,
season=season,
ip_address=request.META["REMOTE_ADDR"],
)
return Response({
"season": SeasonSerializer(season).data,
"participation": serializer.validated_data,
})
@participation.mapping.delete
def cancel_participation(self, request, pk):
"""
Cancels the participation of the current user in the given season.
An error occurs if:
- The user is not participating in this season.
- The registration has closed (too late - now, you must send a gift).
"""
season = self.get_object()
self.check_season_active(season)
participation = self.get_participation(season)
if not season.is_registration_open:
raise GenericAPIError("Нельзя отказаться после окончания регистрации", "the_die_is_cast")
participation.delete()
season.member_count -= 1
season.save()
Event.objects.create(
typ=Event.UNENROLLED,
sub=request.user,
season=season,
ip_address=request.META["REMOTE_ADDR"],
)
return Response({
"season": SeasonSerializer(season).data,
"participation": None,
})
@action(
detail=True,
methods=["DELETE"],
permission_classes=[IsAdminUser],
url_path="participants/(?P<login>[^/.]+)",
)
def kick_participant(self, request, pk, login):
"""
Cancels participation of the given user.
Use with caution! It's one of the most dangerous API methods :-)
Doing a backup before calling this method is highly recommended.
The user calling this method must be an admin.
"""
season = self.get_object()
user = get_object_or_404(User, login__iexact=login)
participation = get_object_or_404(Participation, user=user, season=season)
if season.is_registration_open:
# Easy peasy :-)
participation.delete()
else:
# Reconnect participants.
santa = participation.santa
giftee = participation.giftee
assert santa
assert giftee
participation.delete()
participation = None
santa.giftee = giftee
santa.save()
# Avoid loops of less than 3 people.
assert giftee.giftee != santa
# Send notifications.
transaction.on_commit(send_notification.s(
santa.user.id,
"Замена получателя подарка! Посмотреть адрес нового получателя можно в " +
"<a href=\"https://habra-adm.ru/{}/profile/\">профиле</a>.".format(season.id)
).delay)
transaction.on_commit(send_email.s(
santa.user.id,
"замена получателя подарка",
"Приветствуем!\n\n" +
"Так получилось, что ваш Анонимный Получатель Подарка был заменён. " +
"Для выяснения подробностей свяжитесь с пользователем @clubadm на Хабре - возможно, ещё не всё потеряно!"
).delay)
transaction.on_commit(send_notification.s(
giftee.user.id,
"Замена Анонимного Деда Мороза!"
).delay)
transaction.on_commit(send_email.s(
giftee.user.id,
"замена Деда Мороза",
"Приветствуем!\n\n" +
"Так получилось, что ваш Анонимный Дед Мороз был заменен (на не менее анонимного). " +
"Для выяснения причин свяжитесь с пользователем @clubadm на Хабре - возможно, ещё не всё потеряно!"
).delay)
# TODO: what about private messages in the chat?
# Send a notification to the user itself.
transaction.on_commit(send_notification.s(
user.id,
"Кто-то из организаторов отменил ваше участие в АДМ-{}.".format(season.id)
).delay)
transaction.on_commit(send_email.s(
user.id,
"ваше участие отменено",
"Приветствуем!\n\n" +
"Ваше участие в АДМ-{} было отменено. ".format(season.id) +
"Для выяснения подробностей свяжитесь с пользователем @clubadm на Хабре - возможно, ещё не всё потеряно!"
).delay)
# Update counters.
season.member_count -= 1
season.save()
# Log the event.
Event.objects.create(
typ=Event.UNENROLLED,
sub=request.user,
user=user,
season=season,
ip_address=request.META["REMOTE_ADDR"],
)
# Send 204 No Content.
return Response(status=status.HTTP_204_NO_CONTENT)
@action(
detail=True,
methods=["post"],
permission_classes=[IsAuthenticated],
)
def mark_shipped(self, request, pk):
"""
Tells the club, the current user has shipped a gift to their giftee.
The giftee will be automatically notified.
An error occurs if:
- The user is not participating in this season.
- The user has no giftee assigned yet (is registration still open?)
- The user has already told the club, they had sent a gift.
"""
season = self.get_object()
self.check_season_active(season)
participation = self.get_participation(season)
if not participation.giftee:
raise NotFound("Вам еще не назначен получателя подарка")
if participation.gift_shipped_at:
raise GenericAPIError("Вами уже был отправлен один подарок", "already_shipped")
season.shipped_count += 1
season.save()
participation.gift_shipped_at = timezone.now()
participation.save()
Event.objects.create(
typ=Event.GIFT_SENT,
sub=request.user,
season=season,
ip_address=request.META["REMOTE_ADDR"],
)
transaction.on_commit(send_notification.s(
participation.giftee.user.id,
"Анонимный Дед Мороз отправил подарок! Когда получите, не забудьте отметить это в " +
"<a href=\"https://habra-adm.ru/{}/profile/\">профиле</a>.".format(season.id)
).delay)
transaction.on_commit(send_email.s(
participation.giftee.user.id,
"Вам отправили подарок!",
"Привет, внук!\n\n" +
"Похоже, ты хорошо вёл себя в этом году - Анонимный Дед Мороз отправил тебе подарок!\n\n" +
"Пожалуйста, не забудь отметить в профиле " +
"(https://habra-adm.ru/{}/profile/), ".format(season.id) +
"когда получишь подарок.\n\n" +
"Всего наилучшего в новом году!"
).delay)
return Response({
"season": self.get_serializer(season).data,
"participation": ParticipationSerializer(participation).data,
})
@action(
detail=True,
methods=["post"],
permission_classes=[IsAuthenticated],
)
def mark_delivered(self, request, pk):
"""
Tells the club the current user has received a gift.
The santa will be automatically notified.
An error occurs if:
- The user is not participating in this season.
- The user has no santa assigned yet.
- The user has already told the club, they had received a gift.
- Santa hasn't told the club yet, the gift was sent.
"""
season = self.get_object()
self.check_season_active(season)
participation = self.get_participation(season)
if not hasattr(participation, "santa"):
raise NotFound("Вам еще не назначен Дед Мороз")
if participation.gift_delivered_at:
raise GenericAPIError("Вами уже был получен один подарок", "already_delivered")
if not participation.santa.gift_shipped_at:
raise GenericAPIError("Нельзя получить подарок до того, как он был отправлен", "not_shipped")
season.delivered_count += 1
season.save()
participation.gift_delivered_at = timezone.now()
participation.save()
Event.objects.create(
typ=Event.GIFT_RECEIVED,
sub=request.user,
season=season,
ip_address=request.META["REMOTE_ADDR"],
)
transaction.on_commit(send_notification.s(
participation.santa.user.id,
"Ваш АПП отметил в профиле, что подарок получен!"
).delay)
transaction.on_commit(send_email.s(
participation.santa.user.id,
"ваш получатель отметил, что получил подарок!",
"Привет, Анонимный Дед Мороз!\n\n" +
"Новогоднее чудо случилось — ваш Анонимный Получатель Подарка отметил, что получил подарок!\n\n" +
"Поздравляем и желаем всего наилучшего в новом году!"
).delay)
return Response({
"season": self.get_serializer(season).data,
"participation": ParticipationSerializer(participation).data,
})
@action(
detail=True,
serializer_class=MessageSerializer,
permission_classes=[IsAuthenticated],
)
@method_decorator(cache_control(private=True))
def giftee_chat(self, request, pk):
"""
Returns all chat messages between the current user and their giftee in the given season.
An error occurs if:
- The user is not participating in this season.
- The user has no giftee assigned yet (is registration still open?)
"""
season = self.get_object()
participation = self.get_participation(season)
if not participation.giftee:
raise NotFound("Вам еще не назначен получателя подарка")
# TODO: select_related() ? Check the SQL queries!
messages = Message.objects.filter(
Q(sender=participation, recipient=participation.giftee) |
Q(sender=participation.giftee, recipient=participation)
)
serializer = self.get_serializer(messages, many=True, context={ "me": participation })
return Response(serializer.data)
@giftee_chat.mapping.post
def post_giftee_chat(self, request, pk):
"""
Sends a chat message to the giftee of the current user in the given season.
An error occurs if:
- The user is not participating in this season.
- The user has no giftee assigned yet (is registration still open?)
"""
season = self.get_object()
self.check_season_active(season)
participation = self.get_participation(season)
if not participation.giftee:
raise NotFound("Вам еще не назначен получателя подарка")
# TODO: prevent spamming with too many messages
serializer = self.get_serializer(data=request.data, context={ "me": participation })
serializer.is_valid(raise_exception=True)
serializer.save(sender=participation, recipient=participation.giftee)
Event.objects.create(
typ=Event.GIFTEE_MAILED,
sub=request.user,
season=season,
ip_address=request.META["REMOTE_ADDR"],
)
# Notification will be send by cron.
return Response(serializer.data)
@action(
detail=True,
serializer_class=MessageSerializer,
permission_classes=[IsAuthenticated],
)
@method_decorator(cache_control(private=True))
def santa_chat(self, request, pk):
"""
Returns all chat messages between the current user and their santa in the given season.
An error occurs if:
- The user is not participating in this season.
- The user has no santa assigned yet (is registration still open?)
"""
season = self.get_object()
participation = self.get_participation(season)
if not hasattr(participation, "santa"):
raise NotFound("Вам еще не назначен Дед Мороз")
# TODO: select_related() ? Check the SQL queries!
messages = Message.objects.filter(
Q(sender=participation, recipient=participation.santa) |
Q(sender=participation.santa, recipient=participation)
)
serializer = self.get_serializer(messages, many=True, context={ "me": participation })
return Response(serializer.data)
@santa_chat.mapping.post
def post_santa_chat(self, request, pk):
"""
Sends a chat message to the santa of the current user in the given season.
An error occurs if:
- The user is not participating in this season.
- The user has no santa assigned yet (is registration still open?)
"""
season = self.get_object()
self.check_season_active(season)
participation = self.get_participation(season)
if not hasattr(participation, "santa"):
raise NotFound("Вам еще не назначен Дед Мороз")
# TODO: prevent spamming with too many messages
serializer = self.get_serializer(data=request.data, context={ "me": participation })
serializer.is_valid(raise_exception=True)
serializer.save(sender=participation, recipient=participation.santa)
Event.objects.create(
typ=Event.SANTA_MAILED,
sub=request.user,
season=season,
ip_address=request.META["REMOTE_ADDR"],
)
# Notification will be send by cron.
return Response(serializer.data)
@action(
detail=True,
serializer_class=EventSerializer,
permission_classes=[IsAdminUser],
)
@method_decorator(cache_control(private=True))
def events(self, request, pk):
"""
Returns all events associated with this season,
e.g. enrollments, sending or receiving gifts, etc.
The user calling this method must be an admin.
"""
season = self.get_object()
serializer = self.get_serializer(season.events, many=True)
return Response(serializer.data)
@action(detail=True)
@method_decorator(cache_control(public=True))
def countries(self, request, pk):
"""
Shows country statistics for the given season.
"""
season = self.get_object()
stats = Participation.objects.filter(
season=season,
).values("country").annotate(
count=Count("id"),
).order_by("-count", "country")
result = {}
for item in stats:
result[item["country"]] = item["count"]
return Response(result)
def check_season_active(self, season):
if season.is_closed:
raise GenericAPIError("Этот сезон находится в архиве", "season_archived")
def get_participation(self, season):
try:
return Participation.objects.get(user=self.request.user, season=season)
except Participation.DoesNotExist:
raise GenericAPIError("Ой, а вы во всем этом и не участвуете", "not_participating")
class MessageViewSet(viewsets.GenericViewSet):
permission_classes=[IsAuthenticated]
serializer_class = MessageSerializer
queryset = Season.objects.all()
@action(
detail=False,
methods=["post"],
serializer_class=MessageBulkSerializer,
)
def mark_read(self, request):
"""
Marks the messages with the given IDs as read and returns the number of updated messages.
On success, this number is equal to the number of given IDs.
"""
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
ids = serializer.validated_data["ids"]
count = Message.objects.filter(
id__in=ids,
recipient__user=request.user,
read_date__isnull=True,
# read_date was introduced on this day, all messages before must stay NULL.
send_date__gte=timezone.make_aware(datetime.datetime(2016, 12, 20))
).update(read_date=timezone.now())
return Response({ "updated": count })
class UserViewSet(viewsets.ReadOnlyModelViewSet):
permission_classes=[IsAdminUser]
serializer_class = UserSerializer
queryset = User.objects.all()
lookup_field = "login__iexact"
lookup_url_kwarg = "login"
@action(
detail=True,
methods=["post"],
serializer_class=TestNotificationSerializer,
)
def send_notification(self, request, login):
"""
Sends a notification on Habr for this user, mostly for testing purposes.
The user calling this method must be an admin.
"""
user = self.get_object()
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
async_result = send_notification.delay(user.id, serializer.validated_data["text"])
return Response(AsyncResultSerializer(async_result).data, status=status.HTTP_202_ACCEPTED)
@action(
detail=True,
methods=["post"],
serializer_class=TestEMailSerializer,
)
def send_email(self, request, login):
"""
Sends an email to this user, mostly for testing purposes.
The user calling this method must be an admin.
"""
user = self.get_object()
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
async_result = send_email.delay(
user.id,
serializer.validated_data["subject"],
serializer.validated_data["body"]
)
return Response(AsyncResultSerializer(async_result).data, status=status.HTTP_202_ACCEPTED)
@action(
detail=True,
serializer_class=EventSerializer,
permission_classes=[IsAdminUser],
)
@method_decorator(cache_control(private=True))
def events(self, request, login):
"""
Returns all events caused by this user.
The user calling this method must be an admin.
"""
user = self.get_object()
serializer = self.get_serializer(user.events, many=True)
return Response(serializer.data)
@action(
detail=True,
serializer_class=BanRecordSerializer,
)
def ban_history(self, request, login):
"""
Shows the ban history.
The user calling this method must be an admin.
"""
user = self.get_object()
serializer = self.get_serializer(user.ban_history, many=True)
return Response(serializer.data)
@action(
detail=True,
methods=["post"],
serializer_class=BanRecordSerializer,
)
def ban(self, request, login):
"""
Bans the given user. The reason is not visible to the user, yet must always be provided.
An error occurs if:
- The user is already banned.
- The user is trying to ban themself (it is, however, possible to unban yourself).
The user calling this method must be an admin.
"""
user = self.get_object()
if user.is_banned:
raise GenericAPIError("Пользователь '{}' уже в бане".format(user.login))
if user == request.user:
raise GenericAPIError("Не стоит банить самого себя (потеряете доступ в админку!)")
user.is_banned = True
user.save()
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save(user=user, admin=request.user, is_banned=True)
Event.objects.create(
typ=Event.BANNED,
sub=request.user,
user=user,
ip_address=request.META["REMOTE_ADDR"],
)
transaction.on_commit(send_notification.s(
user.id,
"Ваш аккаунт заблокирован. Для выяснения причин свяжитесь с пользователем @clubadm."
).delay)
transaction.on_commit(send_email.s(
user.id,
"Ваш аккаунт заблокирован",
"Приветствуем! Ваш аккаунт в Клубе Анонимных Дедов Морозов был заблокирован.\n\n" +
"Для выяснения причин свяжитесь с пользователем @clubadm на Хабре - возможно, ещё не всё потеряно!"
).delay)
return Response(serializer.data)
@action(
detail=True,
methods=["post"],
serializer_class=BanRecordSerializer,
)
def unban(self, request, login):
"""
Unbans the given user. The reason is not visible to the user, yet must always be provided.
The user calling this method must be an admin.
"""
user = self.get_object()
if not user.is_banned:
raise GenericAPIError("Пользователь '{}' уже разбанен".format(user.login))
user.is_banned = False
user.save()
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save(user=user, admin=request.user, is_banned=False)
Event.objects.create(
typ=Event.UNBANNED,
sub=request.user,
user=user,
ip_address=request.META["REMOTE_ADDR"],
)
transaction.on_commit(send_notification.s(
user.id,
"Ваш аккаунт разблокирован. Желаем вам счастливого Нового Года и Рождества! :-)"
).delay)
transaction.on_commit(send_email.s(
user.id,
"Ваш аккаунт разблокирован",
"Приветствуем!\n\n" +
"Ваш аккаунт в Клубе Анонимных Дедов Морозов был разблокирован.\n\n" +
"Поздравляем и желаем всего наилучшего в новом году!"
).delay)
return Response(serializer.data)
@action(detail=True, methods=["post"])
def allow_emails(self, request, login):
"""
Allows sending emails to this user.
Sometimes users click the unsubscribe link incidentally and ask us to resubscribe them again,
this method does exactly that.
The user calling this method must be an admin.
"""
user = self.get_object()
if user.email_allowed:
raise GenericAPIError("Пользователь '{}' уже подписан на email-уведомления".format(user.login))
user.email_allowed = True
user.save()
Event.objects.create(
typ=Event.SUBSCRIBED,
sub=request.user,
user=user,
ip_address=request.META["REMOTE_ADDR"],
)
serializer = self.get_serializer(user)
return Response(serializer.data)
class EventViewSet(viewsets.ReadOnlyModelViewSet):
permission_classes=[IsAdminUser]
serializer_class = EventSerializer
queryset = Event.objects.all()
class CountryViewSet(viewsets.ViewSet):
@method_decorator(cache_control(public=True, max_age=60 * 60 * 24 * 30))
def list(self, request):
"""
Lists all accepted countries.
"""
return Response(sorted([{
"code": code,
"name": name,
} for code, name in countries], key=lambda c: c["name"]))
class InfoView(APIView):
def get(self, request, format=None):
data = {
"csrf_token": get_token(request),
"is_authenticated": request.user.is_authenticated,
"is_active": False,
"can_participate": False,
"is_debug": settings.DEBUG,
}
if request.user.is_authenticated:
try:
data["username"] = request.user.login
data["avatar_url"] = request.user.avatar_url
data["karma"] = request.user.karma
data["is_readonly"] = request.user.is_readonly
data["has_badge"] = request.user.has_badge
data["is_active"] = not request.user.is_banned
data["can_participate"] = request.user.can_participate
except requests.exceptions.Timeout as e:
return Response({ "error": str(e) }, status=504)
return Response(data)
class LoginView(View):
def get(self, request):
next = request.GET.get("next")
redirect_uri = reverse("callback")
if url_has_allowed_host_and_scheme(next, None):
redirect_uri += "?" + urlencode({ "next": next }) # TODO: move to state
if settings.DEBUG:
authorize_url = reverse("fake_authorize")
else:
authorize_url = settings.HABR_LOGIN_URL
login_url = authorize_url + "?" + urlencode({
"client_id": settings.HABR_CLIENT_ID,
"response_type": "code",
"redirect_uri": request.build_absolute_uri(redirect_uri),
"state": get_token(request),
})
return HttpResponseRedirect(login_url)
class CallbackView(View):
def get(self, request):
# TODO: check state
next = request.GET.get("next")
if not url_has_allowed_host_and_scheme(next, None):
next = "/"
if request.user.is_authenticated:
# A lot of our users try to press the Back button immediately after login.
return HttpResponseRedirect(next)
code = request.GET.get("code")
if not code:
return render(request, "habrasanta/auth_error.html", status=500)
user = authenticate(request, authorization_code=code)
if not user:
return render(request, "habrasanta/auth_error.html", status=500)
login(request, user)
return HttpResponseRedirect(next)
class LogoutView(View):
def get(self, request):
# TODO: check CSRF token
logout(request)
next = request.GET.get("next")
if not url_has_allowed_host_and_scheme(next, None):
next = "/"
return HttpResponseRedirect(next)
class FakeAuthorizeView(View):
def get(self, request):
return render(request, "habrasanta/fake_authorize.html", {
"redirect_uri": request.GET.get("redirect_uri"),
"state": request.GET.get("state"),
})
def post(self, request):
url = urlparse(request.POST.get("redirect_uri"))
return HttpResponseRedirect(url.path + "?" + urlencode({
"code": request.POST.get("username"),
"state": request.POST.get("state"),
}) + "&" + url.query)
class UnsubscribeView(View):
def get(self, request):
try:
user = User.objects.get(habr_id=request.GET.get("uid"))
except User.DoesNotExist:
return render(request, "habrasanta/unsubscribed.html", {
"error": "пользователь с таким ID не найден",
})
if user.email_token != request.GET.get("token"):
return render(request, "habrasanta/unsubscribed.html", {
"error": "токен невалиден для этого пользователя",
})
if not user.email_allowed:
return render(request, "habrasanta/unsubscribed.html", {
"error": "у нас уже отмечено, что вы не хотите получать наши письма",
})
Event.objects.create(
typ=Event.UNSUBSCRIBED,
sub=user,
ip_address=request.META["REMOTE_ADDR"],
)
user.email_allowed = False
user.save()
return render(request, "habrasanta/unsubscribed.html", {
"email": user.email,
})