mirror of
https://github.com/lemeow125/DocManagerBackend.git
synced 2025-04-28 10:41:23 +08:00
Implement major changes
This commit is contained in:
parent
60eadbed64
commit
63f3bd0eab
16 changed files with 357 additions and 40 deletions
|
@ -1,6 +1,6 @@
|
|||
from django.contrib import admin
|
||||
from unfold.admin import ModelAdmin
|
||||
from .models import AuthorizationRequest
|
||||
from .models import AuthorizationRequest, AuthorizationRequestUnit
|
||||
|
||||
# Register your models here.
|
||||
|
||||
|
@ -9,3 +9,9 @@ from .models import AuthorizationRequest
|
|||
class AuthorizationRequestAdmin(ModelAdmin):
|
||||
search_fields = ["id"]
|
||||
list_display = ["id", "date_requested", "status", "college"]
|
||||
|
||||
|
||||
@admin.register(AuthorizationRequestUnit)
|
||||
class AuthorizationRequestUnitAdmin(ModelAdmin):
|
||||
search_fields = ["id"]
|
||||
list_display = ["id", "status", "document", "pages", "copies"]
|
||||
|
|
|
@ -0,0 +1,63 @@
|
|||
# Generated by Django 5.1.3 on 2025-01-17 15:41
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("authorization_requests", "0001_initial"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name="authorizationrequest",
|
||||
name="documents",
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name="authorizationrequest",
|
||||
name="status",
|
||||
field=models.CharField(
|
||||
choices=[
|
||||
("pending", "Pending"),
|
||||
("approved", "Approved"),
|
||||
("denied", "Denied"),
|
||||
("claimed", "Claimed"),
|
||||
("unclaimed", "Unclaimed"),
|
||||
],
|
||||
default="pending",
|
||||
max_length=32,
|
||||
),
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name="AuthorizationRequestUnit",
|
||||
fields=[
|
||||
(
|
||||
"id",
|
||||
models.BigAutoField(
|
||||
auto_created=True,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
verbose_name="ID",
|
||||
),
|
||||
),
|
||||
("document", models.TextField(max_length=256)),
|
||||
("copies", models.IntegerField(default=1)),
|
||||
(
|
||||
"authorization_request",
|
||||
models.ForeignKey(
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
to="authorization_requests.authorizationrequest",
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name="authorizationrequest",
|
||||
name="documents",
|
||||
field=models.ManyToManyField(
|
||||
to="authorization_requests.authorizationrequestunit"
|
||||
),
|
||||
),
|
||||
]
|
|
@ -0,0 +1,21 @@
|
|||
# Generated by Django 5.1.3 on 2025-01-17 16:11
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
(
|
||||
"authorization_requests",
|
||||
"0002_remove_authorizationrequest_documents_and_more",
|
||||
),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name="authorizationrequestunit",
|
||||
name="pages",
|
||||
field=models.IntegerField(default=1),
|
||||
),
|
||||
]
|
|
@ -0,0 +1,22 @@
|
|||
# Generated by Django 5.1.3 on 2025-01-19 10:33
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("authorization_requests", "0003_authorizationrequestunit_pages"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name="authorizationrequestunit",
|
||||
name="status",
|
||||
field=models.CharField(
|
||||
choices=[("pending", "Pending"), ("checked", "Checked")],
|
||||
default="pending",
|
||||
max_length=32,
|
||||
),
|
||||
),
|
||||
]
|
|
@ -2,10 +2,27 @@ from django.db import models
|
|||
from django.utils.timezone import now
|
||||
|
||||
|
||||
class AuthorizationRequestUnit(models.Model):
|
||||
authorization_request = models.ForeignKey(
|
||||
"authorization_requests.AuthorizationRequest", on_delete=models.CASCADE
|
||||
)
|
||||
document = models.TextField(max_length=256)
|
||||
pages = models.IntegerField(default=1, null=False, blank=False)
|
||||
copies = models.IntegerField(default=1, null=False, blank=False)
|
||||
STATUS_CHOICES = (
|
||||
("pending", "Pending"),
|
||||
("checked", "Checked"),
|
||||
)
|
||||
|
||||
status = models.CharField(
|
||||
max_length=32, choices=STATUS_CHOICES, default="pending")
|
||||
|
||||
|
||||
class AuthorizationRequest(models.Model):
|
||||
requester = models.ForeignKey(
|
||||
"accounts.CustomUser", on_delete=models.CASCADE)
|
||||
documents = models.TextField(max_length=2048, blank=False, null=False)
|
||||
documents = models.ManyToManyField(
|
||||
"authorization_requests.AuthorizationRequestUnit")
|
||||
date_requested = models.DateTimeField(default=now, editable=False)
|
||||
college = models.CharField(max_length=64, blank=False, null=False)
|
||||
purpose = models.TextField(max_length=512, blank=False, null=False)
|
||||
|
@ -14,6 +31,8 @@ class AuthorizationRequest(models.Model):
|
|||
("pending", "Pending"),
|
||||
("approved", "Approved"),
|
||||
("denied", "Denied"),
|
||||
("claimed", "Claimed"),
|
||||
("unclaimed", "Unclaimed"),
|
||||
)
|
||||
|
||||
remarks = models.TextField(max_length=512, blank=True, null=True)
|
||||
|
|
|
@ -1,14 +1,33 @@
|
|||
from rest_framework import serializers
|
||||
from accounts.models import CustomUser
|
||||
from emails.templates import RequestUpdateEmail
|
||||
from .models import AuthorizationRequest
|
||||
from .models import AuthorizationRequest, AuthorizationRequestUnit
|
||||
|
||||
|
||||
class AuthorizationRequestUnitCreationSerializer(serializers.ModelSerializer):
|
||||
document = serializers.CharField()
|
||||
copies = serializers.IntegerField(min_value=1)
|
||||
pages = serializers.IntegerField(min_value=1)
|
||||
|
||||
class Meta:
|
||||
model = AuthorizationRequestUnit
|
||||
fields = ["document", "copies", "pages", "status"]
|
||||
|
||||
|
||||
class AuthorizationRequestUnitSerializer(serializers.ModelSerializer):
|
||||
|
||||
class Meta:
|
||||
model = AuthorizationRequestUnit
|
||||
fields = ["id", "document", "status", "copies", "pages"]
|
||||
read_only_fields = ["id", "document", "status," "copies", "pages"]
|
||||
|
||||
|
||||
class AuthorizationRequestCreationSerializer(serializers.ModelSerializer):
|
||||
requester = serializers.SlugRelatedField(
|
||||
many=False, slug_field="id", queryset=CustomUser.objects.all(), required=False
|
||||
)
|
||||
documents = serializers.CharField(max_length=2048, required=True)
|
||||
documents = AuthorizationRequestUnitCreationSerializer(
|
||||
many=True, required=True)
|
||||
college = serializers.CharField(max_length=64)
|
||||
purpose = serializers.CharField(max_length=512)
|
||||
|
||||
|
@ -18,11 +37,31 @@ class AuthorizationRequestCreationSerializer(serializers.ModelSerializer):
|
|||
|
||||
def create(self, validated_data):
|
||||
user = self.context["request"].user
|
||||
|
||||
documents_data = validated_data.pop("documents")
|
||||
if not documents_data:
|
||||
raise serializers.ValidationError(
|
||||
{"error": "No documents provided"}
|
||||
)
|
||||
# Set requester to user who sent HTTP request to prevent spoofing
|
||||
validated_data["requester"] = user
|
||||
|
||||
return AuthorizationRequest.objects.create(**validated_data)
|
||||
AUTHORIZATION_REQUEST = AuthorizationRequest.objects.create(
|
||||
**validated_data)
|
||||
|
||||
AUTHORIZATION_REQUEST_UNITS = []
|
||||
for AUTHORIZATION_REQUEST_UNIT in documents_data:
|
||||
AUTHORIZATION_REQUEST_UNIT = AuthorizationRequestUnit.objects.create(
|
||||
authorization_request=AUTHORIZATION_REQUEST,
|
||||
document=AUTHORIZATION_REQUEST_UNIT["document"],
|
||||
copies=AUTHORIZATION_REQUEST_UNIT["copies"],
|
||||
pages=AUTHORIZATION_REQUEST_UNIT["pages"]
|
||||
)
|
||||
AUTHORIZATION_REQUEST_UNITS.append(AUTHORIZATION_REQUEST_UNIT)
|
||||
|
||||
AUTHORIZATION_REQUEST.documents.set(AUTHORIZATION_REQUEST_UNITS)
|
||||
AUTHORIZATION_REQUEST.save()
|
||||
|
||||
return AUTHORIZATION_REQUEST
|
||||
|
||||
|
||||
class AuthorizationRequestSerializer(serializers.ModelSerializer):
|
||||
|
@ -34,6 +73,7 @@ class AuthorizationRequestSerializer(serializers.ModelSerializer):
|
|||
date_requested = serializers.DateTimeField(
|
||||
format="%m-%d-%Y %I:%M %p", read_only=True
|
||||
)
|
||||
documents = AuthorizationRequestUnitSerializer(many=True)
|
||||
|
||||
class Meta:
|
||||
model = AuthorizationRequest
|
||||
|
@ -59,6 +99,59 @@ class AuthorizationRequestSerializer(serializers.ModelSerializer):
|
|||
]
|
||||
|
||||
|
||||
class AuthorizationRequestUnitUpdateSerializer(serializers.ModelSerializer):
|
||||
status = serializers.ChoiceField(
|
||||
choices=AuthorizationRequestUnit.STATUS_CHOICES, required=True
|
||||
)
|
||||
|
||||
class Meta:
|
||||
model = AuthorizationRequestUnit
|
||||
fields = ["id", "status"]
|
||||
read_only_fields = ["id"]
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
if instance.authorization_request.status != "pending":
|
||||
raise serializers.ValidationError(
|
||||
{
|
||||
"error": "Already approved/denied requests cannot be updated. You should instead create a new request and approve it from there"
|
||||
}
|
||||
)
|
||||
if instance.status == "checked":
|
||||
raise serializers.ValidationError(
|
||||
{
|
||||
"error": "Already approved/denied request units cannot be updated. You should instead create a new request and approve it from there"
|
||||
}
|
||||
)
|
||||
elif "status" not in validated_data:
|
||||
raise serializers.ValidationError(
|
||||
{
|
||||
"error": "No status value update provided"
|
||||
}
|
||||
)
|
||||
elif validated_data["status"] == instance.status:
|
||||
raise serializers.ValidationError(
|
||||
{"error": "Request unit status provided is the same as current status"}
|
||||
)
|
||||
representation = super().update(instance, validated_data)
|
||||
|
||||
# Check if the parent Authorization Request has had all its documents approved
|
||||
approved_all = True
|
||||
for AUTHORIZATION_REQUEST_UNIT in instance.authorization_request.documents.all():
|
||||
if AUTHORIZATION_REQUEST_UNIT.status != "checked":
|
||||
approved_all = False
|
||||
|
||||
# If all documents have been checked
|
||||
if approved_all:
|
||||
# Set the parent request as approved
|
||||
instance.authorization_request.status = "approved"
|
||||
instance.authorization_request.save()
|
||||
# And send an email notification
|
||||
email = RequestUpdateEmail()
|
||||
email.context = {"request_status": "approved"}
|
||||
email.send(to=[instance.authorization_request.requester.email])
|
||||
return representation
|
||||
|
||||
|
||||
class AuthorizationRequestUpdateSerializer(serializers.ModelSerializer):
|
||||
status = serializers.ChoiceField(
|
||||
choices=AuthorizationRequest.STATUS_CHOICES, required=True
|
||||
|
@ -70,23 +163,30 @@ class AuthorizationRequestUpdateSerializer(serializers.ModelSerializer):
|
|||
read_only_fields = ["id"]
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
print(validated_data)
|
||||
if instance.status == "denied" or instance.status == "approved":
|
||||
raise serializers.ValidationError(
|
||||
{
|
||||
"error": "Already approved/denied requests cannot be updated. You should instead create a new request and approve it from there"
|
||||
}
|
||||
)
|
||||
elif "status" not in validated_data:
|
||||
if "status" not in validated_data:
|
||||
raise serializers.ValidationError(
|
||||
{
|
||||
"error": "No status value update provided"
|
||||
}
|
||||
)
|
||||
elif instance.status == "denied" or instance.status == "claimed":
|
||||
raise serializers.ValidationError(
|
||||
{
|
||||
"error": "Already claimed/denied requests cannot be updated. You should instead create a new request and approve it from there"
|
||||
}
|
||||
)
|
||||
elif validated_data["status"] == instance.status:
|
||||
raise serializers.ValidationError(
|
||||
{"error": "Request form status provided is the same as current status"}
|
||||
)
|
||||
elif instance.status == "approved" and validated_data["status"] not in ["claimed", "unclaimed"]:
|
||||
raise serializers.ValidationError(
|
||||
{"error": "Approved request forms can only be marked as claimed or unclaimed"}
|
||||
)
|
||||
elif instance.status == "unclaimed" and validated_data["status"] not in ["claimed"]:
|
||||
raise serializers.ValidationError(
|
||||
{"error": "Unclaimed request forms can only be marked as claimed"}
|
||||
)
|
||||
elif validated_data["status"] == "denied" and "remarks" not in validated_data:
|
||||
raise serializers.ValidationError(
|
||||
{"error": "Request denial requires remarks"}
|
||||
|
@ -96,14 +196,16 @@ class AuthorizationRequestUpdateSerializer(serializers.ModelSerializer):
|
|||
# Send an email on request status update
|
||||
try:
|
||||
email = RequestUpdateEmail()
|
||||
email.context = {"request_status": validated_data["status"]}
|
||||
if validated_data["status"] == "denied":
|
||||
email.context = {"request_status": "denied"}
|
||||
email.context = {"remarks": validated_data["remarks"]}
|
||||
else:
|
||||
email.context = {"request_status": "approved"}
|
||||
email.context = {"remarks": "N/A"}
|
||||
email.send(to=[instance.requester.email])
|
||||
except:
|
||||
except Exception as e:
|
||||
# Silence out errors if email sending fails
|
||||
print(e)
|
||||
pass
|
||||
|
||||
return representation
|
||||
|
|
|
@ -3,10 +3,13 @@ from .views import (
|
|||
AuthorizationRequestCreateView,
|
||||
AuthorizationRequestUpdateView,
|
||||
AuthorizationRequestListView,
|
||||
AuthorizationRequestUnitUpdateView
|
||||
)
|
||||
|
||||
urlpatterns = [
|
||||
path("create/", AuthorizationRequestCreateView.as_view()),
|
||||
path("list/", AuthorizationRequestListView.as_view()),
|
||||
path("update/<int:pk>/", AuthorizationRequestUpdateView.as_view()),
|
||||
path("authorization_request_units/update/<int:pk>/",
|
||||
AuthorizationRequestUnitUpdateView.as_view()),
|
||||
]
|
||||
|
|
|
@ -6,10 +6,11 @@ from rest_framework.pagination import PageNumberPagination
|
|||
from .serializers import (
|
||||
AuthorizationRequestCreationSerializer,
|
||||
AuthorizationRequestSerializer,
|
||||
AuthorizationRequestUpdateSerializer
|
||||
AuthorizationRequestUpdateSerializer,
|
||||
AuthorizationRequestUnitUpdateSerializer
|
||||
)
|
||||
|
||||
from .models import AuthorizationRequest
|
||||
from .models import AuthorizationRequest, AuthorizationRequestUnit
|
||||
|
||||
|
||||
class AuthorizationRequestCreateView(generics.CreateAPIView):
|
||||
|
@ -51,3 +52,14 @@ class AuthorizationRequestUpdateView(generics.UpdateAPIView):
|
|||
serializer_class = AuthorizationRequestUpdateSerializer
|
||||
permission_classes = [IsAuthenticated, IsHead]
|
||||
queryset = AuthorizationRequest.objects.all()
|
||||
|
||||
|
||||
class AuthorizationRequestUnitUpdateView(generics.UpdateAPIView):
|
||||
"""
|
||||
Used by head approve or deny authorization request units.
|
||||
"""
|
||||
|
||||
http_method_names = ["patch"]
|
||||
serializer_class = AuthorizationRequestUnitUpdateSerializer
|
||||
permission_classes = [IsAuthenticated, IsHead]
|
||||
queryset = AuthorizationRequestUnit.objects.all()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue