1. Richard Jones
  2. pyweek


pyweek / pyweek / challenge / views / user.py

from django import forms
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 HttpResponseRedirect
from pyweek.challenge import models

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'])
        return value

def user_display(request, user_id):
    user = get_object_or_404(models.User, username=user_id)

    entries = models.Entry.objects.filter(
        challenge__number__lt=1000, users=user)
    given_awards = user.award_set.all()
    received_awards = models.EntryAward.objects.filter(
        challenge__number__lt=1000, entry__users=user)
    return render_to_response('challenge/user_display.html',
            'profile_user': user,
            'entries': entries,
            'given_awards': given_awards,
            'received_awards': received_awards,
        }, context_instance=RequestContext(request))

class ProfileForm(forms.Form):
    content = SafeHTMLField(label='Text to appear on your profile page',
        help_text='Basic HTML tags allowed: %s' % (', '.join(safeTags)))

def profile_description(request):
    if request.user.is_anonymous():
        return HttpResponseRedirect('/login/')

    profile = request.user.userprofile_set.all()
    if profile:
        profile = profile[0]
        data = {'content': profile.content}
        data = {}
        profile = None

    if request.POST:
        form = ProfileForm(request.POST)
        if form.is_valid():
            content = form.cleaned_data['content']

            # do the save
            if profile is None:
                profile = models.UserProfile(content=content,
                profile.content = content
            messages.success(request, 'Description saved!')
            return HttpResponseRedirect('/profile_description/')
        form = ProfileForm(data)
    return render_to_response('challenge/profile_description.html',
            '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)

    if not request.user.is_staff:
        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:

    user.password = 'X'

    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
            # print '<<<', last
            diary_entry.last_comment = last
            diary_entry.activity = last.created
            diary_entry.actor = last.user
            diary_entry.reply_count -= len(comments)
        for comment in comments:
            # print '---', comment

    messages.success(request, 'Spammer deleted!')
    return HttpResponseRedirect('/u/%s/' % user_id)