summaryrefslogtreecommitdiff
path: root/src/pydrilla/pydrilla.py
diff options
context:
space:
mode:
authorWojtek Kosior <koszko@koszko.org>2021-11-16 18:34:46 +0100
committerWojtek Kosior <koszko@koszko.org>2021-11-16 18:34:46 +0100
commit9bbc08a2c156be4e5c960e470a85b3d98570d94b (patch)
tree4762ab0c061e191c648949b455b83262118d4e1f /src/pydrilla/pydrilla.py
parent5bb61d9aa41bb698a4339d281a70f0a617ee139e (diff)
downloadhaketilo-hydrilla-9bbc08a2c156be4e5c960e470a85b3d98570d94b.tar.gz
haketilo-hydrilla-9bbc08a2c156be4e5c960e470a85b3d98570d94b.zip
enable fetching of all mapping/resource versions
Diffstat (limited to 'src/pydrilla/pydrilla.py')
-rw-r--r--src/pydrilla/pydrilla.py57
1 files changed, 41 insertions, 16 deletions
diff --git a/src/pydrilla/pydrilla.py b/src/pydrilla/pydrilla.py
index 2c4ad0d..2e91b89 100644
--- a/src/pydrilla/pydrilla.py
+++ b/src/pydrilla/pydrilla.py
@@ -494,6 +494,10 @@ class Content:
ver=version_string(mapping['version'])
)
+ def item_dict(self, type):
+ '''Obtain self.resources or self.mappings, depending on type.'''
+ return self.resources if type == 'resource' else self.mappings
+
def find_item(self, type, identifier, ver=None):
'''
Find and return definition of the newest version of resource/mapping
@@ -502,8 +506,7 @@ class Content:
If ver is specified, instead find and return definition of that version
of the item (or None is absent).
'''
- dict = self.resources if type == 'resource' else self.mappings
- versioned_item = dict.get(identifier)
+ versioned_item = self.item_dict(type).get(identifier)
if not versioned_item:
return None
@@ -511,6 +514,20 @@ class Content:
return versioned_item.by_version.get(ver)
+ def get_item_all_versions(self, type, identifier):
+ '''
+ Return a list of all definitions of given resource or mapping, ordered
+ by version.
+
+ If no item of given type with such identifier exists, return [].
+ '''
+ versioned_item = self.item_dict(type).get(identifier)
+ if not versioned_item:
+ return []
+
+ return [versioned_item.by_version[version_string(ver)]
+ for ver in versioned_item.known_versions]
+
def query(self, url, max=0):
'''
Return return registered patterns and mappings (available as
@@ -604,6 +621,9 @@ def escaping_gettext(text_key):
return str(escape(_(text_key)))
+def content():
+ return current_app._pydrilla_content
+
class MyEnvironment(Environment):
'''
A wrapper class around jinja2.Environment that causes GNU gettext function
@@ -639,20 +659,25 @@ indexpage = j2env.get_template('index.html')
def index():
return indexpage.render()
-for item_type in ['mapping', 'resource']:
- def item(identifier, item_type=item_type):
- ver = request.args.get('ver')
- if ver is not None:
- try:
- ver = normalize_version(parse_version(ver))
- except:
- abort(400)
+def get_item(identifier, item_type):
+ ver = request.args.get('ver')
+ if ver == 'all':
+ definitions = content().get_item_all_versions(item_type, identifier)
+ return json.dumps(definitions)
+ if ver is not None:
+ try:
+ ver = normalize_version(parse_version(ver))
+ except:
+ abort(400)
+
+ definition = content().find_item(item_type, identifier, ver)
+ if definition is None:
+ abort(404)
- definition = current_app._pydrilla_content\
- .find_item(item_type, identifier, ver)
- if definition is None:
- abort(404)
+ return json.dumps(definition)
- return json.dumps(definition)
+for item_type in ['mapping', 'resource']:
+ def _get_item(identifier, item_type=item_type):
+ return get_item(identifier, item_type)
- bp.add_url_rule(f'/{item_type}s/<string:identifier>', item_type, item)
+ bp.add_url_rule(f'/{item_type}s/<string:identifier>', item_type, _get_item)