diff --git a/horilla_widgets/__init__.py b/horilla_widgets/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/horilla_widgets/admin.py b/horilla_widgets/admin.py new file mode 100644 index 000000000..8c38f3f3d --- /dev/null +++ b/horilla_widgets/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/horilla_widgets/apps.py b/horilla_widgets/apps.py new file mode 100644 index 000000000..7557bb558 --- /dev/null +++ b/horilla_widgets/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class HorillaWidgetsConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'horilla_widgets' diff --git a/horilla_widgets/forms.py b/horilla_widgets/forms.py new file mode 100644 index 000000000..8c1cdff8c --- /dev/null +++ b/horilla_widgets/forms.py @@ -0,0 +1,40 @@ +""" +forms.py + +Horilla forms +""" +from typing import Any +from django import forms +from horilla_widgets.widgets.horilla_multi_select_field import HorillaMultiSelectField + + +class HorillaForm(forms.Form): + def clean(self) -> dict[str, Any]: + for field_name, field_instance in self.fields.items(): + if isinstance(field_instance, HorillaMultiSelectField): + self.errors.pop(field_name, None) + if len(self.data.getlist(field_name)) < 1: + raise forms.ValidationError({field_name: "Thif field is required"}) + cleaned_data = super().clean() + employee_data = self.fields[field_name].queryset.filter( + id__in=self.data.getlist(field_name) + ) + cleaned_data[field_name] = employee_data + cleaned_data = super().clean() + return cleaned_data + + +class HorillaModelForm(forms.ModelForm): + def clean(self) -> dict[str, Any]: + for field_name, field_instance in self.fields.items(): + if isinstance(field_instance, HorillaMultiSelectField): + self.errors.pop(field_name, None) + if len(self.data.getlist(field_name)) < 1: + raise forms.ValidationError({field_name: "Thif field is required"}) + cleaned_data = super().clean() + employee_data = self.fields[field_name].queryset.filter( + id__in=self.data.getlist(field_name) + ) + cleaned_data[field_name] = employee_data + cleaned_data = super().clean() + return cleaned_data diff --git a/horilla_widgets/migrations/__init__.py b/horilla_widgets/migrations/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/horilla_widgets/models.py b/horilla_widgets/models.py new file mode 100644 index 000000000..71a836239 --- /dev/null +++ b/horilla_widgets/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/horilla_widgets/templates/horilla_widgets/horilla_multiselect_widget.html b/horilla_widgets/templates/horilla_widgets/horilla_multiselect_widget.html new file mode 100644 index 000000000..2307a8608 --- /dev/null +++ b/horilla_widgets/templates/horilla_widgets/horilla_multiselect_widget.html @@ -0,0 +1,180 @@ +{% load i18n %} + +
+ +
+
+
+ +
+ Filter and filterChoose +
+ diff --git a/horilla_widgets/templates/horilla_widgets/multiselect_components/choose_pills.html b/horilla_widgets/templates/horilla_widgets/multiselect_components/choose_pills.html new file mode 100644 index 000000000..953eb70e2 --- /dev/null +++ b/horilla_widgets/templates/horilla_widgets/multiselect_components/choose_pills.html @@ -0,0 +1,26 @@ + + diff --git a/horilla_widgets/templates/horilla_widgets/multiselect_components/filter_tags.html b/horilla_widgets/templates/horilla_widgets/multiselect_components/filter_tags.html new file mode 100644 index 000000000..d224ee910 --- /dev/null +++ b/horilla_widgets/templates/horilla_widgets/multiselect_components/filter_tags.html @@ -0,0 +1,35 @@ + +
+ +
+
+
+
+ Selected 0/{{queryset|length}} +
+
+ Select All {{queryset|length}} user +
+
+ Unselect All +
+
+ +
+ Select All {{queryset|length}} Item +
+
diff --git a/horilla_widgets/templates/horilla_widgets/multiselect_components/nav.html b/horilla_widgets/templates/horilla_widgets/multiselect_components/nav.html new file mode 100644 index 000000000..d0339daaf --- /dev/null +++ b/horilla_widgets/templates/horilla_widgets/multiselect_components/nav.html @@ -0,0 +1,146 @@ +
+
+
+

{{field.label}}

+
+
+
+
+ +
+
+
+ +
+ {% include filter_template_path %} + +
+
+
+
+
+ diff --git a/horilla_widgets/templates/horilla_widgets/multiselect_components/pagination.html b/horilla_widgets/templates/horilla_widgets/multiselect_components/pagination.html new file mode 100644 index 000000000..4340a5a99 --- /dev/null +++ b/horilla_widgets/templates/horilla_widgets/multiselect_components/pagination.html @@ -0,0 +1,8 @@ +
+ Page 1 of 1 +
\ No newline at end of file diff --git a/horilla_widgets/templates/horilla_widgets/multiselect_components/search.html b/horilla_widgets/templates/horilla_widgets/multiselect_components/search.html new file mode 100644 index 000000000..4ffa713d7 --- /dev/null +++ b/horilla_widgets/templates/horilla_widgets/multiselect_components/search.html @@ -0,0 +1,60 @@ +
+
+ +
+
+ +
+
+
+ Selected 0/138 +
+
+ Select All +
+
Unselect All
+
+ + +
+ + diff --git a/horilla_widgets/templates/horilla_widgets/multiselect_components/table.html b/horilla_widgets/templates/horilla_widgets/multiselect_components/table.html new file mode 100644 index 000000000..ad25c43b6 --- /dev/null +++ b/horilla_widgets/templates/horilla_widgets/multiselect_components/table.html @@ -0,0 +1,205 @@ +
+
+
+
+
+
+
+ +
+ Employee +
+
+
+
+ {% for instance in queryset %} +
+
+
+
+
+ +
+
+
+ +
+ {{instance}} +
+
+
+
+
+ {% endfor %} +
+
+ diff --git a/horilla_widgets/templates/horilla_widgets/multiselect_components/user_tags.html b/horilla_widgets/templates/horilla_widgets/multiselect_components/user_tags.html new file mode 100644 index 000000000..b3e91f398 --- /dev/null +++ b/horilla_widgets/templates/horilla_widgets/multiselect_components/user_tags.html @@ -0,0 +1,30 @@ +
+ + + + + + +
diff --git a/horilla_widgets/tests.py b/horilla_widgets/tests.py new file mode 100644 index 000000000..7ce503c2d --- /dev/null +++ b/horilla_widgets/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/horilla_widgets/views.py b/horilla_widgets/views.py new file mode 100644 index 000000000..7cc4cb5ac --- /dev/null +++ b/horilla_widgets/views.py @@ -0,0 +1,15 @@ +from django.shortcuts import render + +# Create your views here. + +# urls.py +# path("employee-widget-filter",views.widget_filter,name="employee-widget-filter") + +# views.py +# @login_required +# def widget_filter(request): +# """ +# This method is used to return all the ids of the employees +# """ +# ids = EmployeeFilter(request.GET).qs.values_list("id", flat=True) +# return JsonResponse({'ids':list(ids)}) \ No newline at end of file diff --git a/horilla_widgets/widgets/__init__.py b/horilla_widgets/widgets/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/horilla_widgets/widgets/horilla_multi_select_field.py b/horilla_widgets/widgets/horilla_multi_select_field.py new file mode 100644 index 000000000..f5b0b31ef --- /dev/null +++ b/horilla_widgets/widgets/horilla_multi_select_field.py @@ -0,0 +1,11 @@ +""" +horilla_multi_select_field.py +This module is used to write cutom multiple select field +""" +from django import forms + + +class HorillaMultiSelectField(forms.ModelMultipleChoiceField): + """ + HorillaMultiSelectField + """ diff --git a/horilla_widgets/widgets/select_widgets.py b/horilla_widgets/widgets/select_widgets.py new file mode 100644 index 000000000..cb9b9a7cb --- /dev/null +++ b/horilla_widgets/widgets/select_widgets.py @@ -0,0 +1,97 @@ +""" +select_widgets.py + +This module is used to write horilla form select widgets +""" +from django import forms + + +class HorillaMultiSelectWidget(forms.Widget): + """ + HorillaMultiSelectWidget + """ + + def __init__( + self, + *args, + filter_route_name, + filter_class=None, + filter_instance_contex_name=None, + filter_template_path=None, + instance=None, + **kwargs + ) -> None: + self.filter_route_name = filter_route_name + self.filter_class = filter_class + self.filter_instance_contex_name = filter_instance_contex_name + self.filter_template_path = filter_template_path + self.instance = instance + super().__init__() + + template_name = "horilla_widgets/horilla_multiselect_widget.html" + + def get_context(self, name, value, attrs): + # Get the default context from the parent class + context = super().get_context(name, value, attrs) + # Add your custom data to the context + queryset = self.choices.queryset + field = self.choices.field + context["queryset"] = queryset + context["field_name"] = name + context["field"] = field + context["self"] = self + context["filter_template_path"] = self.filter_template_path + context["filter_route_name"] = self.filter_route_name + self.attrs["id"] = ("id_" + name ) if self.attrs.get('id') is None else self.attrs.get("id") + context[self.filter_instance_contex_name] = self.filter_class + if self.instance is not None: + data = getattr(self.instance,field) + print(data) + return context + + def value_from_datadict(self, data, files, name): + print(data) + print('---------------------') + pass + + +""" +Attendance form field updations +""" + +# def clean(self) -> Dict[str, Any]: +# self.instance.employee_id = Employee.objects.filter( +# id=self.data.get("employee_id") +# ).first() + +# self.errors.pop("employee_id", None) +# if self.instance.employee_id is None: +# raise ValidationError({"employee_id": "This field is required"}) +# super().clean() +# employee_ids = self.data.getlist("employee_id") +# existing_attendance = Attendance.objects.filter( +# attendance_date=self.data["attendance_date"] +# ).filter(employee_id__id__in=employee_ids) +# if existing_attendance.exists(): +# raise ValidationError( +# { +# "employee_id": f"""Already attendance exists for {list(existing_attendance.values_list("employee_id__employee_first_name",flat=True))} employees""" +# } +# ) + + +# class AttendanceForm(ModelForm): +# """ +# Model form for Attendance model +# """ + +# employee_id = HorillaMultiSelectField( +# queryset=Employee.objects.filter(employee_work_info__isnull=False), +# widget=HorillaMultiSelectWidget( +# filter_route_name="employee-widget-filter", +# filter_class=EmployeeFilter, +# filter_instance_contex_name="f", +# filter_template_path="employee_filters.html", +# ), +# label=_("Employees"), +# ) \ No newline at end of file