sdi/views.py
author anoop
Thu, 19 Aug 2010 20:06:45 +0530
branchanoop
changeset 232 1bfac108ed6c
parent 225 a942039ded62
child 236 af8cee5a0a27
permissions -rw-r--r--
added link to Gallery.

from django.shortcuts import render_to_response, redirect
from django.http import HttpResponse, Http404
from django.contrib.auth.decorators import login_required
from django.utils.datastructures import MultiValueDictKeyError

from django.contrib.auth import authenticate, login, logout

from sage_days.sdi.models import Registrant, RegistrantInfo, ParticipantInfo
from sage_days.sdi.forms import RegisterForm, SearchForm, EmailForm, LoginForm, UserSelectForm, ParticipantInfoForm, SendAccoForm
from sage_days.sdi.events import send_reg_complete_mail, mail_invi, send_sgd_ptc_confirm, send_cnf_email, send_wsp_ptc_confirm, send_acco_confirm_mail
from sage_days.settings import APACHE_URL_PREFIX as aup

def register(request):
    """ The user register page.
    """

    if request.method == "POST":
        form = RegisterForm(request.POST)
        if form.is_valid():
            user = form.save()

            data = form.cleaned_data
            first_name = data['first_name']
            last_name = data['last_name']
            email = data['email']
            send_reg_complete_mail(email, first_name, last_name)

            user_info = RegistrantInfo()
            user_info.registrant = user
            user_info.status_of_attending_sagedays = "1"
            user_info.status_of_attending_workshop = True if user.need_for_python_workshop else False
            user_info.status_of_accomodation = "1" if user.acco_required else "0"
            user_info.save()

            return redirect("/sage_days/registration/complete")
        else:
            return render_to_response("register.html", {"form":form})
    else:
        form = RegisterForm()
        return render_to_response("register.html", {"form":form})

def reg_complete(request):
    """ Tell the registration is successful.
    """

    return render_to_response("reg_complete.html")

@login_required
def list_stats(request):
    """ List the statiscs of registered participants.
    """

    if request.method == "POST":
        form = SearchForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data

            #need_workshop = data['need_for_python_workshop']
            #acco_required = data['need_accomodation']
            #db_query = "Registrant.objects.filter(need_for_python_workshop=%s, acco_required=%s)"%(need_workshop, acco_required)
            db_query = "Registrant.objects"

            topics_include, topics_exclude = data['topics_interested']
            for number in topics_include:
                db_query += '.filter(topics_interested__contains="%s")'%number

            for number in topics_exclude:
                db_query += '.exclude(topics_interested__contains="%s")'%number

            start_python, stop_python = data['knowledge_of_python']
            if start_python and stop_python:
                db_query += '.filter(knowledge_of_python__gte="%s")'%start_python
                db_query += '.filter(knowledge_of_python__lte="%s")'%stop_python
            elif start_python:
                db_query += '.filter(knowledge_of_python__exact="%s")'%start_python

            start_sage, stop_sage = data['knowledge_of_sage']
            if start_sage and stop_sage:
                db_query += '.filter(knowledge_of_sage__gte="%s")'%start_sage
                db_query += '.filter(knowledge_of_sage__lte="%s")'%stop_sage
            elif start_sage:
                db_query += '.filter(knowledge_of_sage__exact="%s")'%start_sage

            start_likeliness, stop_likeliness = data['likeliness_of_attending']
            if start_likeliness and stop_likeliness:
                db_query += '.filter(likeliness_of_attending__gte="%s")'%start_likeliness
                db_query += '.filter(likeliness_of_attending__lte="%s")'%stop_likeliness
            elif start_likeliness:
                db_query += '.filter(likeliness_of_attending__exact="%s")'%start_likeliness

            db_query += ".all()"

            matches = eval(db_query)
            return render_to_response("list_stats.html", {"form":form, 'matches':matches})
        else:
            return render_to_response("list_stats.html", {"form":form})
    else:
        form = SearchForm()
        return render_to_response("list_stats.html", {"form":form})

@login_required
def send_invi(request):
    """ Take a list of csv email addresses and send mails to them.
    """

    if request.method == "POST":
        form = EmailForm(request.POST)
        if form.is_valid():
            to_emails = form.cleaned_data['emails']
            mail_invi(to_emails)
            return render_to_response("send_invi.html", {"emails":to_emails})
        else:
            return render_to_response("send_invi.html", {"form":form})
    else:
        form = EmailForm()
        return render_to_response("send_invi.html", {"form":form})

@login_required
def send_workshop_confirm(request):
    """ Show a list of all the ppl who requested for a workshop and 
    send a confirmation mail to them if not sent.
    """

    not_selected_ppl = Registrant.objects.filter(need_for_python_workshop=True, registrantinfo__status_of_attending_workshop="1")
    not_confirmed_ppl = Registrant.objects.filter(need_for_python_workshop=True, registrantinfo__status_of_attending_workshop="2")
    attending_ppl = Registrant.objects.filter(need_for_python_workshop=True, registrantinfo__status_of_attending_workshop="3")

    choices = list(not_selected_ppl) + list(not_confirmed_ppl)
    form = UserSelectForm(choices, request.POST)
    if request.method == "POST" and form.is_valid():
        selected_users = form.cleaned_data['selected_users']
        for user in selected_users:
            user_info = user.registrantinfo_set.all()[0]
            user_info.status_of_attending_workshop = "2"
            user_info.save()

        send_wsp_ptc_confirm(user)

        return render_to_response("sent_wsp_confirm.html", {"selected_users":selected_users})
    else:
        return render_to_response("send_wsp_cnf.html", {"attending": attending_ppl,
                                                        "not_confirmed": not_confirmed_ppl,
                                                        "not_selected": not_selected_ppl,})

