import time
from quixote import get_request, get_response, get_session, redirect
from quixote.directory import Directory, AccessControlled
from quixote.html import TemplateIO, htmltext
import wcs
import wcs.admin.root
from wcs.qommon import _
from wcs.qommon import errors, misc
from wcs.qommon.form import *
from wcs.qommon.backoffice.menu import html_top
from wcs.qommon import get_cfg
from .strongbox import StrongboxType, StrongboxItem
class StrongboxTypeDirectory(Directory):
_q_exports = ['', 'edit', 'delete']
def __init__(self, strongboxtype):
self.strongboxtype = strongboxtype
def _q_index(self):
html_top('strongbox', title = _('Item Type: %s') % self.strongboxtype.label)
r = TemplateIO(html=True)
r += htmltext('
')
r += htmltext('
')
r += htmltext('
%s
') % _('Propose a file to a user')
form = Form(enctype='multipart/form-data')
form.add(StringWidget, 'q', title = _('User'), required=True)
form.add_submit('search', _('Search'))
r += form.render()
if form.is_submitted() and not form.has_errors():
q = form.get_widget('q').parse()
users = self.search_for_users(q)
if users:
if len(users) == 1:
return redirect('add_to?user_id=%s' % users[0].id)
if len(users) < 50:
r += _('(first 50 users only)')
r += htmltext('
')
for u in users:
r += htmltext('- %s
') % (u.id, u.display_name)
r += htmltext('
')
else:
r += _('No user found.')
r += htmltext('
')
r += htmltext('
')
r += htmltext('')
r += htmltext('
')
types = StrongboxType.select()
r += htmltext('
%s
') % _('Item Types')
if not types:
r += htmltext('
')
r += _('There is no item types defined at the moment.')
r += htmltext('
')
r += htmltext('
')
for l in types:
type_id = l.id
r += htmltext('- ') % type_id
r += htmltext('%s') % (type_id, l.label)
r += htmltext('
')
r += htmltext('
')
r += htmltext('
')
r += htmltext('
')
return r.getvalue()
def get_sidebar(self):
r = TemplateIO(html=True)
r += htmltext('')
return r.getvalue()
def search_for_users(self, q):
if hasattr(get_publisher().user_class, 'search'):
return get_publisher().user_class.search(q)
if q:
users = [x for x in get_publisher().user_class.select()
if q in (x.name or '') or q in (x.email or '')]
return users
else:
return []
def get_form(self):
types = [(x.id, x.label) for x in StrongboxType.select()]
form = Form(action='add', enctype='multipart/form-data')
form.add(StringWidget, 'description', title=_('Description'), size=60)
form.add(FileWidget, 'file', title=_('File'), required=True)
form.add(SingleSelectWidget, 'type_id', title=_('Document Type'),
options = [(None, _('Not specified'))] + types)
form.add(DateWidget, 'date_time', title = _('Document Date'))
form.add_submit('submit', _('Upload'))
return form
def add(self):
form = self.get_form()
form.add(StringWidget, 'user_id', title=_('User'))
if not form.is_submitted():
return redirect('.')
sffile = StrongboxItem()
sffile.user_id = form.get_widget('user_id').parse()
sffile.description = form.get_widget('description').parse()
sffile.proposed_time = time.localtime()
sffile.proposed_id = get_request().user.id
sffile.type_id = form.get_widget('type_id').parse()
v = form.get_widget('date_time').parse()
sffile.set_expiration_time_from_date(v)
sffile.store()
sffile.set_file(form.get_widget('file').parse())
sffile.store()
return redirect('.')
def add_to(self):
form = Form(enctype='multipart/form-data', action='add_to')
form.add(StringWidget, 'user_id', title = _('User'), required=True)
try:
user_id = form.get_widget('user_id').parse()
user = get_publisher().user_class.get(user_id)
except:
return redirect('.')
if not user:
return redirect('.')
get_request().form = {}
get_request().environ['REQUEST_METHOD'] = 'GET'
html_top('strongbox', _('Strongbox'))
r = TemplateIO(html=True)
r += htmltext('