aboutsummaryrefslogtreecommitdiffstats
path: root/97suifangqa/apps/sfaccount/views.py
blob: 9cc286899be0a331c4b8c47bfcbfcce64cbdeb22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# -*- coding: utf-8 -*-

from django.conf import settings
from django.http import HttpResponse, HttpResponseRedirect
from django.template.response import TemplateResponse
from django.core.urlresolvers import reverse
from django.views.decorators.csrf import csrf_protect
from django.utils.translation import ugettext as _
from django.shortcuts import render, redirect

from django.contrib.auth.tokens import default_token_generator

from sfaccount.models import Account
from sfaccount.forms import AccountForm, SFPasswordResetForm

# email address shown in the sent mail
FROM_EMAIL = getattr(settings, 'SF_EMAIL').get('display_from')


# go_home {{{
def go_home_view(request):
    """
    go to home page (profile)
    """
    if request.user.is_authenticated():
        username = request.user.username
        return redirect(reverse('profile_home',
            kwargs={'username': username}))
    else:
        # not logged in
        return redirect(reverse('login'))
# }}}


# signup {{{
def signup_view(request):
    """
    用户注册
    """
    if request.user.is_authenticated():
        return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)

    if request.method == 'POST':
        form = AccountForm(data=request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            new_account = Account.objects.create_inactive_account(
                    username=cd['username'],
                    email=cd['email'],
                    password=cd['password1'],
                    send_email=True
            )
            return HttpResponseRedirect(reverse('activate'))
    else:
        form = AccountForm()

    data = {
        'form': form,
    }
    return render(request, 'sfaccount/signup.html', data)
# }}}


# activate {{{
def activate_view(request, activation_key=None):
    """
    activate account

    if activation_key=None, then render a page ask user
        to provide the activation key;
    otherwise, directly activate the account and redirect
    """
    if activation_key:
        account = Account.objects.activate(activation_key)
        if account:
            # activated
            home_url = '/profile/%s/' % account.user.username
            return HttpResponseRedirect(home_url)
        else:
            # activate failed
            data = {'activate_failed': True}
            return render(request, 'sfaccount/activate.html', data)
    else:
        # ask user for the 'activation_key'
        return render(request, 'sfaccount/activate.html')
# }}}


# password_reset_view {{{
# own password_reset_view: enable to send multipart email
@csrf_protect
def password_reset_view(request, is_admin_site=False,
            template_name='sfaccount/password_reset.html',
            email_template_name='sfaccount/password_reset_email.txt',
            subject_template_name='sfaccount/password_reset_subject.txt',
            password_reset_form=SFPasswordResetForm,
            token_generator=default_token_generator,
            post_reset_redirect=None,
            from_email=FROM_EMAIL,
            current_app=None,
            extra_context=None,
            html_email_template_name='sfaccount/password_reset_email.html'):
    """
    re-write view to replace django's one
    able to send multipart email by using
    own 'SFPasswordResetForm' and 'send_mail'
    """
    if post_reset_redirect is None:
        post_reset_redirect = reverse('password_reset_done')
    if request.method == "POST":
        form = password_reset_form(request.POST)
        if form.is_valid():
            opts = {
                'use_https': request.is_secure(),
                'token_generator': token_generator,
                'from_email': from_email,
                'email_template_name': email_template_name,
                'subject_template_name': subject_template_name,
                'request': request,
                'html_email_template_name': html_email_template_name,
            }
            if is_admin_site:
                opts = dict(opts, domain_override=request.get_host())
            form.save(**opts)
            return HttpResponseRedirect(post_reset_redirect)
    else:
        form = password_reset_form()
    context = {
        'form': form,
        'title': _('Password reset'),
    }
    if extra_context is not None:
        context.update(extra_context)
    return TemplateResponse(request, template_name, context,
                            current_app=current_app)
# }}}


# social_login_callback {{{
def social_login_callback(request, sitename):
    return HttpResponse('%s' % sitename)
# }}}