def get(self, request): # if not proposal_id: # return HttpResponseBadRequest() # import ipdb; ipdb.set_trace() # p = ProposalProcess.objects.get(pk=proposal_id) if proposal_id else ProposalProcess.objects.first() # for testing p = self.activation.process da = p.deliveryaddress_set.first() try: locale.setlocale(locale.LC_ALL, 'ru_RU.UTF-8') except locale.Error as err: logger.error('Can\'t set russian locale (get month name)!') contract_month_name = calendar.month_name[ p.contract_date.month] if p.contract_date else '' def get_full_address(list_of_parts): return ', '.join(filter(bool, list_of_parts)) context = vars(p) context = {k: (v if v else '') for k, v in context.items()} more = { 'p': p, 'pcd': p.contract_date.day if p.contract_date else '', 'pcm': contract_month_name, 'pcy': p.contract_date.year if p.contract_date else '', 'full_address': get_full_address([ p.zip_code, p.country, p.region, p.city, p.street, p.building, p.block ]), 'jur_full_address': get_full_address([ p.jur_zip_code, p.jur_country, p.jur_region, p.jur_city, p.jur_street, p.jur_building, p.jur_block ]), 'delivery_full_address': get_full_address([ da.delivery_zip_code, da.delivery_country, da.delivery_region, da.delivery_city, da.delivery_street, da.delivery_building, da.delivery_block ]) if da else '', } context = {**context, **more} filename = fill_template('main/contract.odt', context, output_format='pdf') with open(filename, 'rb') as excel: data = excel.read() response = HttpResponse(data, content_type='application/pdf') response[ 'Content-Disposition'] = 'attachment; filename=contract.pdf' return response
def lettre_create(request): formset = None if request.method == 'POST': form = LettreForm(request.POST or None) formset = LigneForm(request.POST or None) ArticleFormSet = formset_factory(LigneForm, extra=2) formset = ArticleFormSet(initial=[{'test': 'Django is now open source', }, {'test': 'Django is now open source2', }]) else: form = LettreForm() if form.is_valid(): data = form.cleaned_data location = data['location'] locataires = location.locataires data.update({'dateJour': timezone.now()}) data = get_personne_detail(data, locataires, location) data = get_batiment_detail(location.batiment, data) data = get_gestionnaire_detail(data) data.update({'tableau': [['ligne1', 'ligne2'], ['ligne1', 'ligne2']]}) filename = fill_template( 'documents/lettre.odt', data, output_format='pdf') return FileResponse(filename, 'lettre.{}'.format('pdf')) else: return render(request, 'documents/lettre.html', {'form': form})
def test_fill_flat_correct_template(self): """ test for flat file format fodt """ filename = fill_template('correct_template.fodt', {'name': 'John'}, output_format='pdf') os.unlink(filename)
def lettre_indexation(request, contrat_location_id): modele = mdl.modele_document.find_by_reference('LETTRE_INDEXATION') a_contrat_location = mdl.contrat_location.find_by_id(contrat_location_id) doctype = request.POST.get('format_fichier', 'pdf') ArticleFormSet = formset_factory(LigneForm, extra=2) formset = ArticleFormSet(initial=[{'test': 'Django is now open source', }, {'test': 'Django is now open source2', }]) data = _build_data(formset) locataires = a_contrat_location.locataires locataire = locataires.first() if locataire: personne = locataire.personne data.update({'titre': _get_civilite(locataire.civilite)}) if personne: data.update({NOM_KEY: personne.nom}) data.update({PRENOM_KEY: personne.prenom}) data.update({"sujet": "Lettre d'indexation"}) data = _batiment_detail(a_contrat_location.batiment, data) data = _gestionnaire_detail(data) filename = fill_template( "documents/{}".format(modele.fichier_modele), data, output_format=doctype) return FileResponse(filename, 'LETTRE_INDEXATION.{}'.format(doctype))
def test_fill_correct_template(self): """ Creating a document from a correct template doesn't raise any exception. """ filename = fill_template('correct_template.odt', {'name': 'John'}, output_format='pdf') os.unlink(filename)
def relatorio(request): if request.method == 'POST': form = RelatorioSetorForm(request, request.POST) if form.is_valid(): chamados = Chamado.objects.filter( setor__setor_id=request.session['setor_id']) if form['setor'].value() != '': s_helper = ServiceHelper() setor = s_helper.get_setor(form['setor'].value()) setor_solicitante = setor.set_nome chamados = chamados.filter(setor_solicitante=setor.set_id) else: setor_solicitante = 'TODOS OS SETORES' data_inicio = datetime.strptime(form['data_inicio'].value(), '%d/%m/%Y') chamados = chamados.filter(data_abertura__gte=data_inicio) if form['data_fim'].value() != '': data_fim = datetime.strptime(form['data_fim'].value(), '%d/%m/%Y') data_fim = data_fim + timedelta(days=1) #chamados = chamados.filter(Q(data_fechamento=None) or Q(data_fechamento__lte=data_fim)) #chamados = chamados.filter(Q(data_fechamento__isnull=True) or Q(data_fechamento__lte=data_fim)) chamados = chamados.exclude(data_fechamento__gt=data_fim) if form['grupo_servico'].value() != '': chamados = chamados.filter( grupo_servico=form['grupo_servico'].value()) chamados = chamados.order_by('data_abertura') context = { 'chamados': chamados, 'inicio': form['data_inicio'].value(), 'fim': form['data_fim'].value(), 'setor': request.session['setor_nome'], 'setor_solicitante': setor_solicitante } filename = fill_template('relatorio2.odt', context, output_format='pdf') visible_filename = 'relatorio_chamados.pdf' return FileResponse(filename, visible_filename) else: return render(request, 'core/relatorio/chamado/index.html', {'form': form}) else: return render(request, 'core/relatorio/chamado/index.html', {'form': form})
def handle(self, template_name, **options): output_format = options['format'] temp_name = fill_template( template_name, self.get_document_context(template_name, **options), output_format) out_file = options['output_file'] or temp_name if out_file != temp_name: os.rename(temp_name, out_file) print(out_file)
def invoice_view(request): form = InvoiceForm(request.POST or None) if form.is_valid(): doctype = form.cleaned_data['format'] filename = fill_template( 'invoices/invoice.odt', form.cleaned_data, output_format=doctype) visible_filename = 'invoice.{}'.format(doctype) return FileResponse(filename, visible_filename) else: return render(request, 'invoices/form.html', {'form': form})
def sample_view(request): form = SampleForm(request.POST or None) if form.is_valid(): doctype = form.cleaned_data['output_format'] filename = fill_template( 'sample.odt', form.cleaned_data, output_format=doctype) visible_filename = 'yummy.{}'.format(doctype) return FileResponse(filename, visible_filename) else: return render(request, 'index.html', {'form': form})
def get(self, request, user_id): user = Profile.objects.get(user_id=user_id) serialized_user = ProfileDocumentSerializer(user) print serialized_user.data doctype = 'pdf' filename = fill_template('privado.odt', serialized_user.data, output_format=doctype) visible_filename = 'teste.{}'.format(doctype) return FileResponse(filename, visible_filename)
def teste(request): from templated_docs import fill_template from templated_docs.http import FileResponse context = { 'teste': 'okgo', 'uai': 10.23, 'v1': 56789, 'v2': 2, 'v3': 3, 'v4': 4, 'v5': 5, 'v6': 6, } filename = fill_template('/code/teste.odt', context, output_format='docx') visible_filename = 'greeting.pdf' return FileResponse(filename, visible_filename)
def invoice_view(request): try: form = InvoiceForm(request.POST) if form.is_valid(): doctype = form.cleaned_data['format'] print('form.cleaned_data', form.cleaned_data) filename = fill_template('Finance/invoice.odt', form.cleaned_data, output_format=doctype) visible_filename = 'invoice.{}'.format(doctype) return FileResponse(filename, visible_filename) else: return render(request, 'Finance/form.html', {'form': form}) except IntegrityError as e: print('error', e)
def render_to_response(self, context): if not self.object.structure.nominated(self.request.user): return HttpResponseForbidden() year = self.object.items.earliest('begin').begin.year try: order = Agreement.objects.filter(date__year=year).latest('order').order + 1 except Agreement.DoesNotExist: order = 1 agreement = Agreement.objects.create(date=settings.NOW().date(), order=order, booking=self.object) context['agreement'] = agreement for ext in ('odt', 'pdf'): filename = fill_template('booking/agreement.odt', context, output_format=ext) visible_filename = "Convention_{number}_{title}.{ext}".format(number=agreement.number(), ext=ext, title=slugify(self.object.title)) f = open(filename, 'rb') getattr(agreement, ext).save(visible_filename, File(f)) f.close() unlink(filename) return HttpResponseRedirect(reverse('booking:booking_detail', kwargs={'pk': self.object.pk}))