2024-04-12 19:22:00 +00:00
|
|
|
# builtins
|
|
|
|
import functools
|
|
|
|
|
|
|
|
# third-party
|
2024-05-22 01:32:17 +00:00
|
|
|
import werkzeug.routing.map
|
2024-04-12 19:22:00 +00:00
|
|
|
import flask
|
|
|
|
|
2024-05-02 18:57:38 +00:00
|
|
|
# internals
|
|
|
|
import util
|
|
|
|
|
2024-05-22 01:32:17 +00:00
|
|
|
class URLMap(werkzeug.routing.map.Map):
|
|
|
|
|
|
|
|
def remove_endpoint(self, endpoint):
|
|
|
|
|
|
|
|
rules = []
|
|
|
|
for rule in self._rules:
|
|
|
|
if rule.endpoint == endpoint:
|
|
|
|
rules.append(rule)
|
|
|
|
|
|
|
|
for rule in rules:
|
|
|
|
self._rules.remove(rule)
|
|
|
|
|
|
|
|
del(self._rules_by_endpoint[endpoint])
|
|
|
|
|
|
|
|
self._remap = True
|
|
|
|
self.update()
|
|
|
|
|
2024-04-12 19:22:00 +00:00
|
|
|
class MenuMixin:
|
|
|
|
|
|
|
|
def menu(self, name):
|
|
|
|
|
|
|
|
def decorator(f):
|
|
|
|
|
|
|
|
self.menu_functions[name] = f
|
|
|
|
|
|
|
|
return f
|
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
def menu_exists(self, name):
|
|
|
|
|
|
|
|
return name in self.menu_functions
|
|
|
|
|
|
|
|
def menu_get(self, name):
|
|
|
|
|
|
|
|
return self.menu_functions[name]()
|
|
|
|
|
2024-05-13 03:57:53 +00:00
|
|
|
class BlockMixin:
|
|
|
|
|
|
|
|
def block(self, name):
|
|
|
|
|
|
|
|
def decorator(f):
|
|
|
|
|
|
|
|
self.block_functions[name] = f
|
|
|
|
|
|
|
|
return f
|
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
def block_get(self, name):
|
|
|
|
|
|
|
|
return self.block_functions[name]()
|
|
|
|
|
|
|
|
class Blueprint(flask.Blueprint, MenuMixin, BlockMixin):
|
2024-04-12 19:22:00 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.menu_functions = {}
|
2024-05-13 03:57:53 +00:00
|
|
|
self.block_functions = {}
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-05-13 03:57:53 +00:00
|
|
|
class Application(flask.Flask, MenuMixin, BlockMixin):
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-05-22 01:32:17 +00:00
|
|
|
url_map_class = URLMap
|
|
|
|
|
2024-04-12 19:22:00 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
|
|
|
super().__init__(*args, **kwargs)
|
2024-05-17 00:56:17 +00:00
|
|
|
|
2024-04-12 19:22:00 +00:00
|
|
|
self.boot_functions = []
|
2024-05-17 00:56:17 +00:00
|
|
|
self.cron_functions = []
|
2024-04-12 19:22:00 +00:00
|
|
|
self.menu_functions = {}
|
2024-05-13 03:57:53 +00:00
|
|
|
self.block_functions = {}
|
2024-04-12 19:22:00 +00:00
|
|
|
self.models_exposed = {}
|
|
|
|
self.models_abstract = []
|
|
|
|
self.admin = Blueprint('admin', __name__, url_prefix='/admin')
|
2024-04-12 19:22:00 +00:00
|
|
|
|
|
|
|
def boot(self, f):
|
|
|
|
|
|
|
|
self.boot_functions.append(f)
|
|
|
|
|
|
|
|
return f
|
|
|
|
|
|
|
|
def boot_run(self):
|
|
|
|
|
|
|
|
for func in self.boot_functions:
|
|
|
|
try:
|
|
|
|
func()
|
|
|
|
except Exception as e:
|
|
|
|
self.logger.error(f"Boot function {func} failed to run: {str(e)}")
|
|
|
|
|
2024-05-17 00:56:17 +00:00
|
|
|
def cron(self, f):
|
|
|
|
|
|
|
|
self.cron_functions.append(f)
|
|
|
|
|
|
|
|
return f
|
|
|
|
|
2024-04-12 19:22:00 +00:00
|
|
|
def expose(self, base=None, mode='full'):
|
|
|
|
|
|
|
|
"""
|
|
|
|
base: str, rule of the listing.
|
|
|
|
single views will be placed in a path below the listing
|
|
|
|
"""
|
|
|
|
def decorator(cls):
|
|
|
|
|
|
|
|
import admin
|
|
|
|
import rendering
|
|
|
|
|
2024-04-12 19:22:00 +00:00
|
|
|
if cls.__name__ in self.models_exposed:
|
2024-04-12 19:22:00 +00:00
|
|
|
raise TypeError(f"A class with name {cls.__name} is already exposed.")
|
|
|
|
|
|
|
|
if not issubclass(cls, admin.Administerable):
|
|
|
|
raise TypeError("Can only expose classes derived from Administerable.")
|
|
|
|
|
|
|
|
if base is None:
|
|
|
|
rule_base = f'/{cls._lowerclass}/'
|
|
|
|
else:
|
|
|
|
rule_base = base
|
|
|
|
|
|
|
|
if rule_base[-1] != '/':
|
|
|
|
raise ValueError("base for @expose MUST end with '/'. Leave it empty to autogenerate based on class name.")
|
|
|
|
|
2024-04-12 19:22:00 +00:00
|
|
|
self.models_exposed[cls.__name__] = cls
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-04-12 19:22:00 +00:00
|
|
|
# public routes
|
2024-04-12 19:22:00 +00:00
|
|
|
@rendering.page(title_show=False)
|
2024-05-13 02:52:45 +00:00
|
|
|
def listing(offset=0):
|
2024-04-12 19:22:00 +00:00
|
|
|
# TODO: Menu?
|
2024-05-13 02:52:45 +00:00
|
|
|
return rendering.Listing(cls.list(), title=cls.__name__, endpoint=f'{cls._lowerclass}_listing', offset=offset)
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-04-23 13:42:01 +00:00
|
|
|
@rendering.page(format='atom')
|
|
|
|
def feed():
|
|
|
|
return rendering.Listing(cls.list(), title=cls.__name__)
|
|
|
|
|
2024-04-12 19:22:00 +00:00
|
|
|
self.add_url_rule(rule_base, f'{cls._lowerclass}_listing', listing)
|
2024-05-14 02:20:47 +00:00
|
|
|
self.add_url_rule(f'{rule_base}/+<int:offset>/', f'{cls._lowerclass}_listing', listing)
|
2024-04-23 13:42:01 +00:00
|
|
|
self.add_url_rule(f'/feed{rule_base}', f'{cls._lowerclass}_feed', feed)
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-04-12 19:22:00 +00:00
|
|
|
if issubclass(cls, admin.Named):
|
|
|
|
self.add_url_rule(f'{rule_base}/<string:name>/', f'{cls._lowerclass}_full', functools.partial(cls.view, mode=mode))
|
|
|
|
else:
|
|
|
|
self.add_url_rule(f'{rule_base}/<int:id>/', f'{cls._lowerclass}_full', functools.partial(cls.view, mode=mode))
|
2024-04-12 19:22:00 +00:00
|
|
|
|
|
|
|
return cls
|
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
2024-04-12 19:22:00 +00:00
|
|
|
def abstract(self, cls):
|
|
|
|
|
2024-05-17 00:56:17 +00:00
|
|
|
"""
|
|
|
|
Marks a class as abstract. Kind of a hack, should ideally be part of
|
|
|
|
ChildAware, but was easier to implement.
|
|
|
|
"""
|
|
|
|
|
2024-04-12 19:22:00 +00:00
|
|
|
self.models_abstract.append(cls)
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-04-12 19:22:00 +00:00
|
|
|
return cls
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-04-22 21:29:28 +00:00
|
|
|
app = Application('pdn', template_folder='themes')
|
2024-05-02 18:57:38 +00:00
|
|
|
|
2024-04-12 19:22:00 +00:00
|
|
|
app.config.from_pyfile('config.py')
|
2024-05-02 18:57:38 +00:00
|
|
|
app.jinja_env.filters['prettydate'] = util.prettydate
|