201 lines
9.0 KiB
Python
201 lines
9.0 KiB
Python
|
# -*- coding: utf-8 -*-
|
||
|
|
||
|
from os.path import join
|
||
|
|
||
|
from trac.admin import IAdminPanelProvider
|
||
|
from trac.core import *
|
||
|
from trac.web.chrome import add_stylesheet
|
||
|
from tracrendezvous.rendezvous.api import RendezVousSystem
|
||
|
from trac.util.translation import _
|
||
|
|
||
|
from model import RendezVousType, TypePermission, RendezVousDate
|
||
|
from ctdotools.utils import validate_id, update_votes_graph, date_cmp
|
||
|
|
||
|
__all__ = ['ComponentRendezVousGeneral', 'ComponentRendezVousTypes']
|
||
|
|
||
|
def get_actions(myactions):
|
||
|
actions = []
|
||
|
for action in myactions:
|
||
|
if isinstance(action, tuple):
|
||
|
actions.append(action[0])
|
||
|
else:
|
||
|
actions.append(action)
|
||
|
return actions
|
||
|
|
||
|
class RendezVousAdminPanel(Component):
|
||
|
|
||
|
implements(IAdminPanelProvider)
|
||
|
|
||
|
abstract = True
|
||
|
|
||
|
# IAdminPanelProvider methods
|
||
|
|
||
|
def get_admin_panels(self, req):
|
||
|
if 'RENDEZVOUS_ADMIN' in req.perm:
|
||
|
yield ('rendezvous', 'RendezVous System', self._type, self._label[1])
|
||
|
|
||
|
def render_admin_panel(self, req, cat, page, rendezvous):
|
||
|
req.perm.require('RENDEZVOUS_ADMIN')
|
||
|
# Trap AssertionErrors and convert them to TracErrors
|
||
|
try:
|
||
|
return self._render_admin_panel(req, cat, page, rendezvous)
|
||
|
except AssertionError, e:
|
||
|
raise TracError(e)
|
||
|
|
||
|
|
||
|
class ComponentRendezVousGeneral(RendezVousAdminPanel):
|
||
|
|
||
|
_type = 'rendezvous'
|
||
|
_label = ('rendezvous', 'General')
|
||
|
|
||
|
def _render_admin_panel(self, req, cat, page, rendezvous):
|
||
|
add_stylesheet (req, 'hw/css/rendezvous.css')
|
||
|
if req.method == "POST":
|
||
|
if req.args.has_key("show_vote_graph"):
|
||
|
self.config.set("rendezvous", "show_vote_graph", True)
|
||
|
else:
|
||
|
self.config.set("rendezvous", "show_vote_graph", False)
|
||
|
|
||
|
if req.args.has_key("show_location_map"):
|
||
|
self.config.set("rendezvous", "show_location_map", True)
|
||
|
else:
|
||
|
self.config.set("rendezvous", "show_location_map", False)
|
||
|
|
||
|
if req.args.has_key("max_description_length"):
|
||
|
tmp = int(req.args["max_description_length"])
|
||
|
self.config.set("rendezvous", "max_description_length", tmp)
|
||
|
|
||
|
if req.args.has_key("max_votes_per_date"):
|
||
|
tmp = int(req.args["max_votes_per_date"])
|
||
|
self.config.set("rendezvous", "max_votes_per_date", tmp)
|
||
|
|
||
|
if req.args.has_key("max_dates_per_rendezvous"):
|
||
|
tmp = int(req.args["max_dates_per_rendezvous"])
|
||
|
self.config.set("rendezvous", "max_dates_per_rendezvous", tmp)
|
||
|
|
||
|
if req.args.has_key("graph_size_x"):
|
||
|
tmp = int(req.args["graph_size_x"])
|
||
|
self.config.set("rendezvous", "graph_size_x", tmp)
|
||
|
update = True
|
||
|
|
||
|
update = False
|
||
|
if req.args.has_key("graph_size_y"):
|
||
|
tmp = int(req.args["graph_size_y"])
|
||
|
self.config.set("rendezvous", "graph_size_y", tmp)
|
||
|
update = True
|
||
|
|
||
|
if req.args.has_key("default_vote_time_start"):
|
||
|
tmp = req.args["default_vote_time_start"]
|
||
|
self.config.set("rendezvous", "default_vote_time_start", tmp)
|
||
|
|
||
|
if req.args.has_key("default_vote_time_end"):
|
||
|
tmp = req.args["default_vote_time_end"]
|
||
|
self.config.set("rendezvous", "default_vote_time_end", tmp)
|
||
|
|
||
|
if req.args.has_key("default_rendezvous_type"):
|
||
|
tmp = req.args["default_rendezvous_type"]
|
||
|
self.config.set("rendezvous", "default_rendezvous_type", tmp)
|
||
|
self.config.save()
|
||
|
if update:
|
||
|
path = join(self.env.path, "htdocs", "rendezvous_graphs")
|
||
|
size = [self.config.getint("rendezvous", "graph_size_x"),
|
||
|
self.config.getint("rendezvous", "graph_size_y")]
|
||
|
dates = RendezVousDate.fetch_all(self.env)
|
||
|
for date in dates:
|
||
|
if date.votes:
|
||
|
update_votes_graph(date.votes, path, size)
|
||
|
data = {"show_vote_graph" : self.config.getbool("rendezvous", "show_vote_graph"),
|
||
|
"show_location_map" : self.config.getbool("rendezvous", "show_location_map"),
|
||
|
"max_description_length" : self.config.getint("rendezvous", "max_description_length"),
|
||
|
"max_votes_per_date" : self.config.getint("rendezvous", "max_votes_per_date"),
|
||
|
"max_dates_per_rendezvous" : self.config.getint("rendezvous", "max_dates_per_rendezvous"),
|
||
|
"graph_size_x" : self.config.getint("rendezvous", "graph_size_x"),
|
||
|
"graph_size_y" : self.config.getint("rendezvous", "graph_size_y"),
|
||
|
"default_vote_time_start" : self.config.get("rendezvous", "default_vote_time_start"),
|
||
|
"default_vote_tTime_end" : self.config.get("rendezvous", "default_vote_time_end")}
|
||
|
return "admin_general.html", data
|
||
|
|
||
|
class ComponentRendezVousTypes(RendezVousAdminPanel):
|
||
|
|
||
|
_type = 'types'
|
||
|
_label = ('types', 'Types')
|
||
|
|
||
|
def _render_admin_panel(self, req, cat, page, rendezvoustype):
|
||
|
|
||
|
add_stylesheet (req, 'hw/css/rendezvous.css')
|
||
|
data = {}
|
||
|
myPermissions = get_actions(RendezVousSystem._actions)
|
||
|
if req.method == "POST":
|
||
|
rtype = req.args.get("rtype")
|
||
|
permission = req.args.get("permission")
|
||
|
|
||
|
if rtype and rtype.isupper():
|
||
|
raise TracError(_('All upper-cased tokens are reserved for '
|
||
|
'permission names'))
|
||
|
#if not rtype:
|
||
|
#raise TracError(_('Unknown RendezVousType'))
|
||
|
if permission and permission not in myPermissions:
|
||
|
raise TracError(_('Unknown permission'))
|
||
|
if req.args.get("add") and rtype and permission:
|
||
|
rtype = RendezVousType.fetch_one(self.env, name=rtype)
|
||
|
if rtype.has_permission(permission):
|
||
|
raise TracError(_('permission already granted to RendezVousType'))
|
||
|
tPerm = TypePermission(self.env, rtype.type_id, permission)
|
||
|
self.validate_type_permission(tPerm)
|
||
|
tPerm.commit()
|
||
|
req.redirect(req.href.admin(cat, page))
|
||
|
elif req.args.has_key("add") and rtype:
|
||
|
realType = RendezVousType.fetch_one(self.env, name=rtype)
|
||
|
if realType:
|
||
|
raise TracError(_('RendezVousType already exists'))
|
||
|
realType = RendezVousType(self.env, 0, rtype)
|
||
|
self.validate_rendezvous_type(realType)
|
||
|
realType.commit()
|
||
|
req.redirect(req.href.admin(cat, page))
|
||
|
elif req.args.has_key("save") and req.args.has_key("rsel"):
|
||
|
req.perm.require('RENDEZVOUS_ADMIN')
|
||
|
rsel = req.args.get('rsel')
|
||
|
rsel = isinstance(rsel, list) and rsel or [rsel]
|
||
|
for key in rsel:
|
||
|
type_id = int(key)
|
||
|
rtype = RendezVousType.fetch_one(self.env, type_id)
|
||
|
if not rtype:
|
||
|
raise TracError(_('Unknown RendezVousType'))
|
||
|
rtype.delete()
|
||
|
req.redirect(req.href.admin(cat, page))
|
||
|
elif req.args.has_key("save") and req.args.has_key("sel"):
|
||
|
req.perm.require('RENDEZVOUS_ADMIN')
|
||
|
sel = req.args.get('sel')
|
||
|
sel = isinstance(sel, list) and sel or [sel]
|
||
|
for key in sel:
|
||
|
rtype, permission = key.split(":")
|
||
|
rtype_id = int(rtype)
|
||
|
if permission and permission not in myPermissions:
|
||
|
raise TracError(_('Unknown type permission relation'))
|
||
|
typePermission = TypePermission.fetch_one(self.env, rtype_id, permission)
|
||
|
if typePermission:
|
||
|
typePermission.delete()
|
||
|
req.redirect(req.href.admin(cat, page))
|
||
|
elif req.args.has_key("save") and req.args.has_key("default"):
|
||
|
default = int(req.args["default"])
|
||
|
self.config.set("rendezvous", "default_rendezvous_type", default)
|
||
|
self.config.save()
|
||
|
|
||
|
data.update({"default_rendezvous_type" : self.config.getint("rendezvous", "default_rendezvous_type"),
|
||
|
"rendezVousTypes" : RendezVousType.fetch_all(self.env),
|
||
|
"actions" : myPermissions})
|
||
|
return "admin_types.html", data
|
||
|
|
||
|
def validate_rendezvous_type(self, typ):
|
||
|
if type(typ.type_id) != int:
|
||
|
raise TypeError("RendezVousType.validate() wrong type")
|
||
|
if type(typ.name) != unicode:
|
||
|
raise TypeError("RendezVousType.validate() wrong type")
|
||
|
|
||
|
def validate_type_permission(self, mytype):
|
||
|
if type(mytype.type_id) != int:
|
||
|
raise TypeError("TypePermission.__init__(): expected type int, got '%s'" % type(mytype.type_id))
|
||
|
if type(mytype.permission) != unicode:
|
||
|
raise TypeError("TypePermission.__init__() expected type 'unicode', got '%s'" % type(mytype.permission))
|
||
|
validate_id(mytype.type_id)
|