@login_required
def send_sagedays_confirm(request):
    """ filter out ppl depending on their status and display accordingly.
    """

    attending_ppl = Registrant.objects.filter(registrantinfo__status_of_attending_sagedays ="3")
    not_confirmed_ppl = Registrant.objects.filter(registrantinfo__status_of_attending_sagedays ="2")
    not_selected_ppl = Registrant.objects.filter(registrantinfo__status_of_attending_sagedays ="1")

    user_choices = list(not_selected_ppl) + list(not_confirmed_ppl)
    form = UserSelectForm(user_choices, request.POST)

    if request.method == "POST" and form.is_valid():
        selected_users = form.cleaned_data['selected_users']
        for user in selected_users:
            user_info = user.registrantinfo_set.all()[0]
            user_info.status_of_attending_sagedays = "2"
            user_info.save()

            send_sgd_ptc_confirm(user)
        
        return render_to_response("sent_sgd_confirm.html", {"selected_users":selected_users})
    else:
        return render_to_response("send_sgd_cnf.html", {"attending":attending_ppl, 
                                                "not_confirmed":not_confirmed_ppl,
                                                "not_selected":not_selected_ppl,
                                               })

@login_required
def send_acco_confirm(request):
    """ display list of confirmed participants who requested for accomodation
    and let the admin decide.
    """

    rejected_ppl = Registrant.objects.filter(
        registrantinfo__status_of_attending_sagedays ="3", 
        registrantinfo__status_of_accomodation="3")
    selected_ppl = Registrant.objects.filter(
        registrantinfo__status_of_attending_sagedays ="3", 
        registrantinfo__status_of_accomodation="2")
    not_selected_ppl = Registrant.objects.filter(
        registrantinfo__status_of_attending_sagedays ="3", 
        registrantinfo__status_of_accomodation="1")

    user_choices = list(rejected_ppl) + list(not_selected_ppl)
    form = SendAccoForm(user_choices, request.POST)

    if request.method == "POST" and form.is_valid():
        selected_users = form.cleaned_data['selected_users']
        message = form.cleaned_data['message']
        for user in selected_users:
            user_info = user.registrantinfo_set.all()[0]
            user_info.status_of_accomodation = "3"
            user_info.save()

        send_acco_confirm_mail(user, message)

        return render_to_response("sent_acco_confirm.html",
                                  {"selected_users":selected_users})
    else:
        return render_to_response("send_acco_cnf.html",
                                  {"rejected": rejected_ppl,
                                   "selected":selected_ppl,
                                   "not_selected":not_selected_ppl,
                                  })

def confirm_wsp_participation(request, uid):
    """ match id versus email and take lappy details.
    """
    return render_to_response("workshop_info.html")

def confirm_sgd_participation(request, uid):
    """ match id versus email and take lappy details.
    """

    try:
        email = request.GET['email']
    except MultiValueDictKeyError:
        raise Http404

    try:
        user = Registrant.objects.get(id=uid, email=email)
    except Registrant.DoesNotExist:
        raise Http404

    user_info = user.registrantinfo_set.all()[0]
    status = user_info.status_of_attending_sagedays
    if status == "3":
        return render_to_response("attending_sgd.html", {"user":user})
    elif status != "2":
        raise Http404

    special_request = [150,151,175,195,189,190]
    if int(uid) < 229 and int(uid) not in special_request:
        return render_to_response("late_for_confirmation.html", {"user":user})

    participant_info = ParticipantInfo()
    participant_info.participant = user

    form = ParticipantInfoForm(request.POST)
    if request.method == "POST" and form.is_valid():
        data = form.cleaned_data
        participant_info.has_laptop_for_sagedays = True if data['has_laptop_for_sagedays'] == "Yes" else False
        participant_info.sprinted_already = True if data['sprinted_already'] == "Yes" else False
        participant_info.will_sprint = data['will_sprint']
        participant_info.save()

        user_info.status_of_attending_sagedays = "3"
        user_info.save()

        send_cnf_email(user)

        return render_to_response("attending_sgd.html", {"user":user})
    else:
        return render_to_response("cnf_sgd_ptc.html", {"user":user})

def admin_login(request):
    """ basic login.
    """

    redirect_url = "/%s/registration/stats"%aup

    user = request.user
    if user.is_authenticated():
        return redirect(redirect_url)

    if request.method == "POST":
        form = LoginForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data

            username = data['username']
            password = data['password']

            user = authenticate(username=username, password=password)
            login(request, user)
            return redirect(redirect_url)
        else:
            return render_to_response("login.html", {"form":form})
    else:
        form = LoginForm()
        return render_to_response("login.html", {"form":form})

@login_required
def admin_logout(request):
    """ simply logout.
    """

    logout(request)
    return render_to_response("logout.html")

def homepage(request):
    return render_to_response("index.html")

def schedule(request):
    return render_to_response("schedule.html")

def organizers(request):
    return render_to_response("organizers.html")

def venue(request):
    return render_to_response("about_venue.html")

def contact(request):
    return render_to_response("contact.html")

def about(request):
    return render_to_response("about.html")
	
def accomodation(request):
    return render_to_response("accomodation.html")

def about_mumbai(request):
    return render_to_response("about_mumbai.html")

def reaching_iitb(request):
    return render_to_response("reaching_iitb.html")

def talks_proposed(request):
    return render_to_response("talks_proposed.html")

def sprint_info(request):
    """ display info on what a sprint is.
    """

    return render_to_response("sprint_info.html")

def workshop_info(request):
    """ disply info on workshop
    """

    return render_to_response("workshop_info.html")