From 3c986f14c48d20b8a44094bf1b273d181fb0683d Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 27 Jun 2023 10:20:06 +0000 Subject: [PATCH] users: Move edit page from people Signed-off-by: Michael Tremer --- Makefile.am | 4 +- .../user-edit.html => users/edit.html} | 0 src/web/__init__.py | 2 +- src/web/people.py | 60 ------------------ src/web/users.py | 61 +++++++++++++++++++ 5 files changed, 64 insertions(+), 63 deletions(-) rename src/templates/{people/user-edit.html => users/edit.html} (100%) diff --git a/Makefile.am b/Makefile.am index 2cfad43f..a8357f07 100644 --- a/Makefile.am +++ b/Makefile.am @@ -278,8 +278,7 @@ templates_people_DATA = \ src/templates/people/sip.html \ src/templates/people/subscribed.html \ src/templates/people/unsubscribe.html \ - src/templates/people/unsubscribed.html \ - src/templates/people/user-edit.html + src/templates/people/unsubscribed.html templates_peopledir = $(templatesdir)/people @@ -307,6 +306,7 @@ templates_static_DATA = \ templates_staticdir = $(templatesdir)/static templates_users_DATA = \ + src/templates/users/edit.html \ src/templates/users/index.html \ src/templates/users/passwd.html \ src/templates/users/show.html diff --git a/src/templates/people/user-edit.html b/src/templates/users/edit.html similarity index 100% rename from src/templates/people/user-edit.html rename to src/templates/users/edit.html diff --git a/src/web/__init__.py b/src/web/__init__.py index 92bb56fc..024f84c6 100644 --- a/src/web/__init__.py +++ b/src/web/__init__.py @@ -168,6 +168,7 @@ class Application(tornado.web.Application): (r"/users", users.IndexHandler), (r"/users/([a-z_][a-z0-9_-]{0,31})", users.ShowHandler), (r"/users/([a-z_][a-z0-9_-]{0,31})\.jpg", users.AvatarHandler), + (r"/users/([a-z_][a-z0-9_-]{0,31})/edit", users.EditHandler), (r"/users/([a-z_][a-z0-9_-]{0,31})/passwd", users.PasswdHandler), # Static Pages @@ -319,7 +320,6 @@ class Application(tornado.web.Application): (r"/register", auth.RegisterHandler), (r"/users/([a-z_][a-z0-9_-]{0,31})/calls/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})", people.CallHandler), (r"/users/([a-z_][a-z0-9_-]{0,31})/calls(?:/(\d{4}-\d{2}-\d{2}))?", people.CallsHandler), - (r"/users/([a-z_][a-z0-9_-]{0,31})/edit", people.UserEditHandler), (r"/users/([a-z_][a-z0-9_-]{0,31})/sip", people.SIPHandler), # Promotional Consent Stuff diff --git a/src/web/people.py b/src/web/people.py index 251e74dc..1e0b331a 100644 --- a/src/web/people.py +++ b/src/web/people.py @@ -4,8 +4,6 @@ import datetime import ldap import tornado.web -from .. import countries - from . import base from . import ui_modules @@ -102,64 +100,6 @@ class SIPHandler(base.BaseHandler): self.render("people/sip.html", account=account) -class UserEditHandler(base.BaseHandler): - @tornado.web.authenticated - def get(self, uid): - account = self.backend.accounts.get_by_uid(uid) - if not account: - raise tornado.web.HTTPError(404, "Could not find account %s" % uid) - - # Check for permissions - if not account.can_be_managed_by(self.current_user): - raise tornado.web.HTTPError(403, "%s cannot manage %s" % (self.current_user, account)) - - self.render("people/user-edit.html", account=account, countries=countries.get_all()) - - @tornado.web.authenticated - def post(self, uid): - account = self.backend.accounts.get_by_uid(uid) - if not account: - raise tornado.web.HTTPError(404, "Could not find account %s" % uid) - - # Check for permissions - if not account.can_be_managed_by(self.current_user): - raise tornado.web.HTTPError(403, "%s cannot manage %s" % (self.current_user, account)) - - # Unfortunately this cannot be wrapped into a transaction - try: - account.first_name = self.get_argument("first_name") - account.last_name = self.get_argument("last_name") - account.nickname = self.get_argument("nickname", None) - account.street = self.get_argument("street", None) - account.city = self.get_argument("city", None) - account.postal_code = self.get_argument("postal_code", None) - account.country_code = self.get_argument("country_code", None) - account.description = self.get_argument("description", None) - - # Avatar - try: - filename, data, mimetype = self.get_file("avatar") - - if not mimetype.startswith("image/"): - raise tornado.web.HTTPError(400, "Avatar is not an image file: %s" % mimetype) - - account.upload_avatar(data) - except TypeError: - pass - - # Email - account.mail_routing_address = self.get_argument("mail_routing_address", None) - - # Telephone - account.phone_numbers = self.get_argument("phone_numbers", "").splitlines() - account.sip_routing_address = self.get_argument("sip_routing_address", None) - except ldap.STRONG_AUTH_REQUIRED as e: - raise tornado.web.HTTPError(403, "%s" % e) from e - - # Redirect back to user page - self.redirect("/users/%s" % account.uid) - - class AgentModule(ui_modules.UIModule): def render(self, account): return self.render_string("people/modules/agent.html", account=account) diff --git a/src/web/users.py b/src/web/users.py index 89e2359d..a1572d83 100644 --- a/src/web/users.py +++ b/src/web/users.py @@ -3,10 +3,13 @@ import PIL import imghdr import io +import ldap import logging import os.path import tornado.web +from .. import countries + from . import base from . import ui_modules @@ -142,6 +145,64 @@ class AvatarHandler(base.BaseHandler): return f.getvalue() +class EditHandler(base.BaseHandler): + @tornado.web.authenticated + def get(self, uid): + account = self.backend.accounts.get_by_uid(uid) + if not account: + raise tornado.web.HTTPError(404, "Could not find account %s" % uid) + + # Check for permissions + if not account.can_be_managed_by(self.current_user): + raise tornado.web.HTTPError(403, "%s cannot manage %s" % (self.current_user, account)) + + self.render("users/edit.html", account=account, countries=countries.get_all()) + + @tornado.web.authenticated + def post(self, uid): + account = self.backend.accounts.get_by_uid(uid) + if not account: + raise tornado.web.HTTPError(404, "Could not find account %s" % uid) + + # Check for permissions + if not account.can_be_managed_by(self.current_user): + raise tornado.web.HTTPError(403, "%s cannot manage %s" % (self.current_user, account)) + + # Unfortunately this cannot be wrapped into a transaction + try: + account.first_name = self.get_argument("first_name") + account.last_name = self.get_argument("last_name") + account.nickname = self.get_argument("nickname", None) + account.street = self.get_argument("street", None) + account.city = self.get_argument("city", None) + account.postal_code = self.get_argument("postal_code", None) + account.country_code = self.get_argument("country_code", None) + account.description = self.get_argument("description", None) + + # Avatar + try: + filename, data, mimetype = self.get_file("avatar") + + if not mimetype.startswith("image/"): + raise tornado.web.HTTPError(400, "Avatar is not an image file: %s" % mimetype) + + account.upload_avatar(data) + except TypeError: + pass + + # Email + account.mail_routing_address = self.get_argument("mail_routing_address", None) + + # Telephone + account.phone_numbers = self.get_argument("phone_numbers", "").splitlines() + account.sip_routing_address = self.get_argument("sip_routing_address", None) + except ldap.STRONG_AUTH_REQUIRED as e: + raise tornado.web.HTTPError(403, "%s" % e) from e + + # Redirect back to user page + self.redirect("/users/%s" % account.uid) + + class PasswdHandler(base.BaseHandler): @tornado.web.authenticated def get(self, uid): -- 2.39.5