Skip to content

Commit

Permalink
Merge pull request #2 from pietjan12/feature/editor
Browse files Browse the repository at this point in the history
Feature/editor
  • Loading branch information
nicburgt authored Dec 7, 2020
2 parents 2bee07c + 89fc15e commit 2d03819
Show file tree
Hide file tree
Showing 13 changed files with 419 additions and 155 deletions.
Binary file modified back-end/database/dev-db.db
Binary file not shown.
11 changes: 5 additions & 6 deletions back-end/database/init_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@
DATABASE = 'database/dev-db.db'

engine = create_engine(f'sqlite:///{DATABASE}', convert_unicode=True)
db_session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
session_factory = sessionmaker(autocommit=False,
autoflush=False,
bind=engine)
db_session = scoped_session(session_factory)
Base = declarative_base()
Base.query = db_session.query_property()


def init_db():
# import all modules here that might define models so that
# they will be registered properly on the metadata. Otherwise
# you will have to import them first before calling init_db()
import database.models
Base.metadata.create_all(bind=engine)

Base.metadata.create_all(bind=engine)
16 changes: 0 additions & 16 deletions back-end/database/models/configuration.py

This file was deleted.

31 changes: 0 additions & 31 deletions back-end/database/models/feed.py

This file was deleted.

64 changes: 64 additions & 0 deletions back-end/database/models/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import enum
from sqlalchemy import Column, Integer, String, ForeignKey, Text, Boolean, Enum
from sqlalchemy.orm import relationship
from ..init_db import Base


class CamType(enum.Enum):
webcam = 1
ip_cam = 2
local_file = 3


class Feed(Base):
__tablename__ = 'feed'
id = Column(Integer, primary_key=True)
name = Column(String(50))
location = Column(String(50))
description = Column(String(120))
feed_type = Column(Enum(CamType))
url = Column(String(120))
active = Column(Boolean)
configuration = relationship("Configuration", uselist=False, back_populates="feed")

def __init__(self, name=None, location=None, description=None, feed_type=None, url=None, active=None):
self.name = name
self.location = location
self.description = description
self.feed_type = feed_type
self.url = url
self.active = active

def __repr__(self):
return '<Feed %r>' % (self.name)


class DetectionTypes(Base):
__tablename__ = 'detectiontypes'
id = Column(Integer, primary_key=True)
detectionType = Column(String(120), unique=False)
configuration_id = Column(Integer, ForeignKey('configuration.id'))

def __init__(self, detectiontype=None):
self.detectionType = detectiontype

def __repr__(self):
return '<DetectionTypes %r>' % (self.detectionType)


class Configuration(Base):
__tablename__ = 'configuration'
id = Column(Integer, primary_key=True)
name = Column(String(60))
resolution = Column(String(120))
drawables = Column(Text, unique=False)
detections = relationship(DetectionTypes)
feed_id = Column(Integer, ForeignKey('feed.id'), nullable=True)
feed = relationship("Feed", back_populates="configuration")

def __init__(self, name=None, resolution=None):
self.name = name
self.resolution = resolution

def __repr__(self):
return '<Configuration %r>' % (self.feed_id)
17 changes: 13 additions & 4 deletions back-end/database/schemas/Schemas.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
from database.models.configuration import Configuration
from database.models.feed import Feed
from marshmallow_sqlalchemy import SQLAlchemyAutoSchema
from database.models.models import Configuration, Feed, DetectionTypes, CamType
from marshmallow_sqlalchemy import SQLAlchemyAutoSchema, fields
from marshmallow_enum import EnumField
from database.models.feed import CamType


class DetectionTypesSchema(SQLAlchemyAutoSchema):
class Meta:
model = DetectionTypes
include_relationships = True
load_instance = True


class ConfigurationSchema(SQLAlchemyAutoSchema):
detections = fields.Nested(DetectionTypesSchema, many=True)

class Meta:
model = Configuration
include_relationships = True
Expand All @@ -14,8 +21,10 @@ class Meta:

class FeedSchema(SQLAlchemyAutoSchema):
feed_type = EnumField(CamType, by_value=True)
configuration = fields.Nested(ConfigurationSchema)

class Meta:
model = Feed
include_relationships = True
load_instance = True

58 changes: 48 additions & 10 deletions back-end/resources/configuration.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from http import HTTPStatus
from flask import Flask, abort, jsonify, request
from flask_restful import Resource
from database.models.configuration import Configuration as Conf
from database.models.models import Configuration as Conf, Feed, DetectionTypes
from database.init_db import db_session
from database.schemas.Schemas import ConfigurationSchema

