2024-06-12 16:44:05 +05:30
|
|
|
import importlib
|
2024-06-13 11:22:59 +05:30
|
|
|
|
2024-06-12 16:44:05 +05:30
|
|
|
from django import forms
|
|
|
|
|
from django.http import HttpResponse, JsonResponse
|
|
|
|
|
from django.shortcuts import render
|
|
|
|
|
from django.views import View
|
2024-06-13 11:22:59 +05:30
|
|
|
|
2024-06-12 16:44:05 +05:30
|
|
|
from horilla_views import models
|
2024-08-05 14:22:44 +05:30
|
|
|
from horilla_views.cbv_methods import get_short_uuid
|
|
|
|
|
from horilla_views.generic.cbv.views import dynamic_create_cache
|
2024-06-12 16:44:05 +05:30
|
|
|
|
|
|
|
|
# Create your views here.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ToggleColumn(View):
|
|
|
|
|
"""
|
|
|
|
|
ToggleColumn
|
|
|
|
|
"""
|
2024-06-13 11:22:59 +05:30
|
|
|
|
2024-06-12 16:44:05 +05:30
|
|
|
def get(self, *args, **kwargs):
|
|
|
|
|
"""
|
|
|
|
|
method to toggle columns
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
query_dict = self.request.GET
|
|
|
|
|
path = query_dict["path"]
|
|
|
|
|
query_dict = dict(query_dict)
|
|
|
|
|
del query_dict["path"]
|
|
|
|
|
|
|
|
|
|
hidden_fields = [key for key, value in query_dict.items() if value[0]]
|
|
|
|
|
|
|
|
|
|
existing_instance = models.ToggleColumn.objects.filter(
|
|
|
|
|
user_id=self.request.user, path=path
|
|
|
|
|
).first()
|
|
|
|
|
|
|
|
|
|
instance = models.ToggleColumn() if not existing_instance else existing_instance
|
|
|
|
|
instance.path = path
|
|
|
|
|
instance.excluded_columns = hidden_fields
|
|
|
|
|
|
|
|
|
|
instance.save()
|
|
|
|
|
|
|
|
|
|
return HttpResponse("success")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ReloadField(View):
|
|
|
|
|
"""
|
|
|
|
|
ReloadField
|
|
|
|
|
"""
|
2024-06-13 11:22:59 +05:30
|
|
|
|
2024-06-12 16:44:05 +05:30
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
|
"""
|
|
|
|
|
Http method to reload dynamic create fields
|
|
|
|
|
"""
|
|
|
|
|
class_path = request.GET["form_class_path"]
|
|
|
|
|
reload_field = request.GET["dynamic_field"]
|
|
|
|
|
|
|
|
|
|
module_name, class_name = class_path.rsplit(".", 1)
|
|
|
|
|
module = importlib.import_module(module_name)
|
|
|
|
|
parent_form = getattr(module, class_name)()
|
|
|
|
|
|
2024-08-05 14:22:44 +05:30
|
|
|
dynamic_cache = dynamic_create_cache.get(
|
|
|
|
|
request.session.session_key + reload_field
|
|
|
|
|
)
|
|
|
|
|
form: forms.ModelForm = dynamic_cache["form"]
|
2024-06-12 16:44:05 +05:30
|
|
|
|
|
|
|
|
cache_field = dynamic_cache["dynamic_field"]
|
|
|
|
|
if cache_field != reload_field:
|
|
|
|
|
cache_field = reload_field
|
|
|
|
|
field = parent_form.fields[cache_field]
|
|
|
|
|
|
2024-08-05 14:22:44 +05:30
|
|
|
queryset = form._meta.model.objects.all()
|
2024-06-12 16:44:05 +05:30
|
|
|
queryset = field.queryset
|
|
|
|
|
choices = [(instance.id, instance) for instance in queryset]
|
|
|
|
|
choices.insert(0, ("", "Select option"))
|
|
|
|
|
choices.append(("dynamic_create", "Dynamic create"))
|
|
|
|
|
|
|
|
|
|
parent_form.fields[cache_field] = forms.ChoiceField(
|
|
|
|
|
choices=choices,
|
|
|
|
|
label=field.label,
|
|
|
|
|
required=field.required,
|
|
|
|
|
widget=forms.Select(attrs=field.widget.attrs),
|
|
|
|
|
)
|
|
|
|
|
parent_form.fields[cache_field].initial = dynamic_cache["value"]
|
|
|
|
|
|
|
|
|
|
field = parent_form[cache_field]
|
|
|
|
|
dynamic_id: str = get_short_uuid(4)
|
|
|
|
|
return render(
|
|
|
|
|
request,
|
|
|
|
|
"generic/reload_select_field.html",
|
|
|
|
|
{"field": field, "dynamic_id": dynamic_id},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ActiveTab(View):
|
|
|
|
|
def get(self, *args, **kwargs):
|
2024-06-13 11:22:59 +05:30
|
|
|
"""
|
2024-06-12 16:44:05 +05:30
|
|
|
CBV method to handle active tab
|
|
|
|
|
"""
|
|
|
|
|
path = self.request.GET.get("path")
|
|
|
|
|
target = self.request.GET.get("target")
|
|
|
|
|
if path and target and self.request.user:
|
|
|
|
|
existing_instance = models.ActiveTab.objects.filter(
|
|
|
|
|
created_by=self.request.user, path=path
|
|
|
|
|
).first()
|
|
|
|
|
|
|
|
|
|
instance = (
|
|
|
|
|
models.ActiveTab() if not existing_instance else existing_instance
|
|
|
|
|
)
|
|
|
|
|
instance.path = path
|
|
|
|
|
instance.tab_target = target
|
|
|
|
|
instance.save()
|
|
|
|
|
return JsonResponse({"message": "Success"})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ActiveGroup(View):
|
|
|
|
|
def get(self, *args, **kwargs):
|
|
|
|
|
"""
|
|
|
|
|
ActiveGroup
|
|
|
|
|
"""
|
|
|
|
|
path = self.request.GET.get("path")
|
|
|
|
|
target = self.request.GET.get("target")
|
|
|
|
|
group_field = self.request.GET.get("field")
|
|
|
|
|
if path and target and group_field and self.request.user:
|
|
|
|
|
existing_instance = models.ActiveGroup.objects.filter(
|
|
|
|
|
created_by=self.request.user,
|
|
|
|
|
path=path,
|
|
|
|
|
group_by_field=group_field,
|
|
|
|
|
).first()
|
|
|
|
|
|
|
|
|
|
instance = (
|
|
|
|
|
models.ActiveGroup() if not existing_instance else existing_instance
|
|
|
|
|
)
|
|
|
|
|
instance.path = path
|
|
|
|
|
instance.group_by_field = group_field
|
|
|
|
|
instance.group_target = target
|
|
|
|
|
instance.save()
|
|
|
|
|
return JsonResponse({"message": "Success"})
|