This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
devicehub-teal/ereuse_devicehub/devicehub.py

54 lines
2 KiB
Python
Raw Normal View History

2018-04-27 17:16:43 +00:00
from typing import Type
from flask_sqlalchemy import SQLAlchemy
2018-09-29 10:24:22 +00:00
from sqlalchemy import event
2018-09-07 10:38:02 +00:00
from teal.config import Config as ConfigClass
from teal.teal import Teal
2018-04-27 17:16:43 +00:00
from ereuse_devicehub.auth import Auth
2018-04-10 15:06:39 +00:00
from ereuse_devicehub.client import Client
2018-04-27 17:16:43 +00:00
from ereuse_devicehub.db import db
2018-06-20 21:18:15 +00:00
from ereuse_devicehub.dummy.dummy import Dummy
2018-09-29 10:24:22 +00:00
from ereuse_devicehub.resources.device.search import DeviceSearch
2018-04-10 15:06:39 +00:00
class Devicehub(Teal):
test_client_class = Client
2018-06-20 21:18:15 +00:00
Dummy = Dummy
2018-04-27 17:16:43 +00:00
2018-05-17 07:59:17 +00:00
def __init__(self,
config: ConfigClass,
2018-04-27 17:16:43 +00:00
db: SQLAlchemy = db,
import_name=__name__.split('.')[0],
2018-04-27 17:16:43 +00:00
static_url_path=None,
static_folder='static',
2018-05-17 07:59:17 +00:00
static_host=None,
host_matching=False,
subdomain_matching=False,
2018-04-27 17:16:43 +00:00
template_folder='templates',
instance_path=None,
instance_relative_config=False,
root_path=None,
Auth: Type[Auth] = Auth):
2018-05-17 07:59:17 +00:00
super().__init__(config, db, import_name, static_url_path, static_folder, static_host,
host_matching, subdomain_matching, template_folder, instance_path,
instance_relative_config, root_path, Auth)
2018-06-20 21:18:15 +00:00
self.dummy = Dummy(self)
2018-09-29 10:24:22 +00:00
self.before_request(self.register_db_events_listeners)
2018-10-31 11:27:16 +00:00
self.cli.command('regenerate-search')(self.regenerate_search)
2018-09-29 10:24:22 +00:00
def register_db_events_listeners(self):
"""Registers the SQLAlchemy event listeners."""
# todo can I make it with a global Session only?
event.listen(db.session, 'before_commit', DeviceSearch.update_modified_devices)
def _init_db(self):
super()._init_db()
DeviceSearch.set_all_devices_tokens_if_empty(self.db.session)
2018-10-31 11:27:16 +00:00
def regenerate_search(self):
"""Re-creates from 0 all the search tables."""
DeviceSearch.regenerate_search_table(self.db.session)
db.session.commit()
print('Done.')