-
Notifications
You must be signed in to change notification settings - Fork 12
/
controllers.py
83 lines (61 loc) · 1.71 KB
/
controllers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from models import AdministratorModel, SlideModel, CategoryModel
from database import db_session
import logging
from flask import current_app
# logger = current_app.logger
class BaseController(object):
"""
Base Controller Class.
"""
model = None
def create(self, **kwargs):
"""
Creates new object.
"""
current_app.logger.info("info")
current_app.logger.debug("debug")
print("data = ")
print(kwargs)
obj = self.model(**kwargs)
db_session.add(obj)
try:
db_session.commit()
return obj
except Exception as e:
print("="*100)
print("Exception")
print(e)
db_session.rollback()
raise
def list(self):
"""
Lists all objects or the one specified by id.
"""
return self.model.query.all()
def search(self, **kwargs):
"""
Searches for objects by the keyword arguments.
"""
return self.model.query.filter_by(**kwargs)
def delete(self, id):
"""
Deletes objects by id.
"""
obj = self.search(id=id)[0]
db_session.delete(obj)
try:
db_session.commit()
return obj
except Exception as e:
db_session.rollback()
raise
return obj
class CategoryController(BaseController):
model = CategoryModel
class AdministratorController(BaseController):
model = AdministratorModel
class SlideController(BaseController):
model = SlideModel
category_controller = CategoryController()
administrator_controller = AdministratorController()
slide_controller = SlideController()