-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTextRepository.py
129 lines (119 loc) · 4.09 KB
/
TextRepository.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from database.Text import Text as TextDB
from models.Text import Text
from repository.base import Base
from sqlalchemy import and_
class TextRepository(Base):
"""
Text repository, dedicated to realize all functions related to text,
including CRUD, and other things.
"""
def __init__(self,
dbuser="",
dbpass="",
dbhost="",
port="",
dbname=""):
super().__init__(dbuser, dbpass, dbhost, port, dbname)
def create(self, text=Text()):
"""
(Image) -> (Image)
Add image to database
"""
textDB = TextDB(text=text)
session = self.session_factory()
session.add(textDB)
session.flush()
session.refresh(textDB)
session.commit()
return Text(textDB.id,
textDB.language,
textDB.plant,
textDB.status,
textDB.attribute,
textDB.value,
textDB.reference)
def update(self, text=Text()):
"""
(Text) -> (Text)
Update database text
"""
session = self.session_factory()
textDB = session.query(TextDB).filter_by(id=text.id).first()
dic = {}
if (textDB.language != text.language):
dic['language'] = text.language
if (textDB.plant != text.plant):
dic['plant'] = text.plant
if (textDB.status != text.status):
dic['status'] = text.status
if (textDB.attribute != text.attribute):
dic['attribute'] = text.attribute
if (textDB.value != text.value):
dic['value'] = text.value
if (textDB.reference != text.reference):
dic['reference'] = text.reference
if (dic != {}):
session.query(TextDB).filter_by(id=text.id).update(dic)
session.commit()
session.flush()
session.refresh(textDB)
return Text(textDB.id,
textDB.language,
textDB.plant,
textDB.status,
textDB.attribute,
textDB.value,
textDB.reference)
def delete(self, text=Text()):
"""
(Text) -> (bool)
Delete database text
"""
status = False
session = self.session_factory()
textDB = session.query(TextDB).filter_by(id=text.id).first()
session.delete(textDB)
session.commit()
session.flush()
if (not session.query(TextDB).filter_by(id=textDB.id).count()):
status = True
session.close()
return status
def search(self, text=Text(), pageSize=10, offset=0):
"""
(Text, pageSize, offset) -> [Text]
"""
session = self.session_factory()
query = session.query(TextDB).filter(and_(
TextDB.language.like('%'+text.language+'%'),
TextDB.plant.like('%'+text.plant+'%'),
TextDB.status.like('%'+text.status+'%'),
TextDB.attribute.like('%'+text.attribute+'%'),
TextDB.value.like('%'+text.value+'%'),
TextDB.reference.like('%'+text.reference+'%')))
content = query.slice(offset, pageSize).all()
total = query.count()
texts = []
for textDB in content:
texts.append(Text(
textDB.id,
textDB.language,
textDB.plant,
textDB.status,
textDB.attribute,
textDB.value,
textDB.reference))
return {'total': total, 'content': texts}
def searchByID(self, textId):
"""
(Int) -> (Text)
"""
session = self.session_factory()
textDB = session.query(TextDB).get(textId)
return Text(textDB.id,
textDB.language,
textDB.plant,
textDB.status,
textDB.attribute,
textDB.value,
textDB.reference)