Mango /

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

import hashlib
import urllib
import urllib2

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

import mango
from mango.settings import *
    from mango.models import Subscription

def flush_cache(request):
    cache.delete('mango:toplevel:%s' % BASE_URL)
    return http.HttpResponse('Top-level cache flushed.', content_type=PLAIN)

def moderate(request, action):
    if not DISQUS:
        return http.HttpResponseServerError('Invalid DISQUS settings.',

    if request.GET.get('api_key') != hashlib.sha1(DISQUS_API_KEY).hexdigest():
        return http.HttpResponseBadRequest('Invalid API key.',

    post_id = request.GET.get('post_id')
    thread_id = request.GET.get('thread_id')

    if thread_id:
        if not FORUM:
            return http.HttpResponseServerError('Invalid DISQUS settings.', content_type=PLAIN)

        for thread in DISQUS.get_thread_list(FORUM, limit=9999):
            if == thread_id:
            return http.HttpResponseBadRequest('Invalid thread id.', content_type=PLAIN)

    if action == 'close':
        DISQUS.update_thread(FORUM, thread, allow_comments=False)
        message = 'Thread closed.'

    elif action == 'approve':
        comment = DISQUS.moderate_post(post_id, 'approve')
        message = 'Comment approved.'
        comment.thread.url = request.GET.get('url', comment.thread.url)
        if SUBSCRIPTIONS and comment.thread.url: # notify subscribers
            subject = u'New comment on "%s"' % comment.thread.title
            text_template = loader.get_template('email/subscriber.dtext')
            html_template = loader.get_template('email/subscriber.dhtml')
            for sub in Subscription.objects.filter(url=comment.thread.url):
                context = Context({'comment': comment, 'subscription_id':})
                msg = EmailMultiAlternatives(subject, text_template.render(context),
                        to=[u'%s <%s>' % (sub.subscriber_name, sub.subscriber_email)])
                msg.attach_alternative(html_template.render(context), 'text/html')
        DISQUS.moderate_post(post_id, 'kill')
        message = 'Comment deleted.'
        if action == 'spam':
            DISQUS.moderate_post(post_id, 'spam')
            message = 'Comment marked as spam and deleted.'

            if AKISMET_API_KEY and thread_id: # inform Akismet of its failure
                for comment in DISQUS.get_thread_posts(FORUM, thread, limit=9999):
                    if == post_id:
                    return http.HttpResponseServerError('Comment not found.', content_type=PLAIN)

                args = {
                    'blog': BASE_URL,
                    'user_ip': comment.ip_address,
                    'user_agent': '',
                    'referrer': comment.thread.url,
                    'permalink': comment.thread.url,
                    'comment_type': 'comment',
                    'comment_content': comment.message,
                if comment.is_anonymous:
                    args['comment_author'] =
                    url = comment.anonymous_author.url
                    args['comment_author'] =
                    url =

                if url:
                    args['comment_author_url'] = url

                req = urllib2.Request('' % AKISMET_API_KEY,
                        data=urllib.urlencode(args), headers={'User-Agent': 'Mango/%s' % mango.VERSION})
                f = urllib2.urlopen(req)

    return http.HttpResponse(message, content_type=PLAIN)

def redirect(request, path):
    return http.HttpResponseRedirect(reverse('', args=[path]))

def unsubscribe(request, path, subscription_id):
    subscription = get_object_or_404(Subscription, pk=subscription_id)
    return http.HttpResponse("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
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.