Commits

Richard Jones committed e11641d

allow deletion of spammers

  • Participants
  • Parent commits 8fad0a1

Comments (0)

Files changed (3)

File pyweek/challenge/templates/challenge/user_display.html

 {{ profile_user.userprofile_set.all.0.content }}
 {% endautoescape %}
 
+{% if user.is_staff %}
+<form action="/u/{{ profile_user.name }}/delete_spam" method="POST">
+  <input type="submit" value="Delete Spammer">
+  <input type="checkbox" name="confirm">
+</form>
+{% endif %}
+
 <p>Entries this user has been in:</p>
 <table id="files">
 <tr><th>PyWeek</th><th>Name</th><th>Team?</th><th>Rating</th></tr>

File pyweek/challenge/urls.py

 
 urlpatterns += patterns('pyweek.challenge.views.user',
     url(r'^u/([\w\. \-\[\]]+)/$', 'user_display', name='user_display'),
+    url(r'^u/([\w\. \-\[\]]+)/delete_spam$', 'delete_spammer', name='delete_spammer'),
     (r'^profile_description/$', 'profile_description'),
 )
 

File pyweek/challenge/views/user.py

 from django.contrib import messages
 from django.shortcuts import render_to_response, get_object_or_404
 from django.template import RequestContext
-from django.http import HttpResponse, HttpResponseRedirect
+from django.http import HttpResponseRedirect
 from pyweek.challenge import models
 
-from stripogram import html2text, html2safehtml
+from stripogram import html2safehtml
+import collections
 
 safeTags = '''b a i br blockquote table tr td img pre p dl dd dt
     ul ol li span div'''.split()
 
+
 class SafeHTMLField(forms.CharField):
     widget = forms.Textarea
+
     def clean(self, value):
         if '<' in value:
             value = html2safehtml(value, safeTags)
-        if not value: raise forms.ValidationError(['This field is required'])
+        if not value:
+            raise forms.ValidationError(['This field is required'])
         return value
 
 
             'form': form,
         }, context_instance=RequestContext(request))
 
+
+def delete_spammer(request, user_id):
+    if not request.POST or 'confirm' not in request.POST:
+        return user_display(request, user_id)
+
+    user = models.User.objects.get(username__exact=user_id)
+    comments = list(models.DiaryComment.objects.filter(user=user))
+    d = collections.defaultdict(list)
+    for comment in comments:
+        d[comment.diary_entry].append(comment)
+
+    user.password = 'X'
+    user.save()
+
+    last = None
+    for diary_entry, comments in d.items():
+        # print 'ENTRY', diary_entry
+        for comment in diary_entry.diarycomment_set.all():
+            # print '...', comment, comment.user
+            if comment.user != user:
+                last = comment
+        if last is None:
+            # print '===', last
+            diary_entry.actor = diary_entry.user
+            diary_entry.last_comment = None
+            diary_entry.activity = diary_entry.created
+            diary_entry.reply_count = 0
+        else:
+            # print '<<<', last
+            diary_entry.last_comment = last
+            diary_entry.activity = last.created
+            diary_entry.actor = last.user
+            diary_entry.reply_count -= len(comments)
+        diary_entry.save()
+        for comment in comments:
+            # print '---', comment
+            comment.delete()
+
+    messages.success(request, 'Spammer deleted!')
+    return HttpResponseRedirect('/u/%s/' % user_id)