Package: django-filesify
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.RequirementsPython 3.6+Django 3.1+Optional : django-mirage-fieldOptional : Pip 22+UsageExtendFilesifyclass in your Django model and define your
custom fields:For plaintext contentfrom filesify.models import Filesify

class MyConfigModel(Filesify):

    class Meta:
        verbose_name = "Some config file"
        verbose_name_plural = "Some config files"For encrypted contentIf your file content is sensitive information, you could decide to
encrypt it. This will be transparent to you, as the thefile_contentfield provided bydjango-filesifywill be encrypted on model save
and decrypted before displaying it on the admin.For encrypted content,django-mirage-fieldis required.You could installdjango-mirage-fieldlike this :pip install django-mirage-fieldsExtend 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 modelModel FieldsThe 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 generationThe mixin class generates a file with the content provided in thefile_contentfield. 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 removealWhen an Filesify object is delete, the files on disk will be deleted as well. This is done
by overriding the modeldelete()method.Filesify AdminDjango 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 instanceSometimes, your project only require one single file instance.
In these cases, Django Filesify can be used in conjunction withDjango 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):
    passFilesify on post migrate signalDjango Filesify provides a mixin class that can be used to create files automatically
after running database migrations.Usagefrom 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 modelsIf you want to limit the list of models to be looked at, you could
define a list of dotted path models with thefilesify_limit_to_modelsattribute.
Iffilesify_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 mixinIf you are looking for a mode generic way to generate files from models
that are extended from the Filesify class, you could use thefilesify.mixins.FilesifyBaseMixinclass.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 FilesifyIf 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