Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Apply changes requested in meeting
  • Loading branch information
EliuX committed Mar 23, 2020
commit 2ed13c97ce6c8ba4dc5a9a2ea834949720f5e2bd
3 changes: 1 addition & 2 deletions tests/sql_repository_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ def test_find_all(sql_repository):
assert all(e in existing_elements_registry for e in existing_elements)


def test_find_all_that_contains_property_with_string(sql_repository):
"""Find all elements that have a property that partially contains a string (case-insensitive)"""
def test_find_all_that_contains_property_with_string_case_insensitive(sql_repository):
fake_name = fake.name()
new_element = dict(name="%s Snow" % fake_name,
email=fake.safe_email(),
Expand Down
3 changes: 1 addition & 2 deletions time_tracker_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,5 @@ def handle_connection_error(e):

@api.errorhandler
def generic_exception_handler(e):
if not app.config.get("FLASK_DEBUG", False):
app.logger.error(e)
app.logger.error(e)
return {'message': 'An unhandled exception occurred.'}, 500
2 changes: 0 additions & 2 deletions time_tracker_api/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os

from time_tracker_api.database import DATABASE_TYPE
from time_tracker_api.security import generate_dev_secret_key


Expand All @@ -26,7 +25,6 @@ class SQLConfig(Config):
SQLALCHEMY_DATABASE_URI = Config.DATABASE_URI
SQLALCHEMY_COMMIT_ON_TEARDOWN = True
SQLALCHEMY_TRACK_MODIFICATIONS = False
DATABASE = DATABASE_TYPE.SQL


class AzureConfig(SQLConfig):
Expand Down
12 changes: 1 addition & 11 deletions time_tracker_api/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
from flask import Flask


class DATABASE_TYPE(enum.Enum):
IN_MEMORY = 'in-memory'
SQL = 'sql'


class CRUDDao(abc.ABC):
@abc.abstractmethod
def get_all(self):
Expand Down Expand Up @@ -76,13 +71,8 @@ def to_dto(*args, **kw):


def init_app(app: Flask) -> None:
database_strategy = app.config['DATABASE']
with app.app_context():
globals()["use_%s" % database_strategy.name.lower()](app)


def use_sql(app: Flask) -> None:
from time_tracker_api.sql_repository import init_app, SQLSeeder
init_app(app)
global seeder
seeder = SQLSeeder()

76 changes: 17 additions & 59 deletions time_tracker_api/projects/projects_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from flask import Flask

from time_tracker_api.database import DATABASE_TYPE, CRUDDao
from time_tracker_api.database import CRUDDao
from time_tracker_api.sql_repository import SQLCRUDDao, AuditedSQLModel, SQLModel


Expand All @@ -20,66 +20,24 @@ class ProjectDao(CRUDDao):


def create_dao(app: Flask) -> ProjectDao:
if app.config['DATABASE'] == DATABASE_TYPE.SQL:
from time_tracker_api.sql_repository import db
from time_tracker_api.sql_repository import db

class ProjectSQLModel(db.Model, SQLModel, AuditedSQLModel):
__tablename__ = 'projects'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), unique=True, nullable=False)
description = db.Column(db.String(250), unique=False, nullable=False)
type = db.Column(db.String(10), nullable=False)
active = db.Column(db.Boolean, default=True)
class ProjectSQLModel(db.Model, SQLModel, AuditedSQLModel):
__tablename__ = 'projects'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), unique=True, nullable=False)
description = db.Column(db.String(250), unique=False, nullable=False)
type = db.Column(db.String(10), nullable=False)
active = db.Column(db.Boolean, default=True)

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

def __str___(self):
return "the project \"%s\"" % self.name
def __str___(self):
return "the project \"%s\"" % self.name

class ProjectSQLDao(SQLCRUDDao):
def __init__(self):
SQLCRUDDao.__init__(self, ProjectSQLModel)
class ProjectSQLDao(SQLCRUDDao):
def __init__(self):
SQLCRUDDao.__init__(self, ProjectSQLModel)

return ProjectSQLDao()
else:
"""
This is legacy, just to make clear how multiple database strategies can be supported
with the current design/architecture.
"""
from time_tracker_api.errors import MissingResource

class ProjectInMemoryDAO(object):
def __init__(self):
self.counter = 0
self.projects = []

def get_all(self):
return self.projects

def get(self, id):
for project in self.projects:
if project.get('id') == id:
return project
raise MissingResource("Project '%s' not found" % id)

def create(self, project):
self.counter += 1
project['id'] = str(self.counter)
self.projects.append(project)
return project

def update(self, id, data):
project = self.get(id)
if project:
project.update(data)
return project
else:
raise MissingResource("Project '%s' not found" % id)

def delete(self, id):
if id:
project = self.get(id)
self.projects.remove(project)

return ProjectInMemoryDAO()
return ProjectSQLDao()