From 576a1b4d07709f48c460ee32673f3ed6523292d3 Mon Sep 17 00:00:00 2001 From: Horilla Date: Mon, 17 Mar 2025 12:18:00 +0530 Subject: [PATCH] [UPDT] ASSET: Updated models class, form class and form html for update the verbose name --- asset/apps.py | 4 +- asset/forms.py | 124 ++++-------------- asset/models.py | 114 ++++++++++++---- .../src/asset_category/assetCategoryView.js | 2 +- asset/templates/asset/asset_creation.html | 22 ++-- asset/templates/asset/asset_list.html | 2 +- asset/templates/asset/asset_return_form.html | 77 ++++------- asset/templates/asset/asset_update.html | 28 ++-- .../batch/asset_batch_number_creation.html | 49 +++---- .../batch/asset_batch_number_update.html | 23 ++-- .../category/asset_category_creation.html | 61 --------- .../category/asset_category_form.html | 45 +++++++ .../category/asset_category_update.html | 60 --------- .../category/asset_category_view.html | 2 +- asset/templates/category/asset_empty.html | 2 +- .../asset_allocation_creation.html | 10 +- .../request_allocation/asset_approve.html | 6 +- .../asset_request_creation.html | 12 +- asset/urls.py | 4 +- asset/views.py | 40 +++--- 20 files changed, 284 insertions(+), 403 deletions(-) delete mode 100644 asset/templates/category/asset_category_creation.html create mode 100644 asset/templates/category/asset_category_form.html delete mode 100644 asset/templates/category/asset_category_update.html diff --git a/asset/apps.py b/asset/apps.py index f9172a7e2..be1f3e259 100644 --- a/asset/apps.py +++ b/asset/apps.py @@ -21,9 +21,9 @@ class AssetConfig(AppConfig): def ready(self): from django.urls import include, path - from horilla.horilla_settings import APPS + + from horilla.horilla_settings import APP_URLS, APPS from horilla.urls import urlpatterns - from horilla.horilla_settings import APP_URLS APPS.append("asset") urlpatterns.append( diff --git a/asset/forms.py b/asset/forms.py index ec527bbf5..e19e482d2 100644 --- a/asset/forms.py +++ b/asset/forms.py @@ -46,82 +46,49 @@ class AssetForm(ModelForm): """ class Meta: - """ - Specifies the model and fields to be used for the AssetForm. - Attributes: - model (class): The model class Asset to be used for the form. - fields (str): A special value "__all__" to include all fields - of the model in the form. - """ - model = Asset fields = "__all__" - exclude = ["is_active", "owner"] + exclude = ["is_active"] widgets = { - "asset_name": forms.TextInput( - attrs={"placeholder": "Macbook Pro.", "class": "oh-input w-100"} - ), - "asset_description": forms.Textarea( - attrs={ - "type": "text", - "placeholder": _("A powerful laptop for business use."), - "class": "oh-input oh-input--textarea oh-input--block", - "rows": 3, - "cols": 40, - } - ), - "asset_tracking_id": forms.TextInput( - attrs={"placeholder": "LPT001", "class": "oh-input w-100"} - ), "asset_purchase_date": forms.DateInput( - attrs={"type": "date", "class": "oh-input w-100"} + attrs={"type": "date", "class": "oh-input w-100"} ), "expiry_date": forms.DateInput( - attrs={"type": "date", "class": "oh-input w-100"} - ), - "asset_purchase_cost": forms.NumberInput( - attrs={"class": "oh-input w-100", "placeholder": "1200.00."} - ), - "asset_category_id": forms.Select( - attrs={ - "class": "oh-select oh-select-2 select2-hidden-accessible", - }, - ), - "asset_status": forms.Select( - attrs={"class": "oh-select oh-select--lg oh-select-no-search "} + attrs={"type": "date", "class": "oh-input w-100"} ), "asset_lot_number_id": forms.Select( - attrs={ - "class": "oh-select oh-select-2 select2-hidden-accessible ", - "placeholder": "LOT001", - "onchange": "batchNoChange($(this))", - } + attrs={"onchange": "batchNoChange($(this))"} ), } def __init__(self, *args, **kwargs): - request = getattr(_thread_locals, "request") + request = getattr(_thread_locals, "request", None) instance = kwargs.get("instance") + if instance: - kwargs["initial"] = set_date_field_initial(instance) + kwargs.setdefault("initial", set_date_field_initial(instance)) + super().__init__(*args, **kwargs) - reload_queryset(self.fields) - self.fields["asset_category_id"].widget.attrs.update({"id": str(uuid.uuid4())}) - self.fields["asset_lot_number_id"].widget.attrs.update( - {"id": str(uuid.uuid4())} - ) - self.fields["asset_status"].widget.attrs.update({"id": str(uuid.uuid4())}) - if request.user.has_perm("asset.add_assetlot"): - batch_no_choices = [("", _("---Choose Batch No.---"))] + list( + + uuid_map = { + field: str(uuid.uuid4()) + for field in ["asset_category_id", "asset_lot_number_id", "asset_status"] + } + for field, uuid_value in uuid_map.items(): + self.fields[field].widget.attrs["id"] = uuid_value + + if request and request.user.has_perm("asset.add_assetlot"): + batch_no_choices = list( self.fields["asset_lot_number_id"].queryset.values_list( "id", "lot_number" ) ) + batch_no_choices.insert(0, ("", _("---Choose Batch No.---"))) + + if not self.instance.pk: + batch_no_choices.append(("create", _("Create new batch number"))) + self.fields["asset_lot_number_id"].choices = batch_no_choices - if self.instance.pk is None: - self.fields["asset_lot_number_id"].choices += [ - ("create", _("Create new batch number")) - ] def clean(self): instance = self.instance @@ -239,27 +206,11 @@ class AssetCategoryForm(ModelForm): model (class): The model class AssetCategory to be used for the form. fields (str): A special value "__all__" to include all fields of the model in the form. - widgets (dict): A dictionary containing widget configurations for - specific form fields. """ model = AssetCategory fields = "__all__" exclude = ["is_active"] - widgets = { - "asset_category_name": forms.TextInput( - attrs={"placeholder": _("Computers."), "class": "oh-input w-100"} - ), - "asset_category_description": forms.Textarea( - attrs={ - "type": "text", - "placeholder": _("A category for all types of laptops."), - "class": "oh-input oh-input--textarea oh-input--block", - "rows": 3, - "cols": 40, - } - ), - } class AssetRequestForm(ModelForm): @@ -336,7 +287,9 @@ class AssetAllocationForm(ModelForm): asset_status="Available" ) - self.fields["assign_images"] = MultipleFileField() + self.fields["assign_images"] = MultipleFileField( + label=_("Assign Condition Images") + ) self.fields["assign_images"].required = True class Meta: @@ -419,7 +372,9 @@ class AssetReturnForm(ModelForm): super().__init__(*args, **kwargs) self.fields["return_date"].initial = date.today() - self.fields["return_images"] = MultipleFileField(label="Images") + self.fields["return_images"] = MultipleFileField( + label=_("Return Condition Images") + ) self.fields["return_images"].required = True def clean_return_date(self): @@ -448,10 +403,6 @@ class AssetBatchForm(ModelForm): A Django ModelForm for creating or updating AssetLot instances. """ - def __init__(self, *args, **kwargs) -> None: - super().__init__(*args, **kwargs) - reload_queryset(self.fields) - class Meta: """ Specifies the model and fields to be used for the AssetBatchForm. @@ -465,20 +416,3 @@ class AssetBatchForm(ModelForm): model = AssetLot fields = "__all__" - widgets = { - "lot_number": forms.TextInput( - attrs={"placeholder": "A12345.", "class": "oh-input w-100"} - ), - "lot_description": forms.Textarea( - attrs={ - "type": "text", - "placeholder": _( - "A batch of 50 laptops, consisting of Lenovo ThinkPad T480s\ - and Dell XPS 13." - ), - "class": "oh-input oh-input--textarea oh-input--block", - "rows": 3, - "cols": 40, - } - ), - } diff --git a/asset/models.py b/asset/models.py index aacfad9c6..608901f63 100644 --- a/asset/models.py +++ b/asset/models.py @@ -20,12 +20,24 @@ class AssetCategory(HorillaModel): Represents a category for different types of assets. """ - asset_category_name = models.CharField(max_length=255, unique=True) - asset_category_description = models.TextField(max_length=255) + asset_category_name = models.CharField( + max_length=255, unique=True, verbose_name=_("Name") + ) + asset_category_description = models.TextField( + max_length=255, verbose_name=_("Description") + ) objects = models.Manager() company_id = models.ManyToManyField(Company, blank=True, verbose_name=_("Company")) objects = HorillaCompanyManager("company_id") + class Meta: + """ + Meta class to add additional options + """ + + verbose_name = _("Asset Category") + verbose_name_plural = _("Asset Categories") + def __str__(self): return f"{self.asset_category_name}" @@ -35,8 +47,16 @@ class AssetLot(HorillaModel): Represents a lot associated with a collection of assets. """ - lot_number = models.CharField(max_length=30, null=False, blank=False, unique=True) - lot_description = models.TextField(null=True, blank=True, max_length=255) + lot_number = models.CharField( + max_length=30, + null=False, + blank=False, + unique=True, + verbose_name=_("Batch Number"), + ) + lot_description = models.TextField( + null=True, blank=True, max_length=255, verbose_name=_("Description") + ) company_id = models.ManyToManyField(Company, blank=True, verbose_name=_("Company")) objects = HorillaCompanyManager() @@ -45,6 +65,7 @@ class AssetLot(HorillaModel): Meta class to add additional options """ + ordering = ["-created_at"] verbose_name = _("Asset Batch") verbose_name_plural = _("Asset Batches") @@ -62,25 +83,50 @@ class Asset(HorillaModel): ("Available", _("Available")), ("Not-Available", _("Not-Available")), ] - asset_name = models.CharField(max_length=255) - owner = models.ForeignKey(Employee, on_delete=models.PROTECT, null=True, blank=True) - asset_description = models.TextField(null=True, blank=True, max_length=255) - asset_tracking_id = models.CharField(max_length=30, null=False, unique=True) - asset_purchase_date = models.DateField() - asset_purchase_cost = models.DecimalField(max_digits=10, decimal_places=2) - asset_category_id = models.ForeignKey(AssetCategory, on_delete=models.PROTECT) + asset_name = models.CharField(max_length=255, verbose_name=_("Asset Name")) + owner = models.ForeignKey( + Employee, + on_delete=models.PROTECT, + null=True, + blank=True, + verbose_name=_("Current User"), + ) + asset_description = models.TextField( + null=True, blank=True, max_length=255, verbose_name=_("Description") + ) + asset_tracking_id = models.CharField( + max_length=30, null=False, unique=True, verbose_name=_("Tracking Id") + ) + asset_purchase_date = models.DateField(verbose_name=_("Purchase Date")) + asset_purchase_cost = models.DecimalField( + max_digits=10, decimal_places=2, verbose_name=_("Cost") + ) + asset_category_id = models.ForeignKey( + AssetCategory, on_delete=models.PROTECT, verbose_name=_("Category") + ) asset_status = models.CharField( - choices=ASSET_STATUS, default="Available", max_length=40 + choices=ASSET_STATUS, + default="Available", + max_length=40, + verbose_name=_("Status"), ) asset_lot_number_id = models.ForeignKey( - AssetLot, on_delete=models.PROTECT, null=True, blank=True + AssetLot, + on_delete=models.PROTECT, + null=True, + blank=True, + verbose_name=_("Batch No"), + ) + expiry_date = models.DateField(null=True, blank=True, verbose_name=_("Expiry Date")) + notify_before = models.IntegerField( + default=1, null=True, verbose_name=_("Notify Before (days)") ) - expiry_date = models.DateField(null=True, blank=True) - notify_before = models.IntegerField(default=1, null=True) objects = HorillaCompanyManager("asset_category_id__company_id") class Meta: ordering = ["-created_at"] + verbose_name = _("Asset") + verbose_name_plural = _("Assets") def __str__(self): return f"{self.asset_name}-{self.asset_tracking_id}" @@ -173,19 +219,31 @@ class AssetAssignment(HorillaModel): ("Healthy", _("Healthy")), ] asset_id = models.ForeignKey( - Asset, on_delete=models.PROTECT, verbose_name=_("asset") + Asset, on_delete=models.PROTECT, verbose_name=_("Asset") ) assigned_to_employee_id = models.ForeignKey( - Employee, on_delete=models.PROTECT, related_name="allocated_employee" + Employee, + on_delete=models.PROTECT, + related_name="allocated_employee", + verbose_name=_("Assigned To"), ) assigned_date = models.DateField(auto_now_add=True) assigned_by_employee_id = models.ForeignKey( - Employee, on_delete=models.PROTECT, related_name="assigned_by" + Employee, + on_delete=models.PROTECT, + related_name="assigned_by", + verbose_name=_("Assigned By"), + ) + return_date = models.DateField(null=True, blank=True, verbose_name=_("Return Date")) + return_condition = models.TextField( + null=True, blank=True, max_length=255, verbose_name=_("Return Condition") ) - return_date = models.DateField(null=True, blank=True) - return_condition = models.TextField(null=True, blank=True, max_length=255) return_status = models.CharField( - choices=STATUS, max_length=30, null=True, blank=True + choices=STATUS, + max_length=30, + null=True, + blank=True, + verbose_name=_("Return Status"), ) return_request = models.BooleanField(default=False) objects = HorillaCompanyManager("asset_id__asset_lot_number_id__company_id") @@ -193,7 +251,10 @@ class AssetAssignment(HorillaModel): ReturnImages, blank=True, related_name="return_images" ) assign_images = models.ManyToManyField( - ReturnImages, blank=True, related_name="assign_images" + ReturnImages, + blank=True, + related_name="assign_images", + verbose_name=_("Assign Condition Images"), ) objects = HorillaCompanyManager( "assigned_to_employee_id__employee_work_info__company_id" @@ -203,6 +264,8 @@ class AssetAssignment(HorillaModel): """Meta class for AssetAssignment model""" ordering = ["-id"] + verbose_name = _("Asset Allocation") + verbose_name_plural = _("Asset Allocations") def __str__(self): return f"{self.assigned_to_employee_id} --- {self.asset_id} --- {self.return_status}" @@ -224,12 +287,15 @@ class AssetRequest(HorillaModel): related_name="requested_employee", null=False, blank=False, + verbose_name=_("Requesting User"), ) asset_category_id = models.ForeignKey( AssetCategory, on_delete=models.PROTECT, verbose_name=_("Asset Category") ) asset_request_date = models.DateField(auto_now_add=True) - description = models.TextField(null=True, blank=True, max_length=255) + description = models.TextField( + null=True, blank=True, max_length=255, verbose_name=_("Description") + ) asset_request_status = models.CharField( max_length=30, choices=STATUS, default="Requested", null=True, blank=True ) @@ -241,6 +307,8 @@ class AssetRequest(HorillaModel): """Meta class for AssetRequest model""" ordering = ["-id"] + verbose_name = _("Asset Request") + verbose_name_plural = _("Asset Requests") def status_html_class(self): COLOR_CLASS = { diff --git a/asset/static/src/asset_category/assetCategoryView.js b/asset/static/src/asset_category/assetCategoryView.js index 8e4ecc151..adffbb06e 100644 --- a/asset/static/src/asset_category/assetCategoryView.js +++ b/asset/static/src/asset_category/assetCategoryView.js @@ -102,7 +102,7 @@ $(document).ready(function () { var csrf_token = $('input[name="csrfmiddlewaretoken"]').attr("value"); $.ajax({ type: "POST", - url: "asset-count-update", + url: "/asset/asset-count-update", data: { asset_category_id: assetCategoryId, csrfmiddlewaretoken: csrf_token, diff --git a/asset/templates/asset/asset_creation.html b/asset/templates/asset/asset_creation.html index a73269572..a7a613e40 100644 --- a/asset/templates/asset/asset_creation.html +++ b/asset/templates/asset/asset_creation.html @@ -20,14 +20,14 @@ -
+
-
{% trans "Asset Creation" %}
+
{% trans "Create" %} {{asset_creation_form.verbose_name}}
@@ -37,14 +37,14 @@
{{asset_creation_form.asset_name}} {{asset_creation_form.asset_name.errors}}
+ {{asset_creation_form.asset_description.label}} {{asset_creation_form.asset_description}} {{asset_creation_form.asset_description.errors}}
@@ -52,7 +52,7 @@
{{asset_creation_form.asset_tracking_id}} {{asset_creation_form.asset_tracking_id.errors}} @@ -61,7 +61,7 @@
{{asset_creation_form.asset_purchase_date |attr:"type:date" }} {{asset_creation_form.asset_purchase_date.errors }} @@ -70,7 +70,7 @@
{{asset_creation_form.asset_purchase_cost}} {{asset_creation_form.asset_purchase_cost.errors}} @@ -81,7 +81,7 @@
{{asset_creation_form.asset_status }} {{asset_creation_form.asset_status.errors }} @@ -90,7 +90,7 @@
{{asset_creation_form.asset_lot_number_id }} {{asset_creation_form.asset_lot_number_id.errors}} @@ -101,7 +101,7 @@
{{asset_creation_form.expiry_date |attr:"type:date" }} {{asset_creation_form.expiry_date.errors }} @@ -110,7 +110,7 @@
{{asset_creation_form.notify_before}} {{asset_creation_form.notify_before.errors}} diff --git a/asset/templates/asset/asset_list.html b/asset/templates/asset/asset_list.html index 611752738..7c29e7c44 100644 --- a/asset/templates/asset/asset_list.html +++ b/asset/templates/asset/asset_list.html @@ -282,7 +282,7 @@ var csrf_token = $('input[name="csrfmiddlewaretoken"]').attr("value"); $.ajax({ type: "POST", - url: "asset-count-update", + url: "/asset/asset-count-update", data: { asset_category_id: assetCategoryId, csrfmiddlewaretoken: csrf_token, diff --git a/asset/templates/asset/asset_return_form.html b/asset/templates/asset/asset_return_form.html index ca3e4f890..ee9fe6513 100644 --- a/asset/templates/asset/asset_return_form.html +++ b/asset/templates/asset/asset_return_form.html @@ -1,86 +1,57 @@ {% load i18n %} {% load horillafilters %} -
- - +
{% trans "Asset Return Form" %}
-
+ {% csrf_token %}
- + {{asset_return_form.return_status}}
- + {{asset_return_form.return_date}} {{asset_return_form.return_date.errors}}
- + {{asset_return_form.return_condition}}
- + {{asset_return_form.return_images}}
- {% if "payroll"|app_installed %} {% if perms.payroll.add_loanaccount %} - {% endif %} {% endif %} -
diff --git a/asset/templates/asset/asset_update.html b/asset/templates/asset/asset_update.html index 0ecbd48b0..6d3c4dfcd 100644 --- a/asset/templates/asset/asset_update.html +++ b/asset/templates/asset/asset_update.html @@ -67,7 +67,9 @@ {% endif %} - {% trans "Asset Update" %} + +
{% trans "Update" %} {{asset_form.verbose_name}}
+
-
+
{{asset_form.asset_name}} {{asset_form.asset_name.errors}} @@ -95,7 +97,7 @@
{% with assigned=instance.assetassignment_set.last %}
{{asset_form.asset_description}} {{asset_form.asset_description.errors}} @@ -117,7 +119,7 @@
{{asset_form.asset_tracking_id}} {{asset_form.asset_tracking_id.errors}} @@ -127,7 +129,7 @@
{{asset_form.asset_category_id }} {{asset_form.asset_category_id.errors }} @@ -139,7 +141,7 @@
{{asset_form.asset_purchase_date }} {{asset_form.asset_purchase_date.errors }} @@ -149,7 +151,7 @@
{{asset_form.asset_purchase_cost}} {{asset_form.asset_purchase_cost.errors}} @@ -160,7 +162,7 @@
{{asset_form.asset_status}} {{asset_form.asset_status.errors}} @@ -170,7 +172,7 @@
{{asset_form.asset_lot_number_id}} {{asset_form.asset_lot_number_id.errors}} @@ -181,7 +183,7 @@
{{asset_form.expiry_date}} {{asset_form.expiry_date.errors}} @@ -192,7 +194,7 @@
{{asset_form.notify_before}} {{asset_form.notify_before.errors}} diff --git a/asset/templates/batch/asset_batch_number_creation.html b/asset/templates/batch/asset_batch_number_creation.html index c5240e752..e535b732b 100644 --- a/asset/templates/batch/asset_batch_number_creation.html +++ b/asset/templates/batch/asset_batch_number_creation.html @@ -1,54 +1,35 @@ {% load static i18n %} {% load i18n %} {% if messages %} -
- {% for message in messages %} -
-
- {{ message }} -
-
- {% endfor %} + + -
{% endif %}
- {% trans "Create Batch Number" %} + {% trans "Create" %} {{asset_batch_form.verbose_name}}
-
- + {% csrf_token %}
- + {{asset_batch_form.lot_number}} {{asset_batch_form.lot_number.errors}}
- + {{asset_batch_form.lot_description}} {{asset_batch_form.lot_description.errors}}
diff --git a/asset/templates/batch/asset_batch_number_update.html b/asset/templates/batch/asset_batch_number_update.html index 2a2f66a64..b56b1723c 100644 --- a/asset/templates/batch/asset_batch_number_update.html +++ b/asset/templates/batch/asset_batch_number_update.html @@ -1,13 +1,11 @@ {% load static i18n %} {% load i18n %} {% if messages %} - + {% endif %} - {% if in_use_message %}
@@ -17,23 +15,26 @@
{% endif %} -
-
{% trans "Batch Number Update" %}
+
{% trans "Update" %} + {{asset_batch_update_form.verbose_name}}
- + {% csrf_token %}
- + {{asset_batch_update_form.lot_number}}
- + {{asset_batch_update_form.lot_description}}