1 from django.http import HttpResponse, HttpResponseRedirect
2 from django.contrib.auth.models import Group
3 from django.contrib.auth.decorators import login_required
4 from django.shortcuts import render_to_response
5 from django.conf import settings
6 from madrona.group_management.models import GroupRequest
7 from django.core.urlresolvers import reverse
8 from django.core.mail import send_mail
9 from django.template.loader import get_template
10 from django.template import Context
14 current_groups = request.user.groups.all()
15
16 if request.method == 'GET':
17 groups = Group.objects.all()
18 if len(groups) == 0:
19 return HttpResponse("There are no potential groups at this time.", status=404)
20 return render_to_response('group_management/group_request_form.html',
21 {'user':request.user, 'groups': groups, 'current_groups': current_groups, 'MEDIA_URL':settings.MEDIA_URL})
22 elif request.method == 'POST':
23 group_ids = [int(x) for x in request.POST.getlist('sharing_groups')]
24
25 groups = Group.objects.filter(pk__in=group_ids).exclude(pk__in=[x.pk for x in current_groups])
26 for group in groups:
27 gr = GroupRequest(user=request.user, group=group)
28 gr.save()
29 if settings.GROUP_REQUEST_EMAIL:
30 t = get_template('group_management/email_subject.txt')
31 subject = t.render(Context({'user': request.user, 'groups': groups})).strip()
32
33 t = get_template('group_management/email_body.txt')
34 body = t.render(Context({'user': request.user, 'groups': groups}))
35
36 send_mail(subject, body, settings.GROUP_REQUEST_EMAIL, [settings.GROUP_REQUEST_EMAIL], fail_silently=False)
37
38 return HttpResponseRedirect(reverse('group_management-request-sent'))
39 else:
40 return HttpResponse("Received unexpected " + request.method + " request.", status=400)
41