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

Updates for Python 3.x, Flask 3.x #283

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions flask_ask/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@

_DATE_PATTERNS = {
# "today", "tomorrow", "november twenty-fifth": 2015-11-25
'^\d{4}-\d{2}-\d{2}$': '%Y-%m-%d',
r'^\d{4}-\d{2}-\d{2}$': '%Y-%m-%d',
# "this week", "next week": 2015-W48
'^\d{4}-W\d{2}$': '%Y-W%U-%w',
r'^\d{4}-W\d{2}$': '%Y-W%U-%w',
# "this weekend": 2015-W48-WE
'^\d{4}-W\d{2}-WE$': '%Y-W%U-WE-%w',
r'^\d{4}-W\d{2}-WE$': '%Y-W%U-WE-%w',
# "this month": 2015-11
'^\d{4}-\d{2}$': '%Y-%m',
r'^\d{4}-\d{2}$': '%Y-%m',
# "next year": 2016
'^\d{4}$': '%Y',
r'^\d{4}$': '%Y',
}


Expand Down
31 changes: 15 additions & 16 deletions flask_ask/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import yaml
import inspect
import io
from datetime import datetime
from datetime import datetime, timezone
from functools import wraps, partial

import aniso8601
from werkzeug.contrib.cache import SimpleCache
from cachelib import SimpleCache
from werkzeug.local import LocalProxy, LocalStack
from jinja2 import BaseLoader, ChoiceLoader, TemplateNotFound
from flask import current_app, json, request as flask_request, _app_ctx_stack
from flask import current_app, json, request as flask_request, g

from . import verifier, logger
from .convert import to_date, to_time, to_timedelta
Expand Down Expand Up @@ -524,47 +524,46 @@ def wrapper(*args, **kwargs):

@property
def request(self):
return getattr(_app_ctx_stack.top, '_ask_request', None)
return g.get('_ask_request')

@request.setter
def request(self, value):
_app_ctx_stack.top._ask_request = value
g._ask_request = value

@property
def session(self):
return getattr(_app_ctx_stack.top, '_ask_session', models._Field())
return g.get('_ask_session', models._Field())

@session.setter
def session(self, value):
_app_ctx_stack.top._ask_session = value
g._ask_session = value

@property
def version(self):
return getattr(_app_ctx_stack.top, '_ask_version', None)
return g.get('_ask_version')

@version.setter
def version(self, value):
_app_ctx_stack.top._ask_version = value
g._ask_version = value

@property
def context(self):
return getattr(_app_ctx_stack.top, '_ask_context', None)
return g.get('_ask_context')

@context.setter
def context(self, value):
_app_ctx_stack.top._ask_context = value
g._ask_context = value

@property
def convert_errors(self):
return getattr(_app_ctx_stack.top, '_ask_convert_errors', None)
return g.get('_ask_convert_errors')

@convert_errors.setter
def convert_errors(self, value):
_app_ctx_stack.top._ask_convert_errors = value
g._ask_convert_errors = value

@property
def current_stream(self):
#return getattr(_app_ctx_stack.top, '_ask_current_stream', models._Field())
user = self._get_user()
if user:
stream = top_stream(self.stream_cache, user)
Expand Down Expand Up @@ -732,10 +731,10 @@ def _parse_timestamp(timestamp):
# raised by aniso8601 if raw_timestamp is not valid string
# in ISO8601 format
try:
return datetime.utcfromtimestamp(timestamp)
return datetime.fromtimestamp(timestamp, timezone.utc).replace(tzinfo=None)
except:
# relax the timestamp a bit in case it was sent in millis
return datetime.utcfromtimestamp(timestamp/1000)
return datetime.fromtimestamp(timestamp/1000, timezone.utc).replace(tzinfo=None)

raise ValueError('Invalid timestamp value! Cannot parse from either ISO8601 string or UTC timestamp.')

Expand Down
7 changes: 4 additions & 3 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
-r requirements.txt
mock==2.0.0
requests==2.13.0
tox==2.7.0
mock==5.1.0
requests==2.32.3
tox==4.23.2
urllib3==2.2.3

16 changes: 9 additions & 7 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
aniso8601==1.2.0
Flask==1.1.1
cryptography==2.1.4
pyOpenSSL==17.0.0
PyYAML==5.4
six==1.11.0
Werkzeug==0.16.1
aniso8601==1.3.0
Flask==3.1.0
cryptography==43.0.3
pyOpenSSL==24.2.1
PyYAML==6.0.2
six==1.16.0
Werkzeug==3.1.3
markupsafe==3.0.2
cachelib==0.13.0
13 changes: 8 additions & 5 deletions tests/test_audio.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import unittest
from mock import patch, MagicMock
from unittest.mock import patch
from mock import MagicMock
from flask import Flask
from flask_ask import Ask, audio
from flask_ask.models import _Field

app = Flask(__name__)

class AudioUnitTests(unittest.TestCase):

def setUp(self):
self.ask_patcher = patch('flask_ask.core.find_ask', return_value=Ask())
self.ask_patcher.start()
self.context_patcher = patch('flask_ask.models.context', return_value=MagicMock())
self.context_patcher.start()
with app.app_context():
self.ask_patcher = patch('flask_ask.core.find_ask', return_value=Ask())
self.ask_patcher.start()
self.context_patcher = patch('flask_ask.models.context', return_value=MagicMock())
self.context_patcher.start()

def tearDown(self):
self.ask_patcher.stop()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cache.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest
from mock import patch, Mock
from werkzeug.contrib.cache import SimpleCache
from cachelib import SimpleCache
from flask_ask.core import Ask
from flask_ask.cache import push_stream, pop_stream, top_stream, set_stream

Expand Down
4 changes: 2 additions & 2 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from aniso8601.timezone import UTCOffset, build_utcoffset
from flask_ask.core import Ask

from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from mock import patch, MagicMock
import json

Expand Down Expand Up @@ -58,7 +58,7 @@ def test_tries_parsing_on_valueerror(self):

# should cause a ValueError normally
with self.assertRaises(ValueError):
datetime.utcfromtimestamp(max_timestamp)
datetime.fromtimestamp(max_timestamp, timezone.utc).replace(tzinfo=None)

# should safely parse, assuming scale change needed
# note: this assert looks odd, but Py2 handles the parsing
Expand Down