[ADD] HORILLA DOCUMENTS: App to manage documents

This commit is contained in:
Horilla
2024-01-23 15:21:14 +05:30
parent c464bf3195
commit 66d6612b26
8 changed files with 179 additions and 0 deletions

View File

View File

@@ -0,0 +1,8 @@
from django.contrib import admin
from horilla_documents.models import Document, DocumentRequest
# Register your models here.
admin.site.register(Document)
admin.site.register(DocumentRequest)

View File

@@ -0,0 +1,6 @@
from django.apps import AppConfig
class HorillaDoumentsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'horilla_documents'

View File

@@ -0,0 +1,50 @@
from django import forms
from horilla_documents.models import Document, DocumentRequest
from base.forms import ModelForm
class DocumentRequestForm(ModelForm):
""" form to create a new Document Request """
# employee_id = forms.ModelMultipleChoiceField(
# queryset=DocumentRequest._meta.get_field('employee').remote_field.model.objects.all(),
# required=False,
# )
class Meta:
model = DocumentRequest
fields = '__all__'
# def save(self, commit=True):
# instance = super().save(commit=False)
# # Handle the new field name in cleaned_data
# instance.employee.set(self.cleaned_data['employee_id'])
# if commit:
# instance.save()
# return instance
class DocumentForm(ModelForm):
""" form to create a new Document"""
class Meta:
model = Document
fields = "__all__"
exclude = ["document_request_id","status","reject_reason","is_active"]
widgets = {
"employee_id": forms.HiddenInput(),
}
class DocumentUpdateForm(ModelForm):
""" form to Update a Document"""
class Meta:
model = Document
fields = "__all__"
class DocumentRejectForm(ModelForm):
""" form to add rejection reason while rejecting a Document"""
class Meta:
model = Document
fields = ["reject_reason"]

View File

112
horilla_documents/models.py Normal file
View File

@@ -0,0 +1,112 @@
from datetime import date
import os
from django.db import models
from django.forms import ValidationError
from django.utils.translation import gettext as _
from asset.models import Asset, AssetCategory
from base.horilla_company_manager import HorillaCompanyManager
from employee.models import Employee
from django.db.models.signals import post_save, m2m_changed
from django.dispatch import receiver
STATUS = [
('requested', 'Requested'),
('approved', 'Approved'),
('rejected', 'Rejected'),
]
FORMATS = [
('any', 'Any'),
('pdf', 'PDF'),
('txt', 'TXT'),
('docx', 'DOCX'),
('xlsx', 'XLSX'),
('jpg', 'JPG'),
('png', 'PNG'),
('jpeg', 'JPEG'),
]
def document_create(instance):
employees = instance.employee_id.all()
for employee in employees:
Document.objects.get_or_create(
title = f"Upload {instance.title}",
employee_id=employee,
document_request_id = instance
)
class DocumentRequest(models.Model):
title = models.CharField(max_length=100)
employee_id = models.ManyToManyField(Employee)
format = models.CharField(choices=FORMATS,max_length=10)
max_size = models.IntegerField(blank=True,null=True)
description = models.TextField(blank=True,null=True)
objects = HorillaCompanyManager()
is_active = models.BooleanField(default=True)
def __str__ (self):
return self.title
@receiver(post_save, sender=DocumentRequest)
def doc_request_post_save(sender, instance, **kwargs):
document_create(instance)
@receiver(m2m_changed, sender=DocumentRequest.employee_id.through)
def your_model_m2m_changed(sender, instance, action, **kwargs):
if action == 'post_add':
document_create(instance)
elif action == 'post_remove':
document_create(instance)
class Document(models.Model):
title = models.CharField(max_length=25)
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)
is_digital_asset = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
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 file and self.document_request_id:
format = self.document_request_id.format
max_size = self.document_request_id.max_size
if file.size > max_size * 1024 * 1024:
raise ValidationError("File size exceeds the limit")
ext = file.name.split(".")[1].lower()
if format == "any":
pass
elif ext != format:
raise ValidationError(f"Please upload {format} file only.")
def save(self,*args, **kwargs):
if len(self.title)<3:
raise ValidationError(_("Title must be at least 3 characters"))
if self.is_digital_asset:
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,
)
super().save(*args, **kwargs)

View File

@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

View File