From c11363e94069139f89c613c678624458fe167e66 Mon Sep 17 00:00:00 2001 From: Weitian LI Date: Sun, 20 Apr 2014 23:58:30 +0800 Subject: * added 'profile', 'profile_update' and 'profile_update_done' templates and implemented related views * added 'account.forms.UpdateProfileForm' * implemented 'account.views.UpdateProfileView' using class-based view * updated 'account.urls' --- account/forms.py | 14 +++++ account/models.py | 1 + account/templates/account/profile.html | 20 +++++-- account/templates/account/profile_update.html | 24 +++++++++ account/templates/account/profile_update_done.html | 21 ++++++++ account/urls.py | 58 ++++++++++++-------- account/views.py | 61 +++++++++++++++++++++- 7 files changed, 172 insertions(+), 27 deletions(-) create mode 100644 account/templates/account/profile_update.html create mode 100644 account/templates/account/profile_update_done.html (limited to 'account') diff --git a/account/forms.py b/account/forms.py index 6a566e9..ca54711 100644 --- a/account/forms.py +++ b/account/forms.py @@ -8,6 +8,8 @@ from django import forms from registration.forms import RegistrationFormUniqueEmail from django.utils.translation import ugettext_lazy as _ +from account.models import UserProfile + class UserRegForm(RegistrationFormUniqueEmail): """ @@ -38,3 +40,15 @@ class UserRegForm(RegistrationFormUniqueEmail): ] +class UpdateProfileForm(forms.ModelForm): + """ + ModelForm of 'UserProfile' used in 'UpdateProfileView' + """ + # extra email field + email = forms.EmailField(label=_("E-mail")) + + class Meta: + model = UserProfile + fields = ('realname', 'gender', 'institute') + + diff --git a/account/models.py b/account/models.py index 22b11af..38047d6 100644 --- a/account/models.py +++ b/account/models.py @@ -37,6 +37,7 @@ class UserProfile(models.Model): ('C', _("Checking")), ) # model fields + # FK default backward manager name 'userprofile_set' user = models.ForeignKey(User, unique=True, verbose_name=_("Username")) realname = models.CharField(_("Name"), max_length=30) gender = models.CharField(_("Gender"), max_length=1, choices=GENDERS) diff --git a/account/templates/account/profile.html b/account/templates/account/profile.html index cb08717..e8d295c 100644 --- a/account/templates/account/profile.html +++ b/account/templates/account/profile.html @@ -20,6 +20,20 @@ 姓名 {{ profile.realname }} + + 性别 + + {% if profile.gender == 'M' %} + 男 + {% elif profile.gender == 'F' %} + 女 + {% elif profile.gender == 'X' %} + + {% else %} + 系统错误 + {% endif %} + + 邮箱 {{ user.email }} @@ -38,7 +52,7 @@ {% elif profile.is_approved == 'C' %} 审核中 {% else %} - 错误 + 系统错误 {% endif %} @@ -52,7 +66,7 @@ {% elif profile.is_sponsored == 'C' %} 审核中 {% else %} - 错误 + 系统错误 {% endif %} @@ -60,7 +74,7 @@

- 编辑个人信息 + 更新个人信息 修改密码

diff --git a/account/templates/account/profile_update.html b/account/templates/account/profile_update.html new file mode 100644 index 0000000..1802bb0 --- /dev/null +++ b/account/templates/account/profile_update.html @@ -0,0 +1,24 @@ +{% extends 'base.html' %} +{% load staticfiles %} +{% load url from future %} +{% load bootstrap3 %} + +{# login template #} + +{% block title %} +更新个人信息 | 2014 SKA Summer School +{% endblock %} + +{% block content %} +
+

更新个人信息

+
+
+ {% csrf_token %} + {% bootstrap_form form layout='horizontal' %} + {% buttons submit='提交' reset='重置' layout='horizontal' %}{% endbuttons %} +
+
+{% endblock %} + +{# vim: set ts=8 sw=2 tw=0 fenc=utf-8 ft=htmldjango.html: #} diff --git a/account/templates/account/profile_update_done.html b/account/templates/account/profile_update_done.html new file mode 100644 index 0000000..5b10eb2 --- /dev/null +++ b/account/templates/account/profile_update_done.html @@ -0,0 +1,21 @@ +{% extends 'base.html' %} +{% load staticfiles %} +{% load url from future %} +{% load bootstrap3 %} + +{# login template #} + +{% block title %} +信息已更新 | 2014 SKA Summer School +{% endblock %} + +{% block content %} +
+

信息已更新

+

您的个人信息已更新。

+
+

返回个人主页

+
+{% endblock %} + +{# vim: set ts=8 sw=2 tw=0 fenc=utf-8 ft=htmldjango.html: #} diff --git a/account/urls.py b/account/urls.py index 671ac9a..b61e0e2 100644 --- a/account/urls.py +++ b/account/urls.py @@ -12,10 +12,45 @@ from django.contrib.auth.decorators import login_required from registration.backends.default.views import ActivationView from registration.backends.default.views import RegistrationView +from account.views import ProfileView, UpdateProfileView from account.forms import UserRegForm urlpatterns = patterns('', + ## profile + url(r'^profile/$', + login_required(ProfileView.as_view()), + name='profile'), + # update profile + url(r'^profile/update/$', + login_required(UpdateProfileView.as_view()), + name='profile_update'), + # update profile done + url(r'^profile/update/done/$', + login_required(TemplateView.as_view(template_name='account/profile_update_done.html')), + name='profile_update_done'), + ## django auth views + # login + url(r'^login/$', 'django.contrib.auth.views.login', + {'template_name': 'account/login.html'}, + name='login'), + # logout + url(r'^logout/$', 'django.contrib.auth.views.logout', + {'template_name': 'account/logout.html'}, + name='logout'), + # change password + # If 'post_change_redirect' not provided, + # then redirect to url 'password_change_done'. + url(r'^password/change/$', 'django.contrib.auth.views.password_change', + {'template_name': 'account/password_change.html'}, + name='password_change'), + # change password done + url(r'^password/change/done$', 'django.contrib.auth.views.password_change_done', + {'template_name': 'account/password_change_done.html'}, + name='password_change_done'), +) + +urlpatterns += patterns('', ## django-registration # 0. registration_disallowed url(r'^register/closed/$', @@ -41,29 +76,6 @@ urlpatterns = patterns('', url(r'^activate/(?P\w+)/$', ActivationView.as_view(), name='registration_activate'), - ## profile - url(r'^profile/$', - login_required(TemplateView.as_view(template_name='account/profile.html')), - name='profile'), - ## django auth views - # login - url(r'^login/$', 'django.contrib.auth.views.login', - {'template_name': 'account/login.html'}, - name='login'), - # logout - url(r'^logout/$', 'django.contrib.auth.views.logout', - {'template_name': 'account/logout.html'}, - name='logout'), - # change password - # If 'post_change_redirect' not provided, - # then redirect to url 'password_change_done'. - url(r'^password/change/$', 'django.contrib.auth.views.password_change', - {'template_name': 'account/password_change.html'}, - name='password_change'), - # change password done - url(r'^password/change/done$', 'django.contrib.auth.views.password_change_done', - {'template_name': 'account/password_change_done.html'}, - name='password_change_done'), ) diff --git a/account/views.py b/account/views.py index 2419fe6..e6b90b1 100644 --- a/account/views.py +++ b/account/views.py @@ -5,5 +5,64 @@ views.py of app 'account' """ from django.shortcuts import render +from django.views.generic.base import TemplateView +from django.views.generic.edit import UpdateView +from django.core.urlresolvers import reverse_lazy +from django.http import HttpResponseRedirect + +from account.models import UserProfile +from account.forms import UpdateProfileForm + + +###### Class-based views ###### +class ProfileView(TemplateView): + """ + class view to show profile page + """ + template_name = 'account/profile.html' + + def get_context_data(self, **kwargs): + context = super(ProfileView, self).get_context_data(**kwargs) + user = self.request.user + profile = user.userprofile_set.get(user=user) + context['user'] = user + context['profile'] = profile + return context + +class UpdateProfileView(UpdateView): + form_class = UpdateProfileForm + model = UserProfile + template_name = 'account/profile_update.html' + success_url = reverse_lazy('profile_update_done') + + # get profile object + def get_object(self, queryset=None): + user = self.request.user + profile = user.userprofile_set.get(user=user) + return profile + + def get(self, request, *args, **kwargs): + """ + Returns the keyword arguments for instantiating the form. + modify this method to add 'email' data + """ + self.object = self.get_object() + form_class = self.get_form_class() + form = self.get_form(form_class) + # initialize form 'email' field + user = self.request.user + form.fields['email'].initial = user.email + return self.render_to_response(self.get_context_data(form=form)) + + def form_valid(self, form): + """ + modify 'form_valid' to update email field + """ + form_data = form.cleaned_data + # update email and save + user = self.request.user + user.email = form_data.get('email', user.email) + user.save() + return super(UpdateProfileView, self).form_valid(form) + -# Create your views here. -- cgit v1.2.2