Skip to content

Commit

Permalink
Bump to new SPF beta
Browse files Browse the repository at this point in the history
New min Sanic version is 18.12
Remove pytz dependency
  • Loading branch information
ashleysommer committed Jan 31, 2020
1 parent 2023300 commit d805416
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 95 deletions.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Compatibility
=============

Sanic-RestPlus requires Python 3.5+.
Sanic-RestPlus works with Sanic v18.12+


Installation
Expand Down
5 changes: 2 additions & 3 deletions requirements/install.pip
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
aniso8601>=0.82
jsonschema
methodtools
sanic>=0.8.3
sanic>=18.12.0
sanic-jinja2-spf>=0.7.5
sanic-plugins-framework>=0.8.2,<0.9.0
pytz
sanic-plugins-framework>=0.9.0.b1
2 changes: 1 addition & 1 deletion sanic_restplus/__about__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- coding: latin-1 -*-
#
__version__ = '0.4.1.post1'
__version__ = '0.5.0.b1'
__description__ = 'Fully featured framework for fast, easy and documented API development with Sanic'
33 changes: 13 additions & 20 deletions sanic_restplus/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from sanic.router import RouteExists, url_hash
from sanic.response import text, BaseHTTPResponse
from sanic.views import HTTPMethodView
from spf.plugin import FutureRoute

try:
from sanic.response import ALL_STATUS_CODES
Expand Down Expand Up @@ -58,15 +59,6 @@