Expand All @@ -14,12 +14,42 @@ def get(self):

def post(self):
json_data = request.get_json(force=True)
feed_id = json_data['feed_id']
name = json_data['name']
email = json_data['email']
resolution = json_data['resolution']
# Fetch detection_types if they exist
if 'detection_types' in json_data:
detectiontypes = json_data['detection_types']
else:
detectiontypes = None

# Fetch drawables if they exist
if 'drawables' in json_data:
drawables = json_data['drawables']
else:
drawables = None

# get a scoped DB session
scoped_session = db_session()

# create config and link it to the Feed
config = Conf(name=name, resolution=resolution)
feed = Feed.query.get(feed_id)
feed.configuration = config

# Create drawables object if it was passed and add them to the configuration
if drawables is not None:
config.drawables = json_data['drawables']

config = Conf(name=name, email=email)
db_session.add(config)
db_session.commit()
# create detectiontypes object for each within detectiontypes array and add them to the configuration
# relationship
if detectiontypes is not None:
for item in detectiontypes:
dt = DetectionTypes(detectionType=item)
config.detections.append(dt)

# commit the changes
scoped_session.commit()

config_schema = ConfigurationSchema()
return config_schema.dump(config)
Expand All @@ -41,27 +71,35 @@ def delete(self, config_ID):
if config_ID is None:
return abort(400, description="missing required parameter")
else:
# get a scoped DB session
scoped_session = db_session()

configuration = Conf.query.filter_by(id=config_ID).first()
if configuration is None:
abort(404, description=f"Configuration {config_ID} not found")
else:
db_session.delete(configuration)
db_session.commit()
scoped_session.delete(configuration)
scoped_session.commit()
return ('', HTTPStatus.NO_CONTENT)

def put(self, config_ID):
if config_ID is None:
return abort(400, description="missing required parameter")
else:
# get a scoped DB session
scoped_session = db_session()

configuration = Conf.query.filter_by(id=config_ID).first()
if configuration is None:
abort(404, description=f"Configuration {config_ID} not found")
else:
json_data = request.get_json(force=True)
# Update entity based on JSON data
json_data = request.get_json(force=True)
configuration.name = json_data['name']
configuration.email = json_data['email']
db_session.commit()
configuration.resolution = json_data['resolution']
configuration.drawables = json_data['drawables']

scoped_session.commit()
# convert to JSON and return to user
config_schema = ConfigurationSchema()
return config_schema.dump(configuration)
23 changes: 16 additions & 7 deletions back-end/resources/feed.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from http import HTTPStatus
from flask import abort, request
from flask_restful import Resource
from database.models.feed import Feed
from database.models.models import Feed
from database.init_db import db_session
from database.schemas.Schemas import FeedSchema

Expand All @@ -20,10 +20,13 @@ def post(self):
feed_type = json_data['feed_type']
url = json_data['url']

# get a scoped DB session
scoped_session = db_session()

# feed can not be created and instantly go active, it needs to be configured first.
feed = Feed(name=name, location=location, description=description, feed_type=feed_type, url=url, active=False)
db_session.add(feed)
db_session.commit()
scoped_session.add(feed)
scoped_session.commit()

feed_schema = FeedSchema()
return feed_schema.dump(feed)
Expand All @@ -45,31 +48,37 @@ def delete(self, feed_ID):
if feed_ID is None:
return abort(400, description="missing required parameter")
else:
# get a scoped DB session
scoped_session = db_session()

feed = Feed.query.filter_by(id=feed_ID).first()
if feed is None:
abort(404, description=f"Feed {feed_ID} not found")
else:
db_session.delete(feed)
db_session.commit()
scoped_session.delete(feed)
scoped_session.commit()
return '', HTTPStatus.NO_CONTENT

def put(self, feed_ID):
if feed_ID is None:
return abort(400, description="missing required parameter")
else:
# get a scoped DB session
scoped_session = db_session()

feed = Feed.query.filter_by(id=feed_ID).first()
if feed is None:
abort(404, description=f"Feed {feed_ID} not found")
else:
json_data = request.get_json(force=True)
# Update entity based on JSON data
json_data = request.get_json(force=True)
feed.name = json_data['name']
feed.description = json_data['description']
feed.location = json_data['location']
feed.feed_type = json_data['feed_type']
feed.url = json_data['url']

db_session.commit()
scoped_session.commit()
# convert to JSON and return to user
feed_schema = FeedSchema()
return feed_schema.dump(feed)
Loading

0 comments on commit 2d03819

Please sign in to comment.