Skip to content

Commit b632cb8

Browse files
authored
Merge pull request #434 from HebaruSan/feature/flask-2
2 parents 4e73161 + d2485b0 commit b632cb8

18 files changed

+51
-80
lines changed

KerbalStuff/app.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
from flask_login import LoginManager, current_user
1616
from flaskext.markdown import Markdown
1717
from sqlalchemy import desc
18-
from werkzeug.exceptions import HTTPException, InternalServerError
18+
from werkzeug.exceptions import HTTPException, InternalServerError, NotFound
19+
from flask.typing import ResponseReturnValue
1920
from jinja2 import ChainableUndefined
2021

2122
from .blueprints.accounts import accounts
@@ -129,8 +130,8 @@ def load_user(username: str) -> User:
129130
# 1 | 1 | 1 | 4XX in API -> jsonified_exception(e)
130131

131132

132-
@app.errorhandler(404)
133-
def handle_404(e: HTTPException) -> Union[Tuple[str, int], werkzeug.wrappers.Response]:
133+
@app.errorhandler(NotFound)
134+
def handle_404(e: NotFound) -> ResponseReturnValue:
134135
# Switch out the default message
135136
if e.description == werkzeug.exceptions.NotFound.description:
136137
e.description = "Requested page not found. Looks like this was deleted, or maybe was never here."
@@ -143,7 +144,7 @@ def handle_404(e: HTTPException) -> Union[Tuple[str, int], werkzeug.wrappers.Res
143144

144145
# This one handles the remaining 4XX errors. JSONified for XHR requests, otherwise the user gets a nice error screen.
145146
@app.errorhandler(HTTPException)
146-
def handle_http_exception(e: HTTPException) -> Union[Tuple[str, int], werkzeug.wrappers.Response]:
147+
def handle_http_exception(e: HTTPException) -> ResponseReturnValue:
147148
if e.code and e.code >= 500:
148149
return handle_generic_exception(e)
149150
if request.path.startswith("/api/") \
@@ -155,7 +156,7 @@ def handle_http_exception(e: HTTPException) -> Union[Tuple[str, int], werkzeug.w
155156
# And this one handles everything leftover, that means, real otherwise unhandled exceptions.
156157
# https://flask.palletsprojects.com/en/1.1.x/errorhandling/#unhandled-exceptions
157158
@app.errorhandler(Exception)
158-
def handle_generic_exception(e: Union[Exception, HTTPException]) -> Union[Tuple[str, int], werkzeug.wrappers.Response]:
159+
def handle_generic_exception(e: Exception) -> ResponseReturnValue:
159160
site_logger.exception(e)
160161
try:
161162
db.rollback()
@@ -179,7 +180,7 @@ def handle_generic_exception(e: Union[Exception, HTTPException]) -> Union[Tuple[
179180

180181

181182
@app.teardown_request
182-
def teardown_request(exception: Optional[Exception]) -> None:
183+
def teardown_request(exception: Optional[BaseException]) -> None:
183184
db.close()
184185

185186

KerbalStuff/blueprints/login_oauth.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ def get_github_oath() -> Tuple[str, OAuthRemoteApp]:
6666
if resp is None:
6767
raise Exception(
6868
f"Access denied: reason={request.args['error']} error={request.args['error_description']}")
69-
if 'error' in resp:
70-
return jsonify(resp)
7169
session['github_token'] = (resp['access_token'], '')
7270
gh_info = github.get('user').data
7371
return gh_info['login'], github

KerbalStuff/common.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -127,25 +127,6 @@ def wrapper(*args: str, **kwargs: int) -> werkzeug.wrappers.Response:
127127
return wrapper
128128

129129

130-
def cors(f: Callable[..., Any]) -> Callable[..., Any]:
131-
@wraps(f)
132-
def wrapper(*args: str, **kwargs: int) -> Tuple[str, int]:
133-
res = f(*args, **kwargs)
134-
if request.headers.get('x-cors-status', False):
135-
if isinstance(res, tuple):
136-
json_text = res[0].data
137-
code = res[1]
138-
else:
139-
json_text = res.data
140-
code = 200
141-
o = json.loads(json_text)
142-
o['x-status'] = code
143-
return jsonify(o)
144-
return res
145-
146-
return wrapper
147-
148-
149130
def paginate_query(query: Query, page_size: int = 30) -> Tuple[List[Mod], int, int]:
150131
total_pages = math.ceil(query.count() / page_size)
151132
page = get_page()

KerbalStuff/database.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from sqlalchemy import create_engine
2-
from sqlalchemy.ext.declarative import declarative_base
3-
from sqlalchemy.orm import scoped_session, sessionmaker
2+
from sqlalchemy.orm import scoped_session, sessionmaker, declarative_base
43

54
from .config import _cfg
65

KerbalStuff/kerbdown.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import urllib.parse
22
from urllib.parse import parse_qs, urlparse
3-
from typing import Dict, Any, Match, Tuple
3+
from typing import Dict, Any, Match, Tuple, Optional
44

55
from markdown import Markdown
66
from markdown.extensions import Extension
77
from markdown.inlinepatterns import InlineProcessor
8-
from markdown.util import etree
8+
from xml.etree import ElementTree
99

1010

1111
class EmbedInlineProcessor(InlineProcessor):
@@ -23,18 +23,19 @@ def __init__(self, md: Markdown, configs: Dict[str, Any]) -> None:
2323
super().__init__(self.EMBED_RE, md)
2424
self.config = configs
2525

26-
def handleMatch(self, m: Match[str], data: str) -> Tuple[etree.Element, int, int]: # type: ignore[override]
26+
def handleMatch(self, m: Match[str], data: str) -> Tuple[ElementTree.Element, int, int]: # type: ignore[override]
2727
d = m.groupdict()
2828
url = d.get('url')
29+
el: Optional[ElementTree.Element]
2930
if not url:
30-
el = etree.Element('span')
31+
el = ElementTree.Element('span')
3132
el.text = "[[]]"
3233
return el, m.start(0), m.end(0)
3334
try:
3435
link = urlparse(url)
3536
host = link.hostname
3637
except:
37-
el = etree.Element('span')
38+
el = ElementTree.Element('span')
3839
el.text = "[[" + url + "]]"
3940
return el, m.start(0), m.end(0)
4041
el = None
@@ -44,16 +45,16 @@ def handleMatch(self, m: Match[str], data: str) -> Tuple[etree.Element, int, int
4445
except:
4546
pass
4647
if el is None:
47-
el = etree.Element('span')
48+
el = ElementTree.Element('span')
4849
el.text = "[[" + url + "]]"
4950
return el, m.start(0), m.end(0)
5051

5152
def _get_youtube_id(self, link: urllib.parse.ParseResult) -> str:
5253
return (link.path if link.netloc == 'youtu.be'
5354
else parse_qs(link.query)['v'][0])
5455

55-
def _embed_youtube(self, vid_id: str) -> etree.Element:
56-
el = etree.Element('iframe')
56+
def _embed_youtube(self, vid_id: str) -> ElementTree.Element:
57+
el = ElementTree.Element('iframe')
5758
el.set('width', '100%')
5859
el.set('height', '600')
5960
el.set('frameborder', '0')

KerbalStuff/stubs/jinja2.pyi

Lines changed: 0 additions & 8 deletions
This file was deleted.

alembic/versions/2020_06_23_17_49_36-85be165bc5dc.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111
from packaging import version
1212
from sqlalchemy import orm, Column, Integer, Unicode, DateTime, String, ForeignKey
13-
from sqlalchemy.ext.declarative import declarative_base
14-
from sqlalchemy.orm import relationship, backref
13+
from sqlalchemy.orm import relationship, backref, declarative_base
1514

1615
# revision identifiers, used by Alembic.
1716
revision = '85be165bc5dc'

alembic/versions/2020_07_20_19_00_00-73c9d707134b.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from alembic import op
1515
import sqlalchemy as sa
1616

17-
Base = sa.ext.declarative.declarative_base()
17+
Base = sa.orm.declarative_base()
1818

1919

2020
class ModVersion(Base): # type: ignore

alembic/versions/2021_06_28_19_00_00-17fbd4ff8193.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from alembic import op
1414
import sqlalchemy as sa
1515

16-
Base = sa.ext.declarative.declarative_base()
16+
Base = sa.orm.declarative_base()
1717

1818

1919
class User(Base): # type: ignore

requirements-backend.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ celery
66
click
77
dnspython
88
flameprof
9-
Flask<2 # Needs testing before upgrading
9+
Flask
1010
Flask-Login
1111
Flask-Markdown
1212
Flask-OAuthlib
1313
future
1414
GitPython
1515
gunicorn
16-
Jinja2<3 # See Flask
16+
Jinja2
1717
Markdown
1818
MarkupSafe
1919
oauthlib

0 commit comments

Comments
 (0)