3 from pylons
import request
, response
, session
, tmpl_context
as c
, app_globals
as g
4 from pylons
.controllers
.util
import abort
, redirect_to
6 from townsquare
.model
.user
import User
7 from townsquare
.lib
.account
import *
8 from townsquare
.lib
import mail
9 from townsquare
.lib
.base
import BaseController
, render
11 log
= logging
.getLogger(__name__
)
13 class AccountController(BaseController
):
16 # Forward to home if logged in
17 idname
= session
.get('account.idname')
18 if idname
is not None and len(idname
):
19 redirect_to('/') #TODO: application's root, not host's root
23 c
.username
= request
.POST
.getone('username').strip()
24 idname
= c
.username
.lower()
25 acc
= g
.db
.col(Account
).find_one(idname
=idname
)
27 if acc
.authenticate(request
.POST
.getone('password')):
28 #TODO: should use session id
29 session
['account.idname'] = idname
31 #TODO: show message (with auto-redirect) or just redirect
32 redirect_to('/') #TODO: application's root, not host's root
33 return render('/account/login.mako')
36 session
['account.idname'] = ''
38 redirect_to('/') #TODO: application's root, not host's root
41 # Forward to home if logged in
42 idname
= session
.get('account.idname')
43 if idname
is not None and len(idname
):
44 redirect_to('/') #TODO: application's root, not host's root
49 # Process submited form data here
50 #TODO: lots of check here
51 c
.username
= request
.POST
.getone('username').strip()
52 if (len(c
.username
) < 4 or len(c
.username
) > 25):
53 c
.field_errors
.append('username')
54 if (re
.match(r
'^[A-Za-z0-9_]+$', c
.username
) == None):
55 c
.field_errors
.append('username')
56 c
.email
= request
.POST
.getone('email')
57 if (len(c
.email
) < 4):
58 c
.field_errors
.append('email')
59 c
.email_confirm
= request
.POST
.getone('email_confirm')
60 if (c
.email_confirm
!= c
.email
or len(c
.email_confirm
) < 4):
61 c
.field_errors
.append('email_confirm')
62 c
.password
= request
.POST
.getone('password')
63 if (len(c
.password
) < 6):
64 c
.field_errors
.append('password')
65 c
.password_confirm
= request
.POST
.getone('password_confirm')
66 if (c
.password_confirm
!= c
.password
or len(c
.password_confirm
) < 6):
67 c
.field_errors
.append('password_confirm')
68 #TODO: custom fields (example: invitation code, referrer, introductory message)
69 form_valid
= len(c
.field_errors
) == 0
72 # Continue the registration
74 user
.name
= c
.username
76 user
.passkey
= c
.password
81 g
.db
.col(Account
).insert(user
)
83 c
.field_errors
.append('username')
85 c
.field_errors
.append('email')
86 c
.field_errors
.append('email_confirm')
89 vals_valid
= len(c
.field_errors
) == 0
91 #TODO: prepare the content
92 mail
.send_administration_mail(user
.email
, "Activation Code", "TODO: mail message here!")
95 return render('/account/register_succeded.mako')
97 return render('/account/register.mako')
99 def activate(self
, key
=None):
100 idname
= session
.get('account.idname')
101 if idname
is not None and len(idname
):
102 redirect_to('/') #TODO: application's root, not host's root
103 if (len(request
.POST
)):
104 key
= request
.POST
.getone('activation_key').strip()
106 acc
= g
.db
.col(Account
).find_one(activation_key
=key
)
108 if acc
.activate(key
):
110 return render('/account/activate_succeded.mako')
112 return render('/account/activate.mako')
115 idname
= session
.get('account.idname')
116 if idname
is None or len(idname
) == 0:
117 redirect_to('/login') #TODO: application's root, not host's root. with redirect parameter.
118 #TODO: check config (whom can invite, the number of invitations)
120 return render('/account/invite.mako')