ad738d46a28c6293e5ea4ea65aa587328bf1f1dd
[astakos] / astakos / im / backends / invitations.py
1 # Copyright 2011 GRNET S.A. All rights reserved.
2 #
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
5 # conditions are met:
6 #
7 #   1. Redistributions of source code must retain the above
8 #      copyright notice, this list of conditions and the following
9 #      disclaimer.
10 #
11 #   2. Redistributions in binary form must reproduce the above
12 #      copyright notice, this list of conditions and the following
13 #      disclaimer in the documentation and/or other materials
14 #      provided with the distribution.
15 #
16 # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
28 #
29 # The views and conclusions contained in the software and
30 # documentation are those of the authors and should not be
31 # interpreted as representing official policies, either expressed
32 # or implied, of GRNET S.A.
33
34 from astakos.im.forms import InvitedLocalRegisterForm, LocalRegisterForm
35 from astakos.im.models import AstakosUser, Invitation
36
37 class Backend(object):
38     def get_signup_form(self, request):
39         code = request.GET.get('code', '')
40         formclass = 'LocalRegisterForm'
41         if request.method == 'GET':
42             initial_data = None
43             if code:
44                 formclass = 'InvitedLocalRegiterForm'
45                 invitation = Invitation.objects.get(code=code)
46                 if invitation.is_consumed:
47                     return HttpResponseBadRequest('Invitation has beeen used')
48                 initial_data.update({'username':invitation.username,
49                                        'email':invitation.username,
50                                        'realname':invitation.realname})
51                 inviter = AstakosUser.objects.get(username=invitation.inviter)
52                 initial_data['inviter'] = inviter.realname
53         else:
54             initial_data = request.POST
55         return globals()[formclass](initial_data)
56     
57     def is_preaccepted(user, code):
58         invitation = self.invitation
59         if invitation and not invitation.is_consumed and invitation.code == code:
60             return True
61         return False
62     
63     def signup(self, request, form):
64         kwargs = {}
65         for field in form.fields:
66             if hasattr(AstakosUser(), field):
67                 kwargs[field] = form.cleaned_data[field]
68         user = get_or_create_user(**kwargs)
69         
70         code = request.POST.get('code')
71         if is_preaccepted(user, code):
72             user.is_active = True
73             user.save()
74             message = _('Registration completed. You can now login.')
75             next = request.POST.get('next')
76             if next:
77                 return redirect(next)
78         else:
79             message = _('Registration completed. You will receive an email upon your account\'s activation')
80         status = 'success'
81         return status, message