test move to v2.8.2; try to bubble up template source to settings

This commit is contained in:
Joe Rhodes 2020-03-11 16:34:23 -04:00
parent da705ee1ee
commit 14a1083e8c
5 changed files with 29 additions and 46 deletions

View File

@ -1,7 +1,7 @@
import binascii import binascii
from django import forms
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django import forms
from django.forms import CharField, Field, MultipleChoiceField from django.forms import CharField, Field, MultipleChoiceField
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
@ -28,19 +28,6 @@ class GlobalIDFormField(Field):
return value return value
class GlobalIDMultipleChoiceField(MultipleChoiceField):
default_error_messages = {
"invalid_choice": _("One of the specified IDs was invalid (%(value)s)."),
"invalid_list": _("Enter a list of values."),
}
def valid_value(self, value):
# Clean will raise a validation error if there is a problem
GlobalIDFormField().clean(value)
return True
class HeaderForm(forms.Form): class HeaderForm(forms.Form):
# make a ChoiceField for 'JWT' and other methods -- use a widget? # make a ChoiceField for 'JWT' and other methods -- use a widget?
headers = forms.CharField(help_text="Enter auth method to be stored in the <head> as HTTP_AUTHORIZATION", initial="JWT ") headers = forms.CharField(help_text="Enter auth method to be stored in the <head> as HTTP_AUTHORIZATION", initial="JWT ")
@ -52,3 +39,4 @@ class HeaderForm(forms.Form):
# Remember to always return the cleaned data. # Remember to always return the cleaned data.
return data return data

View File

@ -36,13 +36,14 @@ DEFAULTS = {
# Max items returned in ConnectionFields / FilterConnectionFields # Max items returned in ConnectionFields / FilterConnectionFields
"RELAY_CONNECTION_MAX_LIMIT": 100, "RELAY_CONNECTION_MAX_LIMIT": 100,
"CAMELCASE_ERRORS": False, "CAMELCASE_ERRORS": False,
"SOURCE": None
} }
if settings.DEBUG: if settings.DEBUG:
DEFAULTS["MIDDLEWARE"] += ("graphene_django.debug.DjangoDebugMiddleware",) DEFAULTS["MIDDLEWARE"] += ("graphene_django.debug.DjangoDebugMiddleware",)
# List of settings that may be in string import notation. # List of settings that may be in string import notation.
IMPORT_STRINGS = ("MIDDLEWARE", "SCHEMA") IMPORT_STRINGS = ("MIDDLEWARE", "SCHEMA", "SOURCE")
def perform_import(val, setting_name): def perform_import(val, setting_name):

View File

@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<style> <style>
html, body { html, body, #editor {
height: 100%; height: 100%;
margin: 0; margin: 0;
overflow: hidden; overflow: hidden;
@ -14,29 +14,11 @@
} }
</style> </style>
{% if 'cdn' == TEMPLATE_SOURCE %} <link rel="stylesheet" href={% static "graphiql/css/graphiql.css" %}>
<link href="https://cdn.jsdelivr.net/npm/graphiql@{{graphiql_version}}/graphiql.css" <script src={%static "graphiql/js/fetch.min.js" %}></script>
rel="stylesheet" <script src={%static "graphiql/js/react.production.min.js" %}></script>
crossorigin="anonymous" /> <script src={%static "graphiql/js/react-dom.production.min.js" %}></script>
<script src="https://cdn.jsdelivr.net/npm/whatwg-fetch@2.0.3/fetch.min.js" <script src={%static "graphiql/js/graphiql.min.js" %}></script>
integrity="sha384-dcF7KoWRaRpjcNbVPUFgatYgAijf8DqW6NWuqLdfB5Sb4Cdbb8iHX7bHsl9YhpKa"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/react@{{react_version}}/umd/react.production.min.js"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/react-dom@{{react_version}}/umd/react-dom.production.min.js"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/graphiql@{{graphiql_version}}/graphiql.min.js"
crossorigin="anonymous"></script>
{% endif %}
{% if 'static' == TEMPLATE_SOURCE %}
<link rel="stylesheet" href={% static "graphiql/css/graphiql.css" %}>
<script src={%static "graphiql/js/fetch.min.js" %}></script>
<script src={%static "graphiql/js/react.production.min.js" %}></script>
<script src={%static "graphiql/js/react-dom.production.min.js" %}></script>
<script src={%static "graphiql/js/graphiql.min.js" %}></script>
{% endif %}
<title>{% block title %}{% endblock %}</title> <title>{% block title %}{% endblock %}</title>
<meta name="Authorization" content="{% block additional_headers %}{% endblock %}"> <meta name="Authorization" content="{% block additional_headers %}{% endblock %}">

