prepare auth for internal stats

This commit is contained in:
Cayo Puigdefabregas 2021-02-22 21:18:25 +01:00
parent 7552839f9c
commit 2a38070ae0

View file

@ -5,6 +5,7 @@ import uuid
from collections import OrderedDict from collections import OrderedDict
from io import StringIO from io import StringIO
from typing import Callable, Iterable, Tuple from typing import Callable, Iterable, Tuple
from decouple import config
import boltons import boltons
import flask import flask
@ -12,6 +13,7 @@ import flask_weasyprint
import teal.marshmallow import teal.marshmallow
from boltons import urlutils from boltons import urlutils
from flask import make_response, g, request from flask import make_response, g, request
from flask import current_app as app
from flask.json import jsonify from flask.json import jsonify
from teal.cache import cache from teal.cache import cache
from teal.resource import Resource, View from teal.resource import Resource, View
@ -253,28 +255,28 @@ class StampsView(View):
return flask.render_template('documents/stamp.html', rq_url=url_path) return flask.render_template('documents/stamp.html', rq_url=url_path)
class InternalStatsView(View): class InternalStatsView(DeviceView):
@cache(datetime.timedelta(minutes=1))
def get(self): def find(self, args: dict):
return self.get_datas() if not g.user.email == app.config['EMAIL_ADMIN']:
return jsonify('')
def get_datas(self): query = evs.Action.query.filter(
actions = evs.Action.query.filter(
evs.Action.type.in_(('Snapshot', 'Live', 'Allocate', 'Deallocate'))) evs.Action.type.in_(('Snapshot', 'Live', 'Allocate', 'Deallocate')))
return self.generate_post_csv(query)
def generate_post_csv(self, query):
d = {} d = {}
for ac in actions: for ac in query:
create = '{}-{}'.format(ac.created.year, ac.created.month) create = '{}-{}'.format(ac.created.year, ac.created.month)
user = ac.author.email user = ac.author.email
if not user in d: if not user in d:
d[user] = {} d[user] = {}
if not create in d[user]: if not create in d[user]:
d[user][create] = [] d[user][create] = []
d[user][create].append(ac) d[user][create].append(ac)
data = StringIO() data = StringIO()
cw = csv.writer(data, delimiter=';', lineterminator="\n", quotechar='"') cw = csv.writer(data, delimiter=';', lineterminator="\n", quotechar='"')
cw.writerow(InternalStatsRow('', "2000-1", []).keys()) cw.writerow(InternalStatsRow('', "2000-1", []).keys())
@ -345,11 +347,14 @@ class DocumentDef(Resource):
stamps_view = StampsView.as_view('StampsView', definition=self, auth=app.auth) stamps_view = StampsView.as_view('StampsView', definition=self, auth=app.auth)
self.add_url_rule('/stamps/', defaults={}, view_func=stamps_view, methods=get) self.add_url_rule('/stamps/', defaults={}, view_func=stamps_view, methods=get)
stamps_view = InternalStatsView.as_view('InternalStatsView', definition=self, auth=app.auth) internalstats_view = InternalStatsView.as_view(
self.add_url_rule('/internalstats/', defaults={}, view_func=stamps_view, methods=get) 'InternalStatsView', definition=self, auth=app.auth)
internalstats_view = app.auth.requires_auth(internalstats_view)
self.add_url_rule('/internalstats/', defaults=d, view_func=internalstats_view,
methods=get)
actions_view = ActionsDocumentView.as_view('ActionsDocumentView', actions_view = ActionsDocumentView.as_view('ActionsDocumentView',
definition=self, definition=self,
auth=app.auth) auth=app.auth)
actions_view = app.auth.requires_auth(actions_view) actions_view = app.auth.requires_auth(actions_view)
self.add_url_rule('/actions/', defaults=d, view_func=actions_view, methods=get) self.add_url_rule('/actions/', defaults=d, view_func=actions_view, methods=get)