This commit is contained in:
Cayo Puigdefabregas 2023-06-05 16:00:10 +02:00
parent 9671333635
commit 9119143a63
2 changed files with 14 additions and 68 deletions

View File

@ -40,11 +40,19 @@ class DidView(View):
self.get_last_dpp() self.get_last_dpp()
self.get_before_dpp() self.get_before_dpp()
if 'json' in request.headers['Accept']: if self.accept_json():
return jsonify(self.get_result()) return jsonify(self.get_result())
return render_template(self.template_name, **self.context) 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): def get_ids(self, id_dpp):
self.id_dpp = None self.id_dpp = None
self.chid = id_dpp self.chid = id_dpp
@ -151,7 +159,9 @@ class DidView(View):
last_dpp = self.get_last_dpp() last_dpp = self.get_last_dpp()
url_last = '' url_last = ''
if last_dpp: 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 data['url_last'] = url_last
return result return result

View File

@ -1,13 +1,7 @@
import json
from typing import Callable, Iterable, Tuple from typing import Callable, Iterable, Tuple
import flask from flask import redirect, url_for
from boltons import urlutils
from flask import request
from flask.json import jsonify
from ereuse_devicehub.resources.device.models import Device
from ereuse_devicehub.resources.did.models import Dpp
from ereuse_devicehub.teal.resource import Resource, View from ereuse_devicehub.teal.resource import Resource, View
@ -17,66 +11,8 @@ class DidView(View):
of one csv file 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): def get(self, dpp: str):
self.dpp = dpp return redirect(url_for('did.did', id_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
class DidDef(Resource): class DidDef(Resource):