@@ 40-74 (lines=35) @@ | ||
37 | return gw_get(self._contexts, name, plugin) |
|
38 | ||
39 | ||
40 | class Context: |
|
41 | """ |
|
42 | Contexts are used to collect common objects in a single place and make it easy for new web routes to reuse |
|
43 | these objects. |
|
44 | ||
45 | Common objects are: |
|
46 | ||
47 | * A template folder |
|
48 | * A static folder |
|
49 | * A url_prefix |
|
50 | * A static folder web route, which gets automatically calculated based on given context name |
|
51 | ||
52 | They are similar to flask blueprint concept. |
|
53 | """ |
|
54 | ||
55 | def __init__(self, name, template_folder, static_folder, url_prefix, description, plugin, app): |
|
56 | self.name = name |
|
57 | self.template_folder = template_folder |
|
58 | self.static_folder = static_folder |
|
59 | self.url_prefix = url_prefix |
|
60 | self.static_url_path = "/static" |
|
61 | self.description = description |
|
62 | self.plugin = plugin |
|
63 | self.app = app |
|
64 | self.log = logging.getLogger(__name__) |
|
65 | ||
66 | self.blueprint = Blueprint(name, __name__, |
|
67 | url_prefix=url_prefix, |
|
68 | subdomain=None, |
|
69 | template_folder=template_folder, |
|
70 | static_folder=static_folder, |
|
71 | static_url_path=self.static_url_path) |
|
72 | self.app.web.flask.register_blueprint(self.blueprint) |
|
73 | ||
74 | self.log.debug("Context registered: %s (%s) for plugin %s" % (self.name, self.url_prefix, self.plugin.name)) |
|
75 |
@@ 51-72 (lines=22) @@ | ||
48 | return gw_get(self._routes, name, plugin) |
|
49 | ||
50 | ||
51 | class Route: |
|
52 | """ |
|
53 | """ |
|
54 | ||
55 | def __init__(self, url, methods, endpoint, context, name, description, plugin, app): |
|
56 | self.url = url |
|
57 | self.methods = methods |
|
58 | self.endpoint = endpoint |
|
59 | self.context = context |
|
60 | self.name = name |
|
61 | self.description = description |
|
62 | self.plugin = plugin |
|
63 | self.app = app |
|
64 | self.log = logging.getLogger(__name__) |
|
65 | ||
66 | blueprint = self.context.blueprint |
|
67 | blueprint.add_url_rule(url, methods=methods, endpoint=endpoint.__name__, view_func=endpoint) |
|
68 | # We have to (re-)register our blueprint to activate the route |
|
69 | self.app.web.flask.register_blueprint(blueprint) |
|
70 | ||
71 | self.log.info("Route registered: %s for context %s (%s)" % (self.url, self.context.name, |
|
72 | self.context.url_prefix)) |
|
73 |