kopia lustrzana https://github.com/dsblank/activitypub
url_for, port, kwargs for routes
rodzic
a763600b48
commit
1ff85976f8
|
@ -233,6 +233,7 @@ ActivityPubBase.CLASSES = {
|
|||
"Activity": Activity,
|
||||
"Application": Application,
|
||||
"Group": Group,
|
||||
"Create": Create,
|
||||
"Organization": Organization,
|
||||
"Person": Person,
|
||||
"Service": Service,
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import json
|
||||
from .bson import ObjectId
|
||||
|
||||
class JSONEncoder(json.JSONEncoder):
|
||||
def default(self, o):
|
||||
|
@ -14,4 +15,3 @@ class JSONDecoder(json.JSONDecoder):
|
|||
if '$oid' not in obj:
|
||||
return obj
|
||||
return ObjectId(obj['$oid'])
|
||||
|
||||
|
|
|
@ -33,7 +33,7 @@ class Application():
|
|||
>>> t = Test()
|
||||
>>> len(app._data.routes) == 1
|
||||
True
|
||||
>>> path, methods, function = app._data.routes[0]
|
||||
>>> path, methods, function, kwargs = app._data.routes[0]
|
||||
>>> function(t, 1, 2, 3, hello="world")
|
||||
(1, 2, 3) {'hello': 'world'}
|
||||
42
|
||||
|
@ -50,13 +50,13 @@ class Application():
|
|||
self._data.filters.append(f)
|
||||
return f
|
||||
|
||||
def route(self, path, methods=None):
|
||||
def route(self, path, methods=None, **kwargs):
|
||||
"""
|
||||
Wrap a function/method as a route.
|
||||
"""
|
||||
methods = ["GET"] if methods is None else methods
|
||||
def decorator(f):
|
||||
self._data.routes.append((path, methods, f))
|
||||
self._data.routes.append((path, methods, f, kwargs))
|
||||
return f
|
||||
return decorator
|
||||
|
||||
|
@ -106,13 +106,14 @@ class Manager():
|
|||
version = "1.0.0"
|
||||
key_path = "./keys"
|
||||
|
||||
def __init__(self, context=None, defaults=None, database=None):
|
||||
def __init__(self, context=None, defaults=None, database=None, port=5000):
|
||||
from ..classes import ActivityPubBase
|
||||
self.callback = lambda box, activity_id: None
|
||||
self.context = context
|
||||
self.defaults = defaults or self.make_defaults()
|
||||
self.defaults["$UUID"] = lambda: str(uuid.uuid4())
|
||||
self.database = database
|
||||
self.port = port
|
||||
self.config = {}
|
||||
self.CSS = ""
|
||||
self._static_folder = os.path.abspath("./static")
|
||||
|
@ -131,7 +132,7 @@ class Manager():
|
|||
print("This manager has these filters:")
|
||||
print(" %s" % [f.__name__ for f in app.get_filters()])
|
||||
print("This manager has these routes:")
|
||||
for (route, methods, f) in app.get_routes():
|
||||
for (route, methods, f, kwargs) in app.get_routes():
|
||||
print(" %s %s mapped to %s" % (route, methods, f.__name__))
|
||||
|
||||
def render_template(self, template_name, **kwargs):
|
||||
|
@ -144,7 +145,11 @@ class Manager():
|
|||
pass
|
||||
|
||||
def url_for(self, name):
|
||||
pass
|
||||
## admin_notifications
|
||||
for (route, methods, f, kwargs) in app.get_routes():
|
||||
if f.__name__ == name:
|
||||
return route
|
||||
return None
|
||||
|
||||
def error(self, error_number):
|
||||
pass
|
||||
|
|
|
@ -48,14 +48,14 @@ class FlaskManager(Manager):
|
|||
self.app.config.update(self.config)
|
||||
self.csrf = CSRFProtect(self.app)
|
||||
## Add routes:
|
||||
for path, methods, f in app._data.routes:
|
||||
for path, methods, f, kwargs in app._data.routes:
|
||||
## Add the route:
|
||||
self.app.route(path, methods=methods)(wrap_function(self, f))
|
||||
self.app.route(path, methods=methods, **kwargs)(wrap_function(self, f))
|
||||
## Add filters:
|
||||
for f in app._data.filters:
|
||||
## Add the template filter function:
|
||||
self.app.template_filter()(wrap_function(self, f))
|
||||
self.app.run(debug=1)
|
||||
self.app.run(debug=1, port=self.port)
|
||||
|
||||
def load_secret_key(self, name):
|
||||
key = self._load_secret_key(name)
|
||||
|
|
|
@ -8,20 +8,27 @@ import jinja2
|
|||
import re
|
||||
|
||||
from .base import Manager, wrap_function, app
|
||||
from ..classes import ActivityPubBase
|
||||
|
||||
def make_handler(f, manager, methods):
|
||||
def make_handler(f, manager, methods, route, kws):
|
||||
"""
|
||||
Make a Tornado Handler
|
||||
"""
|
||||
## TODO: handle GET, POST methods
|
||||
## TODO: format args via route types, <int:page>
|
||||
## TODO: handle flask-style keywords in kws:
|
||||
# defaults, strict_slashes, endpoint
|
||||
if "endpoint" in kws:
|
||||
f.__name__ = kws["endpoint"]
|
||||
class Handler(RequestHandler):
|
||||
"""
|
||||
A handler that replicates some of the methods in Manager
|
||||
so that developers don't need to know.
|
||||
"""
|
||||
def get(self, *args, **kwargs):
|
||||
self.database = manager.database
|
||||
## TODO: add these in a more dynamic manner?
|
||||
self.Actor = manager.Actor
|
||||
self.Person = manager.Person
|
||||
self.Activity = manager.Activity
|
||||
self.Note = manager.Note
|
||||
for name in ActivityPubBase.CLASSES:
|
||||
setattr(self, name, getattr(manager, name))
|
||||
return f(self, *args, **kwargs)
|
||||
|
||||
def render_template(self, name, **kwargs):
|
||||
|
@ -34,6 +41,12 @@ def make_handler(f, manager, methods):
|
|||
self.set_status(error_number)
|
||||
self.write(manager.render_template("%s.html" % error_number))
|
||||
|
||||
def url_for(self, name):
|
||||
return manager.url_for(name)
|
||||
|
||||
# Get for free:
|
||||
# * get_argument
|
||||
|
||||
return Handler
|
||||
|
||||
class TornadoManager(Manager):
|
||||
|
@ -64,9 +77,6 @@ class TornadoManager(Manager):
|
|||
#def login_required():
|
||||
# tornado.web.authenticated
|
||||
|
||||
def url_for(self, name):
|
||||
return url_for(name)
|
||||
|
||||
@property
|
||||
def request(self):
|
||||
return request
|
||||
|
@ -74,10 +84,9 @@ class TornadoManager(Manager):
|
|||
def run(self):
|
||||
self.config["CSS"] = self.CSS
|
||||
routes = []
|
||||
for route, methods, f in app._data.routes:
|
||||
# Replace "<parameter>" with Tornado re matching string:
|
||||
route = re.sub("\<[^\>]*\>", r"([^/]*)", route)
|
||||
routes.append((route, make_handler(f, self, methods)))
|
||||
for route, methods, f, kwargs in app._data.routes:
|
||||
re_route = re.sub("\<[^\>]*\>", r"([^/]*)", route)
|
||||
routes.append((re_route, make_handler(f, self, methods, route, kwargs)))
|
||||
self.app = Application(routes)
|
||||
self.app.listen(5000)
|
||||
self.app.listen(self.port)
|
||||
tornado.ioloop.IOLoop.current().start()
|
||||
|
|
Ładowanie…
Reference in New Issue