Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python 3 stuff + a URL fix #126

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
38 changes: 17 additions & 21 deletions tastypie_swagger/mapping.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import datetime
import logging

from django.db.models.sql.constants import QUERY_TERMS
try:
from django.db.models.sql.constants import QUERY_TERMS
except ImportError:
# NOTE(pierrealix): django.db.models.sql.constants.QUERY_TERMS has been
# removed in django2.1.
# This is a hack...
QUERY_TERMS = {
'exact', 'iexact', 'contains', 'icontains', 'gt', 'gte', 'lt', 'lte', 'in',
'startswith', 'istartswith', 'endswith', 'iendswith', 'range', 'year',
'month', 'day', 'week_day', 'hour', 'minute', 'second', 'isnull', 'search',
'regex', 'iregex',
}

try:
from django.utils.encoding import force_text
Expand Down Expand Up @@ -160,9 +171,9 @@ def build_parameters_from_ordering(self):
'name': "order_by",
'dataType': "String",
'required': False,
'description': unicode("Orders the result set based on the selection. "
"Ascending order by default, prepending the '-' "
"sign change the sorting order to descending"),
'description': ("Orders the result set based on the selection. "
"Ascending order by default, prepending the '-' "
"sign change the sorting order to descending"),
'allowableValues': {
'valueType' : "LIST",
'values': values
Expand Down Expand Up @@ -204,22 +215,7 @@ def build_parameters_from_filters(self, prefix="", method='GET'):
has_related_resource = hasattr(self.resource.fields[name], 'get_related_resource')

if not has_related_resource:
#This code has been mostly sucked from the tastypie lib
if getattr(self.resource._meta, 'queryset', None) is not None:
# Get the possible query terms from the current QuerySet.
if hasattr(self.resource._meta.queryset.query.query_terms, 'keys'):
# Django 1.4 & below compatibility.
field = self.resource._meta.queryset.query.query_terms.keys()
else:
# Django 1.5+.
field = self.resource._meta.queryset.query.query_terms
else:
if hasattr(QUERY_TERMS, 'keys'):
# Django 1.4 & below compatibility.
field = QUERY_TERMS.keys()
else:
# Django 1.5+.
field = QUERY_TERMS
field = QUERY_TERMS

else: # Show all params from related model
# Add a subset of filter only foreign-key compatible on the relation itself.
Expand Down Expand Up @@ -304,7 +300,7 @@ def build_parameters_from_extra_action(self, method, fields, resource_type):
name=name,
dataType=field['dataType'],
required=field['required'],
description=unicode(field['description'])
description=field['description']
))


Expand Down
25 changes: 16 additions & 9 deletions tastypie_swagger/urls.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
from .views import SwaggerView, ResourcesView, SchemaView
try:
from django.conf.urls import patterns, include, url
from django.conf.urls import include, url

urlpatterns = [
url(r'^$', SwaggerView.as_view(), name='index'),
url(r'^resources/$', ResourcesView.as_view(), name='resources'),
url(r'^schema/(?P<resource>\S+)$', SchemaView.as_view()),
url(r'^schema/$', SchemaView.as_view(), name='schema'),
]
except ImportError:
from django.conf.urls.defaults import patterns, include, url
from django.conf.urls.defaults import patterns, include, url

from .views import SwaggerView, ResourcesView, SchemaView
urlpatterns = patterns('',
url(r'^$', SwaggerView.as_view(), name='index'),
url(r'^resources/$', ResourcesView.as_view(), name='resources'),
url(r'^schema/(?P<resource>\S+)$', SchemaView.as_view()),
url(r'^schema/$', SchemaView.as_view(), name='schema'),
)

urlpatterns = patterns('',
url(r'^$', SwaggerView.as_view(), name='index'),
url(r'^resources/$', ResourcesView.as_view(), name='resources'),
url(r'^schema/(?P<resource>\S+)$', SchemaView.as_view()),
url(r'^schema/$', SchemaView.as_view(), name='schema'),
)
4 changes: 3 additions & 1 deletion tastypie_swagger/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.views.generic import TemplateView
from django.http import HttpResponse, Http404
from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import reverse
from django.urls import reverse

import tastypie

Expand Down Expand Up @@ -125,6 +125,8 @@ class SchemaView(TastypieApiMixin, SwaggerApiDataMixin, JSONView):
def get_context_data(self, *args, **kwargs):
# Verify matching tastypie resource exists
resource_name = kwargs.get('resource', None)
if resource_name:
resource_name = resource_name.strip('/')
if not resource_name in self.tastypie_api._registry:
raise Http404

Expand Down