Mercurial > public > sg101
view forums/views/spam.py @ 943:cf9918328c64
Haystack tweaks for Django 1.7.7.
I had to upgrade to Haystack 2.3.1 to get it to work with Django
1.7.7. I also had to update the Xapian backend. But I ran into
problems.
On my laptop anyway (Ubuntu 14.0.4), xapian gets mad when search terms
are greater than 245 chars (or something) when indexing. So I created
a custom field that would simply omit terms greater than 64 chars and
used this field everywhere I previously used a CharField.
Secondly, the custom search form was broken now. Something changed in
the Xapian backend and exact searches stopped working. Fortunately the
auto_query (which I was using originally and broke during an upgrade)
started working again. So I cut the search form back over to doing an
auto_query. I kept the form the same (3 fields) because I didn't want
to change the form and I think it's better that way.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Wed, 13 May 2015 20:25:07 -0500 |
parents | 9e803323a0d0 |
children | e932f2ecd4a7 |
line wrap: on
line source
""" This module contains views for dealing with spam and spammers. """ import datetime import logging import textwrap from django.contrib.auth.decorators import login_required from django.core.urlresolvers import reverse from django.http import HttpResponseRedirect from django.shortcuts import get_object_or_404 from django.shortcuts import render_to_response from django.template import RequestContext from django.contrib.auth.models import User from forums.models import Post import forums.permissions as perms import bio.models from core.functions import email_admins from antispam.utils import deactivate_spammer SPAMMER_NAILED_SUBJECT = "Spammer Nailed: %s" SPAMMER_NAILED_MSG_BODY = """ The admin/moderator user %s has just deactivated the account of %s for spam. """ def promote_stranger(user): """This function upgrades the user from stranger status to a regular user. """ profile = user.profile if user.is_active and profile.status == bio.models.STA_STRANGER: profile.status = bio.models.STA_ACTIVE profile.status_date = datetime.datetime.now() profile.save(content_update=False) @login_required def spammer(request, post_id): """This view allows moderators to deactivate spammer accounts.""" post = get_object_or_404(Post.objects.select_related(), pk=post_id) poster = post.user poster_profile = poster.profile can_moderate = perms.can_moderate(post.topic.forum, request.user) can_deactivate = (poster_profile.status == bio.models.STA_STRANGER and not poster.is_superuser) if request.method == "POST" and can_moderate and can_deactivate: deactivate_spammer(poster) email_admins(SPAMMER_NAILED_SUBJECT % poster.username, SPAMMER_NAILED_MSG_BODY % ( request.user.username, poster.username)) logging.info(textwrap.dedent("""\ SPAMMER DEACTIVATED: %s nailed %s for spam. IP: %s Message: %s """), request.user.username, poster.username, post.user_ip, post.body) return HttpResponseRedirect(reverse('forums-spammer_nailed', args=[ poster.id])) return render_to_response('forums/spammer.html', { 'can_moderate': can_moderate, 'can_deactivate': can_deactivate, 'post': post, }, context_instance=RequestContext(request)) @login_required def spammer_nailed(request, spammer_id): """This view presents a confirmation screen that the spammer has been deactivated. """ user = get_object_or_404(User, pk=spammer_id) profile = user.profile success = not user.is_active and profile.status == bio.models.STA_SPAMMER return render_to_response('forums/spammer_nailed.html', { 'spammer': user, 'success': success, }, context_instance=RequestContext(request)) @login_required def stranger(request, post_id): """This view allows a forum moderator or super user to promote a user from stranger status to regular user. """ post = get_object_or_404(Post.objects.select_related(), pk=post_id) poster = post.user poster_profile = poster.profile can_moderate = perms.can_moderate(post.topic.forum, request.user) can_promote = poster_profile.status == bio.models.STA_STRANGER if request.method == "POST" and can_moderate and can_promote: promote_stranger(poster) logging.info("STRANGER PROMOTED: %s promoted %s.", request.user.username, poster.username) return HttpResponseRedirect(post.get_absolute_url()) return render_to_response('forums/stranger.html', { 'can_moderate': can_moderate, 'can_promote': can_promote, 'post': post, }, context_instance=RequestContext(request))