|
from quixote import get_request, get_response, get_session, redirect
|
|
from quixote.directory import Directory, AccessControlled
|
|
|
|
import wcs
|
|
from wcs.backoffice.menu import *
|
|
|
|
from qommon import errors
|
|
from qommon.form import *
|
|
from qommon.afterjobs import AfterJob
|
|
|
|
from announces import Announce, AnnounceSubscription
|
|
|
|
|
|
class SubscriptionDirectory(Directory):
|
|
_q_exports = ['delete_email', "delete_sms"]
|
|
|
|
def __init__(self, subscription):
|
|
self.subscription = subscription
|
|
|
|
def delete_email [html] (self):
|
|
form = Form(enctype='multipart/form-data')
|
|
form.widgets.append(HtmlWidget('<p>%s</p>' % _(
|
|
'You are about to delete this subscription.')))
|
|
form.add_submit('submit', _('Submit'))
|
|
form.add_submit('cancel', _('Cancel'))
|
|
if form.get_submit() == 'cancel':
|
|
return redirect('..')
|
|
if not form.is_submitted() or form.has_errors():
|
|
get_response().breadcrumb.append(('delete', _('Delete')))
|
|
html_top('announces', title = _('Delete Subscription'))
|
|
'<h2>%s</h2>' % _('Deleting Subscription')
|
|
form.render()
|
|
else:
|
|
self.subscription.remove("email")
|
|
return redirect('..')
|
|
|
|
def delete_sms [html] (self):
|
|
form = Form(enctype='multipart/form-data')
|
|
form.widgets.append(HtmlWidget('<p>%s</p>' % _(
|
|
'You are about to delete this subscription.')))
|
|
form.add_submit('submit', _('Submit'))
|
|
form.add_submit('cancel', _('Cancel'))
|
|
if form.get_submit() == 'cancel':
|
|
return redirect('..')
|
|
if not form.is_submitted() or form.has_errors():
|
|
get_response().breadcrumb.append(('delete', _('Delete')))
|
|
html_top('announces', title = _('Delete Subscription'))
|
|
'<h2>%s</h2>' % _('Deleting Subscription')
|
|
form.render()
|
|
else:
|
|
self.subscription.remove("sms")
|
|
return redirect('..')
|
|
|
|
|
|
class SubscriptionsDirectory(Directory):
|
|
_q_exports = ['']
|
|
|
|
def _q_traverse(self, path):
|
|
get_response().breadcrumb.append(('subscriptions', _('Subscriptions')))
|
|
return Directory._q_traverse(self, path)
|
|
|
|
def _q_index [html] (self):
|
|
html_top('announces', _('Announces Subscribers'))
|
|
|
|
'<h2>%s</h2>' % _('Announces Subscribers')
|
|
|
|
subscribers = AnnounceSubscription.select()
|
|
'<ul class="biglist" id="subscribers-list">'
|
|
for l in subscribers:
|
|
if l.email:
|
|
if l.enabled is False:
|
|
'<li class="disabled">'
|
|
else:
|
|
'<li>'
|
|
'<strong class="label">'
|
|
if l.user:
|
|
l.user.display_name
|
|
elif l.email:
|
|
l.email
|
|
'</strong>'
|
|
'<p class="details">'
|
|
if l.user:
|
|
l.user.email
|
|
'</p>'
|
|
'<p class="commands">'
|
|
command_icon('%s/delete_email' % l.id, 'remove', popup = True)
|
|
'</p></li>'
|
|
'</li>'
|
|
if l.sms:
|
|
if l.enabled_sms is False:
|
|
'<li class="disabled">'
|
|
else:
|
|
'<li>'
|
|
'<strong class="label">'
|
|
if l.user:
|
|
l.user.display_name
|
|
elif l.email:
|
|
l.email
|
|
'</strong>'
|
|
'<p class="details">'
|
|
l.sms
|
|
'</p>'
|
|
'<p class="commands">'
|
|
command_icon('%s/delete_sms' % l.id, 'remove', popup = True)
|
|
'</p></li>'
|
|
'</li>'
|
|
'</ul>'
|
|
|
|
def _q_lookup(self, component):
|
|
try:
|
|
sub = AnnounceSubscription.get(component)
|
|
except KeyError:
|
|
raise errors.TraversalError()
|
|
get_response().breadcrumb.append((str(sub.id), str(sub.id)))
|
|
return SubscriptionDirectory(sub)
|
|
|
|
def listing(self):
|
|
return redirect('.')
|
|
|
|
class AnnounceDirectory(Directory):
|
|
_q_exports = ['', 'edit', 'delete', 'email', 'sms']
|
|
|
|
def __init__(self, announce):
|
|
self.announce = announce
|
|
|
|
def _q_index [html] (self):
|
|
form = Form(enctype='multipart/form-data')
|
|
get_response().filter['sidebar'] = self.get_sidebar()
|
|
|
|
if self.announce.sent_by_email_time is None:
|
|
form.add_submit('email', _('Send email'))
|
|
|
|
announces_cfg = get_cfg('announces', {})
|
|
if announces_cfg.get('sms_support', 0) and self.announce.sent_by_sms_time is None:
|
|
form.add_submit('sms', _('Send SMS'))
|
|
|
|
if form.get_submit() == 'edit':
|
|
return redirect('edit')
|
|
if form.get_submit() == 'delete':
|
|
return redirect('delete')
|
|
if form.get_submit() == 'email':
|
|
return redirect('email')
|
|
if form.get_submit() == 'sms':
|
|
return redirect('sms')
|
|
|
|
html_top('announces', title = _('Announce: %s') % self.announce.title)
|
|
'<h2>%s</h2>' % _('Announce: %s') % self.announce.title
|
|
'<div class="bo-block">'
|
|
'<p>'
|
|
self.announce.text
|
|
'</p>'
|
|
'</div>'
|
|
|
|
if form.get_submit_widgets():
|
|
form.render()
|
|
|
|
def get_sidebar [html] (self):
|
|
'<ul>'
|
|
'<li><a href="edit">%s</a></li>' % _('Edit')
|
|
'<li><a href="delete">%s</a></li>' % _('Delete')
|
|
'</ul>'
|
|
|
|
def email [html] (self):
|
|
if get_request().form.get('job'):
|
|
try:
|
|
job = AfterJob.get(get_request().form.get('job'))
|
|
except KeyError:
|
|
return redirect('..')
|
|
html_top('announces', title = _('Announce: %s') % self.announce.title)
|
|
get_response().add_javascript(['jquery.js', 'afterjob.js'])
|
|
'<dl class="job-status">'
|
|
'<dt>'
|
|
_(job.label)
|
|
'</dt>'
|
|
'<dd>'
|
|
'<span class="afterjob" id="%s">' % job.id
|
|
_(job.status)
|
|
'</span>'
|
|
'</dd>'
|
|
'</dl>'
|
|
|
|
'<div class="done">'
|
|
'<a href="../">%s</a>' % _('Back')
|
|
'</div>'
|
|
|
|
else:
|
|
job = get_response().add_after_job(
|
|
str(N_('Sending emails for announce')),
|
|
self.announce.email)
|
|
return redirect('email?job=%s' % job.id)
|
|
|
|
def sms [html] (self):
|
|
if get_request().form.get('job'):
|
|
try:
|
|
job = AfterJob.get(get_request().form.get('job'))
|
|
except KeyError:
|
|
return redirect('..')
|
|
html_top('announces', title = _('Announce: %s') % self.announce.title)
|
|
get_response().add_javascript(['jquery.js', 'afterjob.js'])
|
|
'<dl class="job-status">'
|
|
'<dt>'
|
|
_(job.label)
|
|
'</dt>'
|
|
'<dd>'
|
|
'<span class="afterjob" id="%s">' % job.id
|
|
_(job.status)
|
|
'</span>'
|
|
'</dd>'
|
|
'</dl>'
|
|
|
|
'<div class="done">'
|
|
'<a href="../">%s</a>' % _('Back')
|
|
'</div>'
|
|
|
|
else:
|
|
job = get_response().add_after_job(
|
|
str(N_('Sending sms for announce')),
|
|
self.announce.sms)
|
|
return redirect('sms?job=%s' % job.id)
|
|
|
|
def edit [html] (self):
|
|
form = self.form()
|
|
if form.get_submit() == 'cancel':
|
|
return redirect('.')
|
|
|
|
if form.is_submitted() and not form.has_errors():
|
|
self.submit(form)
|
|
return redirect('..')
|
|
|
|
html_top('announces', title = _('Edit Announce: %s') % self.announce.title)
|
|
'<h2>%s</h2>' % _('Edit Announce: %s') % self.announce.title
|
|
form.render()
|
|
|
|
|
|
def form(self):
|
|
form = Form(enctype='multipart/form-data')
|
|
form.add(StringWidget, 'title', title = _('Title'), required = True,
|
|
value = self.announce.title)
|
|
if self.announce.publication_time:
|
|
pub_time = time.strftime(misc.date_format(), self.announce.publication_time)
|
|
else:
|
|
pub_time = None
|
|
form.add(DateWidget, 'publication_time', title = _('Publication Time'),
|
|
value = pub_time)
|
|
if self.announce.expiration_time:
|
|
exp_time = time.strftime(misc.date_format(), self.announce.expiration_time)
|
|
else:
|
|
exp_time = None
|
|
form.add(DateWidget, 'expiration_time', title = _('Expiration Time'),
|
|
value = exp_time)
|
|
form.add(TextWidget, 'text', title = _('Text'), required = True,
|
|
value = self.announce.text, rows = 10, cols = 70)
|
|
if get_cfg('misc', {}).get('announce_themes'):
|
|
form.add(SingleSelectWidget, 'theme', title = _('Announce Theme'),
|
|
value = self.announce.theme,
|
|
options = get_cfg('misc', {}).get('announce_themes'))
|
|
form.add(CheckboxWidget, 'hidden', title = _('Hidden'),
|
|
value = self.announce.hidden)
|
|
form.add_submit('submit', _('Submit'))
|
|
form.add_submit('cancel', _('Cancel'))
|
|
return form
|
|
|
|
def submit(self, form):
|
|
for k in ('title', 'text', 'hidden', 'theme'):
|
|
widget = form.get_widget(k)
|
|
if widget:
|
|
setattr(self.announce, k, widget.parse())
|
|
for k in ('publication_time', 'expiration_time'):
|
|
widget = form.get_widget(k)
|
|
if widget:
|
|
wid_time = widget.parse()
|
|
if wid_time:
|
|
setattr(self.announce, k, time.strptime(wid_time, misc.date_format()))
|
|
else:
|
|
setattr(self.announce, k, None)
|
|
self.announce.store()
|
|
|
|
def delete [html] (self):
|
|
form = Form(enctype='multipart/form-data')
|
|
form.widgets.append(HtmlWidget('<p>%s</p>' % _(
|
|
'You are about to irrevocably delete this announce.')))
|
|
form.add_submit('submit', _('Submit'))
|
|
form.add_submit('cancel', _('Cancel'))
|
|
if form.get_submit() == 'cancel':
|
|
return redirect('..')
|
|
if not form.is_submitted() or form.has_errors():
|
|
get_response().breadcrumb.append(('delete', _('Delete')))
|
|
html_top('announces', title = _('Delete Announce'))
|
|
'<h2>%s</h2>' % _('Deleting Announce: %s') % self.announce.title
|
|
form.render()
|
|
else:
|
|
self.announce.remove_self()
|
|
return redirect('..')
|
|
|
|
|
|
|
|
class AnnouncesDirectory(AccessControlled, Directory):
|
|
_q_exports = ['', 'new', 'listing', 'subscriptions', 'update_order', 'log']
|
|
label = N_('Announces')
|
|
|
|
subscriptions = SubscriptionsDirectory()
|
|
|
|
def _q_access(self):
|
|
user = get_request().user
|
|
if not user:
|
|
raise errors.AccessUnauthorizedError()
|
|
admin_role = get_cfg('aq-permissions', {}).get('announces', None)
|
|
if not (user.is_admin or admin_role in (user.roles or [])):
|
|
raise errors.AccessForbiddenError(
|
|
public_msg = _('You are not allowed to access Announces Management'),
|
|
location_hint = 'backoffice')
|
|
|
|
get_response().breadcrumb.append(('announces/', _('Announces')))
|
|
|
|
|
|
def _q_index [html] (self):
|
|
html_top('announces', _('Announces'))
|
|
|
|
'<ul id="main-actions">'
|
|
' <li><a class="new-item" href="new">%s</a></li>' % _('New')
|
|
' <li><a href="subscriptions/">%s</a></li>' % _('Subscriptions')
|
|
' <li><a href="log">%s</a></li>' % _('Log')
|
|
'</ul>'
|
|
|
|
announces = Announce.select()
|
|
announces.sort(lambda x,y: cmp(x.publication_time or x.modification_time,
|
|
y.publication_time or y.modification_time))
|
|
announces.reverse()
|
|
|
|
'<ul class="biglist" id="announces-list">'
|
|
for l in announces:
|
|
announce_id = l.id
|
|
if l.hidden:
|
|
'<li class="disabled" class="biglistitem" id="itemId_%s">' % announce_id
|
|
else:
|
|
'<li class="biglistitem" id="itemId_%s">' % announce_id
|
|
'<strong class="label"><a href="%s/">%s</a></strong>' % (l.id, l.title)
|
|
if l.publication_time:
|
|
'<p class="details">'
|
|
time.strftime(misc.date_format(), l.publication_time)
|
|
'</p>'
|
|
'</li>'
|
|
'</ul>'
|
|
|
|
def log [html] (self):
|
|
announces = Announce.select()
|
|
log = []
|
|
for l in announces:
|
|
if l.publication_time:
|
|
log.append((l.publication_time, _('Publication'), l))
|
|
if l.sent_by_email_time:
|
|
log.append((l.sent_by_email_time, _('Email'), l))
|
|
if l.sent_by_sms_time:
|
|
log.append((l.sent_by_sms_time, _('SMS'), l))
|
|
log.sort()
|
|
|
|
get_response().breadcrumb.append(('log', _('Log')))
|
|
html_top('announces', title = _('Log'))
|
|
|
|
'<table>'
|
|
'<thead>'
|
|
'<tr>'
|
|
'<th>%s</th>' % _('Time')
|
|
'<th>%s</th>' % _('Type')
|
|
'<td></td>'
|
|
'</tr>'
|
|
'</thead>'
|
|
'<tbody>'
|
|
for log_time, log_type, log_announce in log:
|
|
'<tr>'
|
|
'<td>'
|
|
misc.localstrftime(log_time)
|
|
'</td>'
|
|
'<td>'
|
|
log_type
|
|
'</td>'
|
|
'<td>'
|
|
'<a href="%s">%s</a>' % (log_announce.id, log_announce.title)
|
|
'</td>'
|
|
'</tr>'
|
|
'</tbody>'
|
|
'</table>'
|
|
|
|
|
|
def update_order(self):
|
|
request = get_request()
|
|
new_order = request.form['order'].strip(';').split(';')
|
|
announces = Announce.select()
|
|
dict = {}
|
|
for l in announces:
|
|
dict[str(l.id)] = l
|
|
for i, o in enumerate(new_order):
|
|
dict[o].position = i + 1
|
|
dict[o].store()
|
|
return 'ok'
|
|
|
|
|
|
def new [html] (self):
|
|
announce = Announce()
|
|
announce.publication_time = time.gmtime()
|
|
announce_ui = AnnounceDirectory(announce)
|
|
|
|
form = announce_ui.form()
|
|
if form.get_submit() == 'cancel':
|
|
return redirect('.')
|
|
|
|
if form.is_submitted() and not form.has_errors():
|
|
announce_ui.submit(form)
|
|
return redirect('%s/' % announce_ui.announce.id)
|
|
|
|
get_response().breadcrumb.append(('new', _('New Announce')))
|
|
html_top('announces', title = _('New Announce'))
|
|
'<h2>%s</h2>' % _('New Announce')
|
|
form.render()
|
|
|
|
def _q_lookup(self, component):
|
|
try:
|
|
announce = Announce.get(component)
|
|
except KeyError:
|
|
raise errors.TraversalError()
|
|
get_response().breadcrumb.append((str(announce.id), announce.title))
|
|
return AnnounceDirectory(announce)
|
|
|
|
def listing(self):
|
|
return redirect('.')
|
|
|