forked from mozilla-services/cliquet
/
__init__.py
241 lines (179 loc) · 8.34 KB
/
__init__.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
229
230
231
232
233
234
235
236
237
238
239
240
241
import random
from collections import namedtuple
from . import generators
Filter = namedtuple('Filter', ['field', 'value', 'operator'])
"""Filtering properties."""
Sort = namedtuple('Sort', ['field', 'direction'])
"""Sorting properties."""
DEFAULT_ID_FIELD = 'id'
DEFAULT_MODIFIED_FIELD = 'last_modified'
DEFAULT_DELETED_FIELD = 'deleted'
_HEARTBEAT_DELETE_RATE = 0.6
_HEARTBEAT_COLLECTION_ID = '__heartbeat__'
_HEART_PARENT_ID = _HEARTBEAT_COLLECTION_ID
_HEARTBEAT_RECORD = {'__heartbeat__': True}
class StorageBase(object):
"""Storage abstraction used by resource views.
It is meant to be instantiated at application startup.
Any operation may raise a `HTTPServiceUnavailable` error if an error
occurs with the underlying service.
Configuration can be changed to choose which storage backend will
persist the objects.
:raises: :exc:`~pyramid:pyramid.httpexceptions.HTTPServiceUnavailable`
"""
id_generator = generators.UUID4()
def initialize_schema(self):
"""Create every necessary objects (like tables or indices) in the
backend.
This is excuted when the ``cliquet migrate`` command is ran.
"""
raise NotImplementedError
def flush(self, auth=None):
"""Remove **every** object from this storage.
"""
raise NotImplementedError
def ping(self, request):
"""Test that storage is operationnal.
:param request: current request object
:type request: :class:`~pyramid:pyramid.request.Request`
:returns: ``True`` is everything is ok, ``False`` otherwise.
:rtype: bool
"""
try:
auth = request.headers.get('Authorization')
if random.random() < _HEARTBEAT_DELETE_RATE:
self.delete_all(_HEARTBEAT_COLLECTION_ID, _HEART_PARENT_ID,
auth=auth)
else:
self.create(_HEARTBEAT_COLLECTION_ID, _HEART_PARENT_ID,
_HEARTBEAT_RECORD, auth=auth)
return True
except:
return False
def collection_timestamp(self, collection_id, parent_id, auth=None):
"""Get the highest timestamp of every objects in this `collection_id` for
this `parent_id`.
.. note::
This should take deleted objects into account.
:param str collection_id: the collection id.
:param str parent_id: the collection parent.
:returns: the latest timestamp of the collection.
:rtype: int
"""
raise NotImplementedError
def create(self, collection_id, parent_id, object, id_generator=None,
unique_fields=None, id_field=DEFAULT_ID_FIELD,
modified_field=DEFAULT_MODIFIED_FIELD,
auth=None):
"""Create the specified `object` in this `collection_id` for this `parent_id`.
Assign the id to the object, using the attribute
:attr:`cliquet.resource.BaseResource.id_field`.
.. note::
This will update the collection timestamp.
:raises: :exc:`cliquet.storage.exceptions.UnicityError`
:param str collection_id: the collection id.
:param str parent_id: the collection parent.
:param dict object: the object to create.
:returns: the newly created object.
:rtype: dict
"""
raise NotImplementedError
def get(self, collection_id, parent_id, object_id,
id_field=DEFAULT_ID_FIELD,
modified_field=DEFAULT_MODIFIED_FIELD,
auth=None):
"""Retrieve the object with specified `object_id`, or raise error
if not found.
:raises: :exc:`cliquet.storage.exceptions.RecordNotFoundError`
:param str collection_id: the collection id.
:param str parent_id: the collection parent.
:param str object_id: unique identifier of the object
:returns: the object object.
:rtype: dict
"""
raise NotImplementedError
def update(self, collection_id, parent_id, object_id, object,
unique_fields=None, id_field=DEFAULT_ID_FIELD,
modified_field=DEFAULT_MODIFIED_FIELD,
auth=None):
"""Overwrite the `object` with the specified `object_id`.
If the specified id is not found, the object is created with the
specified id.
.. note::
This will update the collection timestamp.
:raises: :exc:`cliquet.storage.exceptions.UnicityError`
:param str collection_id: the collection id.
:param str parent_id: the collection parent.
:param str object_id: unique identifier of the object
:param dict object: the object to update or create.
:returns: the updated object.
:rtype: dict
"""
raise NotImplementedError
def delete(self, collection_id, parent_id, object_id,
id_field=DEFAULT_ID_FIELD,
modified_field=DEFAULT_MODIFIED_FIELD,
deleted_field=DEFAULT_DELETED_FIELD,
auth=None):
"""Delete the object with specified `object_id`, and raise error
if not found.
Deleted objects must be removed from the database, but their ids and
timestamps of deletion must be tracked for synchronization purposes.
(See :meth:`cliquet.storage.StorageBase.get_all`)
.. note::
This will update the collection timestamp.
:raises: :exc:`cliquet.storage.exceptions.RecordNotFoundError`
:param str collection_id: the collection id.
:param str parent_id: the collection parent.
:param str object_id: unique identifier of the object
:returns: the deleted object, with minimal set of attributes.
:rtype: dict
"""
raise NotImplementedError
def delete_all(self, collection_id, parent_id, filters=None,
id_field=DEFAULT_ID_FIELD,
modified_field=DEFAULT_MODIFIED_FIELD,
deleted_field=DEFAULT_DELETED_FIELD,
auth=None):
"""Delete all objects in this `collection_id` for this `parent_id`.
:param str collection_id: the collection id.
:param str parent_id: the collection parent.
:param filters: Optionnally filter the objects to delete.
:type filters: list of :class:`cliquet.storage.Filter`
:returns: the list of deleted objects, with minimal set of attributes.
:rtype: list of dict
"""
raise NotImplementedError
def get_all(self, collection_id, parent_id, filters=None, sorting=None,
pagination_rules=None, limit=None, include_deleted=False,
id_field=DEFAULT_ID_FIELD,
modified_field=DEFAULT_MODIFIED_FIELD,
deleted_field=DEFAULT_DELETED_FIELD,
auth=None):
"""Retrieve all objects in this `collection_id` for this `parent_id`.
:param str collection_id: the collection id.
:param str parent_id: the collection parent.
:param filters: Optionally filter the objects by their attribute.
Each filter in this list is a tuple of a field, a value and a
comparison (see `cliquet.utils.COMPARISON`). All filters
are combined using *AND*.
:type filters: list of :class:`cliquet.storage.Filter`
:param sorting: Optionnally sort the objects by attribute.
Each sort instruction in this list refers to a field and a
direction (negative means descending). All sort instructions are
cumulative.
:type sorting: list of :class:`cliquet.storage.Sort`
:param pagination_rules: Optionnally paginate the list of objects.
This list of rules aims to reduce the set of objects to the current
page. A rule is a list of filters (see `filters` parameter),
and all rules are combined using *OR*.
:type pagination_rules: list of list of :class:`cliquet.storage.Filter`
:param int limit: Optionnally limit the number of objects to be
retrieved.
:param bool include_deleted: Optionnally include the deleted objects
that match the filters.
:returns: the limited list of objects, and the total number of
matching objects in the collection (deleted ones excluded).
:rtype: tuple (list, integer)
"""
raise NotImplementedError