|
1
|
|
|
import logging |
|
2
|
|
|
|
|
3
|
|
|
import requests |
|
4
|
|
|
from cachecontrol import CacheControl |
|
5
|
|
|
from cachecontrol.heuristics import ExpiresAfter |
|
6
|
|
|
from skosprovider.registry import Registry |
|
7
|
|
|
from skosprovider_getty.providers import AATProvider |
|
8
|
|
|
from skosprovider_getty.providers import TGNProvider |
|
9
|
|
|
from sqlalchemy.orm import Session |
|
10
|
|
|
|
|
11
|
|
|
from atramhasis import utils |
|
12
|
|
|
from atramhasis.data.datamanagers import ProviderDataManager |
|
13
|
|
|
|
|
14
|
|
|
log = logging.getLogger(__name__) |
|
15
|
|
|
LICENSES = [ |
|
16
|
|
|
'https://creativecommons.org/licenses/by/4.0/', |
|
17
|
|
|
'http://data.vlaanderen.be/doc/licentie/modellicentie-gratis-hergebruik/v1.0' |
|
18
|
|
|
] |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
def register_providers_from_db(registry: Registry, session: Session) -> None: |
|
22
|
|
|
""" |
|
23
|
|
|
Retrieve all providers stored in the database and add them to the registry. |
|
24
|
|
|
|
|
25
|
|
|
:param registry: The registry to which the providers will be added. |
|
26
|
|
|
:param session: A database session. |
|
27
|
|
|
""" |
|
28
|
|
|
manager = ProviderDataManager(session) |
|
29
|
|
|
for db_provider in manager.get_all_providers(): |
|
30
|
|
|
provider = utils.db_provider_to_skosprovider(db_provider) |
|
31
|
|
|
registry.register_provider(provider) |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
def create_registry(request): |
|
35
|
|
|
try: |
|
36
|
|
|
registry = Registry(instance_scope='threaded_thread') |
|
37
|
|
|
|
|
38
|
|
|
getty_session = CacheControl(requests.Session(), heuristic=ExpiresAfter(weeks=1)) |
|
39
|
|
|
|
|
40
|
|
|
aat = AATProvider( |
|
41
|
|
|
{'id': 'AAT', 'subject': ['external']}, |
|
42
|
|
|
session=getty_session |
|
43
|
|
|
) |
|
44
|
|
|
|
|
45
|
|
|
tgn = TGNProvider( |
|
46
|
|
|
{'id': 'TGN', 'subject': ['external']}, |
|
47
|
|
|
session=getty_session |
|
48
|
|
|
) |
|
49
|
|
|
|
|
50
|
|
|
registry.register_provider(aat) |
|
51
|
|
|
registry.register_provider(tgn) |
|
52
|
|
|
register_providers_from_db(registry, request.db) |
|
53
|
|
|
|
|
54
|
|
|
return registry |
|
55
|
|
|
except AttributeError: |
|
56
|
|
|
log.exception("Attribute error during creation of Registry.") |
|
57
|
|
|
raise |
|
58
|
|
|
|