From 739dd285aac9482f046850c83cf46411ac63af33 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Sun, 4 Aug 2024 14:23:02 -0500 Subject: [PATCH 1/3] fix: add `AppHandler.make_title()` convenience method --- src/wuttjamaican/app.py | 16 +++++++++++++++- src/wuttjamaican/util.py | 15 +++++++++++++++ tests/test_app.py | 4 ++++ tests/test_util.py | 7 +++++++ 4 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/wuttjamaican/app.py b/src/wuttjamaican/app.py index 53d378b..fef2b30 100644 --- a/src/wuttjamaican/app.py +++ b/src/wuttjamaican/app.py @@ -28,7 +28,7 @@ import importlib import os import warnings -from wuttjamaican.util import load_entry_points, load_object, make_uuid, parse_bool +from wuttjamaican.util import load_entry_points, load_object, make_title, make_uuid, parse_bool class AppHandler: @@ -232,6 +232,20 @@ class AppHandler: return Session(**kwargs) + def make_title(self, text, **kwargs): + """ + Return a human-friendly "title" for the given text. + + This is mostly useful for converting a Python variable name (or + similar) to a human-friendly string, e.g.:: + + make_title('foo_bar') # => 'Foo Bar' + + By default this just invokes + :func:`wuttjamaican.util.make_title()`. + """ + return make_title(text) + def make_uuid(self): """ Generate a new UUID value. diff --git a/src/wuttjamaican/util.py b/src/wuttjamaican/util.py index 5ebc73a..78a755a 100644 --- a/src/wuttjamaican/util.py +++ b/src/wuttjamaican/util.py @@ -114,6 +114,21 @@ def load_object(spec): return getattr(module, name) +def make_title(text): + """ + Return a human-friendly "title" for the given text. + + This is mostly useful for converting a Python variable name (or + similar) to a human-friendly string, e.g.:: + + make_title('foo_bar') # => 'Foo Bar' + """ + text = text.replace('_', ' ') + text = text.replace('-', ' ') + words = text.split() + return ' '.join([x.capitalize() for x in words]) + + def make_uuid(): """ Generate a universally-unique identifier. diff --git a/tests/test_app.py b/tests/test_app.py index 03a9cc3..b2aa391 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -116,6 +116,10 @@ class TestAppHandler(TestCase): def test_get_title(self): self.assertEqual(self.app.get_title(), 'WuttJamaican') + def test_make_title(self): + text = self.app.make_title('foo_bar') + self.assertEqual(text, "Foo Bar") + def test_make_uuid(self): uuid = self.app.make_uuid() self.assertEqual(len(uuid), 32) diff --git a/tests/test_util.py b/tests/test_util.py index 7e0bac5..921e416 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -232,3 +232,10 @@ class TestParseList(TestCase): self.assertEqual(value[0], 'foo') self.assertEqual(value[1], 'C:\\some path\\with spaces\\and, a comma') self.assertEqual(value[2], 'baz') + + +class TestMakeTitle(TestCase): + + def test_basic(self): + text = util.make_title('foo_bar') + self.assertEqual(text, "Foo Bar") From a38a69afa286eba260caa5d4dae9b2ea923e7aa3 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Mon, 5 Aug 2024 14:27:53 -0500 Subject: [PATCH 2/3] fix: add `AuthHandler.user_is_admin()` method --- src/wuttjamaican/auth.py | 14 ++++++++++++++ tests/test_auth.py | 17 +++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/wuttjamaican/auth.py b/src/wuttjamaican/auth.py index d9300ed..008d352 100644 --- a/src/wuttjamaican/auth.py +++ b/src/wuttjamaican/auth.py @@ -369,6 +369,20 @@ class AuthHandler(GenericHandler): """ return self._special_role(session, 'b765a9cc331a11e6ac2a3ca9f40bc550', "Authenticated") + def user_is_admin(self, user, **kwargs): + """ + Check if given user is a member of the "Administrator" role. + + :rtype: bool + """ + if user: + session = self.app.get_session(user) + admin = self.get_role_administrator(session) + if admin in user.roles: + return True + + return False + def get_permissions(self, session, principal, include_anonymous=True, include_authenticated=True): diff --git a/tests/test_auth.py b/tests/test_auth.py index e8d5e15..cf37417 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -253,6 +253,23 @@ else: self.assertEqual(self.session.query(model.Role).count(), 1) self.assertEqual(role.name, "Authenticated") + def test_user_is_admin(self): + model = self.app.model + + # non-user is not admin + self.assertFalse(self.handler.user_is_admin(None)) + + # new user but not yet admin + user = self.handler.make_user(session=self.session) + self.session.commit() + self.assertFalse(self.handler.user_is_admin(user)) + + # but we can make them an admin + admin = self.handler.get_role_administrator(self.session) + user.roles.append(admin) + self.session.commit() + self.assertTrue(self.handler.user_is_admin(user)) + def test_get_permissions(self): model = self.app.model From 0a46dddf3f1443403697728e33bd0fd8e717fa88 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Mon, 5 Aug 2024 15:31:36 -0500 Subject: [PATCH 3/3] =?UTF-8?q?bump:=20version=200.8.2=20=E2=86=92=200.8.3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 7 +++++++ pyproject.toml | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 049ccaa..554f41e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ All notable changes to WuttJamaican will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## v0.8.3 (2024-08-05) + +### Fix + +- add `AuthHandler.user_is_admin()` method +- add `AppHandler.make_title()` convenience method + ## v0.8.2 (2024-07-18) ### Fix diff --git a/pyproject.toml b/pyproject.toml index 4d9be0b..3fd56ab 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,7 +6,7 @@ build-backend = "hatchling.build" [project] name = "WuttJamaican" -version = "0.8.2" +version = "0.8.3" description = "Base package for Wutta Framework" readme = "README.md" authors = [{name = "Lance Edgar", email = "lance@edbob.org"}]