1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
# -*- coding: utf-8 -*-
import datetime
from django.utils.translation import gettext_lazy as _
from mongoengine.document import Document
from mongoengine.fields import DateTimeField, IntField, StringField
from mongoengine.queryset import QuerySetManager
from django_extensions.mongodb.fields import (
AutoSlugField,
CreationDateTimeField,
ModificationDateTimeField,
)
class TimeStampedModel(Document):
"""
TimeStampedModel
An abstract base class model that provides self-managed "created" and
"modified" fields.
"""
created = CreationDateTimeField()
modified = ModificationDateTimeField()
class Meta:
abstract = True
class TitleSlugDescriptionModel(Document):
"""
TitleSlugDescriptionModel
An abstract base class model that provides title and description fields
and a self-managed "slug" field that populates from the title.
"""
title = StringField(max_length=255)
slug = AutoSlugField(populate_from="title")
description = StringField(blank=True, null=True)
class Meta:
abstract = True
class ActivatorModelManager(QuerySetManager):
"""
ActivatorModelManager
Manager to return instances of ActivatorModel:
SomeModel.objects.active() / .inactive()
"""
def active(self):
"""
Return active instances of ActivatorModel:
SomeModel.objects.active()
"""
return super().get_queryset().filter(status=1)
def inactive(self):
"""
Return inactive instances of ActivatorModel:
SomeModel.objects.inactive()
"""
return super().get_queryset().filter(status=0)
class ActivatorModel(Document):
"""
ActivatorModel
An abstract base class model that provides activate and deactivate fields.
"""
STATUS_CHOICES = (
(0, _("Inactive")),
(1, _("Active")),
)
status = IntField(choices=STATUS_CHOICES, default=1)
activate_date = DateTimeField(
blank=True, null=True, help_text=_("keep empty for an immediate activation")
)
deactivate_date = DateTimeField(
blank=True, null=True, help_text=_("keep empty for indefinite activation")
)
objects = ActivatorModelManager()
class Meta:
abstract = True
def save(self, *args, **kwargs):
if not self.activate_date:
self.activate_date = datetime.datetime.now()
super().save(*args, **kwargs)
|