Statistics
| Branch: | Tag: | Revision:

root / invitations / invitations.py @ 2a0eee64

History | View | Annotate | Download (2.5 kB)

1
from django import forms
2
from django.conf import settings
3
from django.db import transaction
4
from django.http import HttpResponse, HttpResponseRedirect
5
from django.shortcuts import render_to_response
6
from django.template.loader import render_to_string
7
from synnefo.api.common import method_not_allowed
8
from synnefo.db.models import Invitations, SynnefoUser
9
from synnefo.logic import users
10

    
11
class InvitationForm(forms.Form):
12
    emails = forms.Textarea
13

    
14
    def send_emails(self, request):
15
        if request.method == 'POST': # If the form has been submitted...
16
            form = InvitationForm(request.POST) # A form bound to the POST data
17
            if form.is_valid(): # All validation rules pass
18
                # Process the data in form.cleaned_data
19
                # ...
20
                return HttpResponseRedirect('/thanks/') # Redirect after POST
21
        else:
22
            form = InvitationForm() # An unbound form
23

    
24
        return render_to_response('invitation.html', {'form': form,})
25

    
26
def inv_demux(request):
27
    if request.method == 'GET':
28
        invitations = Invitations.objects.filter(source = request.user)
29
        data = render_to_string('invitations.html', {'invitations': invitations})
30
        return  HttpResponse(data)
31
    elif request.method == 'POST':
32
        f = InvitationForm(request)
33
    else:
34
        method_not_allowed(request)
35

    
36
@transaction.commit_on_success
37
def add_invitation(source, name, email):
38
    """
39
        Adds an invitation, if the source user has not gone over his/her
40
        invitation count or the target user has not been invited already
41
    """
42
    num_inv = Invitations.objects.filter(source = source).count()
43

    
44
    if num_inv >= settings.MAX_INVITATIONS:
45
        raise TooManyInvitations(source)
46

    
47
    target = SynnefoUser.objects.filter(name = name, uniq = email)
48

    
49
    if target.count() is not 0:
50
        raise AlreadyInvited("User already invited: %s <%s>" % (name, email))
51

    
52
    users.register_user(name, email)
53

    
54
    target = SynnefoUser.objects.filter(uniq = email)
55

    
56
    r = list(target[:1])
57
    if not r:
58
        raise Exception
59

    
60
    inv = Invitations()
61
    inv.source = source
62
    inv.target = target[0]
63
    inv.save()
64

    
65
@transaction.commit_on_success
66
def invitation_accepted(invitation):
67
    """
68
        Mark an invitation as accepted
69
    """
70
    invitation.accepted = True
71
    invitation.save()
72

    
73

    
74
class TooManyInvitations(BaseException):
75

    
76
    def __init__(self, source):
77
        self.source = source
78

    
79

    
80
class AlreadyInvited(BaseException):
81

    
82
    def __init__(self, msg):
83
        self.msg = msg