Models

Swingers’ custom models, abstract models, model managers, etc.

swingers.models.GIS_ENABLED

A constant that indicates whether any of the currently defined databases is GEO-enabled as well as django.contrib.gis being in settings.INSTALLED_APPS.

If GIS_ENABLED, then swingers.base.models imports * from django.contrib.gis.db.models as well as redefines GeoManager as Manager, ActiveGeoModel as ActiveModel and ActiveModelManager as ActiveModelManager. All these redefinitions can be overwritten by importing from the base django/swingers modules. If not GIS_ENABLED, the swingers.base.models imports * from django.db.models and exports ActiveModel and ActiveManager.

Auth

class swingers.models.Audit

Audit is an abstract Django model that handles adding additional auditing attributes to any model that inherits from it. It will automatically version any changes and update the time it was modified and who it was modified by. This makes it easy to see a list of changes and roll back changes that were unintended.

creator

The User that created this model instance.

modifier

The User who last modified this model instance.

created

The datetime that this model instance was created on.

modified

The datetime that this model instance was last modified on.

Fields

class swingers.models.ContentTypeRestrictedField

Extends FileField and adds validation for two additional things:

content types
A list of allowed MIME types.
maximum upload size
The maximum upload size specified in bytes.

To specify either of these things, add them to the constructor of your model field. Example usage:

from django.db import models

class Application(models.Model):
    name = models.CharField(max_length=30)
    approved = models.BooleanField()
    passport_photo = ContentTypeRestrictedField(
        content_types=['image/jpeg'], max_upload_size=10485760)

This will restrict the file uploaded to be a JPEG image of no more than 10MiB.* ContentTypeRestrictedFileField

Managers

class swingers.models.ActiveModelManager

Default model manager for the ActiveModel. It excludes the inactive (“deleted”) objects from the queryset.

class swingers.models.ActiveGeoModelManager

Similar to ActiveModelManager but for Geo models.

Generic

class swingers.models.ActiveModel

A model mixin to allow objects to be saved as ‘non-current’ or ‘inactive’. This will preserve the instance in the database when it is deleted. The standard delete() method is overridden and when an object is removed, sets the models’ effective_to as the current datetime.

effective_from

The time from which this object is effective from. Allows ‘past’ and/or ‘future’ objects to be saved.

Default
now().
effective_to

This is only set once the model has been deleted. When deleted, effective_to is set to the datetime that the model was tagged as deleted on.

objects

A custom objects manager ActiveManager that excludes the ‘inactive’ objects.

objects_all

A default Manager class that includes all objects (including the ‘inactive/deleted’ ones).

is_active()

Returns True if an object is not deleted.

is_deleted()

Returns True if an object is deleted.

class swingers.models.ActiveGeoModel

Similar to ActiveModel but for Geo models.

class swingers.models.DocumentAbstract

Generic class for supporting documents. It can be attached to any other object via GenericForeignKey.

content_type
object_id
content_object
uploaded_file
description
uploaded_file_name()

Return the file name of the uploaded file, minus the server file path.

uploaded_file_ext()

Return the file extension of the uploaded file.

filesize_str()

Return the filesize as a nicely human-readable string (kB, MB, GB, etc.).

class swingers.models.RegionAbstract

Abstract model to represent DPaW regions and district areas, for use within other DPaW corporate applications.

name
description
slug

Geometry models

class swingers.models.PolygonModelMixin

A model mixin to provide a polygon field called the_geom with a default SRID of 4326 (WGS84).

the_geom

The PolygonField for this class.

class swingers.models.LineStringModelMixin

A model mixin to provide a line string spatial field called the_geom with a default SRID of 4326 (WGS84).

the_geom

The LineStringField for this class.

class swingers.models.PointModelMixin

A model mixin to provide a point spatial field called the_geom with a default SRID of 4326 (WGS84).

the_geom

The PointField for this class.

Django-swingers is a library of common utilities, templates and other django customizations used throughout Department of Parks and Wildlife.

Table Of Contents

Related Topics