from django.http import HttpResponse
from django.shortcuts import render

from cparte.models import Campaign, MetaChannel, Channel

import logging


logger = logging.getLogger(__name__)


meta_channel = MetaChannel(initiative="California Report Card")


def index(request):
    return HttpResponse("Welcome to the CParte application!")


def posts(request):
    latest_campaigns = Campaign.objects.all()[:5]
    context = {'latest_campaigns': latest_campaigns}
    return render(request, 'cparte/posts.html', context)


def listen(request, channel):
    app_account_id = Channel.objects.get(name=channel).app_account_id
    if channel == "all":
        msg = "Start listening all channels..."
        meta_channel.authenticate()
        meta_channel.listen(followings=app_account_id)
    else:
        msg = "Start listening %s channel..." % channel
        meta_channel.authenticate(channel)
        meta_channel.listen(channel=channel, followings=[app_account_id])
    #request.session['meta_channel'] = serializers.serialize("json", meta_ch, ignorenonexistent=True)
    logger.info(msg)
    return HttpResponse(msg)


def hangup(request, channel):
    #meta_ch = serializers.deserialize("json", request.session['meta_channel'])
    if meta_channel is not None:
        if channel == "all":
            meta_channel.disconnect()
            msg = "All channels disconnected..."
        else:
            meta_channel.disconnect(channel)
            msg = "Channel %s has been disconnected..." % channel
    else:
        msg = "No channels listening..."
    logger.info(msg)
    return HttpResponse(msg)
@@ -1,20 +1,49 @@
+from threading import Thread%0A
 from django.http imp
@@ -278,29 +278,209 @@
 d%22)%0A
-%0A%0Adef index(request):
+do_threading = True%0A%0A%0Adef count_and_print():%0A    a = 0%0A    while a %3C= 1000000:%0A        a += 1%0A        logger.info(a)%0A%0A%0Adef index(request):%0A    thread = Thread(target=count_and_print)%0A    thread.start()
 %0A   
@@ -556,24 +556,49 @@
 s(request):%0A
+    do_threading = False%0A
     latest_c

Add testing method