Child: [2c8fa4] (diff)

Download this file

dl_main.py    121 lines (100 with data), 4.0 kB

  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
#-*- python -*-
import logging
# Non-stdlib imports
import pkg_resources
from tg import expose, validate, redirect, response, config, flash
from tg.decorators import with_trailing_slash, without_trailing_slash
from pylons import g, c, request
# Pyforge-specific imports
from allura.app import Application, ConfigOption, SitemapEntry, DefaultAdminController
from allura.lib import helpers as h
from allura.lib.security import has_access, require_access
from allura.lib.decorators import require_post
from allura import model as M
from allura.controllers import BaseController
# Local imports
from forgedownloads import version
log = logging.getLogger(__name__)
class ForgeDownloadsApp(Application):
__version__ = version.__version__
permissions = [ 'configure', 'read' ]
searchable=True
# installable=config['auth.method'] == 'sfx'
templates=None
tool_label='Downloads'
default_mount_label='Downloads'
default_mount_point='downloads'
ordinal=8
icons={
24:'allura/images/downloads_24.png',
32:'allura/images/downloads_32.png',
48:'allura/images/downloads_48.png'
}
def __init__(self, project, config):
Application.__init__(self, project, config)
self.root = RootController()
self.admin = DownloadAdminController(self)
@property
@h.exceptionless([], log)
def sitemap(self):
menu_id = self.config.options.mount_label.title()
url='/projects/' + c.project.get_tool_data('sfx', 'unix_group_name') + '/files/'
return [SitemapEntry(menu_id, url)[self.sidebar_menu()] ]
def sidebar_menu(self):
return []
def admin_menu(self):
admin_url = c.project.url()+'admin/'+self.config.options.mount_point+'/'
links = super(ForgeDownloadsApp, self).admin_menu()
if has_access(self, 'configure')():
links.append(SitemapEntry('Options', admin_url + 'options', className='admin_modal'))
return links
def install(self, project):
'Set up any default permissions and roles here'
super(ForgeDownloadsApp, self).install(project)
c.project.show_download_button = True
# Setup permissions
role_admin = M.ProjectRole.by_name('Admin')._id
role_anon = M.ProjectRole.anonymous()._id
self.config.acl = [
M.ACE.allow(role_anon, 'read'),
M.ACE.allow(role_admin, 'configure'),
]
def uninstall(self, project):
"Remove all the tool's artifacts from the database"
c.project.show_download_button = False
super(ForgeDownloadsApp, self).uninstall(project)
class RootController(BaseController):
def __init__(self):
setattr(self, 'nav.json', self.nav)
@expose('json:')
def nav(self):
if c.app.sitemap:
my_entry = c.app.sitemap[0]
else:
my_entry = None
def _entry(s):
d = dict(name=s.label, url=s.url, icon=s.ui_icon)
if my_entry and s.url == my_entry.url:
d['selected'] = True
return d
return dict(menu=[ _entry(s) for s in c.project.sitemap() ] )
class DownloadAdminController(DefaultAdminController):
def _check_security(self):
require_access(self.app, 'configure')
@with_trailing_slash
def index(self, **kw):
redirect('options')
@expose('jinja:forgedownloads:templates/downloads/admin_options.html')
def options(self):
return dict(app=self.app,
allow_config=has_access(self.app, 'configure')())
@h.vardec
@expose()
@require_post()
def update_options(self, **kw):
show_download_button = kw.pop('show_download_button', '')
if bool(show_download_button) != c.project.show_download_button:
h.log_action(log, 'update project download button').info('')
c.project.show_download_button = bool(show_download_button)
flash('Download options updated')
redirect(c.project.url()+'admin/tools')