Django layer on top of the bureaucracy package
Project description
# Django-bureaucracy
Django-Bureaucracy is a small wrapper around the [bureacracy](https://bitbucket.org/maykinmedia/bureaucracy)
package that can be used to generate word and pdf documents from .docx-templates
using Mailmerge fields.
## Installation:
```
pip install --process-dependency-links -e git+https://bitbucket.org/maykinmedia/django-bureaucracy.git#egg=django_bureaucracy-0.1
```
(Note that setuptools is annoying and that this doesn't work without the
`--process-dependency-links` flag which is deprecated. Have fun figuring out
how to do this when it's removed.)
... and then add `django_bureaucracy` to `INSTALLED_APPS`.
## Usage
### Example
```python
from django_bureaucracy.models import Document
# The document model is used to store a template and render documents from a context:
doc = Document(
template_file=File(open('sample.docx')),
type='tps_report',
validate_fields=True,
)
doc.save()
context = {
'table': Table(data=[['this is the first cell of the first row', 'this is the second cell of the first row'],
['the second row', 'etc'],
['etc', 'etc']],
headers=['header 1', 'header 2']),
'image': Image('pigeon.jpg')
'html': HTML("<h1>This is a header from html. And the styling works! Right?</h1><p><strong>bold</strong>-notbold</p><ul><li>hop</li><li>la</li><li>kee</li></ul>")
'text': 'some text',
}
# return bytes of the document generated from the template file and the context
doc_bytes = doc.render(context)
# idem but for pdf bytes:
pdf_bytes = doc.render(context, format='pdf')
# save to a file:
doc.render_to_file(context, '/path/to/file') # docx
doc.render_to_file(context, '/path/to/file', format='pdf') # pdf
```
### Rendering to HTTPResponse with the correct mimetype.
```python
from django_bureaucracy.shortcuts import render_to_download
def view(request)
# (...)
resp = render_to_download(Document.objects.get(...), context, format='docx')
# or...
resp = render_to_download(DocxTemplate(...), context, format='pdf')
# or ...
resp = render_to_download(DocxTemplate('/path/to/file', context)
# or ...
resp = render_to_download(DocxTemplate(file_like_object, context)
return resp
```
### Admin Integration
When the package is installed, `Document`-objects can be created in the
admin. When the `validate_fields` box is checked, the model's `clean` method
will check the `DOCX_TEMPLATE_VARS` setting to see whether the fields in
the template file and the document type match the ones provided in the setting and
raise a `ValidationError` if this is not the case.
```python
# in settings.py
DOCX_TEMPLATE_VARS = {
'tps_report': ['image', 'table', 'html', 'text'],
'letter': ['graph']
}
```
Django-Bureaucracy is a small wrapper around the [bureacracy](https://bitbucket.org/maykinmedia/bureaucracy)
package that can be used to generate word and pdf documents from .docx-templates
using Mailmerge fields.
## Installation:
```
pip install --process-dependency-links -e git+https://bitbucket.org/maykinmedia/django-bureaucracy.git#egg=django_bureaucracy-0.1
```
(Note that setuptools is annoying and that this doesn't work without the
`--process-dependency-links` flag which is deprecated. Have fun figuring out
how to do this when it's removed.)
... and then add `django_bureaucracy` to `INSTALLED_APPS`.
## Usage
### Example
```python
from django_bureaucracy.models import Document
# The document model is used to store a template and render documents from a context:
doc = Document(
template_file=File(open('sample.docx')),
type='tps_report',
validate_fields=True,
)
doc.save()
context = {
'table': Table(data=[['this is the first cell of the first row', 'this is the second cell of the first row'],
['the second row', 'etc'],
['etc', 'etc']],
headers=['header 1', 'header 2']),
'image': Image('pigeon.jpg')
'html': HTML("<h1>This is a header from html. And the styling works! Right?</h1><p><strong>bold</strong>-notbold</p><ul><li>hop</li><li>la</li><li>kee</li></ul>")
'text': 'some text',
}
# return bytes of the document generated from the template file and the context
doc_bytes = doc.render(context)
# idem but for pdf bytes:
pdf_bytes = doc.render(context, format='pdf')
# save to a file:
doc.render_to_file(context, '/path/to/file') # docx
doc.render_to_file(context, '/path/to/file', format='pdf') # pdf
```
### Rendering to HTTPResponse with the correct mimetype.
```python
from django_bureaucracy.shortcuts import render_to_download
def view(request)
# (...)
resp = render_to_download(Document.objects.get(...), context, format='docx')
# or...
resp = render_to_download(DocxTemplate(...), context, format='pdf')
# or ...
resp = render_to_download(DocxTemplate('/path/to/file', context)
# or ...
resp = render_to_download(DocxTemplate(file_like_object, context)
return resp
```
### Admin Integration
When the package is installed, `Document`-objects can be created in the
admin. When the `validate_fields` box is checked, the model's `clean` method
will check the `DOCX_TEMPLATE_VARS` setting to see whether the fields in
the template file and the document type match the ones provided in the setting and
raise a `ValidationError` if this is not the case.
```python
# in settings.py
DOCX_TEMPLATE_VARS = {
'tps_report': ['image', 'table', 'html', 'text'],
'letter': ['graph']
}
```
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-bureaucracy-0.2.1.tar.gz
.
File metadata
- Download URL: django-bureaucracy-0.2.1.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 91d90d7b923bad4c8788bab21698b64b421763e571a3a131e60a9105292f9588 |
|
MD5 | 2cd13ba1e4dbfa5f68b02f750059d268 |
|
BLAKE2b-256 | bb9f2750e2b806a11be51f3cf23c0180411920cd89b36c67acd9d57637dea6e3 |
File details
Details for the file django_bureaucracy-0.2.1-py3-none-any.whl
.
File metadata
- Download URL: django_bureaucracy-0.2.1-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2ae7e59f8d0cdaad7570dc65cc395cb816f63e16e991edfbb01fe7b6ce95279f |
|
MD5 | bcb31e136223cc9bd61a321ab6258a20 |
|
BLAKE2b-256 | db2e4e9c87caabae7cec4702be9b983a81b930933b6438d11821ea86fd447942 |