use izzylog and a few fixes
This commit is contained in:
parent
c8663acc45
commit
11aaaf3499
|
@ -2,10 +2,10 @@ import sys
|
|||
|
||||
from os import getppid, environ as env
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
from enum import IntEnum
|
||||
|
||||
|
||||
class Levels(Enum):
|
||||
class Levels(IntEnum):
|
||||
CRITICAL = 60,
|
||||
ERROR = 50
|
||||
WARNING = 40
|
||||
|
@ -55,7 +55,7 @@ class Log:
|
|||
|
||||
|
||||
def set_config(self, key, value):
|
||||
if key == 'level' and type(value) == str:
|
||||
if key == 'level' and type(value) in [str, int]:
|
||||
value = self.parse_level(value)
|
||||
|
||||
setattr(self, key, value)
|
||||
|
@ -73,7 +73,7 @@ class Log:
|
|||
if isinstance(level, str):
|
||||
Levels[level.upper()]
|
||||
|
||||
if level.value < self.level.value:
|
||||
if level < self.level:
|
||||
return
|
||||
|
||||
default = self.name == 'Default'
|
||||
|
|
|
@ -6,7 +6,7 @@ from multidict import CIMultiDict
|
|||
from multiprocessing import cpu_count, current_process
|
||||
from urllib.parse import parse_qsl, urlparse
|
||||
|
||||
from izzylib import DotDict, Path, logging
|
||||
from izzylib import DotDict, Path, izzylog as logging
|
||||
from izzylib.template import Template
|
||||
|
||||
from .config import Config, UserLevel
|
||||
|
@ -81,16 +81,16 @@ class Application(sanic.Sanic):
|
|||
self.register_middleware(mw.handler, mw.attach)
|
||||
|
||||
|
||||
def get_menu_item(self, name, level=0):
|
||||
return self.cfg.menu[parse_level(level)][name]
|
||||
def get_menu_item(self, name):
|
||||
return self.cfg.menu[name]
|
||||
|
||||
|
||||
def set_menu_item(self, name, path, level=0):
|
||||
self.cfg.menu[parse_level(level)][name] = path
|
||||
self.cfg.menu[name] = (path, parse_level(level))
|
||||
|
||||
|
||||
def del_menu_item(self, name, level=0):
|
||||
del self.cfg.menu[parse_level(level)][name]
|
||||
def del_menu_item(self, name):
|
||||
del self.cfg.menu[name]
|
||||
|
||||
|
||||
def start(self):
|
||||
|
@ -123,12 +123,12 @@ class Application(sanic.Sanic):
|
|||
|
||||
def parse_level(level):
|
||||
if type(level) == int:
|
||||
new_level = UserLevel(level)
|
||||
level = UserLevel(level)
|
||||
|
||||
elif type(level) == str:
|
||||
new_level = UserLevel[level.upper()]
|
||||
level = UserLevel[level.upper()]
|
||||
|
||||
elif type(level) != UserLevel:
|
||||
raise TypeError(f'User level must be a string, integer, or UserLevel, not a {level.__class__.__name__}')
|
||||
|
||||
return new_level
|
||||
return level
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
from enum import Enum
|
||||
from enum import IntEnum
|
||||
from izzylib import DotDict
|
||||
from multiprocessing import cpu_count
|
||||
|
||||
|
@ -6,7 +6,7 @@ from .request import Request
|
|||
from .response import Response
|
||||
|
||||
|
||||
class UserLevel(Enum):
|
||||
class UserLevel(IntEnum):
|
||||
GUEST = 0
|
||||
USER = 10
|
||||
MODERATOR = 20
|
||||
|
@ -35,12 +35,7 @@ class Config(DotDict):
|
|||
tpl_context = None,
|
||||
tpl_autoescape = True,
|
||||
tpl_default = True,
|
||||
menu = {
|
||||
UserLevel.GUEST: {},
|
||||
UserLevel.USER: {},
|
||||
UserLevel.MODERATOR: {},
|
||||
UserLevel.ADMIN: {}
|
||||
}
|
||||
menu = {}
|
||||
)
|
||||
|
||||
|
||||
|
@ -55,9 +50,6 @@ class Config(DotDict):
|
|||
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
if key == 'menu':
|
||||
raise KeyError('Use the Application.set_menu_item function to set menu items')
|
||||
|
||||
if key not in self.defaults.keys():
|
||||
raise KeyError(f'Invalid config key {key}')
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import traceback
|
||||
|
||||
from izzylib import izzylog as logging
|
||||
from jinja2.exceptions import TemplateNotFound
|
||||
|
||||
from .response import Response
|
||||
|
@ -14,12 +15,10 @@ class GenericError:
|
|||
|
||||
|
||||
def __call__(self):
|
||||
return self.error, self.handler
|
||||
return self.error, lambda request, exception: self.handler(request, Response(self.app, request), exception)
|
||||
|
||||
|
||||
def handler(self, request, exception):
|
||||
response = Response(self.app, request)
|
||||
|
||||
def handler(self, request, response, exception):
|
||||
try:
|
||||
status = exception.status_code
|
||||
msg = str(exception)
|
||||
|
@ -42,6 +41,6 @@ class MissingTemplateError(GenericError):
|
|||
error = TemplateNotFound
|
||||
|
||||
|
||||
def handler(self, request, exception):
|
||||
def handler(self, request, response, exception):
|
||||
logging.error('TEMPLATE_ERROR:', f'{exception.__class__.__name__}: {str(exception)}')
|
||||
return request.response.html('I\'m a dingleberry and forgot to create a template for this page', 500)
|
||||
return response.error('I\'m a dingleberry and forgot to create a template for this page', 500)
|
||||
|
|
|
@ -27,8 +27,9 @@
|
|||
#menu.section
|
||||
.title-item.item << Menu
|
||||
#items
|
||||
-for label, path in cfg.menu.items()
|
||||
.item -> %a href='{{cfg.proto}}://{{cfg.web_host}}{{path}}' << {{label}}
|
||||
-for label, data in cfg.menu.items()
|
||||
-if request.user_level >= data[1]
|
||||
.item -> %a href='{{cfg.proto}}://{{cfg.web_host}}{{data[0]}}' << {{label}}
|
||||
|
||||
#content-body.section
|
||||
-block content
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import multiprocessing
|
||||
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from izzylib import logging
|
||||
from izzylib import izzylog as logging, logging as applog
|
||||
|
||||
from . import start_time
|
||||
|
||||
|
@ -37,4 +37,4 @@ class AccessLog(MiddlewareBase):
|
|||
uagent = request.headers.get('user-agent', 'None')
|
||||
address = request.headers.get('x-real-ip', request.forwarded.get('for', request.remote_addr))
|
||||
|
||||
logging.info(f'({multiprocessing.current_process().name}) {address} {request.method} {request.path} {response.status} "{uagent}"')
|
||||
applog.info(f'({multiprocessing.current_process().name}) {address} {request.method} {request.path} {response.status} "{uagent}"')
|
||||
|
|
|
@ -10,6 +10,7 @@ class Request(sanic.request.Request):
|
|||
self.Headers = Headers(headers)
|
||||
self.Data = Data(self)
|
||||
self.template = self.app.template
|
||||
self.user_level = 0
|
||||
self.setup()
|
||||
|
||||
|
||||
|
|
|
@ -147,7 +147,7 @@ class Response:
|
|||
**self.default_theme
|
||||
})
|
||||
|
||||
html = self.app.template.render(tplfile, context, request=self, pprint=pprint)
|
||||
html = self.app.template.render(tplfile, context, request=self.request, pprint=pprint)
|
||||
return self.html(html, headers=headers, status=status, content_type=content_type, cookies=cookies)
|
||||
|
||||
|
||||
|
@ -195,10 +195,9 @@ class Response:
|
|||
ctype = self.content_types.get(content_type, content_type)
|
||||
|
||||
self.body = body
|
||||
self.headers = headers
|
||||
self.status = status
|
||||
self.content_type = content_type
|
||||
self.headers = headers
|
||||
self.headers.update(headers)
|
||||
self.headers.pop('content-type', None)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue