From 0ab707f09b24a9629cbed4e92d62923ccc1f2708 Mon Sep 17 00:00:00 2001 From: Horilla Date: Mon, 29 Jan 2024 15:07:13 +0530 Subject: [PATCH] [UPDT] BASE: Announcement New tag removal if announcement viewed by employee --- base/announcement.py | 16 +++++++++++-- base/models.py | 16 +++++++++++++ base/views.py | 53 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 2 deletions(-) diff --git a/base/announcement.py b/base/announcement.py index 5dd5948de..c04afc4e9 100644 --- a/base/announcement.py +++ b/base/announcement.py @@ -1,7 +1,7 @@ from django.http import HttpResponse, HttpResponseRedirect, JsonResponse -from django.shortcuts import redirect, render +from django.shortcuts import get_object_or_404, redirect, render from base.forms import AnnouncementForm, AnnouncementcommentForm -from base.models import Announcement, AnnouncementComment +from base.models import Announcement, AnnouncementComment, AnnouncementView from employee.models import EmployeeWorkInformation from horilla.decorators import login_required from django.contrib import messages @@ -258,4 +258,16 @@ def announcement_single_view(request, anoun_id): """ announcement = Announcement.objects.filter(id=anoun_id) + + for i in announcement: + # Taking the announcement instance + announcement_instance = get_object_or_404(Announcement, id=i.id) + + # Check if the user has viewed the announcement + announcement_view, created = AnnouncementView.objects.get_or_create(user=request.user, announcement=announcement_instance) + + # Update the viewed status + announcement_view.viewed = True + announcement_view.save() + return render(request, "announcement/announcement_one.html", {'announcements': announcement}) diff --git a/base/models.py b/base/models.py index 6030555df..c7d06a287 100644 --- a/base/models.py +++ b/base/models.py @@ -10,6 +10,7 @@ from base.thread_local_middleware import _thread_locals from django.core.exceptions import ValidationError from django.utils.translation import gettext_lazy as _ from django.db import models +from django.contrib.auth.models import User from base.horilla_company_manager import HorillaCompanyManager # Create your models here. @@ -629,6 +630,10 @@ class RotatingShiftAssign(models.Model): raise ValidationError(_("Date must be greater than or equal to today")) +class BaserequestFile(models.Model): + file = models.FileField(upload_to="base/request_files") + + class WorkTypeRequest(models.Model): """ WorkTypeRequest model @@ -748,6 +753,7 @@ class WorktyperequestComment(models.Model): request_id = models.ForeignKey(WorkTypeRequest, on_delete=models.CASCADE) employee_id = models.ForeignKey(Employee, on_delete=models.CASCADE) comment = models.TextField(null=True, verbose_name=_("Comment")) + files = models.ManyToManyField(BaserequestFile,blank=True) created_at = models.DateTimeField( auto_now_add=True, verbose_name=_("Created At"), @@ -882,6 +888,7 @@ class ShiftrequestComment(models.Model): request_id = models.ForeignKey(ShiftRequest, on_delete=models.CASCADE) employee_id = models.ForeignKey(Employee, on_delete=models.CASCADE) + files = models.ManyToManyField(BaserequestFile,blank=True) comment = models.TextField(null=True, verbose_name=_("Comment")) created_at = models.DateTimeField( auto_now_add=True, @@ -1219,6 +1226,15 @@ class AnnouncementComment(models.Model): ) +class AnnouncementView(models.Model): + """ + Announcemnt View Model + """ + user = models.ForeignKey(User, on_delete=models.CASCADE) + announcement = models.ForeignKey(Announcement, on_delete=models.CASCADE) + viewed = models.BooleanField(default=False) + + class EmailLog(models.Model): """ EmailLog Keeping model diff --git a/base/views.py b/base/views.py index f285296b0..0224dee94 100644 --- a/base/views.py +++ b/base/views.py @@ -79,6 +79,8 @@ from base.forms import ( from base.models import ( Announcement, AnnouncementExpire, + AnnouncementView, + BaserequestFile, Company, DynamicEmailConfiguration, DynamicPagination, @@ -400,6 +402,10 @@ def home(request): calculated_expire_date = announcement.created_on + timedelta(days=general_expire_date) announcement.expire_date = calculated_expire_date + # Check if the user has viewed the announcement + announcement_view = AnnouncementView.objects.filter(announcement=announcement, user=request.user).first() + announcement.has_viewed = announcement_view is not None and announcement_view.viewed + context = { "first_day_of_week": first_day_of_week.strftime("%Y-%m-%d"), "last_day_of_week": last_day_of_week.strftime("%Y-%m-%d"), @@ -4074,6 +4080,53 @@ def create_shiftrequest_comment(request, shift_id): ) +@login_required +def view_shift_comment(request, shift_id): + """ + This method is used to render all the notes of the employee + """ + comments = ShiftrequestComment.objects.filter(request_id = shift_id) + if request.FILES: + files = request.FILES.getlist("files") + comment_id = request.GET["comment_id"] + comment = ShiftrequestComment.objects.get(id=comment_id) + attachments = [] + for file in files: + file_instance = BaserequestFile() + file_instance.file = file + file_instance.save() + attachments.append(file_instance) + comment.files.add(*attachments) + return render( + request, + "shift_request/htmx/shift_comment.html", + { + "comments": comments, + "request_id": shift_id, + }, + ) + + +@login_required +@permission_required("offboarding.delete_offboardingnote") +def delete_comment_file(request): + """ + Used to delete attachment + """ + ids = request.GET.getlist("ids") + BaserequestFile.objects.filter(id__in=ids).delete() + shift_id = request.GET["shift_id"] + comments = ShiftrequestComment.objects.filter(request_id = shift_id) + return render( + request, + "shift_request/htmx/shift_comment.html", + { + "comments": comments, + "request_id": shift_id, + }, + ) + + @login_required def view_shiftrequest_comment(request, shift_id): """