-
Notifications
You must be signed in to change notification settings - Fork 25
/
ext.py
110 lines (92 loc) · 3.77 KB
/
ext.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
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2016 CERN.
#
# Invenio is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# Invenio 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
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Invenio; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307, USA.
#
# In applying this license, CERN does not
# waive the privileges and immunities granted to it by virtue of its status
# as an Intergovernmental Organization or submit itself to any jurisdiction.
"""Invenio module that adds GitHub integration to the platform."""
from __future__ import absolute_import, print_function
from flask import current_app
from six import string_types
from sqlalchemy import event
from werkzeug.utils import cached_property, import_string
from . import config
from .api import GitHubRelease
from .utils import obj_or_import_string
class InvenioGitHub(object):
"""Invenio-GitHub extension."""
def __init__(self, app=None):
"""Extension initialization."""
if app:
self.init_app(app)
@cached_property
def release_api_class(self):
"""Github Release API class."""
cls = current_app.config['GITHUB_RELEASE_CLASS']
if isinstance(cls, string_types):
cls = import_string(cls)
assert issubclass(cls, GitHubRelease)
return cls
@cached_property
def release_error_handlers(self):
"""Github Release error handlers."""
error_handlers = current_app.config.get('GITHUB_ERROR_HANDLERS') or []
return [(obj_or_import_string(error_cls),
obj_or_import_string(handler))
for error_cls, handler in error_handlers]
@cached_property
def record_serializer(self):
"""Github Release API class."""
imp = current_app.config['GITHUB_RECORD_SERIALIZER']
if isinstance(imp, string_types):
return import_string(imp)
return imp
def init_app(self, app):
"""Flask application initialization."""
self.init_config(app)
app.extensions['invenio-github'] = self
@app.before_first_request
def connect_signals():
"""Connect OAuthClient signals."""
from invenio_oauthclient.models import RemoteAccount
from invenio_oauthclient.signals import account_setup_committed
from .api import GitHubAPI
from .handlers import account_post_init
account_setup_committed.connect(
account_post_init,
sender=GitHubAPI.remote._get_current_object()
)
@event.listens_for(RemoteAccount, 'before_delete')
def receive_before_delete(mapper, connection, target):
"""Listen for the 'before_delete' event."""
# TODO remove hooks
def init_config(self, app):
"""Initialize configuration."""
app.config.setdefault(
'GITHUB_BASE_TEMPLATE',
app.config.get('BASE_TEMPLATE',
'invenio_github/base.html'))
app.config.setdefault(
'GITHUB_SETTINGS_TEMPLATE',
app.config.get('SETTINGS_TEMPLATE',
'invenio_oauth2server/settings/base.html'))
for k in dir(config):
if k.startswith('GITHUB_'):
app.config.setdefault(k, getattr(config, k))