Caching.
This commit is contained in:
parent
6ba6121988
commit
c1eeacc0b4
@ -2,7 +2,7 @@ import pathlib
|
||||
|
||||
from aiohttp import web
|
||||
from app.app import Application as BaseApplication
|
||||
|
||||
from app.cache import time_cache_async
|
||||
from jinja_markdown2 import MarkdownExtension
|
||||
from snek.system import http
|
||||
from snek.system.middleware import cors_middleware
|
||||
@ -41,10 +41,9 @@ class Application(BaseApplication):
|
||||
self.router.add_view("/about.md", AboutMDView)
|
||||
self.router.add_view("/web.html", WebView)
|
||||
self.router.add_view("/login.html", LoginView)
|
||||
self.router.add_view("/login-form.json", LoginFormView)
|
||||
self.router.add_view("/login.json", LoginFormView)
|
||||
self.router.add_view("/register.html", RegisterView)
|
||||
|
||||
self.router.add_view("/register-form.json", RegisterFormView)
|
||||
self.router.add_view("/register.json", RegisterFormView)
|
||||
self.router.add_get("/http-get", self.handle_http_get)
|
||||
self.router.add_get("/http-photo", self.handle_http_photo)
|
||||
|
||||
@ -67,6 +66,11 @@ class Application(BaseApplication):
|
||||
)
|
||||
|
||||
|
||||
@time_cache_async(60)
|
||||
async def render_template(self, template, request, context=None):
|
||||
return await super().render_template(template, request, context)
|
||||
|
||||
|
||||
app = Application()
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
@ -8,7 +8,7 @@ from pygments import highlight
|
||||
from pygments.lexers import get_lexer_by_name
|
||||
from pygments.formatters import html
|
||||
from pygments.styles import get_style_by_name
|
||||
|
||||
import functools
|
||||
|
||||
class MarkdownRenderer(HTMLRenderer):
|
||||
def __init__(self, app, template):
|
||||
@ -37,7 +37,11 @@ class MarkdownRenderer(HTMLRenderer):
|
||||
return markdown(markdown_string)
|
||||
|
||||
|
||||
async def render_markdown(app, markdown_string):
|
||||
@functools.cache
|
||||
def render_markdown_sync(app, markdown_string):
|
||||
renderer = MarkdownRenderer(app,None)
|
||||
markdown = Markdown(renderer=renderer)
|
||||
return markdown(markdown_string)
|
||||
|
||||
async def render_markdown(app, markdown_string):
|
||||
return render_markdown_sync(app,markdown_string)
|
@ -81,8 +81,6 @@ class Validator:
|
||||
self.regex = regex
|
||||
self._value = None
|
||||
self.value = value
|
||||
print("xxxx", value, flush=True)
|
||||
|
||||
self.kind = kind
|
||||
self.help_text = help_text
|
||||
self.__dict__.update(kwargs)
|
||||
@ -106,7 +104,6 @@ class Validator:
|
||||
error_list.append(f"Field should be minimal {self.min_length} characters long.")
|
||||
if self.max_length is not None and len(self.value) > self.max_length:
|
||||
error_list.append(f"Field should be maximal {self.max_length} characters long.")
|
||||
print(self.regex, self.value, flush=True)
|
||||
if self.regex and self.value and not re.match(self.regex, self.value):
|
||||
error_list.append("Invalid value.")
|
||||
if self.kind and not isinstance(self.value, self.kind):
|
||||
@ -224,8 +221,6 @@ class BaseModel:
|
||||
return self
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
print(self.__dict__)
|
||||
print(dir(self.__class__))
|
||||
self._mapper = None
|
||||
self.fields = {}
|
||||
for key in dir(self.__class__):
|
||||
@ -233,7 +228,6 @@ class BaseModel:
|
||||
|
||||
if isinstance(obj, Validator):
|
||||
self.__dict__[key] = copy.deepcopy(obj)
|
||||
print("JAAA")
|
||||
self.__dict__[key].value = kwargs.pop(key, self.__dict__[key].initial_value)
|
||||
self.fields[key] = self.__dict__[key]
|
||||
|
||||
@ -245,7 +239,6 @@ class BaseModel:
|
||||
def __getattr__(self, key):
|
||||
obj = self.__dict__.get(key)
|
||||
if isinstance(obj, Validator):
|
||||
print("HPAPP")
|
||||
return obj.value
|
||||
return obj
|
||||
|
||||
|
@ -13,3 +13,57 @@ I made several design choices:
|
||||
- Homebrew made Form framework based on the homebrew made ORM. Most forms are ModelForms but always require an service to be saved for sake of consistency and structure.
|
||||
- !DRY for HMTL/jinja2 templates. For templates Snek does prefer to repeat itself to implement exceptions for a page easier. For Snek it's preffered do a few updates instead of maintaining a complex generic system that requires maintenance regarding templates.
|
||||
- No existing chat backend like `inspircd` (Popular decent IRC server written in the language of angels) because I prefer to know what is exactly going on above performance and concurrency limit. Also, this approach reduces as networking layer / gateway layer.
|
||||
|
||||
# Some internals
|
||||
|
||||
A few examples of how the system framework works.
|
||||
|
||||
## How to create a user
|
||||
```python
|
||||
# Save user to the table named 'user'
|
||||
# Password gets sha256 encrypted with default a salt string
|
||||
# of the snek.system.security module.
|
||||
|
||||
new_user_object = await app.service.user.register(
|
||||
username="retoor",
|
||||
password="retoorded"
|
||||
)
|
||||
```
|
||||
|
||||
## Encrypt string
|
||||
```python
|
||||
from snek.system import security
|
||||
|
||||
# Support for both utf and bytes.
|
||||
var1 = security.encrypt("data")
|
||||
var2 = security.encrypt(b"data")
|
||||
|
||||
# Is correct:
|
||||
assert(var1 == var2)
|
||||
```
|
||||
|
||||
## How to create a basic HTML / Markdown view
|
||||
```python
|
||||
from snek.system.view import BaseView
|
||||
|
||||
class IndexView(BaseView):
|
||||
|
||||
async def get(self):
|
||||
# The render function supports markdown.
|
||||
# It will render with syntax highlighting.
|
||||
# Just use the .md file extension in the file name.
|
||||
return await self.render("index.html")
|
||||
```
|
||||
## How to create a FormView
|
||||
```python
|
||||
from snek.system.view import BaseFormView
|
||||
from snek.form.register import RegisterForm
|
||||
|
||||
class RegisterFormView(BaseFormView):
|
||||
|
||||
form = RegisterForm
|
||||
```
|
||||
## How to register a class view
|
||||
```python
|
||||
app.routes.add_view("/your-page.html", YourViewClass)
|
||||
```
|
@ -4,6 +4,7 @@
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>{% block title %}{% endblock %}</title>
|
||||
<style>{{ highlight_styles }}</style>
|
||||
<link rel="stylesheet" href="/style.css">
|
||||
<script src="/fancy-button.js"></script>
|
||||
<script src="/html-frame.js"></script>
|
||||
|
@ -2,6 +2,6 @@
|
||||
|
||||
{% block main %}
|
||||
<fancy-button url="/back" text="Back" size="auto"></fancy-button>
|
||||
<generic-form class="center" url="/login-form.json"></generic-form>
|
||||
<generic-form class="center" url="/login.json"></generic-form>
|
||||
|
||||
{% endblock %}
|
||||
|
@ -3,5 +3,5 @@
|
||||
{% block main %}
|
||||
<fancy-button url="/back" text="Back" size="auto"></fancy-button>
|
||||
|
||||
<generic-form class="center" url="/register-form.json"></generic-form>
|
||||
<generic-form class="center" url="/register.json"></generic-form>
|
||||
{% endblock %}
|
Loading…
Reference in New Issue
Block a user