Skip to content

Instantly share code, notes, and snippets.

@MrAhmedElsayed
Created June 26, 2023 13:31
Show Gist options
  • Save MrAhmedElsayed/7deb82d3bf24eb7529cb9d49b535b5a4 to your computer and use it in GitHub Desktop.
Save MrAhmedElsayed/7deb82d3bf24eb7529cb9d49b535b5a4 to your computer and use it in GitHub Desktop.
def clone_kpi(request):
context = dict()
if request.method == 'GET':
context['all_kpis'] = KpiModel.objects.values_list('name', flat=True).distinct()
return JsonResponse(context, )
if request.method == 'POST' and request.is_ajax():
if request.POST.get("operation") == "clone-kpi-operation":
all_selected_checkbox = request.POST.getlist("all_selected_checkbox[]") # NEW
c_year = get_object_or_404(Year, id=request.POST.get("year"))
c_quarter = get_object_or_404(Quarter, id=request.POST.get("quarter"))
if len(all_selected_checkbox) > 0:
for i in all_selected_checkbox:
my_kpi = get_object_or_404(KpiModel, id=i)
my_kpi.pk = None
my_kpi.year = c_year
my_kpi.quarter = c_quarter
# todo: replace last character after -
my_kpi.slug = f"{slugify(my_kpi.name)}-{random_string_generator(size=10)}"
my_kpi.save()
return JsonResponse({'status': 'success'}, status=200)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment