Files
ihrm/horilla/models.py
Horilla 2fee7c18bb [IMP] Remove inter module dependency (#274)
This commit introduces significant changes to the architecture of the Horilla HRMS system by decoupling interdependent modules. The following modifications were made:

1. **Module Independence**: Each module has been refactored to eliminate reliance on other modules, promoting a more modular and maintainable codebase.
2. **Refactored Imports and Dependencies**: Adjusted import statements and dependency injections to support independent module operation.
3. **Compatibility and Functionality**: Ensured that all modules are compatible with existing systems and maintain their intended functionality both independently and when integrated with other modules.

These changes enhance the modularity, maintainability, and scalability of the Horilla HRMS, allowing developers to work on individual modules without affecting the entire system. Future development and deployment will be more efficient and less prone to issues arising from tightly coupled code.

**NOTE**
For existing Horilla users, if you face any issues during the migrations, please run the following command and try again the migrations.

- `python3 manage.py makemigrations`
- `python3 manage.py migrate base`
- `python3 manage.py migrate`





* [IMP] ASSET: Asset module dependency removal from other Horilla apps

* [IMP] ATTENDANCE: Attendance module dependency removal from other Horilla apps

* [IMP] BASE: Base module dependency removal from other Horilla apps

* [IMP] EMPLOYEE: Employee module dependency removal from other Horilla apps

* [IMP] HELPDESK: Helpdesk module dependency removal from other Horilla apps

* [IMP] HORILLA AUDIT: Horilla Audit module dependency removal from other Horilla apps

* [IMP] HORILLA CRUMBS: Horilla Crumbs module dependency removal from other Horilla apps

* [IMP] HORILLA AUTOMATIONS: Horilla Automations module dependency removal from other Horilla apps

* [IMP] HORILLA VIEWS: Horilla Views module dependency removal from other Horilla apps

* [IMP] LEAVE: Leave module dependency removal from other Horilla apps

* [IMP] OFFBOARDING: Offboarding module dependency removal from other Horilla apps

* [IMP] ONBOARDING: Onboarding module dependency removal from other Horilla apps

* [IMP] PMS: PMS module dependency removal from other Horilla apps

* [IMP] PAYROLL: Payroll module dependency removal from other Horilla apps

* [IMP] RECRUITMENT: Recruitment module dependency removal from other Horilla apps

* [IMP] HORILLA: Dependency removal updates

* [IMP] TEMPLATES: Dependency removal updates

* [IMP] STATIC: Dependency removal updates

* [IMP] HORILLA DOCUMENTS: Horilla Documents module dependency removal from other Horilla apps

* [ADD] HORILLA: methods.py

* [UPDT] HORILLA: Settings.py

* [FIX] EMPLOYEE: About tab issue

* Update horilla_settings.py

* Remove dummy db init password
2024-08-05 14:22:44 +05:30

126 lines
3.4 KiB
Python

"""
models.py
=========
This module defines the abstract base model `HorillaModel` for the Horilla HRMS project.
The `HorillaModel` provides common fields and functionalities for other models within
the application, such as tracking creation and modification timestamps and user
information, audit logging, and active/inactive status management.
"""
from auditlog.models import AuditlogHistoryField
from auditlog.registry import auditlog
from django.contrib.auth.models import User
from django.db import models
from django.db.models.fields.files import FieldFile
from django.urls import reverse
from django.utils.translation import gettext as _
from horilla.horilla_middlewares import _thread_locals
@property
def url(self: FieldFile):
"""
Custom url attribute/property
"""
try:
self._require_file()
except Exception as e:
return reverse("404")
return self.storage.url(self.name)
setattr(FieldFile, "url", url)
class HorillaModel(models.Model):
"""
An abstract base model that includes common fields and functionalities
for models within the Horilla application.
"""
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"),
)
modified_by = models.ForeignKey(
User,
on_delete=models.SET_NULL,
null=True,
blank=True,
editable=False,
verbose_name=_("Modified By"),
related_name="%(class)s_modified_by",
)
horilla_history = AuditlogHistoryField()
objects = models.Manager()
is_active = models.BooleanField(default=True, verbose_name=_("Is Active"))
class Meta:
"""
Meta class for HorillaModel
"""
abstract = True
def save(self, *args, **kwargs):
"""
Override the save method to automatically set the created_by and
modified_by fields based on the current request user.
"""
request = getattr(_thread_locals, "request", None)
if request:
user = request.user
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
):
if request and not self.pk:
if user.is_authenticated:
self.created_by = user
if request and not request.user.is_anonymous:
self.modified_by = user
super(HorillaModel, self).save(*args, **kwargs)
@classmethod
def find(cls, object_id):
"""
Find an object of this class by its ID.
"""
try:
obj = cls.objects.filter(id=object_id).first()
return obj
except Exception as e:
# Log the exception if needed
return None
@classmethod
def activate_deactivate(cls, object_id):
"""
Toggle the is_active status of an object of this class.
"""
obj = cls.find(object_id)
if obj:
obj.is_active = not obj.is_active
obj.save()
auditlog.register(HorillaModel, serialize_data=True)