diff options
| -rw-r--r-- | account/forms.py | 14 | ||||
| -rw-r--r-- | account/models.py | 1 | ||||
| -rw-r--r-- | account/templates/account/profile.html | 20 | ||||
| -rw-r--r-- | account/templates/account/profile_update.html | 24 | ||||
| -rw-r--r-- | account/templates/account/profile_update_done.html | 21 | ||||
| -rw-r--r-- | account/urls.py | 58 | ||||
| -rw-r--r-- | account/views.py | 61 | 
7 files changed, 172 insertions, 27 deletions
| 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 @@ -21,6 +21,20 @@            <td class="profile-name-data">{{ profile.realname }}</td>          </tr>          <tr> +          <th class="profile-gender">性别</th> +          <td class="profile-gender-data"> +            {% if profile.gender == 'M' %} +            男 +            {% elif profile.gender == 'F' %} +            女 +            {% elif profile.gender == 'X' %} +            <span class="glyphicon glyphicon-ban-circle"></span> +            {% else %} +            <span class="glyphicon glyphicon-warning-sign"></span> <span class="label label-danger">系统错误</span> +            {% endif %} +          </td> +        </tr> +        <tr>            <th class="profile-email">邮箱</th>            <td class="profile-email-data">{{ user.email }}</td>          </tr> @@ -38,7 +52,7 @@              {% elif profile.is_approved == 'C' %}              <span class="glyphicon glyphicon-question-sign"></span> <span class="label label-default">审核中</span>              {% else %} -            <span class="glyphicon glyphicon-warning-sign"></span> <span class="label label-danger">错误</span> +            <span class="glyphicon glyphicon-warning-sign"></span> <span class="label label-danger">系统错误</span>              {% endif %}            </td>          </tr> @@ -52,7 +66,7 @@              {% elif profile.is_sponsored == 'C' %}              <span class="glyphicon glyphicon-question-sign"></span> <span class="label label-default">审核中</span>              {% else %} -            <span class="glyphicon glyphicon-warning-sign"></span> <span class="label label-danger">错误</span> +            <span class="glyphicon glyphicon-warning-sign"></span> <span class="label label-danger">系统错误</span>              {% endif %}            </td>          </tr> @@ -60,7 +74,7 @@      <br>      <p> -      <a href="#" class="btn btn-default">编辑个人信息</a> +      <a href="{% url 'profile_update' %}" class="btn btn-default">更新个人信息</a>        <a href="{% url 'password_change' %}" class="btn btn-default">修改密码</a>      </p>    </div> 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 %} +  <div class="container"> +    <h2>更新个人信息</h2> +    <br> +    <form role="form" class="form-horizontal" method="post"> +      {% csrf_token %} +      {% bootstrap_form form layout='horizontal' %} +      {% buttons submit='提交' reset='重置' layout='horizontal' %}{% endbuttons %} +    </form> +  </div> +{% 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 %} +  <div class="container"> +    <h2>信息已更新</h2> +    <p class="lead">您的个人信息已更新。</p> +    <br> +    <p><a href="{% url 'profile' %}" class="btn btn-default">返回个人主页</a></p> +  </div> +{% 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<activation_key>\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. | 
