diff --git a/ereuse_devicehub/did/views.py b/ereuse_devicehub/did/views.py index 53311275..8f3ab4cb 100644 --- a/ereuse_devicehub/did/views.py +++ b/ereuse_devicehub/did/views.py @@ -40,11 +40,19 @@ class DidView(View): self.get_last_dpp() self.get_before_dpp() - if 'json' in request.headers['Accept']: + if self.accept_json(): return jsonify(self.get_result()) return render_template(self.template_name, **self.context) + def accept_json(self): + if 'json' in request.headers.get('Accept', []): + return True + if "application/json" in request.headers.get("Content-Type", []): + return True + + return False + def get_ids(self, id_dpp): self.id_dpp = None self.chid = id_dpp @@ -151,7 +159,9 @@ class DidView(View): last_dpp = self.get_last_dpp() url_last = '' if last_dpp: - url_last = 'http://did.ereuse.org/{did}'.format(did=last_dpp) + url_last = 'https://{host}/{did}'.format( + did=last_dpp, host=app.config.get('HOST') + ) data['url_last'] = url_last return result diff --git a/ereuse_devicehub/resources/did/did.py b/ereuse_devicehub/resources/did/did.py index 28dc548d..ccfe5b32 100644 --- a/ereuse_devicehub/resources/did/did.py +++ b/ereuse_devicehub/resources/did/did.py @@ -1,13 +1,7 @@ -import json from typing import Callable, Iterable, Tuple -import flask -from boltons import urlutils -from flask import request -from flask.json import jsonify +from flask import redirect, url_for -from ereuse_devicehub.resources.device.models import Device -from ereuse_devicehub.resources.did.models import Dpp from ereuse_devicehub.teal.resource import Resource, View @@ -17,66 +11,8 @@ class DidView(View): of one csv file """ - def get_url_path(self): - url = urlutils.URL(request.url) - url.normalize() - url.path_parts = url.path_parts[:-2] + ['check', ''] - return url.to_text() - def get(self, dpp: str): - self.dpp = dpp - template = 'dpp.html' - if len(dpp.split(":")) == 2: - result = Dpp.query.filter_by(key=dpp).one() - else: - result = Device.query.filter_by(chid=dpp).one() - template = 'chid.html' - - if 'json' not in request.headers['Accept']: - result = self.get_result(result, template) - return flask.render_template( - template, - rq_url=self.get_url_path(), - result={"dpp": dpp, "result": result}, - ) - - return jsonify(self.get_result(result, template)) - - def get_result(self, dpp, template): - data = { - 'hardware': {}, - 'dpp': self.dpp, - } - result = {'data': data} - - if template == 'dpp.html': - data['hardware'] = json.loads(dpp.snapshot.json_hw) - last_dpp = self.get_last_dpp(dpp) - url_last = '' - if last_dpp: - url_last = 'http://did.ereuse.org/{did}'.format(did=last_dpp) - data['url_last'] = url_last - return result - - # if dpp is not a dpp then is a device - device = dpp - dpps = [] - for d in device.dpps: - rr = {'dpp': d.key, 'hardware': json.loads(d.snapshot.json_hw)} - dpps.append(rr) - return {'data': dpps} - - def get_last_dpp(self, dpp): - dpps = [ - act.dpp[0] for act in dpp.device.actions if act.t == 'Snapshot' and act.dpp - ] - last_dpp = '' - for d in dpps: - if d.key == dpp.key: - return last_dpp - last_dpp = d.key - - return last_dpp + return redirect(url_for('did.did', id_dpp=dpp)) class DidDef(Resource):