Implement graphs properly
This commit is contained in:
@@ -41,14 +41,17 @@ class Order(LoginRequiredMixin, View):
|
||||
def get(self, request, plan_name):
|
||||
plan = Plan.objects.get(name=plan_name)
|
||||
try:
|
||||
session = stripe.checkout.Session.create(
|
||||
payment_method_types=settings.ALLOWED_PAYMENT_METHODS,
|
||||
mode="subscription",
|
||||
customer=request.user.stripe_id,
|
||||
line_items=assemble_plan_map(product_id_filter=plan.product_id),
|
||||
success_url=request.build_absolute_uri(reverse("success")),
|
||||
cancel_url=request.build_absolute_uri(reverse("cancel")),
|
||||
)
|
||||
cast = {
|
||||
"payment_method_types": settings.ALLOWED_PAYMENT_METHODS,
|
||||
"mode": "subscription",
|
||||
"customer": request.user.stripe_id,
|
||||
"line_items": assemble_plan_map(product_id_filter=plan.product_id),
|
||||
"success_url": request.build_absolute_uri(reverse("success")),
|
||||
"cancel_url": request.build_absolute_uri(reverse("cancel")),
|
||||
}
|
||||
if request.user.is_superuser:
|
||||
cast["discounts"] = [{"coupon": settings.STRIPE_ADMIN_COUPON}]
|
||||
session = stripe.checkout.Session.create(**cast)
|
||||
Session.objects.create(user=request.user, session=session.id)
|
||||
return redirect(session.url)
|
||||
# return JsonResponse({'id': session.id})
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
from chartjs.views.lines import BaseLineChartView
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
|
||||
|
||||
class CSRFExemptMixin(object):
|
||||
@method_decorator(csrf_exempt)
|
||||
def dispatch(self, *args, **kwargs):
|
||||
return super(CSRFExemptMixin, self).dispatch(*args, **kwargs)
|
||||
|
||||
|
||||
class VolumeChartJSONView(CSRFExemptMixin, LoginRequiredMixin, BaseLineChartView):
|
||||
def post(self, request, *args, **kwargs):
|
||||
print("POST")
|
||||
context = self.get_context_data(**kwargs)
|
||||
return self.render_to_response(context)
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
data = super(VolumeChartJSONView, self).get_context_data(**kwargs)
|
||||
# data["colors"] = islice(next_color(), 0, 50)
|
||||
print("KWARGS", kwargs)
|
||||
return data
|
||||
|
||||
def get_labels(self):
|
||||
"""Return 7 labels for the x-axis."""
|
||||
return ["January", "February", "March", "April", "May", "June", "July"]
|
||||
|
||||
def get_providers(self):
|
||||
"""Return names of datasets."""
|
||||
return ["Central", "Eastside", "Westside"]
|
||||
|
||||
def get_data(self):
|
||||
"""Return 3 datasets to plot."""
|
||||
|
||||
return [
|
||||
[75, 44, 92, 11, 44, 95, 35],
|
||||
[41, 92, 18, 3, 73, 87, 92],
|
||||
[87, 21, 94, 3, 90, 13, 65],
|
||||
]
|
||||
|
||||
|
||||
class SentimentChartJSONView(CSRFExemptMixin, LoginRequiredMixin, BaseLineChartView):
|
||||
def post(self, request, *args, **kwargs):
|
||||
print("POST")
|
||||
print(request.POST)
|
||||
context = self.get_context_data(**kwargs)
|
||||
return self.render_to_response(context)
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
data = super(SentimentChartJSONView, self).get_context_data(**kwargs)
|
||||
# data["colors"] = islice(next_color(), 0, 50)
|
||||
print("KWARGS", kwargs)
|
||||
return data
|
||||
|
||||
def get_labels(self):
|
||||
"""Return 7 labels for the x-axis."""
|
||||
return ["January", "February", "March", "April", "May", "June", "July"]
|
||||
|
||||
def get_providers(self):
|
||||
"""Return names of datasets."""
|
||||
return ["Central", "Eastside", "Westside"]
|
||||
|
||||
def get_data(self):
|
||||
"""Return 3 datasets to plot."""
|
||||
|
||||
return [
|
||||
[75, 44, 92, 11, 44, 95, 35],
|
||||
[41, 92, 18, 3, 73, 87, 92],
|
||||
[87, 21, 94, 3, 90, 13, 65],
|
||||
]
|
||||
0
core/views/dynamic/__init__.py
Normal file
0
core/views/dynamic/__init__.py
Normal file
86
core/views/dynamic/search.py
Normal file
86
core/views/dynamic/search.py
Normal file
@@ -0,0 +1,86 @@
|
||||
from rest_framework.views import APIView
|
||||
import logging
|
||||
from django.conf import settings
|
||||
from django.shortcuts import render
|
||||
from django.http import HttpResponse, JsonResponse
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
import json
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
from django.views import View
|
||||
from rest_framework.parsers import JSONParser
|
||||
from core.lib.opensearch import initialise_opensearch, run_main_query
|
||||
client = initialise_opensearch()
|
||||
|
||||
|
||||
def query_results(request, post_params, api=False):
|
||||
fields = None
|
||||
if "fields" in request.POST:
|
||||
fields = request.POST.getlist("fields")
|
||||
if "size" in request.POST:
|
||||
size = request.POST["size"]
|
||||
if "query" in request.POST:
|
||||
query = request.POST["query"]
|
||||
results = run_main_query(client, request.user, query, fields, size)
|
||||
if not results:
|
||||
return False
|
||||
# pp.pprint(results)
|
||||
results_parsed = []
|
||||
if "hits" in results.keys():
|
||||
if "hits" in results["hits"]:
|
||||
for item in results["hits"]["hits"]:
|
||||
results_parsed.append(item["_source"])
|
||||
context = {
|
||||
"query": query,
|
||||
"results": results_parsed,
|
||||
"card": results["hits"]["total"]["value"],
|
||||
"took": results["took"],
|
||||
"redacted": results["redacted"],
|
||||
"exemption": results["exemption"],
|
||||
"fields": settings.OPENSEARCH_MAIN_SEARCH_FIELDS,
|
||||
"sizes": settings.OPENSEARCH_MAIN_SIZES,
|
||||
"timescales": settings.OPENSEARCH_MAIN_TIMESCALES,
|
||||
}
|
||||
return context
|
||||
|
||||
class Search(LoginRequiredMixin, View):
|
||||
#parser_classes = [JSONParser]
|
||||
template_name = "ui/results.html"
|
||||
plan_name = "drilldown"
|
||||
|
||||
def post(self, request):
|
||||
if not request.user.has_plan(self.plan_name):
|
||||
return render(request, "denied.html")
|
||||
|
||||
context = query_results(request, request.POST)
|
||||
print("context: ", context)
|
||||
context['data'] = json.dumps(
|
||||
[
|
||||
{
|
||||
'id': item.get('id'),
|
||||
'value': item.get("sentiment", 0),
|
||||
'date': item.get("ts"),
|
||||
}
|
||||
for item in context["results"]
|
||||
]
|
||||
)
|
||||
print("context['data']: ", context['data'])
|
||||
if context:
|
||||
print("OGING TO RENDER")
|
||||
return render(request, self.template_name, context)
|
||||
else:
|
||||
return HttpResponse("No results")
|
||||
|
||||
class APISearch(LoginRequiredMixin, View):
|
||||
#parser_classes = [JSONParser]
|
||||
template_name = "ui/results.html"
|
||||
plan_name = "drilldown"
|
||||
|
||||
def post(self, request):
|
||||
print("POST")
|
||||
if not request.user.has_plan(self.plan_name):
|
||||
return JsonResponse({"success": False})
|
||||
print("PERMS")
|
||||
|
||||
context = query_results(request, request.POST)
|
||||
print("CONTEXT", context)
|
||||
return JsonResponse(context)
|
||||
Reference in New Issue
Block a user