3
0
Fork 0

Compare commits

...

3 commits

Author SHA1 Message Date
Lance Edgar 0a46dddf3f bump: version 0.8.2 → 0.8.3 2024-08-05 15:31:36 -05:00
Lance Edgar a38a69afa2 fix: add AuthHandler.user_is_admin() method 2024-08-05 15:08:16 -05:00
Lance Edgar 739dd285aa fix: add AppHandler.make_title() convenience method 2024-08-05 15:08:14 -05:00
8 changed files with 80 additions and 2 deletions

View file

@ -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

View file

@ -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"}]

View file

@ -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.

View file

@ -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):

View file

@ -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.

View file

@ -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)

View file

@ -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

View file

@ -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")