from django.http import HttpResponse, HttpResponseRedirect from django.urls import reverse from django.views.generic.list import ListView from django.views.generic import TemplateView from .models import * class FramadateListView(ListView): model = Framadate template_name = 'framadate/framadate_list.html' class NewFramadate(TemplateView): template_name = 'framadate/new_framadate.html' def post(self, request, *args, **kwargs): new_framadate = Framadate.objects.create(name=request.POST['name']) dates = request.POST.getlist('date') times = request.POST.getlist('time') for i in range(len(dates)): FramadateDate.objects.create(framadate=new_framadate, day=dates[i], time=(times[i] if times[i] else None)) return HttpResponseRedirect("/framadate/" + str(new_framadate.id)) class FramadateDetail(TemplateView): template_name = 'framadate/framadate_detail.html' def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) addContextForFramadate(context, **kwargs) return context class FramadateDetailEdit(TemplateView): template_name = 'framadate/framadate_detail.html' def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) addContextForFramadate(context, **kwargs) # data to edit context["edditing"] = True context["name"] = NameInFramadate.objects.get(pk=kwargs['name_id']) return context def save_all(request, pk): if request.method != "POST": return HttpResponse("Not a POST request") if request.POST.get("name") == "": return HttpResponseRedirect(reverse('framadate_detail', args=[pk])) framadate = Framadate.objects.get(pk=pk) # deleting if request.POST.get("delete"): try: NameInFramadate.objects.get(pk=request.POST['delete_id']).delete() except: return HttpResponse("Ups something went wrong, can't delete") return HttpResponseRedirect(reverse('framadate_detail', args=[pk])) # new if not request.POST.get("delete_id"): new_name = NameInFramadate.objects.create(framadate=framadate, name=request.POST['name']) for day in framadate.days_set.all(): Record.objects.create(nameInFramadate=new_name, framadateDate=day, choice=(request.POST["choice_" + str(day.id)] if request.POST.get("choice_" + str(day.id)) else 0)) else: # edditing name_id = request.POST['delete_id'] name = NameInFramadate.objects.get(pk=name_id) name.name = request.POST['name'] name.save() for record in name.records_set.all(): record.choice = (request.POST["choice_" + str(record.id)] if request.POST.get("choice_" + str(record.id)) else 0) record.save() return HttpResponseRedirect(reverse('framadate_detail', args=[pk])) def delete_framadate(request, pk): framadate = Framadate.objects.get(pk=pk) framadate.delete() return HttpResponseRedirect("/framadate") def addContextForFramadate(context, **kwargs): context['framadate'] = Framadate.objects.get(pk=kwargs['pk']) context['choices'] = Choice.choices context['best_dates'] = [] best_dates = [] best = -1 for day in context['framadate'].days_set.all(): # kinda slow? count = 0 for record in day.records_set.all(): if record.choice == 3: count += 1 if count > best: best_dates = [day] best = count elif count == best: best_dates.append(day) context['best_dates'] = best_dates context['max_count'] = best