# -*- coding: utf-8 -*- from django.http import HttpResponse, HttpResponseRedirect from django.template import Context, loader from django.shortcuts import render_to_response from django.core.urlresolvers import reverse from django.utils.translation import gettext as _ import re from mailman_rest_client import MailmanRESTClient, MailmanRESTClientError from forms import * def login_required(fn): """Function (decorator) letting the user log in. """ def _login_decorator(*request, **kwargs): """Inner decorator to login. """ # If the user is already logged in, let them continue directly. try: if request[0].session['member_id']: return fn(request[0], **kwargs) except: pass template = 'mailman-django/lists/login.html' # Authenticate the user # This is just a mockup since the authenticate functionality in # the rest server is still missing. # TODO Anna 2010-08-04: implement real authenticate when possible valid_users = {"james@example.com": "james", "katie@example.com": "katie", "kevin@example.com": "kevin"} if request[0].method == 'POST': form = Login(request[0].POST) if form.is_valid(): if request[0].POST["address"] in valid_users.keys(): if request[0].POST["password"] == valid_users[request[0].POST["address"]]: request[0].session['member_id'] = request[0].POST["address"] # make sure to "reset" the method before continuing request[0].method = 'GET' return fn(request[0], **kwargs) message = "Your username and password didn't match." else: message = "" return render_to_response(template, {'form': Login(), 'message': message}) return _login_decorator @login_required def list_new(request, template = 'mailman-django/lists/new.html'): """Show or process form to add a new mailing list. """ if request.method == 'POST': form = ListNew(request.POST) if form.is_valid(): listname = form.cleaned_data['listname'] try: c = MailmanRESTClient('localhost:8001') except Exception, e: return HttpResponse(e) parts = listname.split('@') domain = c.get_domain(parts[1]) if domain.info == 404: # failed to get domain so try creating one try: domain = c.create_domain(parts[1]) except MailmanRESTClientError, e: # I don't think this error can ever appear... -- Anna return HttpResponse(e) try: response = domain.create_list(parts[0]) return render_to_response('mailman-django/lists/created.html', {'fqdn_listname': response.info['fqdn_listname'] }) except MailmanRESTClientError, e: return HttpResponse(e) else: form = ListNew() return render_to_response(template, {'form': form}) def list_index(request, template = 'mailman-django/lists/index.html'): """Show a table of all mailing lists. """ try: c = MailmanRESTClient('localhost:8001') except MailmanRESTClientError, e: return render_to_response('mailman-django/errors/generic.html', {'message': e}) try: lists = c.get_lists() return render_to_response(template, {'lists': lists}) except MailmanRESTClientError, e: return render_to_response('mailman-django/errors/generic.html', {'message': e}) def list_info(request, fqdn_listname = None, template = 'mailman-django/lists/info.html'): """Display list info and/or subscribe or unsubscribe a user to a list. """ try: c = MailmanRESTClient('localhost:8001') the_list = c.get_list(fqdn_listname) except Exception, e: return HttpResponse(e) if request.method == 'POST': form = False action = request.POST.get('name', '') if action == "subscribe": form = ListSubscribe(request.POST) elif action == "unsubscribe": form = ListUnsubscribe(request.POST) if form and form.is_valid(): listname = form.cleaned_data['listname'] email = form.cleaned_data['email'] if action == "subscribe": real_name = form.cleaned_data.get('real_name', '') try: response = the_list.subscribe(address=email, real_name=real_name) return HttpResponseRedirect(reverse('list_index')) except Exception, e: return HttpResponse(e) elif action == "unsubscribe": try: response = the_list.unsubscribe(address=email) return render_to_response('mailman-django/lists/unsubscribed.html', {'listname': fqdn_listname}) except Exception, e: return HttpResponse(e) else: if action == "subscribe": subscribe = ListSubscribe(request.POST) unsubscribe = ListUnsubscribe(initial = {'listname': fqdn_listname, 'name' : 'unsubscribe'}) elif action == "unsubscribe": subscribe = ListSubscribe(initial = {'listname': fqdn_listname, 'name' : 'subscribe'}) unsubscribe = ListUnsubscribe(request.POST) else: subscribe = ListSubscribe(initial = {'listname': fqdn_listname, 'name' : 'subscribe'}) unsubscribe = ListUnsubscribe(initial = {'listname': fqdn_listname, 'name' : 'unsubscribe'}) listinfo = c.get_list(fqdn_listname) return render_to_response(template, {'subscribe': subscribe, 'unsubscribe': unsubscribe, 'fqdn_listname': fqdn_listname, 'listinfo': listinfo}) def list_delete(request, fqdn_listname = None, template = 'mailman-django/lists/index.html'): """Delete a list. """ # create a connection to Mailman and get the list try: c = MailmanRESTClient('localhost:8001') the_list = c.get_list(fqdn_listname) except Exception, e: return HttpResponse(e) # get the parts of the list necessary to delete it parts = fqdn_listname.split('@') domain = the_list.get_domain(parts[1]) domain.delete_list(parts[0]) # let the user return to the list index page try: lists = c.get_lists() return render_to_response(template, {'lists': lists}) except MailmanRESTClientError, e: return render_to_response('mailman-django/errors/generic.html', {'message': e}) @login_required def list_settings(request, fqdn_listname = None, template = 'mailman-django/lists/settings.html'): """The settings of a list.""" message = "" try: c = MailmanRESTClient('localhost:8001') the_list = c.get_list(fqdn_listname) except Exception, e: return HttpResponse(e) if request.method == 'POST': form = ListSettings(request.POST) if form.is_valid(): the_list.update_list(request.POST) message = "The list has been updated." else: form = ListSettings(the_list.info) return render_to_response(template, {'form': form, 'message': message, 'fqdn_listname': the_list.info['fqdn_listname']}) @login_required def mass_subscribe(request, fqdn_listname = None, template = 'mailman-django/lists/mass_subscribe.html'): """Mass subscribe users to a list.""" message = "" try: c = MailmanRESTClient('localhost:8001') the_list = c.get_list(fqdn_listname) except Exception, e: return HttpResponse(e) if request.method == 'POST': form = ListMassSubscription(request.POST) if form.is_valid(): try: emails = request.POST["emails"].splitlines() message = "The mass subscription was successful." for email in emails: # very simple test if email address is valid parts = email.split('@') if len(parts) == 2 and '.' in parts[1]: the_list.subscribe(address=email, real_name="") else: message = "Please enter valid email addresses." except Exception, e: return HttpResponse(e) else: form = ListMassSubscription() return render_to_response(template, {'form': form, 'message': message, 'fqdn_listname': the_list.info['fqdn_listname']}) def logout(request): """Let the user logout. """ try: del request.session['member_id'] except KeyError: pass return list_index(request, template = 'mailman-django/lists/index.html')