Skip to main content

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

Metadata

Release files for django-filesify 0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-filesify 0.1
File Size Uploaded
django-filesify-0.1.tar.gz 12.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-filesify 0.1
File Interpreter ABI Platform
django_filesify-0.1-py3-none-any.whl Python 3 none any Details

Total release size: 31.3 kB

Release files / django-filesify-0.1.tar.gz

Download URL django-filesify-0.1.tar.gz
Size 12.2 kB
Tags Source
SHA-256 checksum
How to use checksums
322009209d0248949da7655d69a8f70afea8afbb1e62a712e250ed86dd8fa0c7
BLAKE2b-256 checksum
How to use checksums
6dccbbae9469a8050f286925ee47b0dbe08a69735159d8ce394339e29d8e1f1a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.11

Release files / django_filesify-0.1-py3-none-any.whl

Download URL django_filesify-0.1-py3-none-any.whl
Size 19.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5257eddf258a8eb56dd9075643abd4226b73dacfe69fd33ac5c20cf01baf18ae
BLAKE2b-256 checksum
How to use checksums
13feffa4d5b549835b9e2e393af415f5fcce54eebc819100c622b807738aa0f4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.11

Release history Release notifications | RSS feed

This release

0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page