Add email template for request status update, fix admin view for request unit view, and add initial app for questionnaires

This commit is contained in:
Keannu Bernasol 2024-11-24 02:56:36 +08:00
parent ba19412d31
commit 8bd8df9042
15 changed files with 79 additions and 7 deletions

View file

@ -88,6 +88,7 @@ INSTALLED_APPS = [
"corsheaders", "corsheaders",
"drf_spectacular", "drf_spectacular",
"drf_spectacular_sidecar", "drf_spectacular_sidecar",
"emails",
"accounts", "accounts",
"documents", "documents",
"document_requests", "document_requests",
@ -110,7 +111,9 @@ ROOT_URLCONF = "config.urls"
TEMPLATES = [ TEMPLATES = [
{ {
"BACKEND": "django.template.backends.django.DjangoTemplates", "BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [], "DIRS": [
BASE_DIR / "emails/templates/",
],
"APP_DIRS": True, "APP_DIRS": True,
"OPTIONS": { "OPTIONS": {
"context_processors": [ "context_processors": [

View file

@ -12,7 +12,7 @@ class DocumentRequestUnitAdmin(ModelAdmin):
list_display = ["id", "get_document_title", "copies"] list_display = ["id", "get_document_title", "copies"]
def get_document_title(self, obj): def get_document_title(self, obj):
return obj.documents.title # Assuming the Document model has a 'title' field return obj.document.name
get_document_title.short_description = "Document" get_document_title.short_description = "Document"

View file

@ -2,6 +2,7 @@ from rest_framework import serializers
from documents.models import Document from documents.models import Document
from documents.serializers import DocumentSerializer, DocumentFileSerializer from documents.serializers import DocumentSerializer, DocumentFileSerializer
from accounts.models import CustomUser from accounts.models import CustomUser
from emails.templates import RequestUpdateEmail
from .models import DocumentRequest, DocumentRequestUnit from .models import DocumentRequest, DocumentRequestUnit
@ -116,4 +117,13 @@ class DocumentRequestUpdateSerializer(serializers.ModelSerializer):
{"error": "Request form status provided is the same as current status"} {"error": "Request form status provided is the same as current status"}
) )
return super().update(instance, validated_data) representation = super().update(instance, validated_data)
# Send an email on request status update
email = RequestUpdateEmail()
email.context = {
"request_status": instance.status
}
email.send(to=[instance.requester.email])
return representation

View file

@ -1,10 +1,9 @@
from rest_framework import serializers from rest_framework import serializers
from config import settings
from .models import Document from .models import Document
class DocumentUploadSerializer(serializers.ModelSerializer): class DocumentUploadSerializer(serializers.ModelSerializer):
# For staff # For staff document uploads
file = serializers.FileField() file = serializers.FileField()
date_uploaded = serializers.DateTimeField( date_uploaded = serializers.DateTimeField(
format="%m-%d-%Y %I:%M %p", read_only=True format="%m-%d-%Y %I:%M %p", read_only=True
@ -30,14 +29,15 @@ class DocumentDeleteSerializer(serializers.ModelSerializer):
class DocumentSerializer(serializers.ModelSerializer): class DocumentSerializer(serializers.ModelSerializer):
# Read-only serializer # Read-only serializer without link to the file
date_uploaded = serializers.DateTimeField( date_uploaded = serializers.DateTimeField(
format="%m-%d-%Y %I:%M %p", read_only=True format="%m-%d-%Y %I:%M %p", read_only=True
) )
class Meta: class Meta:
model = Document model = Document
fields = ["id", "name", "document_type", "number_pages", "date_uploaded"] fields = ["id", "name", "document_type",
"number_pages", "date_uploaded"]
read_only_fields = [ read_only_fields = [
"id", "id",
"name", "name",

View file

View file

@ -0,0 +1,6 @@
from django.apps import AppConfig
class EmailsConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "emails"

View file

@ -0,0 +1,13 @@
from djoser import email
from config.settings import FRONTEND_URL
class RequestUpdateEmail(email.BaseEmailMessage):
template_name = "request_approved.html"
def get_context_data(self):
context = super().get_context_data()
context["request_status"] = context.get("request_status")
context["url"] = FRONTEND_URL
context.update(self.context)
return context

View file

@ -0,0 +1,25 @@
{% load i18n %}
{% block subject %}
{% blocktrans %}USTP - Document Request Form Update{% endblocktrans %}
{% endblock %}
{% block text_body %}
{% blocktrans %}You're receiving this email because your document request has been {{ request_status }}.{% endblocktrans %}
{% trans 'Please visit the site to check your request:' %}
{{ url|safe }}
{% endblock %}
{% block html_body %}
<p>
{% blocktrans %}You're receiving this email because your document request has been {{ request_status }}.{% endblocktrans %}
</p>
<p>
{% trans 'Please visit the site to check your request:' %}
</p>
<p>
<a href="{{ url|safe }}"></a>
</p>
{% endblock %}

View file

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

View file

@ -0,0 +1,6 @@
from django.apps import AppConfig
class QuestionnairesConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "questionnaires"

View file

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

View file

@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.