A Django reusable app to create physical files from database text field.
Project description
Django Filesify helps generate a physical files from a text content stored in database and accessible from Django admin. The file content can be a plaintext or an encrypted field.
Requirements
Python 3.6+
Django 3.1+
Optional : django-mirage-field
Optional : Pip 22+
Usage
Extend Filesify class in your Django model and define your custom fields:
For plaintext content
from filesify.models import Filesify class MyConfigModel(Filesify): class Meta: verbose_name = "Some config file" verbose_name_plural = "Some config files"
For encrypted content
If your file content is sensitive information, you could decide to encrypt it. This will be transparent to you, as the the file_content field provided by django-filesify will be encrypted on model save and decrypted before displaying it on the admin.
For encrypted content, django-mirage-field is required.
You could install django-mirage-field like this :
pip install django-mirage-fields
Extend CryptoFilesify:
from filesify.models import CryptoFilesify class MyConfigModel(CryptoFilesify): class Meta: verbose_name = "Some config file" verbose_name_plural = "Some config files"
About the abstract model
Model Fields
The model class provide the following fields:
file_path: The path of the file to generate.
file_content: The text file content - either plain text or encrypted, depending on the mixin used.
comment: A TextField for optional comments about the file.
File generation
The mixin class generates a file with the content provided in the file_content field. The file generation will be trigger on the save() method, that means it’s done automatically when creating or updating your model instances.
# This create 'example.txt' containing 'Hello, World!' obj = MyConfigModel() obj.file_path = "/tmp/example.txt" obj.file_content = "Hello, World!" obj.save() # Equivalent to: obj = MyConfigModel(file_path="/tmp/example.txt", file_content="Hello, World!") # The file generation can be triggered manually like this: obj.create_file()
File removeal
When an Filesify object is delete, the files on disk will be deleted as well. This is done by overriding the model delete() method.
Filesify Admin
Django Filesify provide a admin class that can be used this way :
from django.contrib import admin from my_app.models import MyConfigModel from filesify.admin import FilesifyAdmin @admin.register(MyConfigModel) class MyConfigModelAdmin(FilesifyAdmin): pass # Or using the alternate way: class MyConfigModel(FilesifyAdmin): pass admin.site.register(MyConfigModel, MyConfigModel)
The admin class comes with a custom action to delete selected objects and their associated files on disk.
Single file instance
Sometimes, your project only require one single file instance. In these cases, Django Filesify can be used in conjunction with Django Solo, a third-party app that helps dealing with singleton database model.
To achieve this, install Django Solo and use it together with Filesify in your models and your admin classes.
Singleton models:
from django.db import models from django_solo.models import SingletonModel from filesify.models import Filesify class MyConfigModel(SingletonModel, Filesify): class Meta: verbose_name = "Some Config File" verbose_name_plural = "Some Config File"
Singleton admin:
from django.contrib import admin from django_solo.admin import SingletonModelAdmin from filesify.admin import FilesifyAdmin from my_app.models import MyConfigModel @admin.register(MyConfigModel) class MyConfigModelAdmin(SingletonModelAdmin, FilesifyAdmin): pass
Filesify on post migrate signal
Django Filesify provides a mixin class that can be used to create files automatically after running database migrations.
Usage
from django.apps import AppConfig from filesify.mixins import FilesifyPostMigrateMixin class MyAppConfig(FilesifyPostMigrateMixin, AppConfig): default_auto_field = "django.db.models.BigAutoField" name = "my_app"
This will discover all models extended from Filesify abstract model and will create the corresponding files.
Limit to models
If you want to limit the list of models to be looked at, you could define a list of dotted path models with the filesify_limit_to_models attribute. If filesify_limit_to_models` is None, it calls the management command with no arguments, considering all models.
class MyAppConfig(FilesifyPostMigrateMixin, AppConfig): default_auto_field = "django.db.models.BigAutoField" name = "my_app" filesify_limit_to_models = ["my_app.MyConfigModel"]
Filesify base mixin
If you are looking for a mode generic way to generate files from models that are extended from the Filesify class, you could use the filesify.mixins.FilesifyBaseMixin class.
from my_app.filesify import SomeGenericClass something = SomeGenericClass() something.filesify_limit_to_models = ['my_app.MyConfigModel1', 'my_app.MyConfigModel2'] something.create_files()
Notice how you can optionally limit the models that the file creation should look at.
Contribute to Django Filesify
If you already have a working environnement with django running, you could install django-filesify in “editable” mode in that receiving project.
Get the package source code somewhere outside your project folders, in this example, will will use the parent folder.
cd you/working/django/project/ git clone https://github.com/lazybird/django-filesify.git ../django-filesify/ or git clone git@github.com:lazybird/django-filesify.git ../django-filesify/
Now the code inside ../django-filesify/ is where you’ll make changes.
You can install the package in then “editable” mode in you working project. Here we assume you are in you project’s virtual environnement.
pip uninstall django-filesify # just in case you have it already... pip install --editable ../django-filesify/
Run tests :
python ../django-filesify/filesify/tests/runtests.py pytest ../django-filesify/filesify/tests/tests.py --ds=filesify.tests.settings
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-filesify-0.1.tar.gz
.
File metadata
- Download URL: django-filesify-0.1.tar.gz
- Upload date:
- Size: 12.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 322009209d0248949da7655d69a8f70afea8afbb1e62a712e250ed86dd8fa0c7 |
|
MD5 | f25219d752980fdab486f583e380d29d |
|
BLAKE2b-256 | 6dccbbae9469a8050f286925ee47b0dbe08a69735159d8ce394339e29d8e1f1a |
File details
Details for the file django_filesify-0.1-py3-none-any.whl
.
File metadata
- Download URL: django_filesify-0.1-py3-none-any.whl
- Upload date:
- Size: 19.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5257eddf258a8eb56dd9075643abd4226b73dacfe69fd33ac5c20cf01baf18ae |
|
MD5 | 515ad6028c050901f73d3ab1e8a789b9 |
|
BLAKE2b-256 | 13feffa4d5b549835b9e2e393af415f5fcce54eebc819100c622b807738aa0f4 |