Mango / handlers.py

# -*- coding: utf-8 -*-

from django.core.cache import cache
from django.core.mail import EmailMultiAlternatives
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404
from django.template import Context, loader

import mango.settings
from mango.main import Index
from mango.utils import logger, text_response
if mango.settings.SUBSCRIPTIONS:
    from mango.models import Subscription, SubscriptionMessage

def flush_cache(request):
    Index().uncache()
    return text_response('Top-level cache flushed.')

def moderate(request, action):
    from mango import disqus

    if request.GET.get('api_key') != disqus.api_key:
        return text_response('Invalid API key.', 400)

    post_id = request.GET.get('post_id')
    if post_id is not None:
        post_id = int(post_id)
    thread_id = request.GET.get('thread_id')
    thread_url = request.GET.get('url')

    if action == 'close':
        disqus.thread.close(thread_id)
        return text_response('Thread closed.')

    comment = disqus.post.fetch(post_id)
    cache_key = disqus.thread.cache_key(comment.thread.id)

    if action == 'approve':
        disqus.post.approve(post_id)
        cache.delete(cache_key)
        logger.debug('Cache key invalidated: %s' % cache_key)

        try:
            SubscriptionMessage.objects.get(url=thread_url, comment_id=post_id)
        except SubscriptionMessage.DoesNotExist:
            pass
        else:
            return text_response('Comment approved.')

        if mango.settings.SUBSCRIPTIONS and thread_url: # notify subscribers
            subject = u'New comment on "%s"' % comment.thread.title
            text_template = loader.get_template('email/subscriber.text')
            html_template = loader.get_template('email/subscriber.html')

            for sub in Subscription.objects.filter(url=thread_url):
                ctx = Context({'comment': comment, 'subscription_id': sub.id})
                to = [u'%s <%s>' % (sub.subscriber_name, sub.subscriber_email)]
                msg = EmailMultiAlternatives(subject,
                                             text_template.render(ctx), to=to)
                msg.attach_alternative(html_template.render(ctx), 'text/html')
                msg.send(fail_silently=False)

            SubscriptionMessage(url=thread_url, comment_id=post_id).save()

        return text_response('Comment approved.')

    if action == 'spam':
        disqus.post.spam(post_id)
        message = 'Comment marked as spam.'
    else:
        disqus.post.remove(post_id)
        message = 'Comment deleted.'

    # Previously there was a check here for `not COMMENTS_REQUIRE_APPROVAL`.
    # This was a minor optimization based on the assumption that one would
    # only ever delete unmoderated comments: there's no need to invalidate
    # a cached thread when deleting an unmoderated comment if that comment
    # never saw the light of day. This assumption is faulty, though, since
    # deleting an _approved_ comment is a perfectly valid operation.
    cache.delete(cache_key)
    logger.debug('Cache key invalidated: %s' % cache_key)

    return text_response(message)

def redirect(request, path, fragment=None):
    url = reverse('mango.views.post', args=(path,))
    return HttpResponseRedirect('%s#%s' % (url, fragment) if fragment else url)

def unsubscribe(request, path, subscription_id):
    subscription = get_object_or_404(Subscription, pk=subscription_id)
    subscription.delete()
    return text_response("You're no longer subscribed to this thread's comments.")
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.