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}
    else:
        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, user=request.user)
            else:
                profile.content = content
            profile.save()
            messages.success(request, 'Description saved!')
            return HttpResponseRedirect('/profile_description/')
    else:
        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)

    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)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.