init
This commit is contained in:
commit
a4c68ca9e2
76 changed files with 2737 additions and 0 deletions
0
server/application/backend/data/__init__.py
Normal file
0
server/application/backend/data/__init__.py
Normal file
40
server/application/backend/data/dao_device.py
Normal file
40
server/application/backend/data/dao_device.py
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
from .data_models import Device
|
||||
from .data_models import DataError
|
||||
from .db import get_cursor
|
||||
from sqlite3 import IntegrityError
|
||||
|
||||
def _device_from_row(row):
|
||||
return Device(
|
||||
name = row['device_name'],
|
||||
token = row['device_token'],
|
||||
encryption_key = row['encryption_key'],
|
||||
)
|
||||
|
||||
|
||||
def get_devices():
|
||||
with get_cursor() as db_cursor:
|
||||
db_cursor.execute("SELECT * FROM device")
|
||||
rows = db_cursor.fetchall()
|
||||
|
||||
return map(_device_from_row, rows)
|
||||
|
||||
_INSERT_DEVICE_SQL = "INSERT INTO device(device_name, device_token, encryption_key)"\
|
||||
"VALUES(:device_name, :device_token, :encryption_key)"
|
||||
def insert_device(device: Device):
|
||||
params = {
|
||||
"device_name": device.name,
|
||||
"device_token": device.token,
|
||||
"encryption_key": device.encryption_key,
|
||||
}
|
||||
with get_cursor() as db_cursor:
|
||||
try:
|
||||
db_cursor.execute(_INSERT_DEVICE_SQL, params)
|
||||
except IntegrityError as e:
|
||||
return DataError.DEVICE_INSERT_ERROR
|
||||
|
||||
return db_cursor.lastrowid
|
||||
|
||||
def delete_device_by_name(name: str):
|
||||
with get_cursor() as db_cursor:
|
||||
db_cursor.execute('DELETE FROM device WHERE device_name=:name',{'name':name})
|
||||
|
||||
32
server/application/backend/data/data_models.py
Normal file
32
server/application/backend/data/data_models.py
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
from enum import Enum
|
||||
from enum import IntEnum
|
||||
|
||||
class Device:
|
||||
def __init__(self, token, encryption_key, name):
|
||||
self.token = token
|
||||
self.encryption_key = encryption_key
|
||||
self.name = name
|
||||
|
||||
def __eq__(self, other):
|
||||
if not isinstance(other, Device):
|
||||
return False
|
||||
return self.token == other.token \
|
||||
and self.encrpytion_key == other.encryption_key \
|
||||
and self.name == other.mname
|
||||
|
||||
def __str__(self):
|
||||
return 'Device(token={},encryption_key={},name={})'.format(self.token, self.encryption_key, self.name)
|
||||
|
||||
def __repr__(self):
|
||||
return self.__str__
|
||||
|
||||
|
||||
class DataError(Enum):
|
||||
DEVICE_INSERT_ERROR = -1
|
||||
|
||||
class ResponseCode(IntEnum):
|
||||
EMPTY_DEVICE_TOKEN = 401
|
||||
EMPTY_DEVICE_NAME = 402
|
||||
EMPTY_DEVICE_ENCRYPTION = 403
|
||||
DEVICE_SAVE_FAILURE = 404
|
||||
NOTIFICATION_PARAMS_MISSING = 405
|
||||
62
server/application/backend/data/db.py
Normal file
62
server/application/backend/data/db.py
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
import sqlite3
|
||||
from os import path
|
||||
from flask import current_app, g
|
||||
from contextlib import contextmanager
|
||||
|
||||
@contextmanager
|
||||
def get_cursor():
|
||||
db = get_db()
|
||||
cursor = db.cursor()
|
||||
try:
|
||||
yield (cursor)
|
||||
finally:
|
||||
cursor.close()
|
||||
db.commit()
|
||||
|
||||
default_database_name = "sqlitedb"
|
||||
|
||||
def get_db():
|
||||
current_app.config.get('DATABASE_PATH')
|
||||
if 'db' not in g:
|
||||
db_path = current_app.config.get('DATABASE_PATH')
|
||||
if (db_path is None):
|
||||
db_path = path.join(current_app.instance_path, current_app.config['DATABASE_NAME'])
|
||||
g.db = sqlite3.connect(db_path, detect_types=sqlite3.PARSE_DECLTYPES)
|
||||
g.db.row_factory = sqlite3.Row
|
||||
|
||||
return g.db
|
||||
|
||||
def close_db(e=None):
|
||||
db = g.pop('db', None)
|
||||
|
||||
if db is not None:
|
||||
db.close()
|
||||
|
||||
def init_db(db_path = None, schema_path = None):
|
||||
if db_path is None:
|
||||
db = get_db()
|
||||
else:
|
||||
db = sqlite3.connect(db_path, detect_types=sqlite3.PARSE_DECLTYPES)
|
||||
|
||||
if schema_path is None:
|
||||
with current_app.open_resource('data/schema.sql') as f:
|
||||
script = f.read().decode('UTF-8')
|
||||
else:
|
||||
with open(schema_path, "r") as f:
|
||||
script = f.read()
|
||||
|
||||
db.executescript(script)
|
||||
db.commit()
|
||||
db.close()
|
||||
|
||||
def init_app(app):
|
||||
app.teardown_appcontext(close_db)
|
||||
|
||||
if __name__ == "__main__":
|
||||
db_path = path.join('/home/flask/server/instance/', default_database_name)
|
||||
if path.exists(db_path):
|
||||
print('Database already exists at {}. Will NOT override, if necessary first delete first then restart initialization!'.format(db_path))
|
||||
exit(1)
|
||||
schema_path = path.join(path.dirname(__file__), 'schema.sql')
|
||||
init_db(db_path = db_path, schema_path = schema_path)
|
||||
print('done')
|
||||
8
server/application/backend/data/schema.sql
Normal file
8
server/application/backend/data/schema.sql
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
DROP TABLE IF EXISTS user;
|
||||
|
||||
CREATE TABLE device (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
device_token TEXT NOT NULL,
|
||||
encryption_key TEXT NOT NULL,
|
||||
device_name TEXT NOT NULL
|
||||
);
|
||||
Loading…
Add table
Add a link
Reference in a new issue