diff --git a/docmanager_backend/config/settings.py b/docmanager_backend/config/settings.py index 9dd1854..b75ca76 100644 --- a/docmanager_backend/config/settings.py +++ b/docmanager_backend/config/settings.py @@ -88,6 +88,7 @@ INSTALLED_APPS = [ "corsheaders", "drf_spectacular", "drf_spectacular_sidecar", + "emails", "accounts", "documents", "document_requests", @@ -110,7 +111,9 @@ ROOT_URLCONF = "config.urls" TEMPLATES = [ { "BACKEND": "django.template.backends.django.DjangoTemplates", - "DIRS": [], + "DIRS": [ + BASE_DIR / "emails/templates/", + ], "APP_DIRS": True, "OPTIONS": { "context_processors": [ diff --git a/docmanager_backend/document_requests/admin.py b/docmanager_backend/document_requests/admin.py index c7100a0..43d7daf 100644 --- a/docmanager_backend/document_requests/admin.py +++ b/docmanager_backend/document_requests/admin.py @@ -12,7 +12,7 @@ class DocumentRequestUnitAdmin(ModelAdmin): list_display = ["id", "get_document_title", "copies"] 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" diff --git a/docmanager_backend/document_requests/serializers.py b/docmanager_backend/document_requests/serializers.py index a759d67..efb5613 100644 --- a/docmanager_backend/document_requests/serializers.py +++ b/docmanager_backend/document_requests/serializers.py @@ -2,6 +2,7 @@ from rest_framework import serializers from documents.models import Document from documents.serializers import DocumentSerializer, DocumentFileSerializer from accounts.models import CustomUser +from emails.templates import RequestUpdateEmail from .models import DocumentRequest, DocumentRequestUnit @@ -116,4 +117,13 @@ class DocumentRequestUpdateSerializer(serializers.ModelSerializer): {"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 diff --git a/docmanager_backend/documents/serializers.py b/docmanager_backend/documents/serializers.py index 394141e..492f038 100644 --- a/docmanager_backend/documents/serializers.py +++ b/docmanager_backend/documents/serializers.py @@ -1,10 +1,9 @@ from rest_framework import serializers -from config import settings from .models import Document class DocumentUploadSerializer(serializers.ModelSerializer): - # For staff + # For staff document uploads file = serializers.FileField() date_uploaded = serializers.DateTimeField( format="%m-%d-%Y %I:%M %p", read_only=True @@ -30,14 +29,15 @@ class DocumentDeleteSerializer(serializers.ModelSerializer): class DocumentSerializer(serializers.ModelSerializer): - # Read-only serializer + # Read-only serializer without link to the file date_uploaded = serializers.DateTimeField( format="%m-%d-%Y %I:%M %p", read_only=True ) class Meta: model = Document - fields = ["id", "name", "document_type", "number_pages", "date_uploaded"] + fields = ["id", "name", "document_type", + "number_pages", "date_uploaded"] read_only_fields = [ "id", "name", diff --git a/docmanager_backend/emails/__init__.py b/docmanager_backend/emails/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/docmanager_backend/emails/apps.py b/docmanager_backend/emails/apps.py new file mode 100644 index 0000000..e28480c --- /dev/null +++ b/docmanager_backend/emails/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class EmailsConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "emails" diff --git a/docmanager_backend/emails/migrations/__init__.py b/docmanager_backend/emails/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/docmanager_backend/emails/templates.py b/docmanager_backend/emails/templates.py new file mode 100644 index 0000000..acae072 --- /dev/null +++ b/docmanager_backend/emails/templates.py @@ -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 diff --git a/docmanager_backend/emails/templates/request_approved.html b/docmanager_backend/emails/templates/request_approved.html new file mode 100644 index 0000000..7dd8e8f --- /dev/null +++ b/docmanager_backend/emails/templates/request_approved.html @@ -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 %} +
+ {% blocktrans %}You're receiving this email because your document request has been {{ request_status }}.{% endblocktrans %} +
+ ++ {% trans 'Please visit the site to check your request:' %} +
+ +{% endblock %} diff --git a/docmanager_backend/questionnaires/__init__.py b/docmanager_backend/questionnaires/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/docmanager_backend/questionnaires/admin.py b/docmanager_backend/questionnaires/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/docmanager_backend/questionnaires/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/docmanager_backend/questionnaires/apps.py b/docmanager_backend/questionnaires/apps.py new file mode 100644 index 0000000..baf7892 --- /dev/null +++ b/docmanager_backend/questionnaires/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class QuestionnairesConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "questionnaires" diff --git a/docmanager_backend/questionnaires/migrations/__init__.py b/docmanager_backend/questionnaires/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/docmanager_backend/questionnaires/models.py b/docmanager_backend/questionnaires/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/docmanager_backend/questionnaires/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/docmanager_backend/questionnaires/views.py b/docmanager_backend/questionnaires/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/docmanager_backend/questionnaires/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here.