Skip to content

Commit

Permalink
[UPDT] HORILLA DOCUMENTS: Updated docuemnt model delete method by add…
Browse files Browse the repository at this point in the history
…ing condition to check request exist for the document
  • Loading branch information
horilla-opensource committed Dec 20, 2024
1 parent 22fcafc commit eda9e92
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 139 deletions.
72 changes: 32 additions & 40 deletions employee/templates/tabs/document_tab.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,29 +71,19 @@
{% endif %}
</div>
<div class="oh-feedback-card__name-container ms-1 title_form">
<form hx-post = "{% url 'update-document-title' document.id %}"
hx-headers="{'X-CSRFToken': '{{ csrf_token }}'}"
hx-target="this"
hx-swap="none"
hx-trigger="submit"
>
<input class="oh-card__title oh-card__title--sm fw-bold me-1 editable-input"
onclick="event.stopPropagation();"
name="title"
value="{{document.title}}" readonly
/>
<form hx-post = "{% url 'update-document-title' document.id %}" hx-swap="none" hx-on-htmx-after-request="setTimeout(() => { reloadMessage(); }, 300);">
<input class="oh-card__title oh-card__title--sm fw-bold me-1 editable-input" onclick="event.stopPropagation();" name="title" value="{{document.title}}" readonly />
<span class="error oh-user_permission_list-text text-danger d-none" >{% trans "Title needs to be more than 3 letters" %}</span>
<input class="d-none title-submit"
type="submit"
onclick="event.stopPropagation();"
/>
<input class="d-none title-submit" type="submit" onclick="event.stopPropagation();" />
</form>
<span
class="oh-text--light"
title="{{document.document_request_id.description}}"
>
<span class="oh-text--light" title="{{document.document_request_id.description}}">
{{document.document_request_id.description|truncatechars:60}}
</span>
{% if document.issue_date %}
<span class="oh-text--light" title="{{document.document_request_id.description}}">
{% trans "Issue Date" %} : {{ document.issue_date }}
</span>
{% endif %}
</div>
</div>
<div class="oh-btn-group">
Expand Down Expand Up @@ -130,28 +120,30 @@
</a>
{% endif %}
{% endif %}
<form hx-confirm="{% trans 'Are you sure you want to delete this Document Request?' %}"
hx-post="{% url 'document-delete' document.id %}"
hx-target="#document{{document.id}}"
hx-swap="outerHTML"
class=""
onclick="event.stopPropagation()"
>
{% csrf_token %}
<button
type="submit"
class="oh-btn oh-btn--secondary"
title="{% trans 'Delete' %}"
>
<ion-icon
class="me-1 md hydrated"
name="trash-outline"
role="img"
aria-label="trash outline"
></ion-icon
{% if not document.document_request_id or perms.horilla_document.change_documentrequest %}
<form hx-confirm="{% trans 'Are you sure you want to delete this Document Request?' %}"
hx-post="{% url 'document-delete' document.id %}"
hx-target="#document{{document.id}}"
hx-swap="outerHTML"
hx-on-htmx-after-request="setTimeout(() => { reloadMessage(); }, 300);"
onclick="event.stopPropagation()"
>
{% csrf_token %}
<button
type="submit"
class="oh-btn oh-btn--secondary"
title="{% trans 'Delete' %}"
>
</button>
</form>
<ion-icon
class="me-1 md hydrated"
name="trash-outline"
role="img"
aria-label="trash outline"
></ion-icon
>
</button>
</form>
{% endif %}
</div>
</div>
</div>
Expand Down
3 changes: 1 addition & 2 deletions employee/templates/tabs/htmx/document_create_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
</button>
</div>
<div class="oh-modal__dialog-body">
<form id="add-form" hx-post="{% url 'document-create' emp_id %}" hx-target="#objectCreateModalTarget"
hx-swap="innerHTML" method="post" hx-encoding="multipart/form-data">
<form id="add-form" hx-post="{% url 'document-create' emp_id %}" hx-target="#objectCreateModalTarget" hx-encoding="multipart/form-data">
{{form.errors}} {{form.as_p}}
</form>
</div>
Expand Down
111 changes: 45 additions & 66 deletions employee/templates/tabs/htmx/document_form.html
Original file line number Diff line number Diff line change
@@ -1,100 +1,79 @@
{% load i18n %} {% load widget_tweaks %}
<div class="oh-modal__dialog-header pb-0">
<span class="oh-modal__dialog-title" id="uploadFileModalLabel"
>{% trans "Upload File" %}</span
>
<button
class="oh-modal__close--custom"
aria-label="Close"
onclick="event.stopPropagation();event.preventDefault();$(this).closest('.oh-modal--show').removeClass('oh-modal--show');"
>
<ion-icon
name="close-outline"
role="img"
class="md hydrated"
aria-label="close outline"
></ion-icon>
<span class="oh-modal__dialog-title" id="uploadFileModalLabel">{% trans "Upload File" %}</span>
<button class="oh-modal__close--custom" aria-label="Close"
onclick="event.stopPropagation();event.preventDefault();$(this).closest('.oh-modal--show').removeClass('oh-modal--show');">
<ion-icon name="close-outline" role="img" class="md hydrated" aria-label="close outline"></ion-icon>
</button>
</div>
<div class="oh-modal__dialog-body">
<form
id="file-form"
hx-post="{% url 'file-upload' document.id %}"
hx-target="#objectCreateModalTarget"
hx-encoding="multipart/form-data"
class="oh-profile-section pt-3"
>
<form id="file-form" hx-post="{% url 'file-upload' document.id %}" hx-target="#objectCreateModalTarget"
hx-encoding="multipart/form-data" class="oh-profile-section pt-3">
{% csrf_token %}
{% if form.employee_id %}
<input
type="hidden"
name="employee_id"
value="{{form.employee_id.value}}"
/>
<input type="hidden" name="employee_id" value="{{form.employee_id.value}}" />
{% elif form.candidate_id %}
<input
type="hidden"
name="candidate_id"
value="{{form.candidate_id.value}}"
/>
<input type="hidden" name="candidate_id" value="{{form.candidate_id.value}}" />
{% endif %}
<input type="hidden" name="status" value="requested" />
<input type="hidden" name="title" value="{{form.instance.title}}" />
<input
type="hidden"
name="rejection_reason"
value="{{form.rejection_reason.value}}"
/>
<input
type="hidden"
name="document_request_id"
value="{{form.document_request_id.value}}"
/>
{{form.non_field_errors}} {{form.document}} {{form.document.errors}}
<input type="hidden" name="rejection_reason" value="{{form.rejection_reason.value}}" />
<input type="hidden" name="document_request_id" value="{{form.document_request_id.value}}" />
{{ form.non_field_errors }}

