comparison libervia/web/pages/register/page_meta.py @ 1518:eb00d593801d

refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 16:49:28 +0200
parents libervia/pages/register/page_meta.py@106bae41f5c8
children 291a7026cb2b
comparison
equal deleted inserted replaced
1517:b8ed9726525b 1518:eb00d593801d
1 #!/usr/bin/env python3
2
3
4 from libervia.web.server.constants import Const as C
5 from libervia.web.server import session_iface
6 from twisted.internet import defer
7 from libervia.backend.core.log import getLogger
8
9 log = getLogger(__name__)
10
11 """SàT account registration page"""
12
13 name = "register"
14 access = C.PAGES_ACCESS_PUBLIC
15 template = "login/register.html"
16
17
18 def prepare_render(self, request):
19 if not self.host.options["allow_registration"]:
20 self.page_error(request, C.HTTP_FORBIDDEN)
21 profile = self.get_profile(request)
22 if profile is not None:
23 self.page_redirect("/login/logged", request)
24 template_data = request.template_data
25 template_data["login_url"] = self.get_page_by_name("login").url
26 template_data["S_C"] = C # we need server constants in template
27
28 # login error message
29 session_data = self.host.get_session_data(request, session_iface.IWebSession)
30 login_error = session_data.pop_page_data(self, "login_error")
31 if login_error is not None:
32 template_data["login_error"] = login_error
33
34 #  if fields were already filled, we reuse them
35 for k in ("login", "email", "password"):
36 template_data[k] = session_data.pop_page_data(self, k)
37
38
39 @defer.inlineCallbacks
40 def on_data_post(self, request):
41 type_ = self.get_posted_data(request, "type")
42 if type_ == "register":
43 login, email, password = self.get_posted_data(
44 request, ("login", "email", "password")
45 )
46 status = yield self.host.register_new_account(request, login, password, email)
47 session_data = self.host.get_session_data(request, session_iface.IWebSession)
48 if status == C.REGISTRATION_SUCCEED:
49 # we prefill login field for login page
50 session_data.set_page_data(self.get_page_by_name("login"), "login", login)
51 # if we have a redirect_url we follow it
52 self.redirect_or_continue(request)
53 # else we redirect to login page
54 self.http_redirect(request, self.get_page_by_name("login").url)
55 else:
56 session_data.set_page_data(self, "login_error", status)
57 l = locals()
58 for k in ("login", "email", "password"):
59 # we save fields so user doesn't have to enter them again
60 session_data.set_page_data(self, k, l[k])
61 defer.returnValue(C.POST_NO_CONFIRM)
62 else:
63 self.page_error(request, C.HTTP_BAD_REQUEST)