From 202b0633c975c75af87538e42c25e16fa9e9cd17 Mon Sep 17 00:00:00 2001
From: Weitian LI 
Date: Mon, 21 Apr 2014 20:56:25 +0800
Subject: * added 'ResendEmailForm' forms * implemented 'ResendEmailView'
 class-based view and templates * added js code 'account_email_resend.js'
Warning:
cannot load static file located in app directory
---
 account/forms.py                                  | 87 ++++++++++++++++++-----
 account/models.py                                 | 17 +++++
 account/static/js/account_email_resend.js         | 19 +++++
 account/templates/account/email_resend.html       | 29 ++++++++
 account/templates/account/email_resend_done.html  | 24 +++++++
 account/urls.py                                   | 10 ++-
 account/views.py                                  | 19 ++++-
 static/js/account_email_resend.js                 | 19 +++++
 templates/registration/registration_complete.html |  1 +
 9 files changed, 204 insertions(+), 21 deletions(-)
 create mode 100644 account/static/js/account_email_resend.js
 create mode 100644 account/templates/account/email_resend.html
 create mode 100644 account/templates/account/email_resend_done.html
 create mode 100644 static/js/account_email_resend.js
diff --git a/account/forms.py b/account/forms.py
index ca54711..f5f515f 100644
--- a/account/forms.py
+++ b/account/forms.py
@@ -5,6 +5,8 @@ account/forms.py for skaschool
 """
 
 from django import forms
+from django.contrib.auth.models import User
+from django.contrib.sites.models import Site, RequestSite
 from registration.forms import RegistrationFormUniqueEmail
 from django.utils.translation import ugettext_lazy as _
 
@@ -17,27 +19,76 @@ class UserRegForm(RegistrationFormUniqueEmail):
     add fields 'realname', 'gender', 'institute' and 'captcha'
     """
     # XXX: keep consistent with GENDERS in 'models.UserProfile'
-    GENDERS = (
-        ('M', _("Male")),
-        ('F', _("Female")),
-        ('X', _("Secret")),
-    )
+    GENDERS = UserProfile.GENDERS
+    IDENTIFIES = UserProfile.IDENTIFIES
     realname = forms.CharField(max_length=30, label=_("Name"))
     gender = forms.ChoiceField(choices=GENDERS, label=_("Gender"))
     institute = forms.CharField(max_length=100, label=_("Institute"))
+    identify = forms.ChoiceField(choices=IDENTIFIES, label=_("Identify"))
 
-    def __init__(self, *args, **kw):
-        super(UserRegForm, self).__init__(*args, **kw)
-        # order form fields
-        self.fields.keyOrder = [
-                'username',
-                'email',
-                'password1',
-                'password2',
-                'realname',
-                'gender',
-                'institute',
-        ]
+    #def __init__(self, *args, **kw):
+    #    super(UserRegForm, self).__init__(*args, **kw)
+    #    # order form fields
+    #    self.fields.keyOrder = [
+    #            'username',
+    #            'email',
+    #            'password1',
+    #            'password2',
+    #            'realname',
+    #            'gender',
+    #            'institute',
+    #            'identify',
+    #    ]
+
+
+class ResendEmailForm(forms.Form):
+    """
+    form used in ResendEmailView
+    """
+    username = forms.RegexField(regex=r'^[\w.@+-]+$',
+                                max_length=30,
+                                label=_("Username"),
+                                error_messages={'invalid': _("This value may contain only letters, numbers and @/./+/-/_ characters.")})
+    is_update_email = forms.BooleanField(label=_("Is update email"),
+            required=False, initial=False)
+    email = forms.EmailField(label=_("E-mail"), required=False)
+
+    def clean_username(self):
+        username = self.cleaned_data.get('username', '')
+        user = User.objects.filter(username=username)
+        if not user:
+            raise forms.ValidationError(_("Username not exists"), code='invalid')
+        return username
+
+    def clean(self):
+        # if 'is_update_email' True, then email field is required
+        is_update_email = self.cleaned_data.get('is_update_email', False)
+        email = self.cleaned_data.get('email', '')
+        if (is_update_email and (not email)):
+            raise forms.ValidationError(_("Email is required"), code='required')
+        return self.cleaned_data
+
+    def update_email(self):
+        # update User email if user provided a different email
+        username = self.cleaned_data.get('username', '')
+        email = self.cleaned_data.get('email', '')
+        user = User.objects.get(username=username)
+        if email:
+            user.email = email
+            user.save()
+
+    def resend_email(self):
+        # resend activation email
+        if Site._meta.installed:
+            site = Site.objects.get_current()
+        else:
+            site = RequestSite(self.request)
+
+        username = self.cleaned_data.get('username', '')
+        user = User.objects.get(username=username)
+        regprofile = user.registrationprofile_set.get(user=user)
+        if not regprofile.activation_key_expired():
+            regprofile.send_activation_email(site)
 
 
 class UpdateProfileForm(forms.ModelForm):