class Api(object):
'''
The main entry point for the application.
You need to initialize it with a Flask Application: ::
>>> app = Sanic(__name__)
>>> api = Api(app)
Alternatively, you can use :meth:`init_app` to set the Flask application
after it has been constructed.
The endpoint parameter prefix all views and resources:
- The API root/documentation will be ``{endpoint}.root``
Expand Down Expand Up @@ -279,24 +271,25 @@ def _register_doc(self):
(spf, plugin_name, plugin_url_prefix) = self.spf_reg
context = restplus.get_context_from_spf(self.spf_reg)
if self._add_specs and self._doc:
doc_route_name = '{}.{}_doc'.format(plugin_name, str(self._uid))
doc_endpoint_name = '{}_doc'.format(str(self._uid))

def _render_doc(*args, **kwargs):
nonlocal self
return self.render_doc(*args, **kwargs)
render_doc = wraps(self.render_doc)(_render_doc)
render_doc.__name__ = doc_route_name
spf._plugin_register_route(render_doc, restplus, context, self._doc, with_context=True)

render_doc.__name__ = doc_endpoint_name
r = FutureRoute(render_doc, self._doc, (), {'with_context': True})
spf._register_route_helper(r, spf, restplus, context, plugin_name, plugin_url_prefix)
if self._doc != root_path:
try:# app_or_blueprint.add_url_rule(self.prefix or '/', 'root', self.render_root)
root_route_name = '{}.{}_root'.format(plugin_name, str(self._uid))
root_endpoint_name = '{}_root'.format(str(self._uid))
def _render_root(*args, **kwargs):
nonlocal self
return self.render_root(*args, **kwargs)
render_root = wraps(self.render_root)(_render_root)
render_root.__name__ = root_route_name
spf._plugin_register_route(render_root, restplus, context, root_path)
render_root.__name__ = root_endpoint_name
r = FutureRoute(render_root, root_path, (), {})
spf._register_route_helper(r, spf, restplus, context, plugin_name, plugin_url_prefix)

except RouteExists:
pass
Expand All @@ -319,7 +312,7 @@ def _register_view(self, resource, namespace, *urls, **kwargs):
resource_class_args = kwargs.pop('resource_class_args', ())
resource_class_kwargs = kwargs.pop('resource_class_kwargs', {})
(spf, plugin_name, plugin_url_prefix) = self.spf_reg
endpoint = "{}.{}".format(plugin_name, endpoint)
#endpoint = "{}.{}".format(plugin_name, endpoint)
resource.mediatypes = self.mediatypes_method() # Hacky
resource.endpoint = endpoint
methods = resource.methods
Expand Down Expand Up @@ -349,10 +342,10 @@ def _register_view(self, resource, namespace, *urls, **kwargs):
rule = partial(self._complete_url, url)
else:
# If we've got no Blueprint, just build a url with no prefix
rule = self._complete_url(url, plugin_url_prefix)
rule = self._complete_url(url, '')
# Add the url to the application or blueprint
spf._plugin_register_route(resource_func, restplus, context, rule,
methods=methods, with_context=True, **kwargs)
r = FutureRoute(resource_func, rule, (), {'with_context': True})
spf._register_route_helper(r, spf, restplus, context, plugin_name, plugin_url_prefix)

def output(self, resource):
"""
Expand Down
19 changes: 9 additions & 10 deletions sanic_restplus/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,15 @@ def my_type(value):
import re
import socket

from datetime import datetime, time, timedelta
from datetime import datetime, time, timedelta, timezone
from email.utils import parsedate_tz, mktime_tz
from urllib.parse import urlparse

import aniso8601
import pytz

# Constants for upgrading date-based intervals to full datetimes.
START_OF_DAY = time(0, 0, 0, tzinfo=pytz.UTC)
END_OF_DAY = time(23, 59, 59, 999999, tzinfo=pytz.UTC)
START_OF_DAY = time(0, 0, 0, tzinfo=timezone.utc)
END_OF_DAY = time(23, 59, 59, 999999, tzinfo=timezone.utc)


netloc_regex = re.compile(
Expand Down Expand Up @@ -322,11 +321,11 @@ def _normalize_interval(start, end, value):
end = datetime.combine(end, START_OF_DAY)

if start.tzinfo is None:
start = pytz.UTC.localize(start)
end = pytz.UTC.localize(end)
start = start.replace(tzinfo=timezone.utc)
end = end.replace(tzinfo=timezone.utc)
else:
start = start.astimezone(pytz.UTC)
end = end.astimezone(pytz.UTC)
start = start.astimezone(timezone.utc)
end = end.astimezone(timezone.utc)

return start, end

Expand Down Expand Up @@ -531,9 +530,9 @@ def datetime_from_rfc822(value):
timetuple = parsedate_tz(value)
timestamp = mktime_tz(timetuple)
if timetuple[-1] is None:
return datetime.fromtimestamp(timestamp).replace(tzinfo=pytz.utc)
return datetime.fromtimestamp(timestamp).replace(tzinfo=timezone.utc)
else:
return datetime.fromtimestamp(timestamp, pytz.utc)
return datetime.fromtimestamp(timestamp, timezone.utc)
except Exception:
raise ValueError('Invalid date literal "{0}"'.format(raw))

Expand Down
21 changes: 10 additions & 11 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@
from __future__ import unicode_literals

from collections import OrderedDict
from datetime import date, datetime
from datetime import date, datetime, timezone, timedelta
from decimal import Decimal
from functools import partial

import pytz
import pytest

from flask import Blueprint
from sanic_restplus import fields, Api

cet = timezone(timedelta(hours=1), 'CET')

class FieldTestCase(object):
field_class = None
Expand Down Expand Up @@ -477,9 +476,9 @@ def test_max_exclusive(self):
(datetime(2011, 1, 1), 'Sat, 01 Jan 2011 00:00:00 -0000'),
(datetime(2011, 1, 1, 23, 59, 59),
'Sat, 01 Jan 2011 23:59:59 -0000'),
(datetime(2011, 1, 1, 23, 59, 59, tzinfo=pytz.utc),
(datetime(2011, 1, 1, 23, 59, 59, tzinfo=timezone.utc),
'Sat, 01 Jan 2011 23:59:59 -0000'),
(datetime(2011, 1, 1, 23, 59, 59, tzinfo=pytz.timezone('CET')),
(datetime(2011, 1, 1, 23, 59, 59, tzinfo=cet),
'Sat, 01 Jan 2011 22:59:59 -0000')
])
def test_rfc822_value(self, value, expected):
Expand All @@ -492,11 +491,11 @@ def test_rfc822_value(self, value, expected):
'2011-01-01T23:59:59'),
(datetime(2011, 1, 1, 23, 59, 59, 1000),
'2011-01-01T23:59:59.001000'),
(datetime(2011, 1, 1, 23, 59, 59, tzinfo=pytz.utc),
(datetime(2011, 1, 1, 23, 59, 59, tzinfo=timezone.utc),
'2011-01-01T23:59:59+00:00'),
(datetime(2011, 1, 1, 23, 59, 59, 1000, tzinfo=pytz.utc),
(datetime(2011, 1, 1, 23, 59, 59, 1000, tzinfo=timezone.utc),
'2011-01-01T23:59:59.001000+00:00'),
(datetime(2011, 1, 1, 23, 59, 59, tzinfo=pytz.timezone('CET')),
(datetime(2011, 1, 1, 23, 59, 59, tzinfo=cet),
'2011-01-01T23:59:59+01:00')
])
def test_iso8601_value(self, value, expected):
Expand Down Expand Up @@ -587,9 +586,9 @@ def test_max_exclusive(self):
(datetime(2011, 1, 1), '2011-01-01'),
(datetime(2011, 1, 1, 23, 59, 59), '2011-01-01'),
(datetime(2011, 1, 1, 23, 59, 59, 1000), '2011-01-01'),
(datetime(2011, 1, 1, 23, 59, 59, tzinfo=pytz.utc), '2011-01-01'),
(datetime(2011, 1, 1, 23, 59, 59, 1000, tzinfo=pytz.utc), '2011-01-01'),
(datetime(2011, 1, 1, 23, 59, 59, tzinfo=pytz.timezone('CET')), '2011-01-01')
(datetime(2011, 1, 1, 23, 59, 59, tzinfo=timezone.utc), '2011-01-01'),
(datetime(2011, 1, 1, 23, 59, 59, 1000, tzinfo=timezone.utc), '2011-01-01'),
(datetime(2011, 1, 1, 23, 59, 59, tzinfo=cet), '2011-01-01')
])
def test_value(self, value, expected):
self.assert_field(fields.Date(), value, expected)
Expand Down
Loading

0 comments on commit d805416

Please sign in to comment.