bgneal@232
|
1 """
|
bgneal@232
|
2 This module contains views for dealing with spam and spammers.
|
bgneal@232
|
3 """
|
bgneal@232
|
4 import datetime
|
bgneal@232
|
5 import logging
|
bgneal@232
|
6 import textwrap
|
bgneal@232
|
7
|
bgneal@232
|
8 from django.contrib.auth.decorators import login_required
|
bgneal@232
|
9 from django.core.urlresolvers import reverse
|
bgneal@232
|
10 from django.http import HttpResponseRedirect
|
bgneal@232
|
11 from django.shortcuts import get_object_or_404
|
bgneal@1032
|
12 from django.shortcuts import render
|
bgneal@232
|
13 from django.contrib.auth.models import User
|
bgneal@232
|
14
|
bgneal@232
|
15 from forums.models import Post
|
bgneal@460
|
16 import forums.permissions as perms
|
bgneal@232
|
17 import bio.models
|
bgneal@232
|
18 from core.functions import email_admins
|
bgneal@563
|
19 from antispam.utils import deactivate_spammer
|
bgneal@232
|
20
|
bgneal@232
|
21
|
bgneal@232
|
22 SPAMMER_NAILED_SUBJECT = "Spammer Nailed: %s"
|
bgneal@232
|
23 SPAMMER_NAILED_MSG_BODY = """
|
bgneal@232
|
24 The admin/moderator user %s has just deactivated the account of %s for spam.
|
bgneal@232
|
25 """
|
bgneal@232
|
26
|
bgneal@232
|
27
|
bgneal@232
|
28 def promote_stranger(user):
|
bgneal@232
|
29 """This function upgrades the user from stranger status to a regular user.
|
bgneal@232
|
30 """
|
bgneal@789
|
31 profile = user.profile
|
bgneal@232
|
32 if user.is_active and profile.status == bio.models.STA_STRANGER:
|
bgneal@232
|
33 profile.status = bio.models.STA_ACTIVE
|
bgneal@232
|
34 profile.status_date = datetime.datetime.now()
|
bgneal@562
|
35 profile.save(content_update=False)
|
bgneal@232
|
36
|
bgneal@232
|
37
|
bgneal@232
|
38 @login_required
|
bgneal@232
|
39 def spammer(request, post_id):
|
bgneal@232
|
40 """This view allows moderators to deactivate spammer accounts."""
|
bgneal@232
|
41
|
bgneal@232
|
42 post = get_object_or_404(Post.objects.select_related(), pk=post_id)
|
bgneal@232
|
43 poster = post.user
|
bgneal@789
|
44 poster_profile = poster.profile
|
bgneal@232
|
45
|
bgneal@460
|
46 can_moderate = perms.can_moderate(post.topic.forum, request.user)
|
bgneal@232
|
47 can_deactivate = (poster_profile.status == bio.models.STA_STRANGER and not
|
bgneal@232
|
48 poster.is_superuser)
|
bgneal@232
|
49
|
bgneal@232
|
50 if request.method == "POST" and can_moderate and can_deactivate:
|
bgneal@232
|
51 deactivate_spammer(poster)
|
bgneal@232
|
52
|
bgneal@316
|
53 email_admins(SPAMMER_NAILED_SUBJECT % poster.username,
|
bgneal@232
|
54 SPAMMER_NAILED_MSG_BODY % (
|
bgneal@232
|
55 request.user.username, poster.username))
|
bgneal@232
|
56
|
bgneal@232
|
57 logging.info(textwrap.dedent("""\
|
bgneal@232
|
58 SPAMMER DEACTIVATED: %s nailed %s for spam.
|
bgneal@232
|
59 IP: %s
|
bgneal@232
|
60 Message:
|
bgneal@232
|
61 %s
|
bgneal@316
|
62 """),
|
bgneal@316
|
63 request.user.username, poster.username, post.user_ip, post.body)
|
bgneal@232
|
64
|
bgneal@232
|
65 return HttpResponseRedirect(reverse('forums-spammer_nailed', args=[
|
bgneal@232
|
66 poster.id]))
|
bgneal@232
|
67
|
bgneal@1032
|
68 return render(request, 'forums/spammer.html', {
|
bgneal@232
|
69 'can_moderate': can_moderate,
|
bgneal@232
|
70 'can_deactivate': can_deactivate,
|
bgneal@232
|
71 'post': post,
|
bgneal@1032
|
72 })
|
bgneal@232
|
73
|
bgneal@232
|
74
|
bgneal@232
|
75 @login_required
|
bgneal@232
|
76 def spammer_nailed(request, spammer_id):
|
bgneal@232
|
77 """This view presents a confirmation screen that the spammer has been
|
bgneal@232
|
78 deactivated.
|
bgneal@232
|
79 """
|
bgneal@232
|
80 user = get_object_or_404(User, pk=spammer_id)
|
bgneal@789
|
81 profile = user.profile
|
bgneal@232
|
82
|
bgneal@232
|
83 success = not user.is_active and profile.status == bio.models.STA_SPAMMER
|
bgneal@232
|
84
|
bgneal@1032
|
85 return render(request, 'forums/spammer_nailed.html', {
|
bgneal@232
|
86 'spammer': user,
|
bgneal@232
|
87 'success': success,
|
bgneal@1032
|
88 })
|
bgneal@232
|
89
|
bgneal@232
|
90
|
bgneal@232
|
91 @login_required
|
bgneal@232
|
92 def stranger(request, post_id):
|
bgneal@232
|
93 """This view allows a forum moderator or super user to promote a user from
|
bgneal@232
|
94 stranger status to regular user.
|
bgneal@232
|
95 """
|
bgneal@232
|
96 post = get_object_or_404(Post.objects.select_related(), pk=post_id)
|
bgneal@232
|
97 poster = post.user
|
bgneal@789
|
98 poster_profile = poster.profile
|
bgneal@232
|
99
|
bgneal@460
|
100 can_moderate = perms.can_moderate(post.topic.forum, request.user)
|
bgneal@232
|
101 can_promote = poster_profile.status == bio.models.STA_STRANGER
|
bgneal@232
|
102
|
bgneal@232
|
103 if request.method == "POST" and can_moderate and can_promote:
|
bgneal@232
|
104 promote_stranger(poster)
|
bgneal@232
|
105
|
bgneal@316
|
106 logging.info("STRANGER PROMOTED: %s promoted %s.",
|
bgneal@316
|
107 request.user.username, poster.username)
|
bgneal@232
|
108
|
bgneal@232
|
109 return HttpResponseRedirect(post.get_absolute_url())
|
bgneal@232
|
110
|
bgneal@1032
|
111 return render(request, 'forums/stranger.html', {
|
bgneal@232
|
112 'can_moderate': can_moderate,
|
bgneal@232
|
113 'can_promote': can_promote,
|
bgneal@232
|
114 'post': post,
|
bgneal@1032
|
115 })
|