From a74e242e208ff6da4ac685227a0d510d33673993 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 22 Jul 2022 15:23:49 +0200 Subject: [PATCH] add report command cli --- ereuse_devicehub/commands/__init__.py | 0 ereuse_devicehub/commands/reports.py | 51 +++++++++++++++++++ ereuse_devicehub/devicehub.py | 3 ++ .../resources/documents/documents.py | 40 ++++++++------- 4 files changed, 76 insertions(+), 18 deletions(-) create mode 100644 ereuse_devicehub/commands/__init__.py create mode 100644 ereuse_devicehub/commands/reports.py diff --git a/ereuse_devicehub/commands/__init__.py b/ereuse_devicehub/commands/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/ereuse_devicehub/commands/reports.py b/ereuse_devicehub/commands/reports.py new file mode 100644 index 00000000..cdc05211 --- /dev/null +++ b/ereuse_devicehub/commands/reports.py @@ -0,0 +1,51 @@ +import csv + +# import click +# import click_spinner +# import ereuse_utils.cli +from io import StringIO + +from ereuse_devicehub.resources.action import models as evs +from ereuse_devicehub.resources.documents.device_row import InternalStatsRow + + +class Report: + + def __init__(self, app) -> None: + super().__init__() + self.app = app + self.app.cli.command('report', short_help='Creates reports devices and users.')( + self.run + ) + + def run(self): + stats = InternalStatsView() + stats.print() + + +class InternalStatsView: + def print(self): + query = evs.Action.query.filter( + evs.Action.type.in_(('Snapshot', 'Live', 'Allocate', 'Deallocate'))) + return self.generate_post_csv(query) + + def generate_post_csv(self, query): + d = {} + for ac in query: + create = '{}-{}'.format(ac.created.year, ac.created.month) + user = ac.author.email + + if user not in d: + d[user] = {} + if create not in d[user]: + d[user][create] = [] + d[user][create].append(ac) + + data = StringIO() + cw = csv.writer(data, delimiter=';', lineterminator="\n", quotechar='"') + cw.writerow(InternalStatsRow('', "2000-1", []).keys()) + for user, createds in d.items(): + for create, actions in createds.items(): + cw.writerow(InternalStatsRow(user, create, actions).values()) + + return print(data.getvalue()) diff --git a/ereuse_devicehub/devicehub.py b/ereuse_devicehub/devicehub.py index 520ca132..182c064e 100644 --- a/ereuse_devicehub/devicehub.py +++ b/ereuse_devicehub/devicehub.py @@ -15,6 +15,7 @@ from teal.teal import Teal from ereuse_devicehub.auth import Auth from ereuse_devicehub.client import Client, UserClient +from ereuse_devicehub.commands.reports import Report from ereuse_devicehub.config import DevicehubConfig from ereuse_devicehub.db import db from ereuse_devicehub.dummy.dummy import Dummy @@ -27,6 +28,7 @@ from ereuse_devicehub.templating import Environment class Devicehub(Teal): test_client_class = Client Dummy = Dummy + Report = Report jinja_environment = Environment def __init__( @@ -67,6 +69,7 @@ class Devicehub(Teal): self.id = inventory """The Inventory ID of this instance. In Teal is the app.schema.""" self.dummy = Dummy(self) + self.report = Report(self) @self.cli.group( short_help='Inventory management.', diff --git a/ereuse_devicehub/resources/documents/documents.py b/ereuse_devicehub/resources/documents/documents.py index 1a1584e8..a6f4f95f 100644 --- a/ereuse_devicehub/resources/documents/documents.py +++ b/ereuse_devicehub/resources/documents/documents.py @@ -1,9 +1,9 @@ import csv -import json -import enum -import uuid -import time import datetime +import enum +import json +import time +import uuid from collections import OrderedDict from io import StringIO from typing import Callable, Iterable, Tuple @@ -13,27 +13,31 @@ import flask import flask_weasyprint import teal.marshmallow from boltons import urlutils -from flask import make_response, g, request from flask import current_app as app +from flask import g, make_response, request from flask.json import jsonify from teal.cache import cache from teal.resource import Resource, View 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 from ereuse_devicehub.resources.action import models as evs -from ereuse_devicehub.resources.device import models as devs +from ereuse_devicehub.resources.action.models import Trade from ereuse_devicehub.resources.deliverynote.models import Deliverynote +from ereuse_devicehub.resources.device import models as devs +from ereuse_devicehub.resources.device.models import Device from ereuse_devicehub.resources.device.views import DeviceView -from ereuse_devicehub.resources.documents.device_row import (DeviceRow, StockRow, ActionRow, - InternalStatsRow) +from ereuse_devicehub.resources.documents.device_row import ( + ActionRow, + DeviceRow, + InternalStatsRow, + StockRow, +) +from ereuse_devicehub.resources.enums import SessionType +from ereuse_devicehub.resources.hash_reports import ReportHash, insert_hash, verify_hash from ereuse_devicehub.resources.lot import LotView from ereuse_devicehub.resources.lot.models import Lot -from ereuse_devicehub.resources.action.models import Trade -from ereuse_devicehub.resources.device.models import Device -from ereuse_devicehub.resources.hash_reports import insert_hash, ReportHash, verify_hash +from ereuse_devicehub.resources.user.models import Session class Format(enum.Enum): @@ -469,11 +473,11 @@ class DocumentDef(Resource): stamps_view = StampsView.as_view('StampsView', definition=self, auth=app.auth) self.add_url_rule('/stamps/', defaults={}, view_func=stamps_view, methods={'GET', 'POST'}) - internalstats_view = InternalStatsView.as_view( - '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) + # internalstats_view = InternalStatsView.as_view( + # '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', definition=self,