source: publico/colab/trunk/colab/views/signup.py @ 6017

Última Alteração nesse arquivo desde 6017 foi 6017, incluída por seocam, 8 anos atrás

Entrega de numero 5 do Edital ATU-COLAB publicado em 2011

File size: 6.8 KB
Linha 
1#!/usr/bin/env python
2# encoding: utf-8
3"""
4signup.py
5
6Created by Sergio Campos on 2012-01-10.
7"""
8
9import uuid
10from colab import signup as signup_
11
12from django.template import RequestContext
13from django.contrib.auth.models import User
14from django.utils.translation import ugettext as _
15from django.contrib.auth.decorators import login_required
16from django.contrib.auth.forms import SetPasswordForm, PasswordChangeForm
17from django.shortcuts import render_to_response, redirect, get_object_or_404
18
19from colab.super_archives.forms import UserCreationForm
20from colab.super_archives.models import UserProfile, EmailAddress
21
22
23def signup(request):
24
25    # If the request method is GET just return the form
26    if request.method == 'GET':
27        form = UserCreationForm()
28        return render_to_response('signup-form.html', {'form': form},
29                                  RequestContext(request))
30
31    # If the request method is POST try to store data
32    form = UserCreationForm(request.POST)
33   
34    # If there is validation errors give the form back to the user
35    if not form.is_valid():
36        return render_to_response('signup-form.html', {'form': form},
37                                  RequestContext(request))
38
39    user = User(
40        username=form.cleaned_data.get('username'),
41        email=form.cleaned_data.get('email'),
42        first_name=form.cleaned_data.get('first_name'),
43        last_name=form.cleaned_data.get('last_name'),
44        is_active=False,
45    )
46    user.set_password(form.cleaned_data.get('password1'))
47    user.save()
48   
49    profile = UserProfile(
50        user=user,
51        institution=form.cleaned_data.get('institution'),
52        role=form.cleaned_data.get('role'),
53        twitter=form.cleaned_data.get('twitter'),
54        facebook=form.cleaned_data.get('facebook'),
55        google_talk=form.cleaned_data.get('google_talk'),
56        webpage=form.cleaned_data.get('webpage'),
57        verification_hash=uuid.uuid4().get_hex(),
58    )
59    profile.save()
60   
61    signup_.send_verification_email(request, user)
62
63    # Check if the user's email have been used previously
64    #   in the mainling lists to link the user to old messages 
65    email_addr, created = EmailAddress.objects.get_or_create(address=user.email)
66    if created:
67        email_addr.real_name = user.get_full_name()
68
69    email_addr.user = user
70    email_addr.save()
71   
72    template_data = {
73        'msg': _((u'Cadastro efetuado com sucesso. Por favor acesse seu '
74                u'endereço de email para validá-lo.')),
75        'msg_css_class': 'success',
76    }
77   
78    return render_to_response('account_message.html', template_data,
79                              RequestContext(request))
80
81
82def verify_email(request, hash):
83    """Verify hash and activate user's account"""
84   
85    profile = get_object_or_404(UserProfile, verification_hash=hash)
86   
87    profile.verification_hash = 'verified'
88    profile.save()
89   
90    profile.user.is_active = True
91    profile.user.save()
92   
93    template_data = {
94        'msg': _(u'Endereço de e-mail validado corretamente.'),
95        'msg_css_class': 'success',
96    }
97   
98    return render_to_response('account_message.html', template_data,
99                              RequestContext(request))
100   
101
102def request_reset_password(request):
103    """Request a password reset.
104   
105    In case request method is GET it will display the password reset
106    form. Otherwise we'll look for a username in the POST request to
107    have its password reset. This user will receive a link where he
108    will be allowed to change his password.
109   
110    """
111   
112    if request.method == 'GET':
113        return render_to_response('account_request_reset_password.html', {},
114                                  RequestContext(request))
115   
116    username = request.POST.get('username')
117
118    try:
119        user = User.objects.get(username=username)
120    except User.DoesNotExist:
121        user = None
122   
123    if user and user.is_active:
124        profile = user.profile
125        profile.verification_hash = uuid.uuid4().get_hex()
126        profile.save()
127               
128        signup_.send_reset_password_email(request, user)
129   
130    msg = _((u'Para sua segurança, dentro de alguns instantes você '
131           u'receberá um email solicitando a confirmação do pedido '
132           u'de troca de senha. Por favor aguarde.'))
133   
134    template_data = {
135        'msg': msg,
136        'msg_css_class': 'info',
137    }
138   
139    return render_to_response('account_message.html', template_data,
140                              RequestContext(request))
141
142
143def reset_password(request, hash):
144    """Perform a password change.
145   
146    If the request method is set to GET and the hash matches a form
147    will be displayed to the user allowing the password change.
148    If the request method is POST the user password will be changed
149    to the newly set data.
150   
151    """
152   
153    profile = get_object_or_404(UserProfile, verification_hash=hash)
154    user = profile.user
155   
156    form = SetPasswordForm(profile.user)
157   
158    template_data = {
159        'form': form,
160        'hash': hash,
161    }
162   
163    if request.method == 'GET':
164        return render_to_response('account_change_password.html',
165                                  template_data, RequestContext(request))
166
167
168    form = SetPasswordForm(user, request.POST)
169    template_data.update({'form': form})
170   
171    if not form.is_valid():
172        return render_to_response('account_change_password.html',
173                                  template_data, RequestContext(request))
174
175    profile.verification_hash = 'verified'
176    profile.save()
177
178    user.set_password(form.cleaned_data.get('new_password1'))
179    user.save()
180
181    template_data.update({
182        'msg': _(u'Senha alterada com sucesso!'),
183        'msg_css_class': 'success',
184    })
185    return render_to_response('account_message.html', template_data,
186                              RequestContext(request))
187
188@login_required
189def change_password(request):
190    """Change a password for an authenticated user."""
191   
192    form = PasswordChangeForm(request.user)
193   
194    template_data = {
195        'form': form
196    }
197   
198    if request.method == 'GET':
199        return render_to_response('account_change_password.html',
200                                  template_data, RequestContext(request))
201   
202    form = PasswordChangeForm(request.user, request.POST)
203    template_data.update({'form': form})
204   
205    if not form.is_valid():
206        return render_to_response('account_change_password.html',
207                                  template_data, RequestContext(request))
208                                 
209    request.user.set_password(form.cleaned_data.get('new_password1'))
210    request.user.save()
211   
212    template_data.update({
213        'msg': _(u'Senha alterada com sucesso!'),
214        'msg_css_class': 'success',
215    })
216    return render_to_response('account_message.html', template_data,
217                              RequestContext(request))   
218   
Note: Veja TracBrowser para ajuda no uso do navegador do trac.
 

The contents and data of this website are published under license:
Creative Commons 4.0 Brasil - Atribuir Fonte - Compartilhar Igual.