diff --git a/.github/workflows/flask.yml b/.github/workflows/flask.yml index 615efabf..d12570b8 100644 --- a/.github/workflows/flask.yml +++ b/.github/workflows/flask.yml @@ -49,7 +49,7 @@ jobs: pip install virtualenv virtualenv env source env/bin/activate - pip install flake8 pytest + pip install flake8 pytest coverage pip install -r requirements.txt - name: Prepare database @@ -67,5 +67,7 @@ jobs: - name: Run Tests run: | source env/bin/activate - pytest -m mvp --maxfail=5 tests/ + coverage run --source='ereuse_devicehub' env/bin/pytest -m mvp --maxfail=5 tests/ + coverage report --include='ereuse_devicehub/*' + coverage xml diff --git a/.gitignore b/.gitignore index 91073ab6..69f6d4be 100644 --- a/.gitignore +++ b/.gitignore @@ -113,3 +113,6 @@ ENV/ .vscode/ .DS_Store /app.py + +# Environment +.env diff --git a/README.rst b/README.rst index c163c31a..1eba57c5 100644 --- a/README.rst +++ b/README.rst @@ -37,6 +37,11 @@ Create a PostgreSQL database called *devicehub* by running - In MacOS: ``bash examples/create-db.sh devicehub dhub``, and password ``ereuse``. +Configure project using environment file (you can use provided example as quickstart): +.. code:: bash + + $ cp examples/env.example .env + Using the `dh` tool for set up with one or multiple inventories. Create the tables in the database by executing: diff --git a/ereuse_devicehub/__init__.py b/ereuse_devicehub/__init__.py index e69de29b..7574bfd1 100644 --- a/ereuse_devicehub/__init__.py +++ b/ereuse_devicehub/__init__.py @@ -0,0 +1 @@ +__version__ = "1.0a1" diff --git a/ereuse_devicehub/config.py b/ereuse_devicehub/config.py index b1db6e9e..6a176f6e 100644 --- a/ereuse_devicehub/config.py +++ b/ereuse_devicehub/config.py @@ -1,6 +1,7 @@ from distutils.version import StrictVersion from itertools import chain from typing import Set +from decouple import config from teal.auth import TokenAuth from teal.config import Config @@ -12,6 +13,7 @@ from ereuse_devicehub.resources import action, agent, deliverynote, inventory, \ from ereuse_devicehub.resources.device import definitions from ereuse_devicehub.resources.documents import documents from ereuse_devicehub.resources.enums import PriceSoftware +from ereuse_devicehub.resources.versions import versions class DevicehubConfig(Config): @@ -24,10 +26,20 @@ class DevicehubConfig(Config): import_resource(deliverynote), import_resource(proof), import_resource(documents), - import_resource(inventory)), + import_resource(inventory), + import_resource(versions)), ) PASSWORD_SCHEMES = {'pbkdf2_sha256'} # type: Set[str] - SQLALCHEMY_DATABASE_URI = 'postgresql://dhub:ereuse@localhost/devicehub' # type: str + DB_USER = config('DB_USER', 'dhub') + DB_PASSWORD = config('DB_PASSWORD', 'ereuse') + DB_HOST = config('DB_HOST', 'localhost') + DB_DATABASE = config('DB_DATABASE', 'devicehub') + SQLALCHEMY_DATABASE_URI = 'postgresql://{user}:{pw}@{host}/{db}'.format( + user=DB_USER, + pw=DB_PASSWORD, + host=DB_HOST, + db=DB_DATABASE, + ) # type: str MIN_WORKBENCH = StrictVersion('11.0a1') # type: StrictVersion """The minimum version of ereuse.org workbench that this devicehub accepts. we recommend not changing this value. diff --git a/ereuse_devicehub/resources/versions/__init__.py b/ereuse_devicehub/resources/versions/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/ereuse_devicehub/resources/versions/versions.py b/ereuse_devicehub/resources/versions/versions.py new file mode 100644 index 00000000..f2b8d393 --- /dev/null +++ b/ereuse_devicehub/resources/versions/versions.py @@ -0,0 +1,64 @@ +import flask +import json +import requests +import teal.marshmallow + +from typing import Callable, Iterable, Tuple +from urllib.parse import urlparse +from flask import make_response, g +from teal.resource import Resource, View + +from ereuse_devicehub.resources.inventory.model import Inventory +from ereuse_devicehub import __version__ + + +def get_tag_version(app): + """Get version of microservice ereuse-tag.""" + + path = "/versions/version/" + url = urlparse(Inventory.current.tag_provider.to_text())._replace(path=path) + try: + res = requests.get(url.geturl()) + except requests.exceptions.ConnectionError: + app.logger.error("The microservice Tags is down!!") + return {} + + if res.status_code == 200: + return json.loads(res.content) + else: + return {} + +class VersionView(View): + def get(self, *args, **kwargs): + """Get version of DeviceHub and ereuse-tag.""" + + tag_version = get_tag_version(self.resource_def.app) + versions = {'devicehub': __version__, "ereuse_tag": "0.0.0"} + versions.update(tag_version) + return json.dumps(versions) + + +class VersionDef(Resource): + __type__ = 'Version' + SCHEMA = None + VIEW = None # We do not want to create default / documents endpoint + AUTH = False + + def __init__(self, app, + import_name=__name__, + static_folder=None, + static_url_path=None, + template_folder=None, + url_prefix=None, + subdomain=None, + url_defaults=None, + root_path=None, + cli_commands: Iterable[Tuple[Callable, str or None]] = tuple()): + super().__init__(app, import_name, static_folder, static_url_path, template_folder, + url_prefix, subdomain, url_defaults, root_path, cli_commands) + + d = {'devicehub': __version__, "ereuse_tag": "0.0.0"} + get = {'GET'} + + version_view = VersionView.as_view('VersionView', definition=self) + self.add_url_rule('/', defaults=d, view_func=version_view, methods=get) diff --git a/examples/env.example b/examples/env.example new file mode 100644 index 00000000..e0c83b5b --- /dev/null +++ b/examples/env.example @@ -0,0 +1,4 @@ +DB_USER='dhub' +DB_PASSWORD='ereuse' +DB_HOST='localhost' +DB_DATABASE='devicehub' diff --git a/requirements.txt b/requirements.txt index 046e8df9..299a9050 100644 --- a/requirements.txt +++ b/requirements.txt @@ -34,3 +34,5 @@ weasyprint==44 psycopg2-binary==2.8.3 sortedcontainers==2.1.0 tqdm==4.32.2 +python-decouple==3.3 +python-dotenv==0.14.0 diff --git a/setup.py b/setup.py index 2ef5907b..6a0db9b9 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,7 @@ from pathlib import Path - from setuptools import find_packages, setup +from ereuse_devicehub import __version__ + test_requires = [ 'pytest', @@ -9,10 +10,10 @@ test_requires = [ setup( name='ereuse-devicehub', - version='1.0a1', + version=__version__, url='https://github.com/ereuse/devicehub-teal', project_urls={ - 'Documentation': 'http://devicheub.ereuse.org', + 'Documentation': 'http://devicehub.ereuse.org', 'Code': 'http://github.com/ereuse/devicehub-teal', 'Issue tracker': 'https://tree.taiga.io/project/ereuseorg-devicehub/issues?q=rules' }, diff --git a/tests/test_basic.py b/tests/test_basic.py index dc0ce248..fe3dd1da 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -99,7 +99,8 @@ def test_api_docs(client: Client): '/video-scalers/{id}/merge/', '/videoconferences/{id}/merge/', '/videos/{id}/merge/', - '/wireless-access-points/{id}/merge/' + '/wireless-access-points/{id}/merge/', + '/versions/' } assert docs['info'] == {'title': 'Devicehub', 'version': '0.2'} assert docs['components']['securitySchemes']['bearerAuth'] == { diff --git a/tests/test_endpoints.py b/tests/test_endpoints.py index 6b178a42..571c62be 100644 --- a/tests/test_endpoints.py +++ b/tests/test_endpoints.py @@ -1,8 +1,15 @@ import pytest +from ereuse_devicehub import __version__ +from ereuse_devicehub.client import Client +@pytest.mark.mvp +def test_get_version(client: Client): + """Checks GETting versions of services.""" + content, res = client.get("/versions/", None) - - + version = {'devicehub': __version__, 'ereuse_tag': '0.0.0'} + assert res.status_code == 200 + assert content == version