Compare commits
3 commits
27b859c1c7
...
0a46dddf3f
Author | SHA1 | Date | |
---|---|---|---|
![]() |
0a46dddf3f | ||
![]() |
a38a69afa2 | ||
![]() |
739dd285aa |
|
@ -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
|
||||
|
|
|
@ -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"}]
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in a new issue