206 |
206 |
|
207 |
207 |
class FormDefPage(Directory):
|
208 |
208 |
_q_exports = ['', 'fields', 'delete', 'duplicate', 'export',
|
209 |
|
'archive', 'invite', 'enable', 'workflow', 'category',
|
|
209 |
'invite', 'enable', 'workflow', 'category',
|
210 |
210 |
'role', ('workflow-options', 'workflow_options'),
|
211 |
211 |
('workflow-status-remapping', 'workflow_status_remapping'),
|
212 |
212 |
'roles', 'title', 'options', ('acl-read', 'acl_read')]
|
... | ... | |
381 |
381 |
r += htmltext('<li><a href="duplicate">%s</a></li>') % _('Duplicate')
|
382 |
382 |
if ET:
|
383 |
383 |
r += htmltext('<li><a href="export">%s</a></li>') % _('Export')
|
384 |
|
r += htmltext('<li><a href="archive">%s</a></li>') % _('Archive')
|
385 |
384 |
if self.formdef.roles:
|
386 |
385 |
r += htmltext('<li><a href="invite">%s</a></li>') % _('Invites')
|
387 |
386 |
r += htmltext('</ul>')
|
... | ... | |
712 |
711 |
'attachment; filename=%s.wcs' % self.formdef.url_name)
|
713 |
712 |
return '<?xml version="1.0" encoding="iso-8859-15"?>\n' + ET.tostring(x)
|
714 |
713 |
|
715 |
|
def archive(self):
|
716 |
|
if get_request().form.get('job'):
|
717 |
|
return self.archive_processing()
|
718 |
|
if get_request().form.get('download'):
|
719 |
|
return self.archive_download()
|
720 |
|
|
721 |
|
form = Form(enctype='multipart/form-data')
|
722 |
|
|
723 |
|
form.add(DateWidget, 'date', title = _('Archive forms handled before'))
|
724 |
|
form.add(CheckboxWidget, 'not-done', title = _('Include forms that have not been handled'),
|
725 |
|
value = False)
|
726 |
|
form.add(CheckboxWidget, 'keep', title = _('Do not remove forms'),
|
727 |
|
value = False)
|
728 |
|
form.add_submit('submit', _('Submit'))
|
729 |
|
form.add_submit('cancel', _('Cancel'))
|
730 |
|
|
731 |
|
if form.get_widget('cancel').parse():
|
732 |
|
return redirect('.')
|
733 |
|
|
734 |
|
if not form.is_submitted() or form.has_errors():
|
735 |
|
get_response().breadcrumb.append(('archive', _('Archive')))
|
736 |
|
html_top('forms', title = _('Archive Forms'))
|
737 |
|
r = TemplateIO(html=True)
|
738 |
|
r += htmltext('<h2>%s</h2>') % _('Archive Forms')
|
739 |
|
r += form.render()
|
740 |
|
return r.getvalue()
|
741 |
|
else:
|
742 |
|
return self.archive_submit(form)
|
743 |
|
|
744 |
|
def archive_submit(self, form):
|
745 |
|
|
746 |
|
class Archiver:
|
747 |
|
def __init__(self, formdef):
|
748 |
|
self.formdef = formdef
|
749 |
|
|
750 |
|
def archive(self, job=None):
|
751 |
|
all_forms = self.formdef.data_class().select()
|
752 |
|
|
753 |
|
if form.get_widget('not-done').parse() is False:
|
754 |
|
not_endpoint_status = self.formdef.workflow.get_not_endpoint_status()
|
755 |
|
not_endpoint_status_ids = ['wf-%s' % x.id for x in not_endpoint_status]
|
756 |
|
all_forms = [x for x in all_forms if x.status not in not_endpoint_status_ids]
|
757 |
|
|
758 |
|
if form.get_widget('date').parse():
|
759 |
|
date = form.get_widget('date').parse()
|
760 |
|
date = time.strptime(date, misc.date_format())
|
761 |
|
all_forms = [x for x in all_forms if (
|
762 |
|
x.evolution and x.evolution[-1].time < date) or (
|
763 |
|
x.receipt_time < date)]
|
764 |
|
|
765 |
|
self.fd = StringIO()
|
766 |
|
t = tarfile.open('wcs.tar.gz', 'w:gz', fileobj=self.fd)
|
767 |
|
t.add(self.formdef.get_object_filename(), 'formdef')
|
768 |
|
for formdata in all_forms:
|
769 |
|
t.add(formdata.get_object_filename(), '%s/%s' % (self.formdef.url_name, str(formdata.id)))
|
770 |
|
t.close()
|
771 |
|
|
772 |
|
if form.get_widget('keep').parse() is False:
|
773 |
|
for f in all_forms:
|
774 |
|
f.remove_self()
|
775 |
|
|
776 |
|
if job:
|
777 |
|
job.file_content = self.fd.getvalue()
|
778 |
|
job.store()
|
779 |
|
|
780 |
|
count = self.formdef.data_class().count()
|
781 |
|
archiver = Archiver(self.formdef)
|
782 |
|
if count > 100: # Arbitrary threshold
|
783 |
|
job = get_response().add_after_job(
|
784 |
|
str(N_('Archiving forms')),
|
785 |
|
archiver.archive)
|
786 |
|
return redirect('archive?job=%s' % job.id)
|
787 |
|
else:
|
788 |
|
archiver.archive()
|
789 |
|
|
790 |
|
response = get_response()
|
791 |
|
response.set_content_type('application/x-wcs-archive')
|
792 |
|
response.set_header('content-disposition',
|
793 |
|
'attachment; filename=%s-archive.wcs' % self.formdef.url_name)
|
794 |
|
return archiver.fd.getvalue()
|
795 |
|
|
796 |
|
def archive_processing(self):
|
797 |
|
try:
|
798 |
|
job = AfterJob.get(get_request().form.get('job'))
|
799 |
|
except KeyError:
|
800 |
|
return redirect('.')
|
801 |
|
|
802 |
|
html_top('forms', title=_('Archiving'))
|
803 |
|
r = TemplateIO(html=True)
|
804 |
|
r += get_session().display_message()
|
805 |
|
get_response().add_javascript(['jquery.js', 'afterjob.js'])
|
806 |
|
r += htmltext('<dl class="job-status">')
|
807 |
|
r += htmltext('<dt>')
|
808 |
|
r += _(job.label)
|
809 |
|
r += htmltext('</dt>')
|
810 |
|
r += htmltext('<dd>')
|
811 |
|
r += htmltext('<span class="afterjob" id="%s">') % job.id
|
812 |
|
r += _(job.status)
|
813 |
|
r += htmltext('</span>')
|
814 |
|
r += htmltext('</dd>')
|
815 |
|
r += htmltext('</dl>')
|
816 |
|
|
817 |
|
r += htmltext('<div class="done">')
|
818 |
|
r += htmltext('<a href="archive?download=%s">%s</a>') % (job.id, _('Download Archive'))
|
819 |
|
r += htmltext('</div>')
|
820 |
|
return r.getvalue()
|
821 |
|
|
822 |
|
def archive_download(self):
|
823 |
|
try:
|
824 |
|
job = AfterJob.get(get_request().form.get('download'))
|
825 |
|
except KeyError:
|
826 |
|
return redirect('.')
|
827 |
|
|
828 |
|
if not job.status == 'completed':
|
829 |
|
raise TraversalError()
|
830 |
|
response = get_response()
|
831 |
|
response.set_content_type('application/x-wcs-archive')
|
832 |
|
response.set_header('content-disposition',
|
833 |
|
'attachment; filename=%s-archive.wcs' % self.formdef.url_name)
|
834 |
|
return job.file_content
|
835 |
|
|
836 |
714 |
def invite(self):
|
837 |
715 |
if not self.formdef.roles:
|
838 |
716 |
return template.error_page(
|