Esempio n. 1
0
def register(request):
    if request.user.is_authenticated():
        messages.add_message(
            request, messages.INFO,
            _('You already have an account. To register a separate account, please logout.'
              ))
        return HttpResponseRedirect('/account/')
    if request.method == 'POST':
        form = forms.RegisterForm(request.POST)
        if form.is_valid():
            new_data = form.cleaned_data
            new_profile = models.UserProfile.create_user_and_profile(
                email=new_data[u'email'],
                password=new_data[u'password'],
                first_name=new_data[u'first_name'],
                last_name=new_data[u'last_name'])
            new_profile.save()
            coupons.claim_featured_discounts(request, new_profile.user)
            # Send an email with the activation link
            email_subject = _(
                'Please confirm your account with Science Writing Experts')
            t = loader.get_template('email/activation_request.txt')
            c = RequestContext(
                request, {
                    'activation_key': new_profile.activation_key,
                    'customer_service_name': settings.CUSTOMER_SERVICE_NAME,
                    'customer_service_title': _(
                        settings.CUSTOMER_SERVICE_TITLE),
                })
            t_html = loader.get_template('email/activation_request.html')
            email_body = t.render(c)
            email_body_html = t_html.render(c)
            mail = EmailMultiAlternatives(
                subject=email_subject,
                body=email_body,
                from_email='*****@*****.**',
                to=[new_profile.user.email],
            )
            mail.attach_alternative(email_body_html, 'text/html')
            mail.send()
            messages.add_message(
                request, messages.WARNING,
                _('An activation key has been sent to your email address. Please check your email to finish creating your account.'
                  ))
            return HttpResponseRedirect('/confirm/')
        else:  # invalid form
            messages.add_message(request, messages.ERROR,
                                 MessageCatalog.form_invalid)
            return render_to_response('account/register.html',
                                      RequestContext(request, {'form': form}))
    else:
        #GET
        form = forms.RegisterForm()
        return render_to_response('account/register.html',
                                  RequestContext(request, {'form': form}))
Esempio n. 2
0
def register(request):
    message = ""
    if request.session.get('is_login', None):
        # 登录状态不允许注册。你可以修改这条原则!
        return redirect("/")
    if request.method == "POST":
        register_form = forms.RegisterForm(request.POST)
        message = "请检查填写的内容!"
        if register_form.is_valid():  # 获取数据
            username = register_form.cleaned_data['username']
            password1 = register_form.cleaned_data['password1']
            password2 = register_form.cleaned_data['password2']
            email = register_form.cleaned_data['email']
            sex = register_form.cleaned_data['sex']
            if password1 != password2:  # 判断两次密码是否相同
                message = "两次输入的密码不同!"
                return render(request, 'register.html', {'message': message})
            else:
                same_name_user = models.User.objects.filter(name=username)
                if same_name_user:  # 用户名唯一
                    message = '用户已经存在,请重新选择用户名!'
                    return render(request, 'register.html',
                                  {'message': message})
                same_email_user = models.User.objects.filter(email=email)
                if same_email_user:  # 邮箱地址唯一
                    message = '该邮箱地址已被注册,请使用别的邮箱!'
                    return render(request, 'register.html',
                                  {'message': message})

                # 当一切都OK的情况下,创建新用户

                new_user = models.User()
                new_user.name = username
                new_user.password = hash_code(password1)  # 使用加密密码
                new_user.email = email
                new_user.sex = sex
                new_user.save()

                code = make_confirm_string(new_user)
                send_email(email, code)
                message = '请前往注册邮箱,进行邮件确认!'
                return render(request, 'confirm.html',
                              {'message': message})  # 跳转到等待邮件确认页面。
    register_form = forms.RegisterForm()
    return render(request, 'register.html', {
        'message': message,
        'register_form': register_form
    })
Esempio n. 3
0
def register(request):
    # 登录状态不允许注册。
    # 如果我想提醒用户在登录状态下不允许注册,要怎么做??
    if request.session.get('is_login', None):
        return redirect("/index/")
    if request.method == "POST":
        register_form = forms.RegisterForm(
            request.POST)  # 感觉不合语法:为啥这个实例可以填写request.POST这个参数??
        message = "请填写查找内容!"
        if register_form.is_valid():
            username = register_form.cleaned_data['username']
            password1 = register_form.cleaned_data['password1']
            password2 = register_form.cleaned_data['password2']
            email = register_form.cleaned_data['email']
            sex = register_form.cleaned_data['sex']
            # 判断两次输入的密码是否相同
            if password1 != password2:
                message = "两次输入的密码不同!"
                return render(request, 'login/register.html', locals())
            else:
                # 控制用户名唯一
                same_name_user = models.User.objects.filter(name=username)
                # User表里没有name为username的对象,则same_name_user的值为none(即False)
                if same_name_user:
                    message = '用户已经存在,请重新选择用户名!'
                    return render(request, 'login/register.html', locals())
                # 控制邮箱地址唯一
                same_email_user = models.User.objects.filter(email=email)
                if same_email_user:
                    message = '该邮箱地址已被注册,请使用别的邮箱!'
                    return render(request, 'login/register.html', locals())

                # 通过重重检查后,终于可以创建新用户了!
                new_user = models.User()
                new_user.name = username
                new_user.password = password1
                new_user.email = email
                new_user.sex = sex
                # 这个save()操作容易漏掉
                new_user.save()
                # 注册完以后自动跳转到登录页面
                return redirect('/login/')
    # 如果没通过上面的检查,则“清空”一下register_form
    # 并重新注册
    register_form = forms.RegisterForm()
    return render(request, 'login/register.html', locals())
