-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
372 lines (298 loc) · 10.9 KB
/
model.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import contextlib
import json
import logging # Make sure logging is set up properly.
import os
import warnings
from nose.tools import set_trace
from sqlalchemy.orm.session import Session
# from sqlalchemy.engine.url import URL
from sqlalchemy import exc as sa_exc
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import (
relationship,
sessionmaker,
)
from sqlalchemy.orm.exc import (
NoResultFound,
MultipleResultsFound,
)
from sqlalchemy.ext.associationproxy import (
association_proxy,
)
from sqlalchemy.exc import (
IntegrityError
)
from sqlalchemy import (
create_engine,
Column,
ForeignKey,
Integer,
String,
Unicode,
)
Base = declarative_base()
'''
Make postgres database with something like:
CREATE USER amnh_user with password 'test';
simplified_circulation_dev=# create database my_curator_test;
simplified_circulation_dev=# grant all privileges on database my_curator_test to amnh_user;
heroku passwd: amnh_user:abc@
https://amnh-curator.herokuapp.com/ | https://git.heroku.com/amnh-curator.git
'''
def production_session():
url = Configuration.database_url()
if url.startswith('"'):
url = url[1:]
logging.debug("Database url: %s", url)
return SessionManager.session(url)
@contextlib.contextmanager
def temp_config(new_config=None, replacement_classes=None):
old_config = Configuration.instance
replacement_classes = replacement_classes or [Configuration]
if new_config is None:
new_config = copy.deepcopy(old_config)
try:
for c in replacement_classes:
c.instance = new_config
yield new_config
finally:
for c in replacement_classes:
c.instance = old_config
def get_one(db, model, on_multiple='error', **kwargs):
q = db.query(model).filter_by(**kwargs)
try:
return q.one()
except MultipleResultsFound, e:
if on_multiple == 'error':
raise e
elif on_multiple == 'interchangeable':
# These records are interchangeable so we can use
# whichever one we want.
#
# This may be a sign of a problem somewhere else. A
# database-level constraint might be useful.
q = q.limit(1)
return q.one()
except NoResultFound:
return None
def get_one_or_create(db, model, create_method='',
create_method_kwargs=None,
**kwargs):
one = get_one(db, model, **kwargs)
if one:
return one, False
else:
__transaction = db.begin_nested()
try:
if 'on_multiple' in kwargs:
# This kwarg is supported by get_one() but not by create().
del kwargs['on_multiple']
obj = create(db, model, create_method, create_method_kwargs, **kwargs)
__transaction.commit()
return obj
except IntegrityError, e:
logging.info(
"INTEGRITY ERROR on %r %r, %r: %r", model, create_method_kwargs,
kwargs, e)
__transaction.rollback()
return db.query(model).filter_by(**kwargs).one(), False
def create(db, model, create_method='',
create_method_kwargs=None,
**kwargs):
kwargs.update(create_method_kwargs or {})
created = getattr(model, create_method, model)(**kwargs)
db.add(created)
db.flush()
return created, True
class Configuration(object):
INTEGRATIONS = "integrations"
DATA_DIRECTORY = "data_directory"
DATABASE_INTEGRATION = "Postgres"
DATABASE_PRODUCTION_URL = "production_url"
DATABASE_TEST_URL = "test_url"
log = logging.getLogger("Configuration file loader")
instance = None
@classmethod
def database_url(cls, test=False):
if test:
key = cls.DATABASE_TEST_URL
else:
key = cls.DATABASE_PRODUCTION_URL
return cls.integration(cls.DATABASE_INTEGRATION)[key]
@classmethod
def data_directory(cls):
return cls.get(cls.DATA_DIRECTORY)
@classmethod
def get(cls, key, default=None):
if not cls.instance:
raise ValueError("No configuration file loaded!")
return cls.instance.get(key, default)
@classmethod
def integration(cls, name, required=False):
"""Find an integration configuration by name."""
integrations = cls.get(cls.INTEGRATIONS, {})
v = integrations.get(name, {})
if not v and required:
raise ValueError(
"Required integration '%s' was not defined! I see: %r" % (
name, ", ".join(sorted(integrations.keys()))
)
)
return v
@classmethod
def load(cls):
#cfv = 'SIMPLIFIED_CONFIGURATION_FILE'
#if not cfv in os.environ:
# print "CannotLoadConfiguration: No configuration file defined in %s." % cfv
config_path = 'config.json'
try:
cls.log.info("Loading configuration from %s", config_path)
configuration = cls._load(open(config_path).read())
except Exception, e:
print "CannotLoadConfiguration: Error loading configuration file %s: %s" % (
config_path, e)
cls.instance = configuration
return configuration
@classmethod
def _load(cls, str):
lines = [x for x in str.split("\n") if not x.strip().startswith("#")]
try:
result = json.loads("\n".join(lines))
except Exception, e:
print "CannotLoadConfiguration: Error loading configuration file %s: %s" % (e, lines)
return result
class ItemCollection(Base):
""" Linker for many-to-many relationship between display items and their
associated collection (curated experiences). """
__tablename__ = 'items_collections'
id = Column(Integer, primary_key=True)
item_id = Column(Integer, ForeignKey('display_items.id'), index=True)
collection_id = Column(Integer, ForeignKey('collections.id'), index=True)
@classmethod
def from_collection(cls, collection):
item_coll = ItemCollection()
item_coll.collection = collection
return item_coll
class Collection(Base):
""" A curated experience, involving a bunch of items. """
__tablename__ = 'collections'
id = Column(Integer, primary_key=True)
name = Column(Unicode)
curator = Column(Unicode)
items = association_proxy('item_collections', 'display_item')
item_collections = relationship("ItemCollection", backref="collection", cascade="all, delete, delete-orphan")
class DisplayItem(Base):
"""
An item on display, s.a. a book, an item of dress, a photograph, etc..
"""
__tablename__ = 'display_items'
id = Column(Integer, primary_key=True)
beacon_major_id = Column(Integer, index=True)
beacon_minor_id = Column(Integer, index=True)
title = Column(Unicode, index=True)
cover_image = Column(Unicode)
# an Item can have any number of downloads or streams associated with it.
media_resources = relationship("MediaResource", backref="display_item")
# an Item belongs to a curated experience
collections = association_proxy('item_collections', 'collection', creator=ItemCollection.from_collection)
item_collections = relationship("ItemCollection", backref="display_item", cascade="all, delete, delete-orphan")
@classmethod
def all(cls, db):
items = [i.info for i in db.query(cls)]
json = dict(items=items)
return json
@property
def info(self):
return dict(
title=self.title,
major=self.beacon_major_id,
minor=self.beacon_minor_id,
image=self.cover_image,
)
@property
def json(self):
resources = self.media_resources
for collection in self.collections:
other_items = [i for i in collection.display_items if i != self]
for item in other_items:
resources.extend(item.resources)
resources = [r.json for r in resources]
json = self.info
json['resources'] = resources
return json
class MediaResource(Base):
"""
A pdf, epub, jpeg, streamed movie, etc..
"""
__tablename__ = 'media_resources'
id = Column(Integer, primary_key=True)
display_item_id = Column(Integer, ForeignKey('display_items.id'), index=True, nullable=False)
# The name / title that identifies the media resource.
title = Column(Unicode)
# A snippet of text from the curator to contribute to the broader story.
snippet = Column(Unicode)
# The human-readable URL where this information can be found
direct_url = Column(Unicode)
# A longer description of the item (from the source)
description = Column(Unicode)
@property
def json(self):
return dict(
title=self.title,
snippet=self.snippet,
description=self.description,
url=self.direct_url
)
class FulfillmentInfo(object):
"""A record of an attempt to download a media object. """
def __init__(self, identifier, content_link, content_type):
"""
:param identifier Item's unique id.
:param content_link Either URL to download ACSM file from or URL to streaming content.
:param content_type Media type of the book version we're getting.
"""
self.identifier = identifier
self.content_link = content_link
self.content_type = content_type
class SessionManager(object):
# Materialized views need to be created and indexed from SQL
# commands kept in files. This dictionary maps the views to the
# SQL files.
engine_for_url = {}
@classmethod
def engine(cls, url=None):
url = url or Configuration.database_url()
return create_engine(url, echo=False)
@classmethod
def sessionmaker(cls, url=None):
engine = cls.engine(url)
return sessionmaker(bind=engine)
@classmethod
def initialize(cls, url):
if url in cls.engine_for_url:
engine = cls.engine_for_url[url]
return engine, engine.connect()
engine = cls.engine(url)
Base.metadata.create_all(engine)
base_path = os.path.split(__file__)[0]
resource_path = os.path.join(base_path, "files")
connection = None
if not connection:
connection = engine.connect()
if connection:
connection.close()
cls.engine_for_url[url] = engine
return engine, engine.connect()
@classmethod
def session(cls, url):
engine = connection = 0
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=sa_exc.SAWarning)
engine, connection = cls.initialize(url)
session = Session(connection)
cls.initialize_data(session)
session.commit()
return session
@classmethod
def initialize_data(cls, session):
pass