From 6e6c64650c41261ac32c56957460440d42d38824 Mon Sep 17 00:00:00 2001 From: Zambom Date: Thu, 22 Dec 2016 17:22:38 -0200 Subject: [PATCH] Adding update user --- users/forms.py | 22 ++++++++++++++++++---- users/templates/users/_form.html | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ users/templates/users/create.html | 63 +-------------------------------------------------------------- users/templates/users/list.html | 2 +- users/templates/users/update.html | 59 ++--------------------------------------------------------- users/urls.py | 1 + users/views.py | 75 ++++++++++++++++++++++++++++++++++++++------------------------------------- 7 files changed, 123 insertions(+), 161 deletions(-) create mode 100644 users/templates/users/_form.html diff --git a/users/forms.py b/users/forms.py index 7af2fc7..6c7d454 100644 --- a/users/forms.py +++ b/users/forms.py @@ -89,15 +89,29 @@ class ProfileForm(Validation): } class UserForm(Validation): - password = forms.CharField(label=_('Password'), widget = forms.PasswordInput, required = False) - password2 = forms.CharField(label = _('Confirm Password'), widget = forms.PasswordInput, required = False) - is_edit = False + def __init__(self, *args, **kwargs): + is_update = kwargs.pop('is_edit', False) + + super(UserForm, self).__init__(*args, **kwargs) + + self.is_edit = is_update + + if self.is_edit: + del self.fields['password'] + del self.fields['password2'] + + if not is_edit: + password = forms.CharField(label=_('Password'), widget = forms.PasswordInput, required = False) + password2 = forms.CharField(label = _('Confirm Password'), widget = forms.PasswordInput, required = False) + + def save(self, commit=True): super(UserForm, self).save(commit=False) - self.instance.set_password(self.cleaned_data['password']) + if not self.is_edit: + self.instance.set_password(self.cleaned_data['password']) self.instance.save() diff --git a/users/templates/users/_form.html b/users/templates/users/_form.html new file mode 100644 index 0000000..6b1aad4 --- /dev/null +++ b/users/templates/users/_form.html @@ -0,0 +1,62 @@ +{% load static i18n %} +{% load widget_tweaks %} + +
+ {% csrf_token %} + {% for field in form %} +
+ {% if field.auto_id == 'id_image' %} + {% if field.field.required %} + + {% else %} + + {% endif %} + {% render_field field class='form-control' %} +
+ + + + +
+ {% elif field.auto_id == 'id_description' %} + {% if field.field.required %} + + {% else %} + + {% endif %} + {% render_field field class='form-control text_wysiwyg' %} + {% elif field.auto_id == 'id_is_staff' or field.auto_id == 'id_is_active' %} +
+ +
+ {% else %} + {% if field.field.required %} + + {% else %} + + {% endif %} + {% render_field field class='form-control' %} + {% endif %} + {{ field.help_text }} + {% if field.errors %} + + {% endif %} +
+ {% endfor %} +
+ +
+
\ No newline at end of file diff --git a/users/templates/users/create.html b/users/templates/users/create.html index 5c9dec7..4d40956 100644 --- a/users/templates/users/create.html +++ b/users/templates/users/create.html @@ -1,8 +1,5 @@ {% extends 'users/list.html' %} -{% load static i18n %} -{% load widget_tweaks %} - {% load django_bootstrap_breadcrumbs %} {% block breadcrumbs %} @@ -14,65 +11,7 @@
-
- {% csrf_token %} - {% for field in form %} -
- {% if field.auto_id == 'id_image' %} - {% if field.field.required %} - - {% else %} - - {% endif %} - {% render_field field class='form-control' %} -
- - - - -
- {% elif field.auto_id == 'id_description' %} - {% if field.field.required %} - - {% else %} - - {% endif %} - {% render_field field class='form-control text_wysiwyg' %} - {% elif field.auto_id == 'id_is_staff' or field.auto_id == 'id_is_active' %} -
- -
- {% else %} - {% if field.field.required %} - - {% else %} - - {% endif %} - {% render_field field class='form-control' %} - {% endif %} - {{ field.help_text }} - {% if field.errors %} - - {% endif %} -
- {% endfor %} -
- -
-
+ {% include 'users/_form.html' %}
diff --git a/users/templates/users/list.html b/users/templates/users/list.html index df8d3e3..7832735 100644 --- a/users/templates/users/list.html +++ b/users/templates/users/list.html @@ -62,7 +62,7 @@
- {% trans 'Edit' %} + {% trans 'Edit' %} {% trans 'Delete' %}
diff --git a/users/templates/users/update.html b/users/templates/users/update.html index 3f94bf5..13ef811 100644 --- a/users/templates/users/update.html +++ b/users/templates/users/update.html @@ -1,4 +1,4 @@ -{% extends 'list_users.html' %} +{% extends 'users/list.html' %} {% load static i18n %} {% load widget_tweaks %} @@ -28,63 +28,8 @@
-
- {% csrf_token %} - {% for field in form %} -
- - {% if field.auto_id == 'id_birth_date' %} - - {{ field.help_text }} - {% elif field.auto_id == 'id_image' or field.auto_id == 'id_curriculum'%} - {% render_field field class='form-control input-sm' %} -
- - - - -
- {% elif field.auto_id == 'id_cpf' %} - {% render_field field class='form-control' onkeypress='campoNumerico(this,event); formatarCpf(this,event);' %} - - {% elif field.auto_id == 'id_phone' %} - {% render_field field class='form-control' onkeypress='campoNumerico(this,event); formatarTelefone(this,event);' %} - - {% elif field.auto_id == 'id_is_staff' or field.auto_id == 'id_is_active' %} -
- -
- {% else %} - {% render_field field class='form-control' %} - {{ field.help_text }} - {% endif %} - {% if field.errors %} - - {% endif %} -
- {% endfor %} -
- -
- -
+ {% include 'users/_form.html' %}
-
{% endblock %} diff --git a/users/urls.py b/users/urls.py index e0f5a5f..4806990 100644 --- a/users/urls.py +++ b/users/urls.py @@ -8,6 +8,7 @@ urlpatterns = [ url(r'^logout/$', auth_views.logout, {'next_page': 'users:login'}, name='logout'), url(r'^signup/$', views.RegisterUser.as_view(), name = 'signup'), url(r'^$', views.UsersListView.as_view(), name = 'manage'), + url(r'^edit/(?P[\w.%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4})/$', views.UpdateView.as_view(), name='update'), url(r'^create/$', views.CreateView.as_view(), name = 'create'), url(r'^profile/$', views.Profile.as_view(), name = 'profile'), url(r'^edit_profile/$', views.UpdateProfile.as_view(), name = 'edit_profile'), diff --git a/users/views.py b/users/views.py index 8f4f36c..aff575c 100644 --- a/users/views.py +++ b/users/views.py @@ -26,39 +26,7 @@ from rest_framework.permissions import IsAuthenticated, IsAuthenticatedOrReadOnl -# class Update(HasRoleMixin, LoginRequiredMixin, generic.UpdateView): -# allowed_roles = ['system_admin'] -# #login_url = reverse_lazy("core:home") -# redirect_field_name = 'next' -# template_name = 'users/update.html' -# slug_field = 'username' -# slug_url_kwarg = 'username' -# context_object_name = 'acc' -# model = User -# form_class = UserForm -# success_url = reverse_lazy('users:manage') - -# def form_valid(self, form): -# self.object = form.save(commit = False) - -# if self.object.type_profile == 2: -# assign_role(self.object, 'student') -# elif self.object.type_profile == 1: -# assign_role(self.object, 'professor') -# elif self.object.is_staff: -# assign_role(self.object, 'system_admin') - -# self.object.save() - -# messages.success(self.request, _('User ')+self.object.name+_(' updated successfully!')) - -# return super(Update, self).form_valid(form) - -# def get_context_data (self, **kwargs): -# context = super(Update, self).get_context_data(**kwargs) -# context['title'] = "Update User" -# return context # class View(LoginRequiredMixin, generic.DetailView): @@ -132,12 +100,10 @@ from rest_framework.permissions import IsAuthenticated, IsAuthenticatedOrReadOnl # return context -class UsersListView(HasRoleMixin, LoginRequiredMixin, generic.ListView): +class UsersListView(LoginRequiredMixin, generic.ListView): login_url = reverse_lazy("users:login") redirect_field_name = 'next' - allowed_roles = ['system_admin'] - template_name = 'users/list.html' context_object_name = 'users' paginate_by = 10 @@ -153,11 +119,10 @@ class UsersListView(HasRoleMixin, LoginRequiredMixin, generic.ListView): return context -class CreateView(HasRoleMixin, LoginRequiredMixin, generic.edit.CreateView): +class CreateView(LoginRequiredMixin, generic.edit.CreateView): login_url = reverse_lazy("users:login") redirect_field_name = 'next' - allowed_roles = ['system_admin'] template_name = 'users/create.html' form_class = UserForm context_object_name = 'acc' @@ -178,6 +143,42 @@ class CreateView(HasRoleMixin, LoginRequiredMixin, generic.edit.CreateView): return context +class UpdateView(LoginRequiredMixin, generic.UpdateView): + login_url = reverse_lazy("users:login") + redirect_field_name = 'next' + + template_name = 'users/update.html' + slug_field = 'email' + slug_url_kwarg = 'email' + context_object_name = 'acc' + model = User + form_class = UserForm + success_url = reverse_lazy('users:manage') + + def get_form_kwargs(self): + kwargs = super(UpdateView, self).get_form_kwargs() + + kwargs.update({'is_edit': True}) + + return kwargs + + def form_valid(self, form): + self.object = form.save(commit = False) + + self.object.save() + + msg = _("User %s updated successfully" % self.object.get_short_name()) + + messages.success(self.request, msg) + + return super(UpdateView, self).form_valid(form) + + def get_context_data (self, **kwargs): + context = super(UpdateView, self).get_context_data(**kwargs) + context['title'] = _("Update User") + + return context + class Profile(LoginRequiredMixin, generic.DetailView): login_url = reverse_lazy("users:login") redirect_field_name = 'next' -- libgit2 0.21.2