def new_visualization(request): demo_viz = ( SCOTUSMap.objects.filter(published=True, deleted=False).annotate( Count("clusters")).filter( # Ensures that we only show good stuff on homepage clusters__count__gt=5, clusters__count__lt=15, ).order_by("-date_published", "-date_modified", "-date_created")[:1]) context = { "SCDB_LATEST_CASE": settings.SCDB_LATEST_CASE.isoformat(), "demo_viz": demo_viz, "private": True, } if request.method == "POST": form = VizForm(request.POST) context["form"] = form if form.is_valid(): # Process the data in form.cleaned_data cd = form.cleaned_data start, end = reverse_endpoints_if_needed(cd["cluster_start"], cd["cluster_end"]) viz = SCOTUSMap( user=request.user, cluster_start=start, cluster_end=end, title=cd["title"], notes=cd["notes"], ) status, viz = build_visualization(viz) if status == "too_many_nodes": msg = message_dict[status] messages.add_message(request, msg["level"], msg["message"]) return render(request, "new_visualization.html", context) elif status == "too_few_nodes": msg = message_dict[status] messages.add_message(request, msg["level"], msg["message"]) return render( request, "new_visualization.html", { "form": form, "private": True }, ) return HttpResponseRedirect( reverse( "view_visualization", kwargs={ "pk": viz.pk, "slug": viz.slug }, )) else: context["form"] = VizForm() return render(request, "new_visualization.html", context)
def perform_create(self, serializer): cd = serializer.validated_data start, end = reverse_endpoints_if_needed(cd["cluster_start"], cd["cluster_end"]) viz = serializer.save(user=self.request.user, cluster_start=start, cluster_end=end) status, viz = build_visualization(viz) return status