* [UPDT] BASE: Updated disciplinary action type model by adding abstract class * [UPDT] BASE: Updated abstract model class save method by fethcing request from thread local * [UPDT] LEAVE: Updated models in leave module by adding abstract class * [UPDT] LEAVE: Updated leave request model by adding abstract class * [UPDT] DASHBOARD: Floating quick action button rather than static template inside dashboard * [UPDT] Test database * [UPDT] ASSET: Updated models in asset app by adding abstract class HorillaModels * [UPDT] ASSET: Updated asset category form by adding exclude fields in class Meta * [FIX] ATTENDANCE: Attendance overtime permission wrong permission * [UPDT] Test Database
39 lines
1.2 KiB
Python
39 lines
1.2 KiB
Python
from django.db import models
|
|
from django.contrib.auth.models import User
|
|
from django.utils.translation import gettext as _
|
|
from base.thread_local_middleware import _thread_locals
|
|
|
|
|
|
class HorillaModel(models.Model):
|
|
created_at = models.DateTimeField(
|
|
auto_now_add=True,
|
|
null=True,
|
|
blank=True,
|
|
verbose_name=_("Created At"),
|
|
)
|
|
created_by = models.ForeignKey(
|
|
User,
|
|
on_delete=models.SET_NULL,
|
|
null=True,
|
|
blank=True,
|
|
editable=False,
|
|
verbose_name=_("Created By"),
|
|
)
|
|
is_active = models.BooleanField(default=True, verbose_name=_("Is Active"))
|
|
|
|
class Meta:
|
|
abstract = True
|
|
|
|
def save(self, *args, **kwargs):
|
|
if (
|
|
hasattr(self, "created_by")
|
|
and hasattr(self._meta.get_field("created_by"), "related_model")
|
|
and self._meta.get_field("created_by").related_model == User
|
|
):
|
|
request = getattr(_thread_locals, "request", None)
|
|
if request and not self.pk:
|
|
user = request.user
|
|
if user.is_authenticated:
|
|
self.created_by = user
|
|
super(HorillaModel, self).save(*args, **kwargs)
|