-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Code & Add Data Modifying #5
- Add data modifying - Add types - Separate to folders and files - Add very simple admin auth
- Loading branch information
1 parent
246832b
commit a658034
Showing
14 changed files
with
221 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
DATABASE_URL="mongodb://<dbuser>:<dbpassword>@<ip>:<port>/ir-commands" | ||
DATABASE_URL="mongodb://<dbuser>:<dbpassword>@<ip>:<port>/ir-commands" | ||
ADMIN_API_KEY="some admin key" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
{ | ||
"python.linting.pylintEnabled": true, | ||
"python.linting.enabled": true | ||
"python.linting.enabled": true, | ||
"cSpell.words": [ | ||
"jsonify" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from data.models import RfDevice | ||
|
||
|
||
def is_device_exists(brand: str, model: str) -> bool: | ||
try: | ||
RfDevice.objects.get(brand=brand, model=model) | ||
return True | ||
except: | ||
return False | ||
|
||
|
||
def get_devices() -> list: | ||
devices = [] | ||
for device in RfDevice.objects: | ||
record = { | ||
'brand': device.brand, | ||
'model': device.model, | ||
'category': device.category, | ||
} | ||
devices.append(record) | ||
return devices | ||
|
||
|
||
def create_device(device) -> None: | ||
brand = device['brand'] | ||
model = device['model'] | ||
if is_device_exists(brand=brand, model=model): | ||
raise Exception("Sorry, the device already exists") | ||
|
||
newDevice = RfDevice(brand=brand, model=model, | ||
category=device['category'], commands=device['commands']) | ||
newDevice.save() | ||
|
||
|
||
def edit_device(brand: str, model: str, device: dict) -> None: | ||
if not is_device_exists(brand=brand, model=model): | ||
raise Exception("Sorry, the device is not exists") | ||
device = RfDevice.objects.get(brand=brand, model=model) | ||
device['brand'] = newName['brand'] | ||
device['model'] = newName['model'] | ||
device['category'] = newName['category'] | ||
device.save() | ||
|
||
|
||
def delete_device(brand: str, model: str) -> None: | ||
device = RfDevice.objects.get(brand=brand, model=model) | ||
device.delete() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from mongoengine import * | ||
import os | ||
|
||
DATABASE_URL = os.getenv("DATABASE_URL") | ||
|
||
# Connect mongoengine driver to the database | ||
connect(host=DATABASE_URL) | ||
|
||
|
||
class RfDevice(Document): | ||
"""Devices document objects model.""" | ||
meta = { | ||
'collection': 'commands' | ||
} | ||
brand = StringField(required=True) | ||
model = StringField(required=True) | ||
category = StringField(required=True) | ||
commands = DynamicField(required=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
from data.models import RfDevice | ||
|
||
|
||
def get_device_commands(brand: str, model: str) -> dict: | ||
device = RfDevice.objects.get(brand=brand, model=model) | ||
return device.commands | ||
|
||
|
||
def set_device_commands(brand: str, model: str, data: dict) -> None: | ||
device = RfDevice.objects.get(brand=brand, model=model) | ||
device.commands = data['commands'] | ||
device.save() |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from flask import Flask, jsonify, request, abort | ||
from route.rest_common import admin_scope, commands_schema, device_schema, editDevice_schema, app | ||
from flask_expects_json import expects_json | ||
from data.devices import edit_device, create_device, delete_device, get_devices | ||
|
||
|
||
@app.route('/devices', methods=['GET']) | ||
def models_list_route(): | ||
return jsonify(get_devices()) | ||
|
||
|
||
@app.route('/device', methods=['POST']) | ||
@expects_json(device_schema) | ||
@admin_scope() | ||
def create_device_route(): | ||
create_device(request.json) | ||
resp = jsonify(success=True) | ||
return resp | ||
|
||
|
||
@app.route('/device/<brand>/<model>', methods=['PUT']) | ||
@admin_scope() | ||
@expects_json(editDevice_schema) | ||
def set_device_route(brand: str, model: str): | ||
edit_device(brand=brand, model=model, newDevice=request.json) | ||
resp = jsonify(success=True) | ||
return resp | ||
|
||
|
||
@app.route('/device/<brand>/<model>', methods=['DELETE']) | ||
@admin_scope() | ||
def delete_device_route(brand: str, model: str): | ||
delete_device(brand=brand, model=model) | ||
resp = jsonify(success=True) | ||
return resp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
from flask import Flask, jsonify, request, abort | ||
from functools import wraps | ||
from settings import app_name | ||
import os | ||
import sys | ||
|
||
app = Flask(app_name) | ||
|
||
ADMIN_API_KEY = os.getenv("ADMIN_API_KEY") | ||
if not ADMIN_API_KEY: | ||
msg = 'ADMIN_API_KEY not exists... exiting' | ||
print(msg) | ||
sys.exit(msg) | ||
|
||
device_schema = { | ||
"type": "object", | ||
"properties": { | ||
"brand": {"type": "string"}, | ||
"model": {"type": "string"}, | ||
"category": {"type": "string"}, | ||
"commands": {"type": "object"}, | ||
}, | ||
'required': ['brand', 'model', 'category', 'commands'] | ||
} | ||
|
||
commands_schema = { | ||
"type": "object", | ||
"properties": { | ||
"commands": {"type": "object"}, | ||
}, | ||
'required': ['commands'] | ||
} | ||
|
||
editDevice_schema = { | ||
"type": "object", | ||
"properties": { | ||
"brand": {"type": "string"}, | ||
"model": {"type": "string"}, | ||
"category": {"type": "string"}, | ||
}, | ||
'required': ['brand', 'model', 'category'] | ||
} | ||
|
||
|
||
@app.after_request | ||
def remove_header(response): | ||
# Hide server info from possilbe attakers. | ||
response.headers['Server'] = '' | ||
return response | ||
|
||
|
||
def admin_scope(): | ||
"""Very simple admin scope authorazed""" | ||
def _admin_scope(f): | ||
@wraps(f) | ||
def __admin_scope(*args, **kwargs): | ||
try: | ||
if request.headers['api-key'] != ADMIN_API_KEY: | ||
raise Exception('Invalid api-key') | ||
except: | ||
abort(403, 'Invalid api-key') | ||
return f(*args, **kwargs) | ||
return __admin_scope | ||
return _admin_scope | ||
|
||
|
||
@app.route('/.well-known/security.txt') | ||
def security_info_route(): | ||
""" Get security help info """ | ||
return app.send_static_file('.well-known/security.txt') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from flask import Flask, jsonify, request, abort | ||
from route.rest_common import app, admin_scope, commands_schema | ||
from flask_expects_json import expects_json | ||
from data.rf import set_device_commands, get_device_commands | ||
|
||
|
||
@app.route('/rf/<brand>/<model>', methods=['GET']) | ||
def model_rf_commands_route(brand: str, model: str): | ||
return jsonify(get_device_commands(brand=brand, model=model)) | ||
|
||
|
||
@app.route('/rf/<brand>/<model>', methods=['PUT']) | ||
@admin_scope() | ||
@expects_json(commands_schema) | ||
def set_device_commans_route(brand: str, model: str): | ||
set_device_commands(brand=brand, model=model, data=request.json) | ||
resp = jsonify(success=True) | ||
return resp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
python-3.7.3 | ||
python-3.8.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
from dotenv import load_dotenv | ||
import os | ||
load_dotenv() | ||
|
||
app_name = 'rf-commands-repo' |