Encryted file field for Django
Project description
DJANGO FILE FIELD ENCRYPTED
django-filefield-encrypted - An encrypted file field for Django.
This will encrypt when saving and decrypt on reading files on the fly. Files will be stored in a directory other than MEDIA_ROOT, so not exposed through 'media/...'.
INSTALLATION
Install django-filefield-encrypted:
.. code:: shell
pip install django-filefield-encrypted
Then add to your settings.py:
.. code:: python
# the key to encrypt the files. Keep it safe!
ENCRYPTED_FILES_KEY = b"<your key here>"
# The directory where files will be stored.
SAFE_MEDIA_ROOT = BASE_DIR / "safe/"
You can generate a key with:
.. code:: python
from cryptography.fernet import Fernet
key = Fernet.generate_key()
print(key)
USAGE
In your models.py:
.. code:: python
from encrypted_files.fields import EncryptedFileField
class Foobar(models.Model):
foo = EncryptedFileField(upload_to="whatever/")
You can create your records normally. To read the file contents:
.. code:: python
from encrypted_files.fields import EncryptedFileField
obj = Foobar.objects.get(pk=pk)
with obj.foo.open(mode="rb") as f:
content = f.read()
To retrieve the contents of the file in a view:
.. code:: python
from encrypted_files.views import EncryptedFileDetailView
class FoobarView(EncryptedFileDetailView):
model = Foobar
encrypted_file_field = "foo"
Add this view to your urls.py. When you go to that url it will return the plain contents of the file. Treat if like a ´django.views.generic.detail.DetailView´. All you need is to indicate the model and the ´encrypted_file_field´ which is the field that contains the encrypted file. So let's say you want to store an image, and use it in your html. Given the above example. This is how the template would look like:
.. code:: html
<img src="{% url 'foo_view_url_name' object.pk %}" />
That's it! The rendering page will load the image from that url, that will return the contents of the encrypted file transparently.
Alternatively you can also use it to download the file, in your template add the download link:
.. code:: html
<a href="{% url 'foo_view_url_name' object.pk %}" download>
click here...
</a>
Project details
Release history Release notifications | RSS feed
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-filefield-encrypted-0.1.1.tar.gz
.
File metadata
- Download URL: django-filefield-encrypted-0.1.1.tar.gz
- Upload date:
- Size: 16.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cbe0871178fa5c330ad9598666941bc56abb78ebe4556633b2519fe8e5083831 |
|
MD5 | 032f338bc0ffd9a23602294ac9d064b7 |
|
BLAKE2b-256 | dcaa303abeee7b12b2d0f44417029d37dfed7258a92981bb494a2841ffd4f2a3 |
File details
Details for the file django_filefield_encrypted-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: django_filefield_encrypted-0.1.1-py3-none-any.whl
- Upload date:
- Size: 18.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | db13177ff0593c5f539e641618aa869419405279945261e953a316fd265b47b5 |
|
MD5 | a543aacf3cc981d65b43bf289304cb54 |
|
BLAKE2b-256 | f53267f58017a75c2b8d0cfbc7a28cc3bbd6e2325043a6c54ba13c9279fe4aba |