Add app.context_processor; error pages set status

master
Douglas Blank 2018-07-27 05:46:00 -04:00
rodzic 588798fae1
commit a763600b48
3 zmienionych plików z 30 dodań i 97 usunięć

Wyświetl plik

@ -6,12 +6,14 @@ import uuid
class Data():
routes = []
filters = []
context_processors = []
def clear(self):
self.routes.clear()
self.filters.clear()
self.context_processors.clear()
class DataWrapper():
class Application():
"""
Instance for saving routes, filters, etc. for app.
@ -58,12 +60,22 @@ class DataWrapper():
return f
return decorator
def context_processor(self, f):
"""
Wrap a plain function/method to provide context processor function.
"""
self._data.context_processors.append(f)
return f
def get_routes(self):
return self._data.routes
def get_filters(self):
return self._data.filters
def get_context_processors(self):
return self._data.context_processors
def wrap_function(manager, f):
"""
General Function/Method Wrapper
@ -71,7 +83,7 @@ def wrap_function(manager, f):
## Check the signature:
params = [x.name for x in inspect.signature(f).parameters.values()]
if len(params) == 0 or params[0] != "self":
raise Exception("route function %s needs 'self' as first parameter"
raise Exception("function %s needs 'self' as first parameter"
% f.__name__)
def function(*args, **kwargs):
results = f(manager, *args, **kwargs)
@ -135,7 +147,7 @@ class Manager():
pass
def error(self, error_number):
return self.render_template("%s.html" % error_number)
pass
@property
def request(self):
@ -381,4 +393,4 @@ class Manager():
## Singleton for the Application
## Allows it to be in scope for decorating the app's
## methods and functions
app = DataWrapper()
app = Application()

Wyświetl plik

@ -9,7 +9,6 @@ except:
pass # flask not available
from .base import Manager, wrap_function, app
from .._version import VERSION
class FlaskManager(Manager):
"""
@ -21,50 +20,14 @@ class FlaskManager(Manager):
return request.args.get(name, default_value)
def render_template(self, template_name, **kwargs):
## TODO : add context_processor
q = {
"type": "Create",
"activity.object.type": "Note",
"activity.object.inReplyTo": None,
"meta.deleted": False,
}
notes_count = self.database.activities.find(
{"box": "outbox", "$or": [q, {"type": "Announce", "meta.undo": False}]}
).count()
q = {"type": "Create", "activity.object.type": "Note", "meta.deleted": False}
with_replies_count = self.database.activities.find(
{"box": "outbox", "$or": [q, {"type": "Announce", "meta.undo": False}]}
).count()
liked_count = self.database.activities.count(
{
"box": "outbox",
"meta.deleted": False,
"meta.undo": False,
"type": "Like",
}
)
followers_q = {
"box": "inbox",
"type": "follow",
"meta.undo": False,
}
following_q = {
"box": "outbox",
"type": "follow",
"meta.undo": False,
}
kwargs.update({
"request": {"args": {}},
"session": {"logged_in": True},
"microblogpub_version": VERSION,
"followers_count": self.database.activities.count(followers_q),
"following_count": self.database.activities.count(following_q),
"notes_count": 0, #notes_count,
"liked_count": 0, #liked_count,
"with_replies_count": 0, #with_replies_count,
"DOMAIN": "localhost:5000/test", # for tornado TODO: update on each fetch, include full URL, /test
})
return render_template(template_name, **kwargs)
values = {}
for f in app.get_context_processors():
values.update(f(self))
values.update(kwargs)
return render_template(template_name, **values)
def error(self, error_number):
return self.render_template("%s.html" % error_number), error_number
def redirect(self, url):
return redirect(url)

Wyświetl plik

@ -4,12 +4,10 @@ try:
except:
pass # tornado not available
import inspect
import jinja2
import re
from .base import Manager, wrap_function, app
from .._version import VERSION
def make_handler(f, manager, methods):
"""
@ -33,6 +31,7 @@ def make_handler(f, manager, methods):
self.write(obj) # will set header to JSON mimetype
def error(self, error_number):
self.set_status(error_number)
self.write(manager.render_template("%s.html" % error_number))
return Handler
@ -51,55 +50,15 @@ class TornadoManager(Manager):
return self._filters
def render_template(self, name, **kwargs):
## TODO : add context_processor
q = {
"type": "Create",
"activity.object.type": "Note",
"activity.object.inReplyTo": None,
"meta.deleted": False,
}
notes_count = self.database.activities.find(
{"box": "outbox", "$or": [q, {"type": "Announce", "meta.undo": False}]}
).count()
q = {"type": "Create", "activity.object.type": "Note", "meta.deleted": False}
with_replies_count = self.database.activities.find(
{"box": "outbox", "$or": [q, {"type": "Announce", "meta.undo": False}]}
).count()
liked_count = self.database.activities.count(
{
"box": "outbox",
"meta.deleted": False,
"meta.undo": False,
"type": "Like",
}
)
followers_q = {
"box": "inbox",
"type": "follow",
"meta.undo": False,
}
following_q = {
"box": "outbox",
"type": "follow",
"meta.undo": False,
}
kwargs.update({
"request": {"args": {}},
"session": {"logged_in": True},
"microblogpub_version": VERSION,
"followers_count": self.database.activities.count(followers_q),
"following_count": self.database.activities.count(following_q),
"notes_count": 0, #notes_count,
"liked_count": 0, #liked_count,
"with_replies_count": 0, #with_replies_count,
"DOMAIN": "localhost:5000/test", # for tornado TODO: update on each fetch, include full URL, /test
})
values = {}
for f in app.get_context_processors():
values.update(f(self))
values.update(kwargs)
filters = self.get_filters()
tornado.log.logging.warning("%s" % list(filters.keys()))
self.template_env.filters.update(filters)
template = self.template_env.get_template(name)
return template.render(config=self.config,
**kwargs)
return template.render(config=self.config, **values)
## TODO: move to app.Data
#def login_required():
@ -116,7 +75,6 @@ class TornadoManager(Manager):
self.config["CSS"] = self.CSS
routes = []
for route, methods, f in app._data.routes:
params = [x.name for x in inspect.signature(f).parameters.values()]
# Replace "<parameter>" with Tornado re matching string:
route = re.sub("\<[^\>]*\>", r"([^/]*)", route)
routes.append((route, make_handler(f, self, methods)))