diff options
author | Étienne Loks <etienne.loks@peacefrogs.net> | 2013-12-02 14:51:09 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@peacefrogs.net> | 2013-12-02 14:59:06 +0100 |
commit | 6326867adb72dd9ded160769ef09e77b2e482784 (patch) | |
tree | e7c44902b0dc74675ae3dda5658fc29c14ec71ba /archaeological_files | |
parent | de9fbce423f780a02b73a9e943995b0b7efb0008 (diff) | |
download | Ishtar-6326867adb72dd9ded160769ef09e77b2e482784.tar.bz2 Ishtar-6326867adb72dd9ded160769ef09e77b2e482784.zip |
Manage document template
* ooo_replace: generate a document by mapping ooo variables with
a given dict
* DocumentTemplate model: store templates associated with a type
of objects
* get_values method: generate a dict of value from a model
* new form/view to generate document from administrativ acts
Diffstat (limited to 'archaeological_files')
-rw-r--r-- | archaeological_files/forms.py | 26 | ||||
-rw-r--r-- | archaeological_files/ishtar_menu.py | 6 | ||||
-rw-r--r-- | archaeological_files/models.py | 14 | ||||
-rw-r--r-- | archaeological_files/templates/ishtar/administrativeact_document.html | 23 | ||||
-rw-r--r-- | archaeological_files/urls.py | 7 | ||||
-rw-r--r-- | archaeological_files/views.py | 28 |
6 files changed, 95 insertions, 9 deletions
diff --git a/archaeological_files/forms.py b/archaeological_files/forms.py index 2adc58ba1..9affc0901 100644 --- a/archaeological_files/forms.py +++ b/archaeological_files/forms.py @@ -33,7 +33,7 @@ from django.utils.translation import ugettext_lazy as _ from django.utils.safestring import mark_safe from ishtar_common.models import Person, PersonType, Town, Organization, \ - OrganizationType, valid_id, is_unique + OrganizationType, valid_id, is_unique, DocumentTemplate from archaeological_operations.models import ActType, AdministrativeAct import models from ishtar_common.forms import FinalForm, FormSet, ClosingDateFormSelection, \ @@ -185,16 +185,36 @@ class FileFormPreventive(forms.Form): self.fields['permit_type'].choices = models.PermitType.get_types() self.fields['permit_type'].help_text = models.PermitType.get_help() - class FinalFileClosingForm(FinalForm): confirm_msg = " " confirm_end_msg = _(u"Would you like to close this archaeological file?") - class FinalFileDeleteForm(FinalForm): confirm_msg = " " confirm_end_msg = _(u"Would you like to delete this archaelogical file ?") +class DocumentGenerationAdminActForm(forms.Form): + _associated_model = AdministrativeAct + document_template = forms.ChoiceField(label=_("Template"), choices=[]) + + def __init__(self, *args, **kwargs): + super(DocumentGenerationAdminActForm, self).__init__(*args, **kwargs) + self.fields['document_template'].choices = DocumentTemplate.get_tuples( + dct={'associated_object_name': + 'archaeological_operations.models.AdministrativeAct'}) + + def save(self, object_pk): + try: + c_object = self._associated_model.objects.get(pk=object_pk) + except self._associated_model.DoesNotExist: + return + try: + template = DocumentTemplate.objects.get( + pk=self.cleaned_data.get('document_template')) + except DocumentTemplate.DoesNotExist: + return + return template.publish(c_object) + class AdministrativeActFileSelect(TableSelect): associated_file__towns = get_town_field() act_type = forms.ChoiceField(label=_("Act type"), choices=[]) diff --git a/archaeological_files/ishtar_menu.py b/archaeological_files/ishtar_menu.py index 6655a381c..87958848a 100644 --- a/archaeological_files/ishtar_menu.py +++ b/archaeological_files/ishtar_menu.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# Copyright (C) 2010-2012 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet> +# Copyright (C) 2010-2013 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet> # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as @@ -60,6 +60,10 @@ MENU_SECTIONS = [ _(u"Deletion"), model=AdministrativeAct, access_controls=['delete_file', 'delete_own_file']), + MenuItem('file_administrativeact_document', + _(u"Documents"), + model=AdministrativeAct, + access_controls=['change_file', 'change_own_file']), ],), ]), ), diff --git a/archaeological_files/models.py b/archaeological_files/models.py index 0095dd13b..2b739fb1c 100644 --- a/archaeological_files/models.py +++ b/archaeological_files/models.py @@ -29,7 +29,7 @@ from ishtar_common.utils import cached_label_changed from ishtar_common.models import GeneralType, BaseHistorizedItem, \ HistoricalRecords, OwnPerms, Person, Organization, Department, Town, \ - Dashboard, IshtarUser + Dashboard, IshtarUser, ValueGetter class FileType(GeneralType): class Meta: @@ -53,14 +53,14 @@ class PermitType(GeneralType): ordering = ('label',) if settings.COUNTRY == 'fr': - class SaisineType(GeneralType): + class SaisineType(GeneralType, ValueGetter): delay = models.IntegerField(_(u"Delay (in days)")) class Meta: verbose_name = u"Type Saisine" verbose_name_plural = u"Types Saisine" ordering = ('label',) -class File(BaseHistorizedItem, OwnPerms): +class File(BaseHistorizedItem, OwnPerms, ValueGetter): TABLE_COLS = ['numeric_reference', 'year', 'internal_reference', 'file_type', 'saisine_type', 'towns', ] year = models.IntegerField(_(u"Year"), @@ -139,6 +139,14 @@ class File(BaseHistorizedItem, OwnPerms): def get_total_number(cls): return cls.objects.count() + def get_values(self, prefix=''): + values = super(File, self).get_values(prefix=prefix) + values['adminact_associated_file_towns_count'] = unicode( + self.towns.count()) + values['adminact_associated_file_towns'] = u", ".join( + [unicode(town)for town in self.towns.all()]) + return values + def __unicode__(self): if self.cached_label: return self.cached_label diff --git a/archaeological_files/templates/ishtar/administrativeact_document.html b/archaeological_files/templates/ishtar/administrativeact_document.html new file mode 100644 index 000000000..cdb2b45be --- /dev/null +++ b/archaeological_files/templates/ishtar/administrativeact_document.html @@ -0,0 +1,23 @@ +{% extends "base.html" %}Q +{% load i18n %} +{% block extra_head %} +{{search_form.media}} +{{ template_form.media }} +{% endblock %} + +{% block content %} +<h2>{% trans "Document generation" %}</h2> +<form action="." method="post">{% csrf_token %} +<div class='form'> +<table> +{{ search_form.as_table }} +</table> +<h4>{% trans "Choose the type of document" %}</h4> +<table> +{{ template_form }} +</table> +<input type="submit" id="submit_form" name='validate' value="{% trans "Generate" %}"/> +</div> +</form> +{% endblock %} + diff --git a/archaeological_files/urls.py b/archaeological_files/urls.py index 72a6e2df7..9d1f4f56f 100644 --- a/archaeological_files/urls.py +++ b/archaeological_files/urls.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# Copyright (C) 2010-2012 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet> +# Copyright (C) 2010-2013 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet> # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as @@ -59,5 +59,8 @@ urlpatterns += patterns('archaeological_files.views', 'show_file', name='show-historized-file'), url(r'revert-file/(?P<pk>.+)/(?P<date>.+)$', 'revert_file', name='revert-file'), - url(r'dashboard_file/$', 'dashboard_file', name='dashboard-file') + url(r'dashboard_file/$', 'dashboard_file', name='dashboard-file'), + url(r'file_administrativeact_document/$', + 'file_administrativeactfile_document', + name='administrativeact_document'), ) diff --git a/archaeological_files/views.py b/archaeological_files/views.py index 407bbe821..e55368e62 100644 --- a/archaeological_files/views.py +++ b/archaeological_files/views.py @@ -18,10 +18,12 @@ # See the file COPYING for details. import json +import os from django.db.models import Q from django.http import HttpResponse from django.shortcuts import render_to_response +from django.template.defaultfilters import slugify from django.utils.translation import ugettext_lazy as _ from ishtar_common.views import get_item, show_item, revert_item @@ -156,3 +158,29 @@ file_administrativeactfile_deletion_wizard = \ label=_(u"File: administrative act deletion"), url_name='file_administrativeactfile_deletion',) +def file_administrativeactfile_document(request): + dct = {} + if request.POST: + dct['search_form'] = AdministrativeActFileFormSelection(request.POST) + dct['template_form'] = DocumentGenerationAdminActForm(request.POST) + if dct['search_form'].is_valid() and dct['template_form'].is_valid(): + doc = dct['template_form'].save( + dct['search_form'].cleaned_data.get('pk')) + if doc: + MIMES = {'odt':'application/vnd.oasis.opendocument.text', + 'ods':'application/vnd.oasis.opendocument.spreadsheet'} + ext = doc.split('.')[-1] + doc_name = slugify(doc.split(os.path.sep)[-1][:-len(ext)])+ "."\ + + ext + mimetype = 'text/csv' + if ext in MIMES: + mimetype = MIMES[ext] + response = HttpResponse(open(doc), mimetype=mimetype) + response['Content-Disposition'] = 'attachment; filename=%s' % \ + doc_name + return response + else: + dct['search_form'] = AdministrativeActFileFormSelection() + dct['template_form'] = DocumentGenerationAdminActForm() + return render_to_response('ishtar/administrativeact_document.html', dct, + context_instance=RequestContext(request)) |