forked from superdesk/superdesk-core
/
__init__.py
133 lines (101 loc) · 3.9 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
# -*- coding: utf-8; -*-
#
# This file is part of Superdesk.
#
# Copyright 2013, 2014 Sourcefabric z.u. and contributors.
#
# For the full copyright and license information, please see the
# AUTHORS and LICENSE files distributed with this source code, or
# at https://www.sourcefabric.org/superdesk/license
"""Superdesk"""
import blinker
from flask import abort, json, Blueprint, current_app as app # noqa
from flask.ext.script import Command as BaseCommand, Option # noqa @UnresolvedImport
from werkzeug.exceptions import HTTPException
from eve.utils import config # noqa
from eve.methods.common import document_link # noqa
from .eve_backend import EveBackend
from .datalayer import SuperdeskDataLayer # noqa
from .services import BaseService as Service # noqa
from .resource import Resource # noqa
from .privilege import privilege, intrinsic_privilege, get_intrinsic_privileges # noqa
from .workflow import * # noqa
from .logging import logger as superdesk_logger
API_NAME = 'Superdesk API'
VERSION = (0, 0, 1)
DOMAIN = {}
COMMANDS = {}
BLUEPRINTS = []
app_components = dict()
app_models = dict()
resources = dict()
eve_backend = EveBackend()
default_user_preferences = dict()
default_session_preferences = dict()
signals = blinker.Namespace()
logger = superdesk_logger
class Command(BaseCommand):
"""
The Eve framework changes introduced with https://github.com/nicolaiarocci/eve/issues/213 make the commands fail.
Reason being the flask-script's run the commands using test_request_context() which is invalid.
That's the reason we are inheriting the Flask-Script's Command to overcome this issue.
"""
logger = superdesk_logger
def __call__(self, _app=None, *args, **kwargs):
try:
with app.app_context():
res = self.run(*args, **kwargs)
self.logger.info('Command finished with: {}'.format(res))
return 0
except Exception as ex:
self.logger.info('Uhoh, an exception occured while running the command...')
self.logger.exception(ex)
return 1
def get_headers(self, environ=None):
"""Fix CORS for abort responses.
todo(petr): put in in custom flask error handler instead
"""
return [
('Content-Type', 'text/html'),
('Access-Control-Allow-Origin', '*'),
('Access-Control-Allow-Headers', '*'),
]
setattr(HTTPException, 'get_headers', get_headers)
def domain(resource, res_config):
"""Register domain resource"""
DOMAIN[resource] = res_config
def command(name, command):
"""Register command"""
COMMANDS[name] = command
def blueprint(blueprint, **kwargs):
"""Register blueprint"""
blueprint.kwargs = kwargs
BLUEPRINTS.append(blueprint)
def get_backend():
"""Returns the available backend, this will be changed in a factory if needed."""
return eve_backend
def get_resource_service(resource_name):
return resources[resource_name].service
def get_resource_privileges(resource_name):
attr = getattr(resources[resource_name], 'privileges', {})
return attr
def register_default_user_preference(preference_name, preference):
default_user_preferences[preference_name] = preference
def register_default_session_preference(preference_name, preference):
default_session_preferences[preference_name] = preference
def register_resource(name, resource, service=None, backend=None, privilege=None):
"""Shortcut for registering resource and service together.
:param name: resource name
:param resource: resource class
:param service: service class
:param backend: backend instance
:param privilege: privilege to register with resource
"""
if not backend:
backend = get_backend()
if not service:
service = Service
if privilege:
intrinsic_privilege(name, privilege)
service_instance = service(name, backend=backend)
resource(name, app=app, service=service_instance)