Initial checkin of some base functionality
[pillar-svnman.git] / svnman / __init__.py
1 import logging
2 import os.path
3
4 import flask
5 from werkzeug.local import LocalProxy
6
7 import pillarsdk
8 from pillar.extension import PillarExtension
9 from pillar.auth import current_user
10
11 EXTENSION_NAME = 'svnman'
12
13
14 class SVNManExtension(PillarExtension):
15     user_caps = {
16         'subscriber-pro': frozenset({'svn-use'}),
17         'demo': frozenset({'svn-use'}),
18         'admin': frozenset({'svn-use', 'svn-admin'}),
19     }
20
21     def __init__(self):
22         from . import remote
23
24         self._log = logging.getLogger('%s.SVNManExtension' % __name__)
25         self.remote: remote.Remote = None
26
27     @property
28     def name(self):
29         return EXTENSION_NAME
30
31     def flask_config(self):
32         """Returns extension-specific defaults for the Flask configuration.
33
34         Use this to set sensible default values for configuration settings
35         introduced by the extension.
36
37         :rtype: dict
38         """
39
40         # Just so that it registers the management commands.
41         from . import cli
42
43         return {
44             'SVNMAN_API_URL': 'http://configure-SVNMAN_API_URL/api/',
45             'SVNMAN_API_USERNAME': 'SVNMAN_API_USERNAME',
46             'SVNMAN_API_PASSWORD': 'SVNMAN_API_PASSWORD',
47         }
48
49     def eve_settings(self):
50         """Returns extensions to the Eve settings.
51
52         Currently only the DOMAIN key is used to insert new resources into
53         Eve's configuration.
54
55         :rtype: dict
56         """
57         return {'DOMAIN': {}}
58
59     def blueprints(self):
60         """Returns the list of top-level blueprints for the extension.
61
62         These blueprints will be mounted at the url prefix given to
63         app.load_extension().
64
65         :rtype: list of flask.Blueprint objects.
66         """
67
68         from . import routes
69
70         return [
71             routes.blueprint,
72         ]
73
74     def setup_app(self, app):
75         from . import remote
76
77         self.remote = remote.Remote(
78             remote_url=app.config['SVNMAN_API_URL'],
79             username=app.config['SVNMAN_API_USERNAME'],
80             password=app.config['SVNMAN_API_PASSWORD'],
81         )
82
83     @property
84     def template_path(self):
85         return os.path.join(os.path.dirname(__file__), 'templates')
86
87     @property
88     def static_path(self):
89         return os.path.join(os.path.dirname(__file__), 'static')
90
91     def sidebar_links(self, project):
92         if not current_user.has_cap('svn-use'):
93             return ''
94         return flask.render_template('svnman/sidebar.html', project=project)
95
96     @property
97     def has_project_settings(self) -> bool:
98         return current_user.has_cap('svn-use')
99
100     def project_settings(self, project: pillarsdk.Project, **template_args: dict) -> flask.Response:
101         """Renders the project settings page for this extension.
102
103         Set YourExtension.has_project_settings = True and Pillar will call this function.
104
105         :param project: the project for which to render the settings.
106         :param template_args: additional template arguments.
107         :returns: a Flask HTTP response
108         """
109
110         from .routes import project_settings
111
112         return project_settings(project, **template_args)
113
114
115 def _get_current_svnman() -> SVNManExtension:
116     """Returns the SVNMan extension of the current application."""
117
118     return flask.current_app.pillar_extensions[EXTENSION_NAME]
119
120
121 current_svnman: SVNManExtension = LocalProxy(_get_current_svnman)
122 """SVNMan extension of the current app."""