Files
ihrm/horilla_documents/models.py

138 lines
4.6 KiB
Python
Raw Normal View History

import os
from datetime import date
[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
from django.apps import apps
from django.db import models
from django.db.models.signals import m2m_changed, post_save
from django.dispatch import receiver
from django.forms import ValidationError
from django.utils.translation import gettext as _
from base.horilla_company_manager import HorillaCompanyManager
from employee.models import Employee
from horilla.models import HorillaModel
STATUS = [
2024-01-31 12:14:51 +05:30
("requested", "Requested"),
("approved", "Approved"),
("rejected", "Rejected"),
]
FORMATS = [
2024-01-31 12:14:51 +05:30
("any", "Any"),
("pdf", "PDF"),
("txt", "TXT"),
("docx", "DOCX"),
("xlsx", "XLSX"),
("jpg", "JPG"),
("png", "PNG"),
("jpeg", "JPEG"),
]
2024-01-31 12:14:51 +05:30
def document_create(instance):
employees = instance.employee_id.all()
for employee in employees:
document = Document.objects.get_or_create(
employee_id=employee,
2024-01-31 12:14:51 +05:30
document_request_id=instance,
defaults={"title": f"Upload {instance.title}"},
)
document[0].title = f"Upload {instance.title}"
document[0].save()
2024-01-31 12:14:51 +05:30
class DocumentRequest(HorillaModel):
title = models.CharField(max_length=100)
employee_id = models.ManyToManyField(Employee)
2024-01-31 12:14:51 +05:30
format = models.CharField(choices=FORMATS, max_length=10)
max_size = models.IntegerField(blank=True, null=True)
description = models.TextField(blank=True, null=True, max_length=255)
objects = HorillaCompanyManager(
related_company_field="employee_id__employee_work_info__company_id"
)
2024-01-31 12:14:51 +05:30
def __str__(self):
return self.title
2024-01-31 12:14:51 +05:30
@receiver(m2m_changed, sender=DocumentRequest.employee_id.through)
def document_request_m2m_changed(sender, instance, action, **kwargs):
2024-01-31 12:14:51 +05:30
if action == "post_add":
document_create(instance)
2024-01-31 12:14:51 +05:30
elif action == "post_remove":
document_create(instance)
class Document(HorillaModel):
title = models.CharField(max_length=250)
2024-01-31 12:14:51 +05:30
employee_id = models.ForeignKey(Employee, on_delete=models.PROTECT)
document_request_id = models.ForeignKey(
DocumentRequest, on_delete=models.PROTECT, null=True
)
document = models.FileField(upload_to="employee/documents", null=True)
status = models.CharField(choices=STATUS, max_length=10, default="requested")
reject_reason = models.TextField(blank=True, null=True, max_length=255)
expiry_date = models.DateField(null=True, blank=True)
notify_before = models.IntegerField(default=1, null=True)
2024-02-05 11:46:20 +05:30
is_digital_asset = models.BooleanField(default=False)
objects = HorillaCompanyManager(
related_company_field="employee_id__employee_work_info__company_id"
)
def __str__(self) -> str:
return f"{self.title}"
def clean(self, *args, **kwargs):
super().clean(*args, **kwargs)
file = self.document
if len(self.title) < 3:
raise ValidationError({"title": _("Title must be at least 3 characters")})
if file and self.document_request_id:
format = self.document_request_id.format
max_size = self.document_request_id.max_size
if max_size:
if file.size > max_size * 1024 * 1024:
raise ValidationError(
{"document": _("File size exceeds the limit")}
)
2024-01-31 12:14:51 +05:30
ext = file.name.split(".")[1].lower()
if format == "any":
pass
elif ext != format:
raise ValidationError(
{"document": _("Please upload {} file only.").format(format)}
)
2024-01-31 12:14:51 +05:30
def save(self, *args, **kwargs):
2024-02-05 11:46:20 +05:30
super().save(*args, **kwargs)
if self.is_digital_asset:
[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
if apps.is_installed("asset"):
from asset.models import Asset, AssetCategory
asset_category = AssetCategory.objects.get_or_create(
asset_category_name="Digital Asset"
)
Asset.objects.create(
asset_name=self.title,
asset_purchase_date=date.today(),
asset_category_id=asset_category[0],
asset_status="Not-Available",
asset_purchase_cost=0,
expiry_date=self.expiry_date,
notify_before=self.notify_before,
asset_tracking_id=f"DIG_ID0{self.pk}",
)
2024-01-31 12:14:51 +05:30
def upload_documents_count(self):
total_requests = Document.objects.filter(
document_request_id=self.document_request_id
)
without_documents = total_requests.filter(document="").count()
2024-01-31 12:14:51 +05:30
count = total_requests.count() - without_documents
return count