Mango /

# -*- 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):
    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':
        return text_response('Thread closed.')

    comment =
    cache_key = disqus.thread.cache_key(

    if action == 'approve':
        logger.debug('Cache key invalidated: %s' % cache_key)

            SubscriptionMessage.objects.get(url=thread_url, comment_id=post_id)
        except SubscriptionMessage.DoesNotExist:
            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':})
                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')

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

        return text_response('Comment approved.')

    if action == 'spam':
        message = 'Comment marked as spam.'
        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.
    logger.debug('Cache key invalidated: %s' % cache_key)

    return text_response(message)

def redirect(request, path, fragment=None):
    url = reverse('', 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)
    return text_response("You're no longer subscribed to this thread's comments.")