110 lines
3.9 KiB
Python
110 lines
3.9 KiB
Python
# w.c.s. - web application for online forms
|
|
# Copyright (C) 2005-2014 Entr'ouvert
|
|
#
|
|
# This program is free software; you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
import json
|
|
|
|
from quixote import get_request, get_response, redirect
|
|
|
|
import qommon.myspace
|
|
from qommon import misc
|
|
from qommon import errors
|
|
|
|
from api import get_user_from_api_query_string
|
|
from formdef import FormDef
|
|
|
|
class MyspaceDirectory(qommon.myspace.MyspaceDirectory):
|
|
_q_exports = ['', 'profile', 'new', 'password', 'remove', 'drafts', 'forms']
|
|
|
|
def get_user_forms(self, user):
|
|
formdefs = FormDef.select(lambda x: not x.is_disabled())
|
|
user_forms = []
|
|
for formdef in formdefs:
|
|
user_forms.extend(formdef.data_class().get_with_indexed_value(
|
|
'user_id', user.id))
|
|
try:
|
|
user_forms.extend(formdef.data_class().get_with_indexed_value(
|
|
'user_hash', user.hash))
|
|
except AttributeError:
|
|
pass
|
|
user_forms.sort(lambda x, y: cmp(x.receipt_time, y.receipt_time))
|
|
return user_forms
|
|
|
|
def drafts(self):
|
|
if get_request().is_json():
|
|
return self.drafts_json()
|
|
return redirect('.')
|
|
|
|
def drafts_json(self):
|
|
get_response().set_content_type('application/json')
|
|
user = get_user_from_api_query_string() or get_request().user
|
|
if not user:
|
|
return errors.AccessForbiddenError()
|
|
drafts = []
|
|
for form in self.get_user_forms(user):
|
|
if not form.is_draft():
|
|
continue
|
|
title = '%(name)s, draft saved on %(datetime)s' % {
|
|
'name': form.formdef.name,
|
|
'datetime': misc.localstrftime(form.receipt_time)
|
|
}
|
|
url = form.get_url()
|
|
d = {'title': title,
|
|
'name': form.formdef.name,
|
|
'url': url,
|
|
'datetime': misc.strftime.strftime('%Y-%m-%d %H:%M:%S', form.receipt_time),
|
|
}
|
|
drafts.append(d)
|
|
|
|
return json.dumps(drafts)
|
|
|
|
def forms(self):
|
|
if get_request().is_json():
|
|
return self.forms_json()
|
|
return redirect('.')
|
|
|
|
def forms_json(self):
|
|
get_response().set_content_type('application/json')
|
|
user = get_user_from_api_query_string() or get_request().user
|
|
if not user:
|
|
return errors.AccessForbiddenError()
|
|
forms = []
|
|
for form in self.get_user_forms(user):
|
|
if form.is_draft():
|
|
continue
|
|
visible_status = form.get_visible_status(user=user)
|
|
# skip hidden forms
|
|
if not visible_status:
|
|
continue
|
|
name = form.formdef.name
|
|
id = form.get_display_id()
|
|
status = visible_status.name
|
|
title = _('%(name)s #%(id)s (%(status)s)') % {
|
|
'name': name,
|
|
'id': id,
|
|
'status': status
|
|
}
|
|
url = form.get_url()
|
|
d = {'title': title,
|
|
'name': form.formdef.name,
|
|
'url': url,
|
|
'datetime': misc.strftime.strftime('%Y-%m-%d %H:%M:%S', form.receipt_time),
|
|
'status': status,
|
|
}
|
|
d.update(form.get_substitution_variables(minimal=True))
|
|
forms.append(d)
|
|
|
|
return json.dumps(forms)
|