/
project_repository.py
228 lines (178 loc) · 8.11 KB
/
project_repository.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# -*- coding: utf8 -*-
# This file is part of PyBossa.
#
# Copyright (C) 2014 SF Isle of Man Limited
#
# PyBossa is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyBossa is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with PyBossa. If not, see <http://www.gnu.org/licenses/>.
from sqlalchemy.exc import IntegrityError
from pybossa.model.app import App
from pybossa.model.category import Category
from pybossa.exc import WrongObjectError, DBIntegrityError
class ProjectRepository(object):
def __init__(self, db):
self.db = db
# Methods for App/Project objects
def get(self, id):
return self.db.session.query(App).get(id)
def get_by_shortname(self, short_name):
return self.db.session.query(App).filter_by(short_name=short_name).first()
def get_by(self, **attributes):
return self.db.session.query(App).filter_by(**attributes).first()
def get_all(self):
return self.db.session.query(App).all()
def filter_by(self, limit=None, offset=0, **filters):
query = self.db.session.query(App).filter_by(**filters)
query = query.order_by(App.id).limit(limit).offset(offset)
return query.all()
def save(self, project):
self._validate_can_be('saved', project)
try:
self.db.session.add(project)
self.db.session.commit()
except IntegrityError as e:
self.db.session.rollback()
raise DBIntegrityError(e)
def update(self, project):
self._validate_can_be('updated', project)
try:
self.db.session.merge(project)
self.db.session.commit()
except IntegrityError as e:
self.db.session.rollback()
raise DBIntegrityError(e)
def delete(self, project):
self._validate_can_be('deleted', project)
app = self.db.session.query(App).filter(App.id==project.id).first()
self.db.session.delete(app)
self.db.session.commit()
# Methods for Category objects
def get_category(self, id=None):
if id is None:
return self.db.session.query(Category).first()
return self.db.session.query(Category).get(id)
def get_category_by(self, **attributes):
return self.db.session.query(Category).filter_by(**attributes).first()
def get_all_categories(self):
return self.db.session.query(Category).all()
def filter_categories_by(self, limit=None, offset=0, **filters):
query = self.db.session.query(Category).filter_by(**filters)
query = query.order_by(Category.id).limit(limit).offset(offset)
return query.all()
def save_category(self, category):
self._validate_can_be('saved as a Category', category, klass=Category)
try:
self.db.session.add(category)
self.db.session.commit()
except IntegrityError as e:
self.db.session.rollback()
raise DBIntegrityError(e)
def update_category(self, new_category):
self._validate_can_be('updated as a Category', new_category, klass=Category)
try:
self.db.session.merge(new_category)
self.db.session.commit()
except IntegrityError as e:
self.db.session.rollback()
raise DBIntegrityError(e)
def delete_category(self, category):
self._validate_can_be('deleted as a Category', category, klass=Category)
self.db.session.query(Category).filter(Category.id==category.id).delete()
self.db.session.commit()
def _validate_can_be(self, action, element, klass=App):
if not isinstance(element, klass):
name = element.__class__.__name__
msg = '%s cannot be %s by %s' % (name, action, self.__class__.__name__)
raise WrongObjectError(msg)
class MemoryProjectRepository(object):
def __init__(self):
self.store = {}
self.count = 0
self.category_store = {}
self.category_count = 0
def clean(self):
self.store = {}
self.count = 0
self.category_store = {}
self.category_count = 0
def _next_id(self):
self.count += 1
return self.count
def _next_category_id(self):
self.category_count += 1
return self.category_count
# Methods for App/Project objects
def get(self, id):
return self.store.get(id)
def get_by_shortname(self, short_name):
projects = filter(lambda x: x.short_name == short_name, self.store.values())
return None if len(projects) == 0 else projects.pop()
def get_by(self, **attributes):
projects = filter(lambda project: reduce(lambda y, z: y and getattr(project, z) == attributes[z], attributes.keys(), True), self.store.values())
return None if len(projects) == 0 else projects.pop()
def get_all(self):
return self.store.values()
def filter_by(self, limit=None, offset=0, **filters):
projects = filter(lambda project: reduce(lambda y, z: y and getattr(project, z) == filters[z], filters.keys(), True), self.store.values())
projects = sorted(projects, key=lambda item: item.id)
projects = projects[offset:] if limit is None else projects[offset:offset+limit]
return projects
def save(self, project):
self._validate_can_be('saved', project)
if not project.id:
project.id = self._next_id()
self.store[project.id] = project
def update(self, project):
self._validate_can_be('updated', project)
self.store[project.id] = project
def delete(self, project, task_repo=None, blog_repo=None):
self._validate_can_be('deleted', project)
if task_repo is not None:
tasks = task_repo.filter_tasks_by(app_id=project.id)
task_repo.delete_all(tasks)
if blog_repo is not None:
blogposts = blog_repo.filter_by(app_id=project.id)
for post in blogposts:
blog_repo.delete(post)
del self.store[project.id]
# Methods for Category objects
def get_category(self, id=None):
if id is None:
return
return self.category_store.get(id)
def get_category_by(self, **attributes):
categories = filter(lambda category: reduce(lambda y, z: y and getattr(category, z) == attributes[z], attributes.keys(), True), self.category_store.values())
return None if len(categories) == 0 else categories.pop()
def get_all_categories(self):
return self.category_store.values()
def filter_categories_by(self, limit=None, offset=0, **filters):
cats = filter(lambda category: reduce(lambda y, z: y and getattr(category, z) == filters[z], filters.keys(), True), self.category_store.values())
cats = sorted(cats, key=lambda item: item.id)
cats = cats[offset:] if limit is None else cats[offset:offset+limit]
return cats
def save_category(self, category):
self._validate_can_be('saved as a Category', category, klass=Category)
if not category.id:
category.id = self._next_category_id()
self.category_store[category.id] = category
def update_category(self, new_category):
self._validate_can_be('updated as a Category', new_category, klass=Category)
self.category_store[new_category.id] = new_category
def delete_category(self, category):
self._validate_can_be('deleted as a Category', category, klass=Category)
del self.category_store[category.id]
def _validate_can_be(self, action, element, klass=App):
if not isinstance(element, klass):
name = element.__class__.__name__
msg = '%s cannot be %s by %s' % (name, action, self.__class__.__name__)
raise WrongObjectError(msg)