Commits

offline  committed 8119743

user edit form modified

  • Participants
  • Parent commits 85f2eb2

Comments (0)

Files changed (3)

File openauth/forms.py

         pass
 
 
+
 class EditUserForm(forms.Form):
     def __init__(self, user, *args, **kwargs):
+        self.user = user
+        self.base_fields['username'] = forms.CharField(max_length=254, initial=user.username)
         super(EditUserForm, self).__init__(*args, **kwargs)
-        self.user = user
-
-    username = forms.CharField(widget=forms.TextInput(attrs={'class':'field_input'}))
-    country = forms.CharField(widget=forms.TextInput(attrs={'class':'field_input'}), required=False)
-    jabber = forms.EmailField(widget=forms.TextInput(attrs={'class':'field_input'}), required=False)
-    about = forms.CharField(widget=forms.Textarea(attrs={'class':'field_input f130', 'style': 'height: 250px'}), required=False)
 
 
 
     def save(self):
-        user = self.user
-        profile = user.profile
-        user.username = self.cleaned_data['username']
-        profile.country = self.cleaned_data['country']
-        profile.jabber = self.cleaned_data['jabber']
-        profile.about = self.cleaned_data['about']
+        self.user.username = self.cleaned_data['username']
         user.save()
-        profile.save()
         return user
 
 EditUserForm = autostrip(EditUserForm)

File openauth/settings.py

 CUSTOM_CONFIRMATION = get_config("CUSTOM_CONFIRMATION", None) # custom confirmation function
 REGISTRATION_ALLOWED = get_config("REGISTRATION_ALLOWED", True)
 REGISTRATION_FORM = get_config("REGISTRATION_FORM", "openauth.forms.RegistrationForm")
+EDIT_USER_FORM = get_config("EDIT_USER_FORM", "openauth.forms.EditUserForm")
 UNIQUE_USERNAME = get_config("UNIQUE_USERNAME", True)
 
 GRAVATAR_URL = get_config("GRAVATAR_URL", "http://www.gravatar.com/avatar.php?")

File openauth/views.py

     return message(request, _('You have been logged out'), 'notice', '/')
 
 
+
 @render_to('openauth/edit_user.html')
 def edit_user(request, user_id):
     user = get_object_or_404(User, pk=user_id)
     if not request.user.is_authenticated() or user != request.user and not request.user.is_staff:
-        return HttpResponseReload(request)
+        return redirect("/")
     if request.POST:
-        form = EditUserForm(user, request.POST, request.FILES)
+        form = str_to_class(openauth.settings.EDIT_USER_FORM)(user, request.POST, request.FILES)
         if form.is_valid():
             form.save()
             return message(request, _('User details successfully changed'), 'success', 'openauth-show-user', args=[user.id])
     else:
-        initial = {
-                    'username': user.username, 
-                    'country': user.profile.country,
-                    'jabber': user.profile.jabber,
-                    'about': user.profile.about
-                    }
-        form = EditUserForm(user, initial=initial)
+        form = str_to_class(openauth.settings.EDIT_USER_FORM)(user)
 
     return {'form': form, 'user': user}
 
 
+
+class EditUser(object):
+
+    def __call__(self, request, user_id):
+        self.request = request
+        self.user_id = user_id
+        self.user = get_object_or_404(User, pk=user_id)
+        if self.request.POST:
+            return self.proceed_form()
+        else:
+            return self.create_form()
+
+
+    def permissions(self):
+        if not self.request.user.is_authenticated() or self.user != self.request.user and not self.request.user.is_staff:
+            return HttpResponseReload(request)
+
+    def proceed_form(self):
+        form = EditUserForm(self.user, self.request.POST, self.request.FILES)
+        if form.is_valid():
+            form.save()
+            return message(request, _('User details successfully changed'), 'success', 'openauth-show-user', args=[user.id])
+
+    def create_form(self):
+        initial = self.get_initial_data()
+        form = EditUserForm(user, initial=initial)
+        return {'form': form, 'user': user}
+
+    def get_initial_data(self):
+        return {}
+
+
+
 @render_to('openauth/profile.html')
 def show_user(request, user_id=None):
     """