<div class="row">
<div class="col-12 col-sm-12 col-md-4 col-lg-6">
<div class="oh-input__group">
<label
class="oh-input__label"
for="{{form.expiry_date.id_for_label}}"
>{% trans "Expiry Date" %}</label
>
{{form.expiry_date |attr:"type:date" }}
{{form.expiry_date.errors }}
{{ form.document.errors }}
<label class="oh-input__label"
for="{{form.document.id_for_label}}">{{ form.document.label }}</label>
{{ form.document }}
</div>
</div>
{% if not model == "CandidateDocument" %}
<div class="col-12 col-sm-12 col-md-4 col-lg-6 d-none notify">
<div class="col-12 col-sm-12 col-md-4 col-lg-6">
<div class="oh-input__group">
<label
class="oh-input__label"
for="{{form.notify_before.id_for_label}}"
>{% trans "Notify Before (days)" %}</label
>
{{form.notify_before}} {{form.notify_before.errors}}
<label class="oh-input__label"
for="{{form.issue_date.id_for_label}}">{{ form.issue_date.label }}</label>
{{ form.issue_date }}
{{ form.issue_date.errors }}
</div>
</div>
</div>
<div class="row">
<div class="col-12 col-sm-12 col-md-4 col-lg-6">
<div class="oh-input__group">
<label class="oh-input__label" for="{{form.expiry_date.id_for_label}}">
{% trans "Expiry Date" %}
</label>
{{ form.expiry_date }}
{{ form.expiry_date.errors }}
</div>
</div>
{% if not model == "CandidateDocument" %}
<div class="col-12 col-sm-12 col-md-4 col-lg-6 d-none notify">
<div class="oh-input__group">
<label class="oh-input__label" for="{{form.notify_before.id_for_label}}">
{% trans "Notify Before (days)" %}
</label>
{{ form.notify_before }} {{ form.notify_before.errors }}
</div>
</div>
{% endif %}
</div>
<div class="file-validation mt-3">
<ul class="m-0">
<li style="list-style: disc">
{% trans "Upload " %}<b
>{{form.instance.document_request_id.format}}</b
>
{% trans "Upload " %}<b>{{ form.instance.document_request_id.format }}</b>
{% trans "file" %}
</li>
<li style="list-style: disc">
{% trans "Max size of the file" %}
{{form.instance.document_request_id.max_size}} MB
{{ form.instance.document_request_id.max_size }} MB
</li>
</ul>
</div>
<div class="oh-modal__dialog-footer p-0 mt-3">
<button
type="submit"
class="oh-btn oh-btn--secondary oh-btn--shadow"
>
<button type="submit" class="oh-btn oh-btn--secondary oh-btn--shadow">
{% trans "Save" %}
</button>
</div>
Expand All @@ -104,7 +83,7 @@
<script>
$(document).ready(function () {
$("#id_expiry_date").on("change", function () {
if ($("#id_expiry_date").val()){
if ($("#id_expiry_date").val()) {
$(".notify").removeClass("d-none");
} else {
$(".notify").addClass("d-none");
Expand Down
44 changes: 25 additions & 19 deletions employee/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,14 +600,10 @@ def update_document_title(request, id):
if request.method == "POST":
document.title = name
document.save()

return JsonResponse(
{"success": True, "message": "Document title updated successfully"}
)
messages.success(request, _("Document title updated successfully"))
else:
return JsonResponse(
{"success": False, "message": "Invalid request"}, status=400
)
messages.error(request, _("Invalid request"))
return HttpResponse("")


@login_required
Expand All @@ -624,30 +620,40 @@ def document_delete(request, id):
"""
try:
document = Document.objects.filter(id=id)
# users can delete own documents
if not request.user.has_perm("horilla_documents.delete_document"):
document = document.filter(employee_id__employee_user_id=request.user)
document = document.filter(
employee_id__employee_user_id=request.user
).exclude(document_request_id__isnull=False)
if document:
document_first = document.first()
document.delete()
messages.success(
request,
_(
f"Document request {document.first()} for {document.first().employee_id} deleted successfully"
f"Document request {document_first} for {document_first.employee_id} deleted successfully"
),
)
referrer = request.META.get("HTTP_REFERER", "")
referrer = "/" + "/".join(referrer.split("/")[3:])
if referrer.startswith("/employee/employee-view/") or referrer.endswith(
"/employee/employee-profile/"
):
existing_documents = Document.objects.filter(
employee_id=document_first.employee_id
)
if not existing_documents:
return HttpResponse(
f"""
<span hx-get='/employee/document-tab/{document_first.employee_id.id}?employee_view=true'
hx-target='#document_target' hx-trigger='load'></span>
"""
)
return HttpResponse()
else:
messages.error(request, _("Document not found"))

except ProtectedError:
messages.error(request, _("You cannot delete this document."))

if "HTTP_HX_TARGET" in request.META and request.META.get(
"HTTP_HX_TARGET"
).startswith("document"):
clear_messages(request)
return HttpResponse()
else:
return HttpResponse("<script>window.location.reload();</script>")
return HttpResponse("<script>window.location.reload();</script>")


@login_required
Expand Down
23 changes: 14 additions & 9 deletions horilla_documents/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ def __init__(self, *args, **kwargs):
class DocumentForm(ModelForm):
"""form to create a new Document"""

expiry_date = forms.DateField(
widget=forms.DateInput(attrs={"type": "date"}),
required=False,
)

verbose_name = "Document"

class Meta:
Expand All @@ -65,6 +60,12 @@ class Meta:
exclude = ["document_request_id", "status", "reject_reason", "is_active"]
widgets = {
"employee_id": forms.HiddenInput(),
"issue_date": forms.DateInput(
attrs={"type": "date", "class": "oh-input w-100"}
),
"expiry_date": forms.DateInput(
attrs={"type": "date", "class": "oh-input w-100"}
),
}

def as_p(self):
Expand All @@ -80,15 +81,19 @@ class DocumentUpdateForm(ModelForm):
"""form to Update a Document"""

verbose_name = "Document"
expiry_date = forms.DateField(
widget=forms.DateInput(attrs={"type": "date"}),
required=False,
)

class Meta:
model = Document
fields = "__all__"
exclude = ["is_active"]
widgets = {
"issue_date": forms.DateInput(
attrs={"type": "date", "class": "oh-input w-100"}
),
"expiry_date": forms.DateInput(
attrs={"type": "date", "class": "oh-input w-100"}
),
}


class DocumentRejectForm(ModelForm):
Expand Down
11 changes: 8 additions & 3 deletions horilla_documents/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,14 @@ class Document(HorillaModel):
document = models.FileField(upload_to="employee/documents", null=True)
status = models.CharField(choices=STATUS, max_length=10, default="requested")
reject_reason = models.TextField(blank=True, null=True, max_length=255)
expiry_date = models.DateField(null=True, blank=True)
notify_before = models.IntegerField(default=1, null=True)
is_digital_asset = models.BooleanField(default=False)
issue_date = models.DateField(null=True, blank=True, verbose_name=_("Issue Date"))
expiry_date = models.DateField(null=True, blank=True, verbose_name=_("Expiry Date"))
notify_before = models.IntegerField(
default=1, null=True, verbose_name=_("Notify Before")
)
is_digital_asset = models.BooleanField(
default=False, verbose_name=_("Is Digital Asset")
)
objects = HorillaCompanyManager(
related_company_field="employee_id__employee_work_info__company_id"
)
Expand Down

0 comments on commit eda9e92

Please sign in to comment.