skillbox/server/core/views.py

65 lines
2.2 KiB
Python

from django.http.request import HttpRequest
import requests
from django.conf import settings
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http.response import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.views.decorators.csrf import ensure_csrf_cookie
from graphene_django.views import GraphQLView
from sentry_sdk.api import start_transaction
from wagtail.admin.views.pages.listing import index as wagtailadmin_explore
# For sentry perfomance monitoring
# taken from https://jerrynsh.com/how-to-monitor-python-graphql-api-with-sentry/
class SentryGraphQLView(GraphQLView):
def execute_graphql_request(
self,
request: HttpRequest,
data,
query,
variables,
operation_name,
show_graphiql,
):
operation_type = (
self.get_backend(request)
.document_from_string(self.schema, query)
.get_operation_type(operation_name)
)
with start_transaction(op=operation_type, name=operation_name):
return super().execute_graphql_request(
request, data, query, variables, operation_name, show_graphiql
)
class PrivateGraphQLView(LoginRequiredMixin, SentryGraphQLView):
pass
@ensure_csrf_cookie
def home(request):
if settings.SERVE_VIA_WEBPACK:
try:
res = requests.get(
"http://localhost:8080{}".format(request.get_full_path())
)
headers = res.headers
content_type = headers.get("content-type", "text/html")
return HttpResponse(res.text, content_type=content_type)
except Exception as e:
print("Can not connect to dev server at http://localhost:8080:", e)
return render(request, "index.html", {})
def override_wagtailadmin_explore_default_ordering(request, parent_page_id):
"""
Wrap Wagtail's explore view to change the default ordering
"""
if request.method == "GET" and "ordering" not in request.GET:
# Display reordering handles by default for children of all Page types.
return HttpResponseRedirect(request.path_info + "?ordering=ord")
return wagtailadmin_explore(request, parent_page_id=parent_page_id)