View File

@ -6,6 +6,7 @@
{% block additional_headers %}{{ auth_header }}{% endblock %} {% block additional_headers %}{{ auth_header }}{% endblock %}
{% block content %} {% block content %}
<div id="editor"></div>
{% csrf_token %} {% csrf_token %}
<script src="{% static 'graphene_django/graphiql.js' %}"></script> <script src="{% static 'graphene_django/graphiql.js' %}"></script>
{% endblock %} {% endblock %}

View File

@ -18,7 +18,7 @@ from graphql.execution import ExecutionResult
from graphql.type.schema import GraphQLSchema from graphql.type.schema import GraphQLSchema
from graphene_django.settings import graphene_settings from graphene_django.settings import graphene_settings
from graphene_django.forms.forms import HeaderForm
class HttpError(Exception): class HttpError(Exception):
def __init__(self, response, message=None, *args, **kwargs): def __init__(self, response, message=None, *args, **kwargs):
@ -97,7 +97,7 @@ class GraphQLView(APIView):
assert isinstance( assert isinstance(
self.schema, GraphQLSchema self.schema, GraphQLSchema
), "A Schema is required to be provided to GraphQLView." ), "A Schema is required to be provided to GraphQLView."
assert not all((graphiql, batch)), "Use either graphiql or batch processing" assert not all((graphiql_headers, graphiql, batch)), "Use either graphiql, graphiql_headers, or batch processing"
# noinspection PyUnusedLocal # noinspection PyUnusedLocal
def get_root_value(self, request): def get_root_value(self, request):
@ -114,9 +114,18 @@ class GraphQLView(APIView):
@method_decorator(ensure_csrf_cookie) @method_decorator(ensure_csrf_cookie)
def dispatch(self, request, *args, **kwargs): def dispatch(self, request, *args, **kwargs):
GET_FROM_CDN = kwargs.get('GET_FROM_CDN', None) # if specified in settings.py:
if GET_FROM_CDN is None: # GRAPHENE = {
GET_FROM_CDN = 'static' # this is diconnected # why does this not make it????
# 'SOURCE': 'cdn'
# }
try:
GET_FROM_CDN = graphene_settings.SOURCE # get IF it exists
if GET_FROM_CDN is None:
# should not need
GET_FROM_CDN = 'static'
except:
GET_FROM_CDN = 'static' # this is diconnected by default
graphiql_arguments = {} graphiql_arguments = {}
if GET_FROM_CDN == 'cdn': if GET_FROM_CDN == 'cdn':
@ -128,7 +137,7 @@ class GraphQLView(APIView):
graphiql_arguments.update({'graphiql_template': 'graphene/graphiql.html'}) graphiql_arguments.update({'graphiql_template': 'graphene/graphiql.html'})
graphiql_arguments.update({'TEMPLATE_SOURCE': 'static'}) graphiql_arguments.update({'TEMPLATE_SOURCE': 'static'})
else: else:
print('Unsuppored option in setting. Choose <cdn> or <static>') print('The option %s is unsuppored option in setting. Choose <cdn> or <static>' % GET_FROM_CDN)
try: try:
if request.method.lower() not in ("get", "post"): if request.method.lower() not in ("get", "post"):
@ -386,6 +395,8 @@ class GraphQLView(APIView):
return content_type.split(";", 1)[0].lower() return content_type.split(";", 1)[0].lower()
def _get_auth_header(iQLView, request, graphiql_arguments): def _get_auth_header(iQLView, request, graphiql_arguments):
from libs.graphene_django_auth.forms import HeaderForm
# If this is a POST request then process the Form data # If this is a POST request then process the Form data
if request.method == 'POST': if request.method == 'POST':
@ -396,7 +407,7 @@ def _get_auth_header(iQLView, request, graphiql_arguments):
if form.is_valid(): if form.is_valid():
# process the data in form.cleaned_data as required (here we just write it to the model due_back field) # process the data in form.cleaned_data as required (here we just write it to the model due_back field)
auth_header = form.cleaned_data['headers'] auth_header = form.cleaned_data['headers']
# return extra stuff to put in META tag for graphiql: # return extra stuff to put in META tag for graphiql:
request.session['HTTP_AUTHORIZATION'] = auth_header request.session['HTTP_AUTHORIZATION'] = auth_header
request.session['use_graphiql'] = True request.session['use_graphiql'] = True