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 = ['account', 'former_accounts', 'is_developer'] |
56 exclude = ['account', 'former_accounts', 'is_developer'] |
57 |
57 |
58 def clean_link_name(self): |
58 def clean_link_id(self): |
59 link_name = self.cleaned_data.get('link_name') |
59 link_id = self.cleaned_data.get('link_id') |
60 if not validate.isLinkNameFormatValid(link_name): |
60 if not validate.isLinkIdFormatValid(link_id): |
61 raise forms.ValidationError("This link name is in wrong format.") |
61 raise forms.ValidationError("This link ID is in wrong format.") |
62 |
62 |
63 user = models.user.logic.getForFields({'link_name': link_name}, |
63 user = models.user.logic.getForFields({'link_id': link_id}, |
64 unique=True) |
64 unique=True) |
65 |
65 |
66 # Get the currently logged in user account |
66 # Get the currently logged in user account |
67 current_account = users.get_current_user() |
67 current_account = users.get_current_user() |
68 |
68 |
69 if user: |
69 if user: |
70 if current_account != user.account: |
70 if current_account != user.account: |
71 raise forms.ValidationError("This link name is already in use.") |
71 raise forms.ValidationError("This link ID is already in use.") |
72 |
72 |
73 return link_name |
73 return link_id |
74 |
74 |
75 |
75 |
76 DEF_USER_PROFILE_EDIT_TMPL = 'soc/user/edit_self.html' |
76 DEF_USER_PROFILE_EDIT_TMPL = 'soc/user/edit_self.html' |
77 DEF_USER_ACCOUNT_INVALID_MSG = 'This account is invalid.' |
77 DEF_USER_ACCOUNT_INVALID_MSG = 'This account is invalid.' |
78 |
78 |
87 SUBMIT_PROFILE_SAVED_PARAMS = { |
87 SUBMIT_PROFILE_SAVED_PARAMS = { |
88 SUBMIT_MSG_PARAM_NAME: SUBMIT_MSG_PROFILE_SAVED, |
88 SUBMIT_MSG_PARAM_NAME: SUBMIT_MSG_PROFILE_SAVED, |
89 } |
89 } |
90 |
90 |
91 @decorators.view |
91 @decorators.view |
92 def edit(request, page_name=None, link_name=None, |
92 def edit(request, page_name=None, link_id=None, |
93 template=DEF_USER_PROFILE_EDIT_TMPL): |
93 template=DEF_USER_PROFILE_EDIT_TMPL): |
94 """View for a User to modify the properties of a User Model entity. |
94 """View for a User to modify the properties of a User Model entity. |
95 |
95 |
96 Args: |
96 Args: |
97 request: the standard django request object |
97 request: the standard django request object |
98 page_name: the page name displayed in templates as page and header title |
98 page_name: the page name displayed in templates as page and header title |
99 link_name: the User's site-unique "link_name" extracted from the URL |
99 link_id: the User's site-unique "link_id" extracted from the URL |
100 template: the template path to use for rendering the template |
100 template: the template path to use for rendering the template |
101 |
101 |
102 Returns: |
102 Returns: |
103 A subclass of django.http.HttpResponse which either contains the form to |
103 A subclass of django.http.HttpResponse which either contains the form to |
104 be filled out, or a redirect to the correct view in the interface. |
104 be filled out, or a redirect to the correct view in the interface. |
106 account = users.get_current_user() |
106 account = users.get_current_user() |
107 |
107 |
108 # create default template context for use with any templates |
108 # create default template context for use with any templates |
109 context = helper.responses.getUniversalContext(request) |
109 context = helper.responses.getUniversalContext(request) |
110 |
110 |
111 if (not account) and (not link_name): |
111 if (not account) and (not link_id): |
112 # not logged in, and no link name, so request that the user sign in |
112 # not logged in, and no link ID, so request that the user sign in |
113 return simple.requestLogin(request, page_name, template, context, |
113 return simple.requestLogin(request, page_name, template, context, |
114 # TODO(tlarsen): /user/profile could be a link to a help page instead |
114 # TODO(tlarsen): /user/profile could be a link to a help page instead |
115 login_message_fmt=ugettext_lazy( |
115 login_message_fmt=ugettext_lazy( |
116 'To create a new <a href="/user/profile">User Profile</a>' |
116 'To create a new <a href="/user/profile">User Profile</a>' |
117 ' or modify an existing one, you must first' |
117 ' or modify an existing one, you must first' |
118 ' <a href="%(sign_in)s">sign in</a>.')) |
118 ' <a href="%(sign_in)s">sign in</a>.')) |
119 |
119 |
120 if (not account) and link_name: |
120 if (not account) and link_id: |
121 # not logged in, so show read-only public profile for link_name user |
121 # not logged in, so show read-only public profile for link_id user |
122 return simple.public(request, page_name=page_name, template=template, |
122 return simple.public(request, page_name=page_name, template=template, |
123 link_name=link_name, context=context) |
123 link_id=link_id, context=context) |
124 |
124 |
125 link_name_user = None |
125 link_id_user = None |
126 |
126 |
127 # try to fetch User entity corresponding to link_name if one exists |
127 # try to fetch User entity corresponding to link_id if one exists |
128 try: |
128 try: |
129 if link_name: |
129 if link_id: |
130 link_name_user = accounts.getUserFromLinkNameOr404(link_name) |
130 link_id_user = accounts.getUserFromLinkIdOr404(link_id) |
131 except out_of_band.ErrorResponse, error: |
131 except out_of_band.ErrorResponse, error: |
132 # show custom 404 page when link name doesn't exist in Datastore |
132 # show custom 404 page when link ID doesn't exist in Datastore |
133 return simple.errorResponse(request, page_name, error, template, context) |
133 return simple.errorResponse(request, page_name, error, template, context) |
134 |
134 |
135 # link_name_user will be None here if link name was already None... |
135 # link_id_user will be None here if link ID was already None... |
136 if link_name_user and (link_name_user.account != account): |
136 if link_id_user and (link_id_user.account != account): |
137 # link_name_user exists but is not the currently logged in Google Account, |
137 # link_id_user exists but is not the currently logged in Google Account, |
138 # so show public view for that (other) User entity |
138 # so show public view for that (other) User entity |
139 return simple.public(request, page_name=page_name, template=template, |
139 return simple.public(request, page_name=page_name, template=template, |
140 link_name=link_name, context=context) |
140 link_id=link_id, context=context) |
141 |
141 |
142 if request.method == 'POST': |
142 if request.method == 'POST': |
143 form = UserForm(request.POST) |
143 form = UserForm(request.POST) |
144 |
144 |
145 if form.is_valid(): |
145 if form.is_valid(): |
146 new_link_name = form.cleaned_data.get('link_name') |
146 new_link_id = form.cleaned_data.get('link_id') |
147 properties = { |
147 properties = { |
148 'link_name': new_link_name, |
148 'link_id': new_link_id, |
149 'nick_name': form.cleaned_data.get("nick_name"), |
149 'nick_name': form.cleaned_data.get("nick_name"), |
150 'account': account, |
150 'account': account, |
151 } |
151 } |
152 |
152 |
153 # check if user account is not in former_accounts |
153 # check if user account is not in former_accounts |
155 if models.user.logic.isFormerAccount(account): |
155 if models.user.logic.isFormerAccount(account): |
156 msg = DEF_USER_ACCOUNT_INVALID_MSG |
156 msg = DEF_USER_ACCOUNT_INVALID_MSG |
157 error = out_of_band.ErrorResponse(msg) |
157 error = out_of_band.ErrorResponse(msg) |
158 return simple.errorResponse(request, page_name, error, template, context) |
158 return simple.errorResponse(request, page_name, error, template, context) |
159 |
159 |
160 user = models.user.logic.updateOrCreateFromFields(properties, {'link_name': new_link_name}) |
160 user = models.user.logic.updateOrCreateFromFields(properties, {'link_id': new_link_id}) |
161 |
161 |
162 # redirect to /user/profile?s=0 |
162 # redirect to /user/profile?s=0 |
163 # (causes 'Profile saved' message to be displayed) |
163 # (causes 'Profile saved' message to be displayed) |
164 return helper.responses.redirectToChangedSuffix( |
164 return helper.responses.redirectToChangedSuffix( |
165 request, None, params=SUBMIT_PROFILE_SAVED_PARAMS) |
165 request, None, params=SUBMIT_PROFILE_SAVED_PARAMS) |
170 if user: |
170 if user: |
171 # is 'Profile saved' parameter present, but referrer was not ourself? |
171 # is 'Profile saved' parameter present, but referrer was not ourself? |
172 # (e.g. someone bookmarked the GET that followed the POST submit) |
172 # (e.g. someone bookmarked the GET that followed the POST submit) |
173 if (request.GET.get(SUBMIT_MSG_PARAM_NAME) |
173 if (request.GET.get(SUBMIT_MSG_PARAM_NAME) |
174 and (not helper.requests.isReferrerSelf(request, |
174 and (not helper.requests.isReferrerSelf(request, |
175 suffix=link_name))): |
175 suffix=link_id))): |
176 # redirect to aggressively remove 'Profile saved' query parameter |
176 # redirect to aggressively remove 'Profile saved' query parameter |
177 return http.HttpResponseRedirect(request.path) |
177 return http.HttpResponseRedirect(request.path) |
178 |
178 |
179 # referrer was us, so select which submit message to display |
179 # referrer was us, so select which submit message to display |
180 # (may display no message if ?s=0 parameter is not present) |
180 # (may display no message if ?s=0 parameter is not present) |