Commit 149dbe0ac561e5cfdb15bc2e0c9b0b27bbc7690a
1 parent
d44d52e5
Exists in
master
and in
39 other branches
Adding new user form
Showing
4 changed files
with
39 additions
and
20 deletions
Show diff stats
src/accounts/forms.py
@@ -10,16 +10,35 @@ from super_archives.models import MailingList | @@ -10,16 +10,35 @@ from super_archives.models import MailingList | ||
10 | User = get_user_model() | 10 | User = get_user_model() |
11 | 11 | ||
12 | 12 | ||
13 | -class NewUserForm(forms.ModelForm): | 13 | +class UserForm(forms.ModelForm): |
14 | + required = ('first_name', 'last_name', 'email', 'username') | ||
15 | + | ||
14 | class Meta: | 16 | class Meta: |
15 | model = User | 17 | model = User |
16 | - fields = ('first_name', 'last_name', 'email', 'username') | ||
17 | 18 | ||
18 | def __init__(self, *args, **kwargs): | 19 | def __init__(self, *args, **kwargs): |
19 | - super(NewUserForm, self).__init__(*args, **kwargs) | ||
20 | - for field in self.fields.values(): | 20 | + super(UserForm, self).__init__(*args, **kwargs) |
21 | + for field_name, field in self.fields.items(): | ||
22 | + # Adds form-control class to all form fields | ||
21 | field.widget.attrs.update({'class': 'form-control'}) | 23 | field.widget.attrs.update({'class': 'form-control'}) |
22 | - field.required = True | 24 | + |
25 | + # Set UserForm.required fields as required | ||
26 | + if field_name in UserForm.required: | ||
27 | + field.required = True | ||
28 | + | ||
29 | + | ||
30 | +class UserCreationForm(UserForm): | ||
31 | + class Meta: | ||
32 | + model = User | ||
33 | + fields = ('first_name', 'last_name', 'email', 'username') | ||
34 | + | ||
35 | + | ||
36 | +class UserUpdateForm(UserForm): | ||
37 | + class Meta: | ||
38 | + model = User | ||
39 | + fields = ('first_name', 'last_name', 'email', 'username', | ||
40 | + 'institution', 'role', 'twitter', 'facebook', | ||
41 | + 'google_talk', 'webpage') | ||
23 | 42 | ||
24 | 43 | ||
25 | class ListsForm(forms.Form): | 44 | class ListsForm(forms.Form): |
@@ -0,0 +1,11 @@ | @@ -0,0 +1,11 @@ | ||
1 | +{% extends "base.html" %} | ||
2 | +{% load i18n gravatar %} | ||
3 | + | ||
4 | +{% block main-content %} | ||
5 | + <form method="post"> | ||
6 | + {% csrf_token %} | ||
7 | + {{ form.as_p }} | ||
8 | + | ||
9 | + <button type="submit" class="btn btn-primary">{% trans "Update" %}</button> | ||
10 | + </form> | ||
11 | +{% endblock %} |
src/accounts/views.py
@@ -7,12 +7,13 @@ from django.contrib.auth import get_user_model | @@ -7,12 +7,13 @@ from django.contrib.auth import get_user_model | ||
7 | from django.views.generic import DetailView, UpdateView | 7 | from django.views.generic import DetailView, UpdateView |
8 | from django.utils.translation import ugettext as _ | 8 | from django.utils.translation import ugettext as _ |
9 | from django.shortcuts import render, redirect | 9 | from django.shortcuts import render, redirect |
10 | +from django.core.urlresolvers import reverse | ||
10 | 11 | ||
11 | from colab.deprecated import solrutils | 12 | from colab.deprecated import solrutils |
12 | from colab.deprecated import signup as signup_ | 13 | from colab.deprecated import signup as signup_ |
13 | 14 | ||
14 | from super_archives.models import EmailAddress, Message | 15 | from super_archives.models import EmailAddress, Message |
15 | -from .forms import NewUserForm, ListsForm | 16 | +from .forms import UserCreationForm, ListsForm, UserUpdateForm |
16 | 17 | ||
17 | 18 | ||
18 | class UserProfileBaseMixin(object): | 19 | class UserProfileBaseMixin(object): |
@@ -24,23 +25,11 @@ class UserProfileBaseMixin(object): | @@ -24,23 +25,11 @@ class UserProfileBaseMixin(object): | ||
24 | 25 | ||
25 | class UserProfileUpdateView(UserProfileBaseMixin, UpdateView): | 26 | class UserProfileUpdateView(UserProfileBaseMixin, UpdateView): |
26 | template_name = 'accounts/user_form.html' | 27 | template_name = 'accounts/user_form.html' |
27 | - #form_class = UserUpdateForm | 28 | + form_class = UserUpdateForm |
28 | 29 | ||
29 | def get_success_url(self): | 30 | def get_success_url(self): |
30 | return reverse('user_profile', kwargs={'username': self.object.username}) | 31 | return reverse('user_profile', kwargs={'username': self.object.username}) |
31 | 32 | ||
32 | - def get_initial(self): | ||
33 | - return { | ||
34 | - 'first_name': self.object.first_name, | ||
35 | - 'last_name': self.object.last_name, | ||
36 | - 'email': self.object.email, | ||
37 | - 'institution': self.object.profile.institution, | ||
38 | - 'role': self.object.profile.role, | ||
39 | - 'twitter': self.object.profile.twitter, | ||
40 | - 'facebook': self.object.profile.facebook, | ||
41 | - 'google_talk': self.object.profile.google_talk, | ||
42 | - 'webpage': self.object.profile.webpage, | ||
43 | - } | ||
44 | 33 | ||
45 | 34 | ||
46 | class UserProfileDetailView(UserProfileBaseMixin, DetailView): | 35 | class UserProfileDetailView(UserProfileBaseMixin, DetailView): |
@@ -72,7 +61,7 @@ class UserProfileDetailView(UserProfileBaseMixin, DetailView): | @@ -72,7 +61,7 @@ class UserProfileDetailView(UserProfileBaseMixin, DetailView): | ||
72 | def signup(request): | 61 | def signup(request): |
73 | # If the request method is GET just return the form | 62 | # If the request method is GET just return the form |
74 | if request.method == 'GET': | 63 | if request.method == 'GET': |
75 | - user_form = NewUserForm() | 64 | + user_form = UserCreationForm() |
76 | lists_form = ListsForm() | 65 | lists_form = ListsForm() |
77 | return render(request, 'accounts/user_create_form.html', | 66 | return render(request, 'accounts/user_create_form.html', |
78 | {'user_form': user_form, 'lists_form': lists_form}) | 67 | {'user_form': user_form, 'lists_form': lists_form}) |
7.37 KB