From 08b76ddbf0faaad86866dcca63291e81ab382410 Mon Sep 17 00:00:00 2001 From: Horilla Date: Thu, 28 Mar 2024 14:28:49 +0530 Subject: [PATCH] [UPDT] BASE: Updated models in base by adding abstract class --- base/admin.py | 8 ++--- base/forms.py | 80 +++++++++++++---------------------------------- base/models.py | 39 ++++++----------------- base/scheduler.py | 2 +- base/views.py | 42 +++++++++++++------------ 5 files changed, 59 insertions(+), 112 deletions(-) diff --git a/base/admin.py b/base/admin.py index 8273c8b0f..551d773cb 100644 --- a/base/admin.py +++ b/base/admin.py @@ -22,7 +22,7 @@ from base.models import ( EmployeeShiftDay, EmployeeType, MultipleApprovalManagers, - ShiftrequestComment, + ShiftRequestComment, Tags, WorkType, RotatingWorkType, @@ -31,7 +31,7 @@ from base.models import ( RotatingShiftAssign, ShiftRequest, WorkTypeRequest, - WorktyperequestComment, + WorkTypeRequestComment, DashboardEmployeeCharts, ) @@ -55,8 +55,8 @@ admin.site.register(WorkTypeRequest) admin.site.register(Tags) admin.site.register(DynamicEmailConfiguration) admin.site.register(MultipleApprovalManagers) -admin.site.register(ShiftrequestComment) -admin.site.register(WorktyperequestComment) +admin.site.register(ShiftRequestComment) +admin.site.register(WorkTypeRequestComment) admin.site.register(DynamicPagination) admin.site.register(Announcement) admin.site.register(Attachment) diff --git a/base/forms.py b/base/forms.py index 8eb86d790..f94429da6 100644 --- a/base/forms.py +++ b/base/forms.py @@ -35,7 +35,7 @@ from base.models import ( JobPosition, JobRole, MultipleApprovalCondition, - ShiftrequestComment, + ShiftRequestComment, WorkType, EmployeeType, EmployeeShift, @@ -48,7 +48,7 @@ from base.models import ( ShiftRequest, EmployeeShiftDay, Tags, - WorktyperequestComment, + WorkTypeRequestComment, ) from base.methods import reload_queryset from horilla_audit.models import AuditTag @@ -688,7 +688,12 @@ class RotatingWorkTypeAssignUpdateForm(forms.ModelForm): model = RotatingWorkTypeAssign fields = "__all__" - exclude = ("next_change_date", "current_work_type", "next_work_type") + exclude = [ + "next_change_date", + "current_work_type", + "next_work_type", + "is_active", + ] widgets = { "start_date": DateInput(attrs={"type": "date"}), } @@ -969,7 +974,7 @@ class RotatingShiftAssignForm(forms.ModelForm): model = RotatingShiftAssign fields = "__all__" - exclude = ("next_change_date", "current_shift", "next_shift") + exclude = ["next_change_date", "current_shift", "next_shift", "is_active"] widgets = { "start_date": DateInput(attrs={"type": "date"}), } @@ -1073,7 +1078,6 @@ class RotatingShiftAssignForm(forms.ModelForm): "sunday", ] target_day = day_names.index(day_name.lower()) - for employee_id in employee_ids: employee = Employee.objects.filter(id=employee_id).first() rotating_shift_assign = RotatingShiftAssign() @@ -1082,7 +1086,6 @@ class RotatingShiftAssignForm(forms.ModelForm): rotating_shift_assign.based_on = self.cleaned_data["based_on"] rotating_shift_assign.start_date = self.cleaned_data["start_date"] rotating_shift_assign.next_change_date = self.cleaned_data["start_date"] - rotating_shift_assign.is_active = self.cleaned_data["is_active"] rotating_shift_assign.rotate_after_day = self.data.get("rotate_after_day") rotating_shift_assign.rotate_every = self.cleaned_data["rotate_every"] rotating_shift_assign.rotate_every_weekend = self.cleaned_data[ @@ -1122,7 +1125,7 @@ class RotatingShiftAssignUpdateForm(ModelForm): model = RotatingShiftAssign fields = "__all__" - exclude = ("next_change_date", "current_shift", "next_shift") + exclude = ["next_change_date", "current_shift", "next_shift", "is_active"] widgets = { "start_date": DateInput(attrs={"type": "date"}), } @@ -1227,7 +1230,7 @@ class ShiftRequestForm(ModelForm): model = ShiftRequest fields = "__all__" - exclude = ( + exclude = [ "reallocate_to", "approved", "canceled", @@ -1236,7 +1239,7 @@ class ShiftRequestForm(ModelForm): "previous_shift_id", "is_active", "shift_changed", - ) + ] widgets = { "requested_date": DateInput(attrs={"type": "date"}), "requested_till": DateInput(attrs={"type": "date"}), @@ -1614,7 +1617,7 @@ class TagsForm(ModelForm): model = Tags fields = "__all__" widgets = {"color": TextInput(attrs={"type": "color", "style": "height:50px"})} - exclude = ("objects",) + exclude = ["objects", "is_active"] class EmployeeTagForm(ModelForm): @@ -1629,6 +1632,7 @@ class EmployeeTagForm(ModelForm): model = EmployeeTag fields = "__all__" + exclude = ["is_active"] widgets = {"color": TextInput(attrs={"type": "color", "style": "height:50px"})} @@ -1646,7 +1650,7 @@ class AuditTagForm(ModelForm): fields = "__all__" -class ShiftrequestcommentForm(ModelForm): +class ShiftRequestCommentForm(ModelForm): """ Shift request comment form """ @@ -1656,55 +1660,13 @@ class ShiftrequestcommentForm(ModelForm): Meta class for additional options """ - model = ShiftrequestComment + model = ShiftRequestComment fields = ("comment",) -class shiftCommentForm(ModelForm): +class WorkTypeRequestCommentForm(ModelForm): """ - Shift request comment model form - """ - - verbose_name = "Add Comment" - - class Meta: - model = ShiftrequestComment - fields = "__all__" - - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - self.fields["files"] = MultipleFileField(label="files") - self.fields["files"].required = False - - def as_p(self): - """ - Render the form fields as HTML table rows with Bootstrap styling. - """ - context = {"form": self} - table_html = render_to_string("common_form.html", context) - return table_html - - def save(self, commit: bool = ...) -> Any: - multiple_files_ids = [] - files = None - if self.files.getlist("files"): - files = self.files.getlist("files") - self.instance.attachemnt = files[0] - multiple_files_ids = [] - for attachemnt in files: - file_instance = BaserequestFile() - file_instance.file = attachemnt - file_instance.save() - multiple_files_ids.append(file_instance.pk) - instance = super().save(commit) - if commit: - instance.files.add(*multiple_files_ids) - return instance, files - - -class WorktyperequestcommentForm(ModelForm): - """ - Worktype request comment form + WorkType request comment form """ class Meta: @@ -1712,7 +1674,7 @@ class WorktyperequestcommentForm(ModelForm): Meta class for additional options """ - model = WorktyperequestComment + model = WorkTypeRequestComment fields = ("comment",) @@ -1764,7 +1726,9 @@ class MultipleApproveConditionForm(ModelForm): class Meta: model = MultipleApprovalCondition fields = "__all__" - exclude = ["is_active",] + exclude = [ + "is_active", + ] class DynamicPaginationForm(ModelForm): diff --git a/base/models.py b/base/models.py index aad3c1aad..74e4787e4 100644 --- a/base/models.py +++ b/base/models.py @@ -265,7 +265,7 @@ BASED_ON = [ ] -class RotatingWorkTypeAssign(models.Model): +class RotatingWorkTypeAssign(HorillaModel): """ RotatingWorkTypeAssign model """ @@ -322,8 +322,6 @@ class RotatingWorkTypeAssign(models.Model): verbose_name=_("Rotate Every Month"), ) - is_active = models.BooleanField(default=True) - created_at = models.DateTimeField(auto_now_add=True, null=True) history = HorillaAuditLog( related_name="history_set", bases=[ @@ -566,7 +564,7 @@ class RotatingShift(HorillaModel): raise ValidationError(_("Choose different shifts")) -class RotatingShiftAssign(models.Model): +class RotatingShiftAssign(HorillaModel): """ RotatingShiftAssign model """ @@ -621,8 +619,6 @@ class RotatingShiftAssign(models.Model): choices=DAY_DATE, verbose_name=_("Rotate Every Month"), ) - is_active = models.BooleanField(default=True) - created_at = models.DateTimeField(auto_now_add=True, null=True) history = HorillaAuditLog( related_name="history_set", bases=[ @@ -657,7 +653,7 @@ class BaserequestFile(models.Model): objects = models.Manager() -class WorkTypeRequest(models.Model): +class WorkTypeRequest(HorillaModel): """ WorkTypeRequest model """ @@ -696,8 +692,6 @@ class WorkTypeRequest(models.Model): approved = models.BooleanField(default=False, verbose_name=_("Approved")) canceled = models.BooleanField(default=False, verbose_name=_("Canceled")) work_type_changed = models.BooleanField(default=False) - is_active = models.BooleanField(default=True) - created_at = models.DateTimeField(auto_now_add=True, null=True) history = HorillaAuditLog( related_name="history_set", bases=[ @@ -789,9 +783,9 @@ class WorkTypeRequest(models.Model): {self.employee_id.employee_last_name} - {self.requested_date}" -class WorktyperequestComment(models.Model): +class WorkTypeRequestComment(HorillaModel): """ - WorktyperequestComment Model + WorkTypeRequestComment Model """ from employee.models import Employee @@ -800,18 +794,13 @@ class WorktyperequestComment(models.Model): 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"), - null=True, - ) objects = models.Manager() def __str__(self) -> str: return f"{self.comment}" -class ShiftRequest(models.Model): +class ShiftRequest(HorillaModel): """ ShiftRequest model """ @@ -860,8 +849,6 @@ class ShiftRequest(models.Model): approved = models.BooleanField(default=False, verbose_name=_("Approved")) canceled = models.BooleanField(default=False, verbose_name=_("Canceled")) shift_changed = models.BooleanField(default=False) - is_active = models.BooleanField(default=True) - created_at = models.DateTimeField(auto_now_add=True, null=True) history = HorillaAuditLog( related_name="history_set", bases=[ @@ -956,9 +943,9 @@ class ShiftRequest(models.Model): {self.employee_id.employee_last_name} - {self.requested_date}" -class ShiftrequestComment(models.Model): +class ShiftRequestComment(HorillaModel): """ - ShiftrequestComment Model + ShiftRequestComment Model """ from employee.models import Employee @@ -967,21 +954,15 @@ class ShiftrequestComment(models.Model): 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, - verbose_name=_("Created At"), - null=True, - ) objects = models.Manager() def __str__(self) -> str: return f"{self.comment}" -class Tags(models.Model): +class Tags(HorillaModel): title = models.CharField(max_length=30) color = models.CharField(max_length=30) - is_active = models.BooleanField(default=True) company_id = models.ForeignKey( Company, null=True, editable=False, on_delete=models.PROTECT ) @@ -1285,7 +1266,7 @@ class AnnouncementExpire(models.Model): class Announcement(models.Model): """ - Anonuncement Model for stroing all announcements. + Announcement Model for storing all announcements. """ from employee.models import Employee diff --git a/base/scheduler.py b/base/scheduler.py index 305160e39..c460dd6a3 100644 --- a/base/scheduler.py +++ b/base/scheduler.py @@ -166,7 +166,7 @@ def shift_rotate_weekend(rotating_shift, today=datetime.now()): switch_date = rotating_shift.next_change_date if switch_date.strftime("%Y-%m-%d") == today.strftime("%Y-%m-%d"): # calculate the next work type switch date - day = datetime.now().strftime("%A").lower() + day = today.strftime("%A").lower() switch_day = rotating_shift.rotate_every_weekend if day == switch_day: new_date = today + timedelta(days=7) diff --git a/base/views.py b/base/views.py index 83acf934f..f9857bd90 100644 --- a/base/views.py +++ b/base/views.py @@ -63,7 +63,7 @@ from base.forms import ( RotatingWorkTypeAssignExportForm, ShiftAllocationForm, ShiftRequestColumnForm, - ShiftrequestcommentForm, + ShiftRequestCommentForm, WorkTypeForm, UserGroupForm, RotatingShiftForm, @@ -83,7 +83,7 @@ from base.forms import ( ChangePasswordForm, TagsForm, MultipleApproveConditionForm, - WorktyperequestcommentForm, + WorkTypeRequestCommentForm, ) from base.models import ( Announcement, @@ -99,7 +99,7 @@ from base.models import ( Department, MultipleApprovalCondition, MultipleApprovalManagers, - ShiftrequestComment, + ShiftRequestComment, WorkType, EmployeeShift, EmployeeShiftDay, @@ -111,7 +111,7 @@ from base.models import ( ShiftRequest, WorkTypeRequest, Tags, - WorktyperequestComment, + WorkTypeRequestComment, ) from base.filters import ( RotatingShiftRequestReGroup, @@ -4861,23 +4861,23 @@ def create_shiftrequest_comment(request, shift_id): """ shift = ShiftRequest.objects.filter(id=shift_id).first() emp = request.user.employee_get - form = ShiftrequestcommentForm( + form = ShiftRequestCommentForm( initial={"employee_id": emp.id, "request_id": shift_id} ) if request.method == "POST": - form = ShiftrequestcommentForm(request.POST) + form = ShiftRequestCommentForm(request.POST) if form.is_valid(): form.instance.employee_id = emp form.instance.request_id = shift form.save() - comments = ShiftrequestComment.objects.filter(request_id=shift_id).order_by( + comments = ShiftRequestComment.objects.filter(request_id=shift_id).order_by( "-created_at" ) no_comments = False if not comments.exists(): no_comments = True - form = ShiftrequestcommentForm( + form = ShiftRequestCommentForm( initial={"employee_id": emp.id, "request_id": shift_id} ) messages.success(request, _("Comment added successfully!")) @@ -4964,7 +4964,7 @@ 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).order_by( + comments = ShiftRequestComment.objects.filter(request_id=shift_id).order_by( "-created_at" ) no_comments = False @@ -4973,7 +4973,7 @@ def view_shift_comment(request, shift_id): if request.FILES: files = request.FILES.getlist("files") comment_id = request.GET["comment_id"] - comment = ShiftrequestComment.objects.get(id=comment_id) + comment = ShiftRequestComment.objects.get(id=comment_id) attachments = [] for file in files: file_instance = BaserequestFile() @@ -5000,8 +5000,9 @@ def delete_shift_comment_file(request): """ ids = request.GET.getlist("ids") BaserequestFile.objects.filter(id__in=ids).delete() + messages.success(request,_("File deleted successfully")) shift_id = request.GET["shift_id"] - comments = ShiftrequestComment.objects.filter(request_id=shift_id).order_by( + comments = ShiftRequestComment.objects.filter(request_id=shift_id).order_by( "-created_at" ) return render( @@ -5019,7 +5020,7 @@ def view_work_type_comment(request, work_type_id): """ This method is used to render all the notes of the employee """ - comments = WorktyperequestComment.objects.filter(request_id=work_type_id).order_by( + comments = WorkTypeRequestComment.objects.filter(request_id=work_type_id).order_by( "-created_at" ) no_comments = False @@ -5028,7 +5029,7 @@ def view_work_type_comment(request, work_type_id): if request.FILES: files = request.FILES.getlist("files") comment_id = request.GET["comment_id"] - comment = WorktyperequestComment.objects.get(id=comment_id) + comment = WorkTypeRequestComment.objects.get(id=comment_id) attachments = [] for file in files: file_instance = BaserequestFile() @@ -5055,8 +5056,9 @@ def delete_work_type_comment_file(request): """ ids = request.GET.getlist("ids") BaserequestFile.objects.filter(id__in=ids).delete() + messages.success(request,_("File deleted successfully")) work_type_id = request.GET["work_type_id"] - comments = WorktyperequestComment.objects.filter(request_id=work_type_id) + comments = WorkTypeRequestComment.objects.filter(request_id=work_type_id) return render( request, "work_type_request/htmx/work_type_comment.html", @@ -5072,7 +5074,7 @@ def delete_shiftrequest_comment(request, comment_id): """ This method is used to delete shift request comments """ - comment = ShiftrequestComment.objects.get(id=comment_id) + comment = ShiftRequestComment.objects.get(id=comment_id) shift_id = comment.request_id.id comment.delete() messages.success(request, _("Comment deleted successfully!")) @@ -5086,23 +5088,23 @@ def create_worktyperequest_comment(request, worktype_id): """ work_type = WorkTypeRequest.objects.filter(id=worktype_id).first() emp = request.user.employee_get - form = WorktyperequestcommentForm( + form = WorkTypeRequestCommentForm( initial={"employee_id": emp.id, "request_id": worktype_id} ) if request.method == "POST": - form = WorktyperequestcommentForm(request.POST) + form = WorkTypeRequestCommentForm(request.POST) if form.is_valid(): form.instance.employee_id = emp form.instance.request_id = work_type form.save() - comments = WorktyperequestComment.objects.filter( + comments = WorkTypeRequestComment.objects.filter( request_id=worktype_id ).order_by("-created_at") no_comments = False if not comments.exists(): no_comments = True - form = WorktyperequestcommentForm( + form = WorkTypeRequestCommentForm( initial={"employee_id": emp.id, "request_id": worktype_id} ) messages.success(request, _("Comment added successfully!")) @@ -5192,7 +5194,7 @@ def delete_worktyperequest_comment(request, comment_id): """ This method is used to delete Work type request comments """ - comment = WorktyperequestComment.objects.get(id=comment_id) + comment = WorkTypeRequestComment.objects.get(id=comment_id) worktype_id = comment.request_id.id comment.delete() messages.success(request, _("Comment deleted successfully!"))