2022-06-23 16:08:56 +00:00
|
|
|
import os
|
2022-05-11 09:48:53 +00:00
|
|
|
import time
|
2022-06-24 10:39:46 +00:00
|
|
|
from pathlib import Path
|
2022-05-11 09:48:53 +00:00
|
|
|
|
|
|
|
import flask
|
|
|
|
from flask import Blueprint
|
|
|
|
from flask import current_app as app
|
|
|
|
from flask import g, make_response, request
|
|
|
|
from flask_login import login_required
|
|
|
|
|
|
|
|
from ereuse_devicehub import auth
|
|
|
|
from ereuse_devicehub.db import db
|
|
|
|
from ereuse_devicehub.resources.enums import SessionType
|
|
|
|
from ereuse_devicehub.resources.user.models import Session
|
2022-05-16 16:38:36 +00:00
|
|
|
from ereuse_devicehub.views import GenericMixin
|
2022-05-11 09:48:53 +00:00
|
|
|
|
|
|
|
workbench = Blueprint('workbench', __name__, url_prefix='/workbench')
|
|
|
|
|
|
|
|
|
2022-05-16 16:38:36 +00:00
|
|
|
class SettingsView(GenericMixin):
|
2022-05-11 09:48:53 +00:00
|
|
|
decorators = [login_required]
|
|
|
|
template_name = 'workbench/settings.html'
|
|
|
|
page_title = "Workbench Settings"
|
|
|
|
|
|
|
|
def dispatch_request(self):
|
|
|
|
self.get_context()
|
|
|
|
self.context.update(
|
|
|
|
{
|
|
|
|
'page_title': self.page_title,
|
2022-08-04 12:38:43 +00:00
|
|
|
'demo': g.user.email == app.config['EMAIL_DEMO'],
|
2022-05-11 09:48:53 +00:00
|
|
|
}
|
|
|
|
)
|
2022-08-04 12:38:43 +00:00
|
|
|
self.get_iso()
|
2022-05-11 09:48:53 +00:00
|
|
|
|
|
|
|
self.opt = request.values.get('opt')
|
2022-05-12 15:25:02 +00:00
|
|
|
if self.opt in ['register']:
|
2022-05-11 09:48:53 +00:00
|
|
|
return self.download()
|
|
|
|
|
|
|
|
return flask.render_template(self.template_name, **self.context)
|
|
|
|
|
2022-06-23 16:08:56 +00:00
|
|
|
def get_iso(self):
|
2022-06-24 10:39:46 +00:00
|
|
|
path = Path(__file__).parent.parent
|
2022-08-04 12:38:43 +00:00
|
|
|
uri = f'{path}/static/iso/'
|
|
|
|
if self.context.get('demo'):
|
|
|
|
uri = f'{path}/static/iso/demo/'
|
|
|
|
|
2022-06-23 16:08:56 +00:00
|
|
|
files = [
|
2022-08-04 12:38:43 +00:00
|
|
|
f for f in os.listdir(uri) if f[-3:].lower() == 'iso'
|
2022-06-23 16:08:56 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
self.context['iso'] = ''
|
|
|
|
self.context['iso_sha'] = ''
|
|
|
|
|
|
|
|
if files:
|
|
|
|
self.context['iso'] = files[0]
|
|
|
|
self.context['iso_sha'] = 'aaa'
|
|
|
|
|
2022-05-11 09:48:53 +00:00
|
|
|
def download(self):
|
2022-05-12 15:25:02 +00:00
|
|
|
url = "https://{}/api/inventory/".format(app.config['HOST'])
|
2022-05-11 09:48:53 +00:00
|
|
|
self.wbContext = {
|
|
|
|
'token': self.get_token(),
|
2022-05-12 15:19:22 +00:00
|
|
|
'url': url,
|
2022-05-11 09:48:53 +00:00
|
|
|
}
|
2022-05-12 15:19:22 +00:00
|
|
|
options = {"register": self.register}
|
2022-05-11 09:48:53 +00:00
|
|
|
return options[self.opt]()
|
|
|
|
|
|
|
|
def register(self):
|
|
|
|
data = flask.render_template('workbench/wbSettings.ini', **self.wbContext)
|
|
|
|
return self.response_download(data)
|
|
|
|
|
|
|
|
def response_download(self, data):
|
|
|
|
bfile = str.encode(data)
|
|
|
|
output = make_response(bfile)
|
|
|
|
output.headers['Content-Disposition'] = 'attachment; filename=settings.ini'
|
|
|
|
output.headers['Content-type'] = 'text/plain'
|
|
|
|
return output
|
|
|
|
|
|
|
|
def get_token(self):
|
|
|
|
if not g.user.sessions:
|
|
|
|
ses = Session(user=g.user)
|
|
|
|
db.session.add(ses)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
tk = ''
|
|
|
|
now = time.time()
|
|
|
|
for s in g.user.sessions:
|
|
|
|
if s.type == SessionType.Internal and (s.expired == 0 or s.expired > now):
|
|
|
|
tk = s.token
|
|
|
|
break
|
|
|
|
|
|
|
|
assert tk != ''
|
|
|
|
|
|
|
|
token = auth.Auth.encode(tk)
|
|
|
|
return token
|
|
|
|
|
|
|
|
|
|
|
|
workbench.add_url_rule('/settings/', view_func=SettingsView.as_view('settings'))
|