annotate libervia/pages/register/page_meta.py @ 1128:6414fd795df4

server, pages: multi-sites refactoring: Libervia is now handling external sites (i.e. other sites than Libervia official site). The external site are declared in sites_path_public_dict (in [DEFAULT] section) which is read by template engine, then they are linked to virtual host with vhosts_dict (linking host name to site name) in [libervia] section. Sites are only instanced once, so adding an alias is just a matter of mapping the alias host name in vhosts_dict with the same site name. menu_json and url_redirections_dict can now accept keys named after site name, which will be linked to the data for the site. Data for default site can still be keyed at first level. Libervia official pages are added to external site (if pages are not overriden), allowing to call pages of the framework and to have facilities like login handling. Deprecated url_redirections_profile option has been removed.
author Goffi <goffi@goffi.org>
date Fri, 14 Sep 2018 21:41:28 +0200
parents 28e3eb3bb217
children 29eb15062416
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
963
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python2.7
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
3
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
4 from libervia.server.constants import Const as C
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
5 from libervia.server import session_iface
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
6 from twisted.internet import defer
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
7 from sat.core.log import getLogger
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 963
diff changeset
8
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 963
diff changeset
9 log = getLogger("pages/register")
963
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
10
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
11 """SàT account registration page"""
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
12
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
13 name = u"register"
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
14 access = C.PAGES_ACCESS_PUBLIC
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
15 template = u"login/register.html"
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
16
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
17
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
18 def prepare_render(self, request):
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
19 profile = self.getProfile(request)
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
20 if profile is not None:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 963
diff changeset
21 self.pageRedirect("/login/logged", request)
963
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
22 template_data = request.template_data
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 963
diff changeset
23 template_data["login_url"] = self.getPageByName("login").url
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 963
diff changeset
24 template_data["S_C"] = C # we need server constants in template
963
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
25
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
26 # login error message
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
27 session_data = self.host.getSessionData(request, session_iface.ISATSession)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 963
diff changeset
28 login_error = session_data.popPageData(self, "login_error")
963
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
29 if login_error is not None:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 963
diff changeset
30 template_data["login_error"] = login_error
963
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
31
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 963
diff changeset
32 #  if fields were already filled, we reuse them
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 963
diff changeset
33 for k in (u"login", u"email", u"password"):
963
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
34 template_data[k] = session_data.popPageData(self, k)
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
35
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 963
diff changeset
36
963
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
37 @defer.inlineCallbacks
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
38 def on_data_post(self, request):
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 963
diff changeset
39 type_ = self.getPostedData(request, u"type")
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 963
diff changeset
40 if type_ == u"register":
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 963
diff changeset
41 login, email, password = self.getPostedData(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 963
diff changeset
42 request, (u"login", u"email", u"password")
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 963
diff changeset
43 )
963
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
44 status = yield self.host.registerNewAccount(request, login, password, email)
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
45 session_data = self.host.getSessionData(request, session_iface.ISATSession)
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
46 if status == C.REGISTRATION_SUCCEED:
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
47 # we prefill login field for login page
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 963
diff changeset
48 session_data.setPageData(self.getPageByName(u"login"), u"login", login)
963
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
49 # if we have a redirect_url we follow it
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
50 self.redirectOrContinue(request)
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
51 # else we redirect to login page
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 963
diff changeset
52 self.HTTPRedirect(request, self.getPageByName(u"login").url)
963
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
53 else:
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
54 session_data.setPageData(self, u"login_error", status)
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
55 l = locals()
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 963
diff changeset
56 for k in (u"login", u"email", u"password"):
963
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
57 # we save fields so user doesn't have to enter them again
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
58 session_data.setPageData(self, k, l[k])
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
59 defer.returnValue(C.POST_NO_CONFIRM)
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
60 else:
2932170bb526 pages: added login/logged and register pages
Goffi <goffi@goffi.org>
parents:
diff changeset
61 self.pageError(request, C.HTTP_BAD_REQUEST)