Esempio n. 4
0
def register(request):
    if request.session.get('is_login', None):
        return redirect('/index/')
    if request.method == 'POST':
        register_form = forms.RegisterForm(request.POST)
        message = "请检查填写的内容"
        if register_form.is_valid():
            username = register_form.cleaned_data['username']
            password1 = register_form.cleaned_data['password1']
            password2 = register_form.cleaned_data['password2']
            email = register_form.cleaned_data['email']
            sex = register_form.cleaned_data['sex']
            if password1 != password2:
                message = "两次输入的密码不同"
                return render(request, 'login/register.html', locals())
            else:
                same_name_user = models.User.objects.filter(name=username)
                if same_name_user:  #用户名唯一
                    message = "用户名已存在"
                    return render(request, 'login/register.html', locals())
                same_email_user = models.User.objects.filter(email=email)
                if same_email_user:  #邮箱地址唯一
                    message = "该邮箱已被注册"
                    return render(request, 'login/register.html', locals())
                #当一切情况都正常,创建新用户
                new_user = models.User()
                new_user.name = username
                new_user.password = hash_code(password1)  #使用加密密码
                new_user.email = email
                new_user.sex = sex
                new_user.save()

                code = make_confirm_string(new_user)
                send_email(email, code)

                message = "前往邮箱进行确认"
                return render(request, 'login/confirm.html',
                              locals())  #跳转到等待邮件确认页面
    register_form = forms.RegisterForm()
    return render(request, 'login/register.html', locals())
Esempio n. 5
0
def register(request):
    if request.session.get('is_login', None):
        # 登录状态不允许注册。你可以修改这条原则!
        return redirect("/index/")
    if request.method == "POST":
        register_form = forms.RegisterForm(request.POST)
        message = "请检查填写的内容!"
        if register_form.is_valid():  # 获取数据
            username = register_form.cleaned_data['username']
            password1 = register_form.cleaned_data['password1']
            password2 = register_form.cleaned_data['password2']
            email = register_form.cleaned_data['email']
            sex = register_form.cleaned_data['sex']
            qq = register_form.cleaned_data['qq']
            if password1 != password2:  # 判断两次密码是否相同
                message = "两次输入的密码不同!"
                return render(request, 'loginapp/register.html', locals())
            else:
                same_name_user = models.User.objects.filter(name=username)
                if same_name_user:  # 用户名唯一
                    message = '用户已经存在,请重新选择用户名!'
                    return render(request, 'loginapp/register.html', locals())
                same_email_user = models.User.objects.filter(email=email)
                if same_email_user:  # 邮箱地址唯一
                    message = '该邮箱地址已被注册,请使用别的邮箱!'
                    return render(request, 'loginapp/register.html', locals())

                # 当一切都OK的情况下,创建新用户

                new_user = models.User.objects.create()
                new_user.name = username
                new_user.password = hash_code(password1)  # 使用加密密码
                new_user.email = email
                new_user.qq = qq
                new_user.sex = sex
                new_user.save()
                return redirect('/login/')  # 自动跳转到登录页面
    register_form = forms.RegisterForm()
    return render(request, 'loginapp/register.html', locals())
Esempio n. 6
0
def register(request):
    context = {}
    if request.method == "POST":
        register_form = forms.RegisterForm(request.POST)
        if register_form.is_valid():
            username = register_form.data.get('username')
            s_name = register_form.data.get('s_name')
            s_xi = register_form.data.get('s_xi')
            password = register_form.data.get('password')
            email = register_form.data.get('email')
            s_sex = register_form.data.get('s_sex')
            models.student_user.objects.create(s_no=username,
                                               s_name=s_name,
                                               s_sex=s_sex,
                                               s_xi=s_xi,
                                               s_pwd=password,
                                               s_email=email)
            return redirect('index.html')
    else:
        register_form = forms.RegisterForm()
    context['register_form'] = register_form
    return render(request, 'register.html', context)
