From a6088ec7b764b25df92510da85623a6befdcf233 Mon Sep 17 00:00:00 2001 From: Krateng Date: Wed, 2 Sep 2020 14:28:04 +0200 Subject: [PATCH] Merged remaining references to two uri modules --- maloja/__pkginfo__.py | 2 +- maloja/database.py | 2 +- maloja/jinjaenv/context.py | 3 +- maloja/malojauri.py | 5 +- maloja/server.py | 4 +- maloja/urihandler.py | 167 ------------------ maloja/web/jinja/artist.jinja | 7 +- maloja/web/jinja/partials/awards_artist.jinja | 2 +- maloja/web/jinja/partials/awards_track.jinja | 2 +- maloja/web/jinja/snippets/pagination.jinja | 8 +- maloja/web/jinja/track.jinja | 2 +- 11 files changed, 18 insertions(+), 186 deletions(-) delete mode 100644 maloja/urihandler.py diff --git a/maloja/__pkginfo__.py b/maloja/__pkginfo__.py index 56c333c..f443d02 100644 --- a/maloja/__pkginfo__.py +++ b/maloja/__pkginfo__.py @@ -5,7 +5,7 @@ author = { "email":"maloja@krateng.dev", "github": "krateng" } -version = 2,8,4 +version = 2,8,5 versionstr = ".".join(str(n) for n in version) links = { "pypi":"malojaserver", diff --git a/maloja/database.py b/maloja/database.py index 6608572..f3daa39 100644 --- a/maloja/database.py +++ b/maloja/database.py @@ -5,7 +5,7 @@ from bottle import request, response, FormsDict from .cleanup import CleanerAgent, CollectorAgent from . import utilities from .malojatime import register_scrobbletime, time_stamps, ranges -from .urihandler import uri_to_internal, internal_to_uri, compose_querystring +from .malojauri import uri_to_internal, internal_to_uri, compose_querystring from . import compliant_api from .thirdparty import proxy_scrobble_all diff --git a/maloja/jinjaenv/context.py b/maloja/jinjaenv/context.py index b3629ca..9906877 100644 --- a/maloja/jinjaenv/context.py +++ b/maloja/jinjaenv/context.py @@ -1,7 +1,7 @@ from .. import database_packed from . import filters -from .. import database, database_packed, malojatime, utilities, urihandler, malojauri +from .. import database, database_packed, malojatime, utilities, malojauri from doreah import settings from doreah.regular import repeatdaily @@ -29,7 +29,6 @@ def update_jinja_environment(): "dbp":dbp, "malojatime": malojatime, "utilities": utilities, - "urihandler": urihandler, "mlj_uri": malojauri, "settings": settings.get_settings, # external diff --git a/maloja/malojauri.py b/maloja/malojauri.py index 249f416..ebf1243 100644 --- a/maloja/malojauri.py +++ b/maloja/malojauri.py @@ -63,11 +63,14 @@ def uri_to_internal(keys,forceTrack=False,forceArtist=False): def create_uri(path,*keydicts): + return path + "?" + uriencode(*keydicts) + +def uriencode(*keydicts): keyd = {} for kd in keydicts: keyd.update(kd) - return path + "?" + compose_querystring(internal_to_uri(keyd)) + return compose_querystring(internal_to_uri(keyd)) def internal_to_uri(keys): diff --git a/maloja/server.py b/maloja/server.py index ab324d3..7ead1d1 100755 --- a/maloja/server.py +++ b/maloja/server.py @@ -15,9 +15,7 @@ from . import malojatime from . import utilities from . import malojauri from .utilities import resolveImage -from .urihandler import remove_identical -from .malojauri import uri_to_internal -from . import urihandler +from .malojauri import uri_to_internal, remove_identical from . import globalconf from .jinjaenv.context import jinja_environment # doreah toolkit diff --git a/maloja/urihandler.py b/maloja/urihandler.py deleted file mode 100644 index 440b854..0000000 --- a/maloja/urihandler.py +++ /dev/null @@ -1,167 +0,0 @@ -import urllib -from bottle import FormsDict -from .malojatime import time_fix, time_str, get_range_object -import math - -# necessary because urllib.parse.urlencode doesnt handle multidicts -def compose_querystring(*dicts,exclude=[]): - - st = "" - keys = remove_identical(*dicts) - for k in keys: - if k in exclude: continue - values = keys.getall(k) - st += "&".join([urllib.parse.urlencode({k:v},safe="/") for v in values]) - st += "&" - return st - - -# takes any number of multidicts and normal dicts and creates a formsdict with duplicate values removed -def remove_identical(*dicts): - #combine multiple dicts - keys = FormsDict() - for d in dicts: - for k in d: - try: #multidicts - for v in d.getall(k): - keys.append(k,v) - except: #normaldicts - v = d.get(k) - keys.append(k,v) - - new = FormsDict() - for k in keys: - #values = set(keys.getall(k)) - values = keys.getall(k) # NO IDENTICAL REMOVAL FOR NOW - for v in values: - new.append(k,v) - - return new - - -# this also sets defaults! -def uri_to_internal(keys,forceTrack=False,forceArtist=False): - - # output: - # 1 keys that define the filtered object like artist or track - # 2 keys that define time limits of the whole thing - # 3 keys that define interal time ranges - # 4 keys that define amount limits - - # 1 - if "title" in keys and not forceArtist: - resultkeys1 = {"track":{"artists":keys.getall("artist"),"title":keys.get("title")}} - elif "artist" in keys and not forceTrack: - resultkeys1 = {"artist":keys.get("artist")} - if "associated" in keys: resultkeys1["associated"] = True - else: - resultkeys1 = {} - - # 2 - resultkeys2 = {} -# if "since" in keys: resultkeys2["since"] = time_fix(keys.get("since")) -# elif "from" in keys: resultkeys2["since"] = time_fix(keys.get("from")) -# elif "start" in keys: resultkeys2["since"] = time_fix(keys.get("start")) -# # -# if "to" in keys: resultkeys2["to"] = time_fix(keys.get("to")) -# elif "until" in keys: resultkeys2["to"] = time_fix(keys.get("until")) -# elif "end" in keys: resultkeys2["to"] = time_fix(keys.get("end")) -# # -# if "since" in resultkeys2 and "to" in resultkeys2 and resultkeys2["since"] == resultkeys2["to"]: -# resultkeys2["within"] = resultkeys2["since"] -# del resultkeys2["since"] -# del resultkeys2["to"] -# # -# if "in" in keys: resultkeys2["within"] = time_fix(keys.get("in")) -# elif "within" in keys: resultkeys2["within"] = time_fix(keys.get("within")) -# elif "during" in keys: resultkeys2["within"] = time_fix(keys.get("during")) -# if "within" in resultkeys2: -# if "since" in resultkeys2: -# del resultkeys2["since"] -# if "to" in resultkeys2: -# del resultkeys2["to"] - since,to,within = None,None,None - if "since" in keys: since = keys.get("since") - elif "from" in keys: since = keys.get("from") - elif "start" in keys: since = keys.get("start") - if "to" in keys: to = keys.get("to") - elif "until" in keys: to = keys.get("until") - elif "end" in keys: to = keys.get("end") - if "in" in keys: within = keys.get("in") - elif "within" in keys: within = keys.get("within") - elif "during" in keys: within = keys.get("during") - resultkeys2["timerange"] = get_range_object(since=since,to=to,within=within) - - #3 - resultkeys3 = {"step":"month","stepn":1,"trail":1} - if "step" in keys: [resultkeys3["step"],resultkeys3["stepn"]] = (keys["step"].split("-") + [1])[:2] - if "stepn" in keys: resultkeys3["stepn"] = keys["stepn"] #overwrite if explicitly given - if "stepn" in resultkeys3: resultkeys3["stepn"] = int(resultkeys3["stepn"]) #in both cases, convert it here - if "trail" in keys: resultkeys3["trail"] = int(keys["trail"]) - if "cumulative" in keys: resultkeys3["trail"] = math.inf - - - - #4 - resultkeys4 = {"page":0,"perpage":100} -# if "max" in keys: resultkeys4["max_"] = int(keys["max"]) - if "max" in keys: resultkeys4["page"],resultkeys4["perpage"] = 0, int(keys["max"]) - #different max than the internal one! the user doesn't get to disable pagination - if "page" in keys: resultkeys4["page"] = int(keys["page"]) - if "perpage" in keys: resultkeys4["perpage"] = int(keys["perpage"]) - - - return resultkeys1, resultkeys2, resultkeys3, resultkeys4 - -def compose_from_dicts(*dicts): - return compose_querystring(internal_to_uri_p(*dicts)) - -def internal_to_uri_p(*dicts): - return internal_to_uri({k:d[k] for d in dicts for k in d}) - -def internal_to_uri(keys): - urikeys = FormsDict() - - #filter - if "artist" in keys: - urikeys.append("artist",keys["artist"]) - if keys.get("associated"): urikeys.append("associated","yes") - elif "track" in keys: - for a in keys["track"]["artists"]: - urikeys.append("artist",a) - urikeys.append("title",keys["track"]["title"]) - - #time - if "timerange" in keys: - keydict = keys["timerange"].urikeys() - for k in keydict: - urikeys.append(k,keydict[k]) - elif "within" in keys: - urikeys.append("in",time_str(keys["within"])) - else: - if "since" in keys and keys["since"] is not None: - urikeys.append("since",time_str(keys["since"])) - if "to" in keys and keys["to"] is not None: - urikeys.append("to",time_str(keys["to"])) - - # delimit - if "step" in keys: - urikeys.append("step",keys["step"]) - if "stepn" in keys: - urikeys.append("stepn",str(keys["stepn"])) - if "trail" in keys: - if keys["trail"] == math.inf: - urikeys.append("cumulative","yes") - else: - urikeys.append("trail",str(keys["trail"])) - - # stuff - #if "max_" in keys: - # urikeys.append("max",str(keys["max_"])) - if "page" in keys: - urikeys.append("page",str(keys["page"])) - if "perpage" in keys: - urikeys.append("perpage",str(keys["perpage"])) - - - return urikeys diff --git a/maloja/web/jinja/artist.jinja b/maloja/web/jinja/artist.jinja index cf07665..237e859 100644 --- a/maloja/web/jinja/artist.jinja +++ b/maloja/web/jinja/artist.jinja @@ -24,8 +24,7 @@ {% endif %} -{% set encodedartist = urihandler.compose_querystring(urihandler.internal_to_uri({'artist':artist})) %} -{% set encodedcredited = urihandler.compose_querystring(urihandler.internal_to_uri({'artist':credited})) %} +{% set encodedartist = mlj_uri.uriencode({'artist':artist}) %} {% block content %} @@ -57,7 +56,7 @@ Competing under {{ links.link(credited) }} (#{{ info.position }}) {% endif %} -

{{ info['scrobbles'] }} Scrobbles

+

{{ info['scrobbles'] }} Scrobbles

@@ -115,7 +114,7 @@ -

Performance

+

Performance

{% if not competes %}of {{ links.link(credited) }} {% endif %}
diff --git a/maloja/web/jinja/partials/awards_artist.jinja b/maloja/web/jinja/partials/awards_artist.jinja index cfc6c2d..b311820 100644 --- a/maloja/web/jinja/partials/awards_artist.jinja +++ b/maloja/web/jinja/partials/awards_artist.jinja @@ -31,7 +31,7 @@ {% macro topweeks(info) %} -{% set encodedartist = urihandler.compose_querystring(urihandler.internal_to_uri({'artist':info.artist})) %} +{% set encodedartist = mlj_uri.uriencode({'artist':info.artist}) %} diff --git a/maloja/web/jinja/partials/awards_track.jinja b/maloja/web/jinja/partials/awards_track.jinja index 12ca414..bdb8748 100644 --- a/maloja/web/jinja/partials/awards_track.jinja +++ b/maloja/web/jinja/partials/awards_track.jinja @@ -28,7 +28,7 @@ {% macro topweeks(info) %} -{% set encodedtrack = urihandler.compose_querystring(urihandler.internal_to_uri({'track':info.track})) %} +{% set encodedtrack = mlj_uri.uriencode({'track':info.track}) %} diff --git a/maloja/web/jinja/snippets/pagination.jinja b/maloja/web/jinja/snippets/pagination.jinja index 36e8d10..e7b3f63 100644 --- a/maloja/web/jinja/snippets/pagination.jinja +++ b/maloja/web/jinja/snippets/pagination.jinja @@ -9,7 +9,7 @@ {% if pages > 1 %} {% if page > 1 %} - + 1 | {% endif %} @@ -19,7 +19,7 @@ {% endif %} {% if page > 0 %} - + {{ page }} « {% endif %} @@ -29,7 +29,7 @@ {% if page < pages-1 %} - » + » {{ page+2 }} {% endif %} @@ -39,7 +39,7 @@ {% endif %} {% if page < pages-2 %} - | + | {{ pages }} {% endif %} diff --git a/maloja/web/jinja/track.jinja b/maloja/web/jinja/track.jinja index ae6ce79..7e915b7 100644 --- a/maloja/web/jinja/track.jinja +++ b/maloja/web/jinja/track.jinja @@ -18,7 +18,7 @@ {% set initialrange ='month' %} -{% set encodedtrack = urihandler.compose_querystring(urihandler.internal_to_uri({'track':track})) %} +{% set encodedtrack = mlj_uri.uriencode({'track':track}) %} {% block content %}