19 |
19 |
20 __authors__ = [ |
20 __authors__ = [ |
21 '"Pawel Solyga" <pawel.solyga@gmail.com>', |
21 '"Pawel Solyga" <pawel.solyga@gmail.com>', |
22 ] |
22 ] |
23 |
23 |
|
24 import re |
24 |
25 |
25 from google.appengine.api import users |
26 from google.appengine.api import users |
26 from django import http |
27 from django import http |
27 from django import shortcuts |
28 from django import shortcuts |
28 from django import newforms as forms |
29 from django import newforms as forms |
29 |
30 |
30 from soc.models import user |
|
31 from soc.views.helpers import forms_helpers |
31 from soc.views.helpers import forms_helpers |
32 from soc.views.helpers import response_helpers |
32 from soc.views.helpers import response_helpers |
33 |
33 |
|
34 import soc.models.user |
34 |
35 |
35 class UserForm(forms_helpers.DbModelForm): |
36 class UserForm(forms_helpers.DbModelForm): |
36 """Django form displayed when creating or editing a User. |
37 """Django form displayed when creating or editing a User. |
37 """ |
38 """ |
|
39 LINKNAME_PATTERN = r'''(?x) |
|
40 ^ |
|
41 [0-9a-z] # start with ASCII digit or lowercase |
|
42 ( |
|
43 [0-9a-z] # additional ASCII digit or lowercase |
|
44 | # -OR- |
|
45 _[0-9a-z] # underscore and ASCII digit or lowercase |
|
46 )* # zero or more of OR group |
|
47 $''' |
|
48 LINKNAME_REGEX = re.compile(LINKNAME_PATTERN) |
38 |
49 |
|
50 |
39 class Meta: |
51 class Meta: |
40 """Inner Meta class that defines some behavior for the form. |
52 """Inner Meta class that defines some behavior for the form. |
41 """ |
53 """ |
42 #: db.Model subclass for which the form will gather information |
54 #: db.Model subclass for which the form will gather information |
43 model = user.User |
55 model = soc.models.user.User |
44 |
56 |
45 #: list of model fields which will *not* be gathered by the form |
57 #: list of model fields which will *not* be gathered by the form |
46 exclude = ['id'] |
58 exclude = ['id'] |
|
59 |
|
60 def clean_link_name(self): |
|
61 linkname = self.cleaned_data.get('link_name') |
|
62 linkname_user = soc.models.user.User.getUserForLinkname(linkname) |
|
63 user = users.get_current_user() |
|
64 # if linkname exist in datastore and doesn't belong to current user |
|
65 if linkname_user and (linkname_user.id != user): |
|
66 raise forms.ValidationError("This link name is already in use.") |
|
67 elif not self.LINKNAME_REGEX.match(linkname): |
|
68 raise forms.ValidationError("This link name is in wrong format.") |
|
69 return linkname |
47 |
70 |
48 |
71 |
49 def edit(request, linkname=None, template='soc/user/profile/edit.html'): |
72 def edit(request, linkname=None, template='soc/user/profile/edit.html'): |
50 """View for a User to modify the properties of a UserModel. |
73 """View for a User to modify the properties of a UserModel. |
51 |
74 |
52 Args: |
75 Args: |
53 request: the standard django request object. |
76 request: the standard django request object. |
|
77 linkname: the User's site-unique "linkname" extracted from the URL |
54 template: the template path to use for rendering the template. |
78 template: the template path to use for rendering the template. |
55 |
79 |
56 Returns: |
80 Returns: |
57 A subclass of django.http.HttpResponse which either contains the form to |
81 A subclass of django.http.HttpResponse which either contains the form to |
58 be filled out, or a redirect to the correct view in the interface. |
82 be filled out, or a redirect to the correct view in the interface. |
59 """ |
83 """ |
|
84 #TODO(solydzajs): create controller for User and cleanup code in this handler |
|
85 |
|
86 #: If user not signed in redirect to sign-in page |
60 user = users.get_current_user() |
87 user = users.get_current_user() |
61 if not user: |
88 if not user: |
62 return http.HttpResponseRedirect(users.create_login_url(request.path)) |
89 return http.HttpResponseRedirect(users.create_login_url(request.path)) |
63 |
90 |
|
91 soc_user = soc.models.user.User.getUser(user) |
|
92 |
|
93 #: Show custom 404 page when linkname in url doesn't match current user |
|
94 if linkname: |
|
95 linkname_user = soc.models.user.User.getUserForLinkname(linkname) |
|
96 if (linkname_user and linkname_user.id != user) or not linkname_user: |
|
97 return http.HttpResponseNotFound('No user exists with that link name "%s"' % |
|
98 linkname) |
|
99 |
|
100 #: GET method |
|
101 if (request.method != 'POST') and soc_user: |
|
102 form = UserForm(initial={'nick_name': soc_user.nick_name, |
|
103 'link_name': soc_user.link_name}) |
|
104 return response_helpers.respond(request, |
|
105 template, {'template': template, |
|
106 'form': form, |
|
107 'soc_nick_name': soc_user.nick_name}) |
|
108 |
|
109 #: POST method |
64 form = UserForm() |
110 form = UserForm() |
65 if request.method=='POST': |
111 if request.method == 'POST': |
66 form = UserForm(request.POST) |
112 form = UserForm(request.POST) |
67 |
113 |
68 if not form.errors: |
114 if form.is_valid(): |
69 return http.HttpResponse('This would update the model') |
115 linkname = form.cleaned_data.get('link_name') |
|
116 nickname = form.cleaned_data.get("nick_name") |
|
117 if not soc_user: |
|
118 soc_user = soc.models.user.User(id = user,link_name = linkname, |
|
119 nick_name = nickname) |
|
120 else: |
|
121 soc_user.nick_name = nickname |
|
122 soc_user.link_name = linkname |
|
123 soc_user.put() |
|
124 return response_helpers.respond(request, |
|
125 template, {'template': template, |
|
126 'form': form, |
|
127 'soc_nick_name': nickname, |
|
128 'submit_message': 'Profile saved.'}) |
70 |
129 |
71 return response_helpers.respond(request, |
130 return response_helpers.respond(request, |
72 template, {'template': template, 'form': form}) |
131 template, {'template': template, 'form': form}) |