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..8d020e7d --- /dev/null +++ b/ereuse_devicehub/commands/reports.py @@ -0,0 +1,52 @@ +import csv + +# 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 + +# import click + + +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/commands/users.py b/ereuse_devicehub/commands/users.py new file mode 100644 index 00000000..0537944f --- /dev/null +++ b/ereuse_devicehub/commands/users.py @@ -0,0 +1,20 @@ +import click + +from ereuse_devicehub import auth +from ereuse_devicehub.resources.user.models import User + + +class GetToken: + + def __init__(self, app) -> None: + super().__init__() + self.app = app + self.app.cli.command('get_token', short_help='show the user token.')( + self.run + ) + + @click.argument('email') + def run(self, email): + user = User.query.filter_by(email=email, active=True, phantom=False).one_or_none() + if user: + print(auth.Auth.encode(user.token)) diff --git a/ereuse_devicehub/devicehub.py b/ereuse_devicehub/devicehub.py index 520ca132..5fec2cbe 100644 --- a/ereuse_devicehub/devicehub.py +++ b/ereuse_devicehub/devicehub.py @@ -15,6 +15,8 @@ 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.commands.users import GetToken from ereuse_devicehub.config import DevicehubConfig from ereuse_devicehub.db import db from ereuse_devicehub.dummy.dummy import Dummy @@ -27,6 +29,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 +70,8 @@ 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.get_token = GetToken(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, diff --git a/ereuse_devicehub/static/js/print.pdf.js b/ereuse_devicehub/static/js/print.pdf.js index 859ea44a..33ead3fe 100644 --- a/ereuse_devicehub/static/js/print.pdf.js +++ b/ereuse_devicehub/static/js/print.pdf.js @@ -258,9 +258,9 @@ function printpdf() { if ($("#qrCheck").prop('checked')) { var h = hspace + border - img_side/2; var w = border*2 + img_side; - pdf.text(tag, w, h); + pdf.text(String(tag), w, h); } else { - pdf.text(tag, border, hspace); + pdf.text(String(tag), border, hspace); } hspace += line; }; diff --git a/tests/test_basic.py b/tests/test_basic.py index 4e81a709..6b890ea3 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -41,7 +41,6 @@ def test_api_docs(client: Client): '/documents/check/', '/documents/devices/', '/documents/erasures/', - '/documents/internalstats/', '/documents/lots/', '/inventory/search/', '/documents/stamps/', diff --git a/tests/test_documents.py b/tests/test_documents.py index 535d39ee..20629653 100644 --- a/tests/test_documents.py +++ b/tests/test_documents.py @@ -761,7 +761,6 @@ def test_verify_stamp_erasure_certificate(user: UserClient, client: Client): assert "alert alert-info" in response -@pytest.mark.mvp def test_get_document_internal_stats(user: UserClient, user2: UserClient): """Tests for get the internal stats."""