diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libervia/web/pages/register/page_meta.py	Fri Jun 02 16:49:28 2023 +0200
@@ -0,0 +1,63 @@
+#!/usr/bin/env python3
+
+
+from libervia.web.server.constants import Const as C
+from libervia.web.server import session_iface
+from twisted.internet import defer
+from libervia.backend.core.log import getLogger
+
+log = getLogger(__name__)
+
+"""SàT account registration page"""
+
+name = "register"
+access = C.PAGES_ACCESS_PUBLIC
+template = "login/register.html"
+
+
+def prepare_render(self, request):
+    if not self.host.options["allow_registration"]:
+        self.page_error(request, C.HTTP_FORBIDDEN)
+    profile = self.get_profile(request)
+    if profile is not None:
+        self.page_redirect("/login/logged", request)
+    template_data = request.template_data
+    template_data["login_url"] = self.get_page_by_name("login").url
+    template_data["S_C"] = C  # we need server constants in template
+
+    # login error message
+    session_data = self.host.get_session_data(request, session_iface.IWebSession)
+    login_error = session_data.pop_page_data(self, "login_error")
+    if login_error is not None:
+        template_data["login_error"] = login_error
+
+    #  if fields were already filled, we reuse them
+    for k in ("login", "email", "password"):
+        template_data[k] = session_data.pop_page_data(self, k)
+
+
+@defer.inlineCallbacks
+def on_data_post(self, request):
+    type_ = self.get_posted_data(request, "type")
+    if type_ == "register":
+        login, email, password = self.get_posted_data(
+            request, ("login", "email", "password")
+        )
+        status = yield self.host.register_new_account(request, login, password, email)
+        session_data = self.host.get_session_data(request, session_iface.IWebSession)
+        if status == C.REGISTRATION_SUCCEED:
+            # we prefill login field for login page
+            session_data.set_page_data(self.get_page_by_name("login"), "login", login)
+            # if we have a redirect_url we follow it
+            self.redirect_or_continue(request)
+            # else we redirect to login page
+            self.http_redirect(request, self.get_page_by_name("login").url)
+        else:
+            session_data.set_page_data(self, "login_error", status)
+            l = locals()
+            for k in ("login", "email", "password"):
+                # we save fields so user doesn't have to enter them again
+                session_data.set_page_data(self, k, l[k])
+            defer.returnValue(C.POST_NO_CONFIRM)
+    else:
+        self.page_error(request, C.HTTP_BAD_REQUEST)