@@ -49,6 +100,6 @@ class UpdateProfileForm(forms.ModelForm):
 
     class Meta:
         model = UserProfile
-        fields = ('realname', 'gender', 'institute')
+        fields = ('realname', 'gender', 'institute', 'identify')
 
 
diff --git a/account/models.py b/account/models.py
index 684f2d8..9f2fff7 100644
--- a/account/models.py
+++ b/account/models.py
@@ -36,12 +36,29 @@ class UserProfile(models.Model):
         ('N', _("No")),
         ('C', _("Checking")),
     )
+    # choices for identify
+    IDENTIFIES = (
+        ('OT', _("Other")),
+        ('UG', _("Undergraudate")),
+        ('MG', _("Master graudate")),
+        ('PG', _("PhD graudate")),
+        ('PD', _("Post-doctoral")),
+        ('SR', _("Assistant researcher")),
+        ('AR', _("Associate researcher")),
+        ('DR', _("Distinguished researcher")),
+        ('RE', _("Researcher")),
+        ('IN', _("Instructor")),
+        ('SP', _("Assistant professor")),
+        ('AP', _("Associate professor")),
+        ('PR', _("Professor")),
+    )
     # 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)
     institute = models.CharField(_("Institute"), max_length=100)
+    identify = models.CharField(_("Identify"), max_length=2, choices=IDENTIFIES)
     # store the infomation about approval and sponsorship
     is_approved = models.CharField(_("Is approved"), max_length=1,
             choices=APPROVED_STATUS, default='C')
diff --git a/account/static/js/account_email_resend.js b/account/static/js/account_email_resend.js
new file mode 100644
index 0000000..e8d2411
--- /dev/null
+++ b/account/static/js/account_email_resend.js
@@ -0,0 +1,19 @@
+/*
+ * email_resend.js for 'account/email_resend.html'
+ */
+
+$(document).ready(function() {
+    // disable email field by default
+    $("input#id_email").prop("disabled", true);
+
+    // update email field when 'is_update_email' checkbox changed
+    $("input#id_is_update_email").change(function(e) {
+        if ($(this).prop("checked")) {
+            $("input#id_email").prop("disabled", false);
+        }
+        else {
+            $("input#id_email").prop("disabled", true);
+        }
+    });
+});
+
diff --git a/account/templates/account/email_resend.html b/account/templates/account/email_resend.html
new file mode 100644
index 0000000..bdb2f30
--- /dev/null
+++ b/account/templates/account/email_resend.html
@@ -0,0 +1,29 @@
+{% extends 'base.html' %}
+{% load staticfiles %}
+{% load url from future %}
+{% load bootstrap3 %}
+
+{# resend activation email done #}
+
+{% block title %}
+重新发送激活邮件 | 2014 SKA Summer School
+{% endblock %}
+
+{% block content %}
+  
+    
重新发送激活邮件
+    
+
+    
+  
+{% endblock %}
+
+{% block js_extra %}
+  
+{% endblock %}
+
+{# vim: set ts=8 sw=2 tw=0 fenc=utf-8 ft=htmldjango.html: #}
diff --git a/account/templates/account/email_resend_done.html b/account/templates/account/email_resend_done.html
new file mode 100644
index 0000000..ff7ad6b
--- /dev/null
+++ b/account/templates/account/email_resend_done.html
@@ -0,0 +1,24 @@
+{% extends 'base.html' %}
+{% load staticfiles %}
+{% load url from future %}
+{% load bootstrap3 %}
+
+{# resend activation email done #}
+
+{% block title %}
+已重新发送激活邮件 | 2014 SKA Summer School
+{% endblock %}
+
+{% block content %}
+  
+    
已重新发送激活邮件
+    
+      我们已向您注册使用的邮箱重新发送了激活邮件,请留意查收,并使用其中的链接激活账户。
+    
+
+    
+    
如果您还未收到激活邮件,或者激活链接已过期,请直接联系我们以激活账户:skaschool2014@163.com
+  
如果您的激活链接已过期,请直接联系我们以激活账户:skaschool2014@163.com
   
 {% endblock %}
 
-- 
cgit v1.2.2