Anonymous avatar Anonymous committed e68ae7e Draft

Fixed for ToscaWidgets2

Comments (0)

Files changed (4)

userprofile/controllers/root.py

 # -*- coding: utf-8 -*-
 """Main Controller"""
+from pylons.controllers.util import abort
 
 from tg import TGController
 from tg import expose, flash, require, url, lurl, request, redirect, validate, config
         user = request.identity['user']
         return dict(user=user, profile_css=get_profile_css(config),
                     form=edit_password_form)
-
     @expose()
     @validate(edit_password_form, error_handler=chpasswd)
     def save_password(self, password, verify_password):
         user = request.identity['user']
         user.password = password
         flash(_('Password successfully changed'))
+        return redirect(plug_url('userprofile', '/%s' % getattr(user, primary_key(app_model.User).name)))
+
+    @expose('userprofile.templates.index')
+    def me(self):
+        try:
+            user=request.identity['user']
+        except:
+            return abort(404)
         return redirect(plug_url('userprofile', '/%s' % getattr(user, primary_key(app_model.User).name)))

userprofile/lib/__init__.py

 # -*- coding: utf-8 -*-
-
+from formencode import validators
+import tg
 from tg import url
 from tgext.pluggable import app_model, plug_url
 
-from tw.forms import ListForm, TextField, PasswordField
-from tw.forms.validators import UnicodeString
-from sprox.formbase import FilteringSchema
-from formencode.validators import FieldsMatch
-
 def get_profile_css(config):
     return url(config['_pluggable_userprofile_config'].get('custom_css',
-                                                           '/_pluggable/userprofile/css/style.css'))
+        '/_pluggable/userprofile/css/style.css'))
 
 def get_user_data(user):
     user_data = getattr(user, 'profile_data', {'display_name':('Display Name', user.display_name),
     for k, v in user_data.items():
         setattr(user, k, v)
 
-def create_user_form(user):
-    profile_form = getattr(user, 'profile_form', None)
-    if not profile_form:
-        user_data, user_avatar = get_user_data(user)
-        form_fields = [TextField(id=name, validator=UnicodeString(not_empty=True),
-                                 label_text=info[0]) for name, info in user_data.items()]
-        profile_form = ListForm(fields=form_fields, submit_text='Save',
-                                action=plug_url('userprofile', '/save'))
-    return profile_form
+if tg.config.get('prefer_toscawidgets2', False):
+    from tw2.forms import ListForm, TextField, TextArea, HiddenField, FileField, SubmitButton, PasswordField
+else:
+    from tw.forms import ListForm, TextField, PasswordField
 
-class ChangePasswordForm(ListForm):
-    password = PasswordField(label_text='Password')
-    password_confirm = PasswordField(label_text='Confirm Password')
+from formencode.validators import UnicodeString, FieldStorageUploadConverter
+from sprox.formbase import FilteringSchema
+from formencode.validators import FieldsMatch
 
 _password_match = FieldsMatch('password', 'verify_password',
                               messages={'invalidNoMatch': 'Passwords do not match'})
+
 if hasattr(TextField, 'req'):
     change_password_form_validator = _password_match
 else:
     change_password_form_validator =  FilteringSchema(chained_validators=[_password_match])
 
-def create_change_password_form():
-    return ListForm(fields=[PasswordField('password', label_text='Password',
-                                          validator=UnicodeString(not_empty=True)),
-                            PasswordField('verify_password', label_text='Confirm Password',
-                                          validator=UnicodeString(not_empty=True))],
-                    action=plug_url('userprofile', '/save_password', lazy=True),
-                    validator=change_password_form_validator,
-                    submit_text='Save')
+if tg.config.get('prefer_toscawidgets2', False):
+    from tw2.core import Required
+
+    class UserForm(ListForm):
+        uid=HiddenField()
+        submit=SubmitButton(value='Save')
+
+    def create_user_form(user):
+        profile_form = getattr(user, 'profile_form', None)
+        if not profile_form:
+            user_data, user_avatar = get_user_data(user)
+            profile_form = UserForm()
+
+            for name, info in user_data.items():
+                profile_form.child = profile_form.child()
+                profile_form.child.children.append(TextField(id=name, validator=Required, label=info[0]))
+
+            profile_form = profile_form()
+        return profile_form
+
+    class ChangePasswordForm(ListForm):
+        password = PasswordField(label=u'Password', validator=Required)
+        verify_password = PasswordField(label=u'Confirm Password', validator=Required)
+        submit=SubmitButton(value='Save')
+        validator = change_password_form_validator
+
+    def create_change_password_form():
+        return ChangePasswordForm()
+else:
+    def create_user_form(user):
+        profile_form = getattr(user, 'profile_form', None)
+        if not profile_form:
+            user_data, user_avatar = get_user_data(user)
+            form_fields = [TextField(id=name, validator=UnicodeString(not_empty=True),
+                                     label_text=info[0]) for name, info in user_data.items()]
+            profile_form = ListForm(fields=form_fields, submit_text='Save',
+                                    action=plug_url('userprofile', '/save'))
+        return profile_form
+
+    def create_change_password_form():
+        return ListForm(fields=[PasswordField('password', label_text='Password',
+                                              validator=UnicodeString(not_empty=True)),
+                                PasswordField('verify_password', label_text='Confirm Password',
+                                              validator=UnicodeString(not_empty=True))],
+                        action=plug_url('userprofile', '/save_password', lazy=True),
+                        validator=change_password_form_validator,
+                        submit_text='Save')

userprofile/templates/chpasswd.html

     <div id="userprofile_container">
         <div id="userprofile_edit_form">
             <h3>Change Password</h3>
-            ${form.display()}
+            ${form.display(action=h.plug_url('userprofile', '/save_password', lazy=True))}
         </div>
     </div>
 </body>

userprofile/templates/edit.html

         </div>
         <div id="userprofile_edit_form">
             <h3>Edit Profile</h3>
-            ${form.display(user)}
+            ${form.display(user, action=h.plug_url('userprofile', '/save'))}
         </div>
     </div>
 </body>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.