26 |
26 |
27 from django import forms |
27 from django import forms |
28 from django import http |
28 from django import http |
29 from django.utils.translation import ugettext_lazy |
29 from django.utils.translation import ugettext_lazy |
30 |
30 |
|
31 from soc.logic import accounts |
31 from soc.logic import models |
32 from soc.logic import models |
32 from soc.logic import out_of_band |
33 from soc.logic import out_of_band |
33 from soc.logic import validate |
34 from soc.logic import validate |
34 from soc.logic.site import id_user |
|
35 from soc.views import helper |
35 from soc.views import helper |
36 from soc.views import simple |
36 from soc.views import simple |
37 from soc.views.helper import decorators |
37 from soc.views.helper import decorators |
38 |
38 |
39 import soc.logic |
39 import soc.logic |
51 """ |
51 """ |
52 #: db.Model subclass for which the form will gather information |
52 #: db.Model subclass for which the form will gather information |
53 model = soc.models.user.User |
53 model = soc.models.user.User |
54 |
54 |
55 #: list of model fields which will *not* be gathered by the form |
55 #: list of model fields which will *not* be gathered by the form |
56 exclude = ['id', 'former_ids', 'is_developer'] |
56 exclude = ['account', 'former_accounts', 'is_developer'] |
57 |
57 |
58 def clean_link_name(self): |
58 def clean_link_name(self): |
59 link_name = self.cleaned_data.get('link_name') |
59 link_name = self.cleaned_data.get('link_name') |
60 if not validate.isLinkNameFormatValid(link_name): |
60 if not validate.isLinkNameFormatValid(link_name): |
61 raise forms.ValidationError("This link name is in wrong format.") |
61 raise forms.ValidationError("This link name is in wrong format.") |
62 |
62 |
63 user = id_user.getUserFromLinkName(link_name) |
63 user = models.user.logic.getForFields({'link_name': link_name}, |
64 |
64 unique=True) |
65 if user: |
65 if user: |
66 raise forms.ValidationError("This link name is already in use.") |
66 raise forms.ValidationError("This link name is already in use.") |
67 |
67 |
68 return link_name |
68 return link_name |
69 |
69 |
97 |
97 |
98 Returns: |
98 Returns: |
99 A subclass of django.http.HttpResponse which either contains the form to |
99 A subclass of django.http.HttpResponse which either contains the form to |
100 be filled out, or a redirect to the correct view in the interface. |
100 be filled out, or a redirect to the correct view in the interface. |
101 """ |
101 """ |
102 id = users.get_current_user() |
102 account = users.get_current_user() |
103 |
103 |
104 # create default template context for use with any templates |
104 # create default template context for use with any templates |
105 context = helper.responses.getUniversalContext(request) |
105 context = helper.responses.getUniversalContext(request) |
106 |
106 |
107 if (not id) and (not link_name): |
107 if (not account) and (not link_name): |
108 # not logged in, and no link name, so request that the user sign in |
108 # not logged in, and no link name, so request that the user sign in |
109 return simple.requestLogin(request, page, template, context, |
109 return simple.requestLogin(request, page, template, context, |
110 # TODO(tlarsen): /user/profile could be a link to a help page instead |
110 # TODO(tlarsen): /user/profile could be a link to a help page instead |
111 login_message_fmt=ugettext_lazy( |
111 login_message_fmt=ugettext_lazy( |
112 'To create a new <a href="/user/profile">User Profile</a>' |
112 'To create a new <a href="/user/profile">User Profile</a>' |
113 ' or modify an existing one, you must first' |
113 ' or modify an existing one, you must first' |
114 ' <a href="%(sign_in)s">sign in</a>.')) |
114 ' <a href="%(sign_in)s">sign in</a>.')) |
115 |
115 |
116 if (not id) and link_name: |
116 if (not account) and link_name: |
117 # not logged in, so show read-only public profile for link_name user |
117 # not logged in, so show read-only public profile for link_name user |
118 return simple.public(request, page=page, template=template, |
118 return simple.public(request, page=page, template=template, |
119 link_name=link_name, context=context) |
119 link_name=link_name, context=context) |
120 |
120 |
121 link_name_user = None |
121 link_name_user = None |
122 |
122 |
123 # try to fetch User entity corresponding to link_name if one exists |
123 # try to fetch User entity corresponding to link_name if one exists |
124 try: |
124 try: |
125 if link_name: |
125 if link_name: |
126 link_name_user = id_user.getUserFromLinkNameOr404(link_name) |
126 link_name_user = accounts.getUserFromLinkNameOr404(link_name) |
127 except out_of_band.ErrorResponse, error: |
127 except out_of_band.ErrorResponse, error: |
128 # show custom 404 page when link name doesn't exist in Datastore |
128 # show custom 404 page when link name doesn't exist in Datastore |
129 return simple.errorResponse(request, page, error, template, context) |
129 return simple.errorResponse(request, page, error, template, context) |
130 |
130 |
131 # link_name_user will be None here if link name was already None... |
131 # link_name_user will be None here if link name was already None... |
132 if link_name_user and (link_name_user.id != id): |
132 if link_name_user and (link_name_user.account != account): |
133 # link_name_user exists but is not the currently logged in Google Account, |
133 # link_name_user exists but is not the currently logged in Google Account, |
134 # so show public view for that (other) User entity |
134 # so show public view for that (other) User entity |
135 return simple.public(request, page=page, template=template, |
135 return simple.public(request, page=page, template=template, |
136 link_name=link_name, context=context) |
136 link_name=link_name, context=context) |
137 |
137 |
141 if form.is_valid(): |
141 if form.is_valid(): |
142 new_link_name = form.cleaned_data.get('link_name') |
142 new_link_name = form.cleaned_data.get('link_name') |
143 properties = { |
143 properties = { |
144 'link_name': new_link_name, |
144 'link_name': new_link_name, |
145 'nick_name': form.cleaned_data.get("nick_name"), |
145 'nick_name': form.cleaned_data.get("nick_name"), |
146 'id': id, |
146 'account': account, |
147 } |
147 } |
148 |
148 |
149 # check if user account is not in former_ids |
149 # check if user account is not in former_accounts |
150 # if it is show error message that account is invalid |
150 # if it is show error message that account is invalid |
151 if models.user.logic.isFormerId(id): |
151 if models.user.logic.isFormerAccount(account): |
152 msg = DEF_USER_ACCOUNT_INVALID_MSG |
152 msg = DEF_USER_ACCOUNT_INVALID_MSG |
153 error = out_of_band.ErrorResponse(msg) |
153 error = out_of_band.ErrorResponse(msg) |
154 return simple.errorResponse(request, page, error, template, context) |
154 return simple.errorResponse(request, page, error, template, context) |
155 |
155 |
156 user = models.user.logic.updateOrCreateFromId(properties, id) |
156 user = models.user.logic.updateOrCreateFromAccount(properties, account) |
157 |
157 |
158 # redirect to /user/profile?s=0 |
158 # redirect to /user/profile?s=0 |
159 # (causes 'Profile saved' message to be displayed) |
159 # (causes 'Profile saved' message to be displayed) |
160 return helper.responses.redirectToChangedSuffix( |
160 return helper.responses.redirectToChangedSuffix( |
161 request, None, params=SUBMIT_PROFILE_SAVED_PARAMS) |
161 request, None, params=SUBMIT_PROFILE_SAVED_PARAMS) |
162 else: # request.method == 'GET' |
162 else: # request.method == 'GET' |
163 # try to fetch User entity corresponding to Google Account if one exists |
163 # try to fetch User entity corresponding to Google Account if one exists |
164 user = models.user.logic.getForFields({'id': id}, unique=True) |
164 user = models.user.logic.getForFields({'account': account}, unique=True) |
165 |
165 |
166 if user: |
166 if user: |
167 # is 'Profile saved' parameter present, but referrer was not ourself? |
167 # is 'Profile saved' parameter present, but referrer was not ourself? |
168 # (e.g. someone bookmarked the GET that followed the POST submit) |
168 # (e.g. someone bookmarked the GET that followed the POST submit) |
169 if (request.GET.get(SUBMIT_MSG_PARAM_NAME) |
169 if (request.GET.get(SUBMIT_MSG_PARAM_NAME) |