2 |
2 |
from django.core.exceptions import ValidationError
|
3 |
3 |
from django.db import transaction
|
4 |
4 |
from django.http import HttpResponse, HttpResponseRedirect
|
|
5 |
from django.template.context import RequestContext
|
5 |
6 |
from django.template.loader import render_to_string
|
6 |
7 |
from django.core.validators import validate_email
|
|
8 |
from django.views.decorators.csrf import csrf_protect
|
7 |
9 |
|
8 |
10 |
from synnefo.api.common import method_not_allowed
|
9 |
11 |
from synnefo.db.models import Invitations, SynnefoUser
|
10 |
12 |
from synnefo.logic import users
|
11 |
13 |
|
12 |
|
import json
|
13 |
|
|
14 |
14 |
def send_emails(request):
|
15 |
|
errors = ()
|
|
15 |
errors = []
|
16 |
16 |
valid_inv = filter(lambda x: x.startswith("name_"), request.POST.keys())
|
17 |
17 |
|
18 |
18 |
for inv in valid_inv:
|
... | ... | |
22 |
22 |
email = request.POST['email_' + inv_id]
|
23 |
23 |
name = request.POST[inv]
|
24 |
24 |
|
25 |
|
validate_email(email)
|
26 |
25 |
validate_name(name)
|
|
26 |
validate_email(email)
|
|
27 |
|
27 |
28 |
add_invitation(request.user, name, email)
|
|
29 |
|
28 |
30 |
except Exception as e:
|
29 |
|
errors += e.message
|
30 |
|
except ValidationError as v:
|
31 |
|
errors += v.message
|
|
31 |
errors += ["Invitation to %s <%s> not sent. Reason: %s"%(name, email, e.messages[0])]
|
|
32 |
|
|
33 |
respose = None
|
|
34 |
if errors:
|
|
35 |
data = render_to_string('invitations.html',
|
|
36 |
{'invitations': invitations_for_user(request),
|
|
37 |
'errors': errors},
|
|
38 |
context_instance=RequestContext(request))
|
|
39 |
response = HttpResponse(data)
|
|
40 |
else:
|
|
41 |
response = HttpResponseRedirect("/invitations/")
|
32 |
42 |
|
33 |
|
response = HttpResponseRedirect("/invitations/")
|
34 |
43 |
return response
|
35 |
44 |
|
36 |
45 |
def validate_name(name):
|
37 |
46 |
if name is None or name.strip() == '' :
|
38 |
|
raise ValidationError("Name cannot be empty")
|
|
47 |
raise ValidationError("Name is empty")
|
|
48 |
|
|
49 |
if name.find(' ') is -1:
|
|
50 |
raise ValidationError("Name must contain at least one space")
|
39 |
51 |
|
40 |
52 |
return True
|
41 |
53 |
|
... | ... | |
59 |
71 |
|
60 |
72 |
return invitation
|
61 |
73 |
|
|
74 |
@csrf_protect
|
62 |
75 |
def inv_demux(request):
|
63 |
76 |
if request.method == 'GET':
|
64 |
77 |
data = render_to_string('invitations.html',
|
65 |
|
{'invitations': invitations_for_user(request)})
|
|
78 |
{'invitations': invitations_for_user(request)},
|
|
79 |
context_instance=RequestContext(request))
|
66 |
80 |
return HttpResponse(data)
|
67 |
81 |
elif request.method == 'POST':
|
68 |
82 |
return send_emails(request)
|
... | ... | |
78 |
92 |
num_inv = Invitations.objects.filter(source = source).count()
|
79 |
93 |
|
80 |
94 |
if num_inv >= settings.MAX_INVITATIONS:
|
81 |
|
raise TooManyInvitations(source)
|
|
95 |
raise TooManyInvitations("User invitation limit (%d) exhausted" % settings.MAX_INVITATIONS)
|
82 |
96 |
|
83 |
97 |
target = SynnefoUser.objects.filter(name = name, uniq = email)
|
84 |
98 |
|
85 |
99 |
if target.count() is not 0:
|
86 |
|
raise AlreadyInvited("User already invited: %s <%s>" % (name, email))
|
|
100 |
raise AlreadyInvited("User %s <%s> already invited" % (name, email))
|
87 |
101 |
|
88 |
102 |
users.register_user(name, email)
|
89 |
103 |
|
... | ... | |
106 |
120 |
invitation.accepted = True
|
107 |
121 |
invitation.save()
|
108 |
122 |
|
109 |
|
class TooManyInvitations(BaseException):
|
110 |
123 |
|
111 |
|
def __init__(self, source):
|
112 |
|
self.source = source
|
|
124 |
class TooManyInvitations(Exception):
|
|
125 |
|
|
126 |
def __init__(self, msg):
|
|
127 |
self.messages.append(msg)
|
113 |
128 |
|
114 |
129 |
|
115 |
|
class AlreadyInvited(BaseException):
|
|
130 |
class AlreadyInvited(Exception):
|
|
131 |
messages = []
|
116 |
132 |
|
117 |
133 |
def __init__(self, msg):
|
118 |
|
self.msg = msg
|
|
134 |
self.messages.append(msg)
|