Esempio n. 7
0
def register(request):
    if request.session.get('is_login', None):
        return redirect('/index/')

    if request.method == "POST":
        register_form = forms.RegisterForm(request.POST)
        message = "请检查填写内容"
        if register_form.is_valid():
            username = register_form.cleaned_data.get('username')
            password1 = register_form.cleaned_data.get('password1')
            password2 = register_form.cleaned_data.get('password2')
            email = register_form.cleaned_data.get('email')
            sex = register_form.cleaned_data.get('sex')

            if password1 != password2:
                message = '两次输入的密码不同!'
                return render(request, 'login/register.html', locals())
            else:
                same_name_user = models.User.objects.filter(name=username)
                if same_name_user:
                    message = '用户名已经存在'
                    return render(request, 'login/register.html', locals())
                same_email_user = models.User.objects.filter(email=email)
                if same_email_user:
                    message = '该邮箱已经被注册了!'
                    return render(request, 'login/register.html', locals())

                new_user = models.User()
                new_user.name = username
                new_user.password = hash_code(password1)
                new_user.email = email
                new_user.sex = sex
                new_user.save()

                return redirect('/login/')
        else:
            return render(request, 'login/register.html', locals())
    register_form = forms.RegisterForm()
    return render(request, 'login/register.html', locals())
Esempio n. 8
0
def register(request):
    if request.session.get('is_login', None):
        # 登录状态不允许注册。你可以修改这条原则!
        return redirect("/index/")
    if request.method == "POST":
        register_form = forms.RegisterForm(request.POST)
        message = "Please validate the information!"
        if register_form.is_valid():  # get data
            username = register_form.cleaned_data['username']
            password1 = register_form.cleaned_data['password1']
            password2 = register_form.cleaned_data['password2']
            email = register_form.cleaned_data['email']
            sex = register_form.cleaned_data['sex']
            if password1 != password2:  # to make sure both password are correct
                message = "both passwords are not match"
                return render(request, 'login/register.html', locals())
            else:
                same_name_user = models.User.objects.filter(name=username)
                if same_name_user:  # check is username unique
                    message = 'please choose different username!'
                    return render(request, 'login/register.html', locals())
                same_email_user = models.User.objects.filter(email=email)
                if same_email_user:  # check unique email
                    message = 'this email has been used, please try again!'
                    return render(request, 'login/register.html', locals())

                # if everything is ok, then register

                new_user = models.User()
                new_user.name = username
                new_user.password = password1
                new_user.email = email
                new_user.sex = sex
                new_user.save()
                return redirect('/login/')  # auto jump to login page
    register_form = forms.RegisterForm()
    return render(request, 'login/register.html', locals())
Esempio n. 9
0
    def post(self, request):
        register_form = forms.RegisterForm(request.POST)
        if register_form.is_valid():  # 获取数据
            username = request.POST.get('username', '')
            password1 = request.POST.get('password1', '')
            password2 = request.POST.get('password2', '')
            email = request.POST.get('email', '')
            sex = request.POST.get('sex', '')
            if password1 != password2:  # 判断两次密码是否相同
                message = "两次输入的密码不同!"
                return render(request, 'login/register.html', {
                    'message': message,
                    'register_form': register_form
                })
            else:
                same_name_user = models.User.objects.filter(name=username)
                same_email_user = models.User.objects.filter(email=email)
                if same_name_user:  # 用户名唯一
                    message = '用户已经存在,请重新选择用户名!'
                    return render(request, 'login/register.html', {
                        'message': message,
                        'register_form': register_form
                    })
                elif same_email_user:  # 邮箱地址唯一
                    message = '该邮箱地址已被注册,请使用别的邮箱!'
                    return render(request, 'login/register.html', {
                        'message': message,
                        'register_form': register_form
                    })

                else:
                    # 当一切都OK的情况下,创建新用户

                    new_user = models.User()
                    new_user.name = username
                    new_user.password = hash_code(password1)  # 使用哈希加密密码
                    new_user.email = email
                    new_user.sex = sex
                    new_user.save()

                    code = make_confirm_string(new_user)
                    send_email(email, code)
                    message = '请前往注册邮箱,进行邮件确认!'
                    return render(request, 'login/confirm.html',
                                  {'message': message})  # 跳转到等待邮件确认页面。
Esempio n. 10
0
def cginfo(request):
    if not request.session.get('is_login', None):
        return redirect("/index/")
    if request.method == "POST":
        register_form = forms.RegisterForm(request.POST)
        message = "请检查填写的内容!"
        if register_form.is_valid():  # 获取数据
            password1 = register_form.cleaned_data['password1']
            password2 = register_form.cleaned_data['password2']
            if password1 != password2:  # 判断两次密码是否相同
                message = "两次输入的密码不同!"
                return render(request, 'loginapp/cginfo.html', locals())
            else:
                username = request.session['user_name']
                user = models.User.objects.filter(name=username)
                user.password = hash_code(password1)
                user.save()
                del request.session['is_login']
                del request.session['user_id']
                del request.session['user_name']
                return redirect('/login/')
    cginfo_form = forms.CginfoForm()
    return render(request, 'loginapp/cginfo.html', locals())
Esempio n. 11
0
 def get(self, request):
     register_form = forms.RegisterForm(request.POST)
     return render(request, 'login/register.html', {
         'message': '',
         'register_form': register_form
     })