Skip to main content

Reusable Django models, utils and storages

Project description

DjangoEssentials

Join Our Community - Kampus

DjangoEssentials is a Python library designed to streamline and enhance the development process of Django applications. It offers a collection of commonly used Django models and deployment settings, encapsulating repetitive patterns and configurations into a reusable package. The library is intended to evolve with contributions from the community, making it a collaborative project.

Features

  • TimeBasedStampModel: An abstract model providing time-based fields for tracking creation, update, and deletion times of model instances.
  • MyS3Storage: A custom storage class for Django, facilitating integration with Amazon S3 for media storage, with features such as non-overwriting of files with the same name and public read access by default.

Getting Started

Below are instructions on how to install DjangoEssentials and examples of how to use its features in your Django projects.

Installation

Install DjangoEssentials using pip:

pip install DjangoEssentials

Usage

  • TimeBasedStampModel

Inherit from TimeBasedStampModel to add automatic creation, update, and soft deletion timestamps to your models.

from django.db import models
from djangoessentials import TimeBasedStampModel

class YourModel(TimeBasedStampModel):
    name = models.CharField(max_length=255)
    # Add your fields here
  • MyS3Storage

Configure your Django project to use MyS3Storage for handling media files with Amazon S3. Example:

# settings.py
USE_S3 = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.2/howto/static-files/

if USE_S3:
    # aws settings
    AWS_ACCESS_KEY_ID = os.getenv("AWS_ACCESS_KEY_ID")  ## get your aws access key
    AWS_SECRET_ACCESS_KEY = os.getenv("AWS_SECRET_ACCESS_KEY")  ## get your aws secret access key
    AWS_STORAGE_BUCKET_NAME = os.getenv("AWS_STORAGE_BUCKET_NAME")  ## Your bucket name
    AWS_S3_CUSTOM_DOMAIN = f'{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com'
    AWS_S3_OBJECT_PARAMETERS = {'CacheControl': 'max-age=86400'}
    # s3 public media settings
    PUBLIC_MEDIA_LOCATION = 'media'
    MEDIA_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/{PUBLIC_MEDIA_LOCATION}/'
else:
    MEDIA_URL = '/media/'
    MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static/'),
]

Than

from django.db import models
from djangoessentials import MyS3Storage

class MyModel(models.Model):
    document = models.FileField(upload_to='documents/', storage=MyS3Storage)
    # Add your fields here
  • CustomUser Model Usages:
#settings.py
AUTH_USER_MODEL = 'yourapp.CustomUser'
#example
from django import forms
from djangoessentials import CustomUser

class YourModelForm(forms.ModelForm):
    password1 = forms.CharField()
    password2 = forms.CharField()
    class Meta:
        model = CustomUser
        fields = ('username', 'phone_number', 'date_of_birth')

Advanced Usage

DjangoEssentials aims to provide more utilities and helpers over time, driven by community contributions and the evolving needs of Django developers.

Contributing

We welcome contributions from the community, whether it's adding new features, improving documentation, or reporting bugs. Please follow these steps to contribute:

Fork the repository.

  • Create your feature branch (git checkout -b feature/AmazingFeature).
  • Commit your changes (git commit -am 'Add some AmazingFeature').
  • Push to the branch (git push origin feature/AmazingFeature).
  • Open a Pull Request.

Contact

For questions or additional information, please reach out to codermungan@gmail.com

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

DjangoEssentials-0.1.5.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

DjangoEssentials-0.1.5-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file DjangoEssentials-0.1.5.tar.gz.

File metadata

  • Download URL: DjangoEssentials-0.1.5.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.0

File hashes

Hashes for DjangoEssentials-0.1.5.tar.gz
Algorithm Hash digest
SHA256 fe6e781e08d74ed326c3d40a278bad8ca1a7761540987b2debc416a13567b196
MD5 e5e499581db2357093cc004e3a8bd0e5
BLAKE2b-256 3de9187a9d1b0ce904c9da9461f968eaeb68252ade38fc459ef0607894857e11

See more details on using hashes here.

File details

Details for the file DjangoEssentials-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for DjangoEssentials-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 e5d2c003be885eed471a25aea8254fe2982d920f0ffe5da7460014206b2e07da
MD5 81f2055e323f5acf19a0b49001aac7ef
BLAKE2b-256 17112e9b388435a82eb1e7fb8aca6b0ad3832d0f1e845271ea7417fbd3b4d749

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page