Some project re-org, and add pytest.
Rename flaskr/ to undercover/ Rename writing_templates/ to letter_templates/ Put db testing code into __main__ check Break css optimization into its own file and add simple testing. Rename writing.py to routes.py. Move cached index directly into routes.py
This commit is contained in:
parent
0eee7d0ef7
commit
ff659db77f
|
@ -1,51 +0,0 @@
|
||||||
# Copyright Sage Vaillancourt 2021
|
|
||||||
|
|
||||||
import os
|
|
||||||
from flask import Flask
|
|
||||||
|
|
||||||
import writing
|
|
||||||
|
|
||||||
INDEX = None
|
|
||||||
|
|
||||||
|
|
||||||
def optimize_css(css: str) -> str:
|
|
||||||
import re
|
|
||||||
minified_with_comments = "".join(list(map(
|
|
||||||
lambda line: re.sub(r'\s*([:{])\s*', '\1', re.sub(r'^\s*', '', line)),
|
|
||||||
css.split('\n')
|
|
||||||
)))
|
|
||||||
return re.sub(r'/\*[\s\S]*?\*/', "", minified_with_comments)
|
|
||||||
|
|
||||||
|
|
||||||
def optimize_css_file():
|
|
||||||
root = os.path.dirname(os.getcwd())
|
|
||||||
static_dir = root + '/undercover/flaskr/static/'
|
|
||||||
css = open(static_dir + 'styles.css', 'r').read()
|
|
||||||
minified = optimize_css(css)
|
|
||||||
minified_file = open(static_dir + 'styles_min.css', 'w')
|
|
||||||
minified_file.write(minified)
|
|
||||||
minified_file.close()
|
|
||||||
|
|
||||||
|
|
||||||
def create_app(test_config=None):
|
|
||||||
optimize_css_file()
|
|
||||||
|
|
||||||
app = Flask(__name__, instance_relative_config=True)
|
|
||||||
app.config.from_mapping(
|
|
||||||
SECRET_KEY='dev',
|
|
||||||
DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'),
|
|
||||||
)
|
|
||||||
|
|
||||||
if test_config is None:
|
|
||||||
app.config.from_pyfile('config.py', silent=True)
|
|
||||||
else:
|
|
||||||
app.config.from_mapping(test_config)
|
|
||||||
|
|
||||||
os.makedirs(app.instance_path, exist_ok=True)
|
|
||||||
|
|
||||||
app.register_blueprint(
|
|
||||||
writing.writing_blueprint,
|
|
||||||
# url_prefix='/writing',
|
|
||||||
)
|
|
||||||
|
|
||||||
return app
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
[tool.pytest.ini_options]
|
||||||
|
pythonpath = [
|
||||||
|
"."
|
||||||
|
]
|
|
@ -7,6 +7,7 @@ gunicorn==20.1.0
|
||||||
itsdangerous==2.1.2
|
itsdangerous==2.1.2
|
||||||
Jinja2==3.1.2
|
Jinja2==3.1.2
|
||||||
MarkupSafe==2.1.1
|
MarkupSafe==2.1.1
|
||||||
psycopg==3.1.1
|
psycopg==3.1.2
|
||||||
|
pytest==7.1.3
|
||||||
Werkzeug==2.2.2
|
Werkzeug==2.2.2
|
||||||
WTForms==3.0.1
|
WTForms==3.0.1
|
||||||
|
|
4
start
4
start
|
@ -14,10 +14,10 @@ fi
|
||||||
|
|
||||||
if [[ "$1" == "prod" ]]; then
|
if [[ "$1" == "prod" ]]; then
|
||||||
echo "Starting gunicorn production server..."
|
echo "Starting gunicorn production server..."
|
||||||
gunicorn -b 0.0.0.0:443 "flaskr:create_app()"
|
gunicorn -b 0.0.0.0:443 "undercover:create_app()"
|
||||||
else
|
else
|
||||||
echo "Starting local dev server..."
|
echo "Starting local dev server..."
|
||||||
export FLASK_ENV=development
|
export FLASK_ENV=development
|
||||||
export FLASK_APP=flaskr
|
export FLASK_APP=undercover
|
||||||
flask run --host=0.0.0.0
|
flask run --host=0.0.0.0
|
||||||
fi
|
fi
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
from undercover import css
|
||||||
|
|
||||||
|
|
||||||
|
def test_empty_string():
|
||||||
|
assert css.optimize_css("") == ""
|
||||||
|
|
||||||
|
|
||||||
|
def test_simple_definition():
|
||||||
|
simple_definition = """
|
||||||
|
div {
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
assert css.optimize_css(simple_definition) == "div{width:100%;}"
|
||||||
|
|
||||||
|
|
||||||
|
def test_multi_part_property():
|
||||||
|
simple_definition = """
|
||||||
|
div {
|
||||||
|
padding: 1em 2em 3em 4em;
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
assert css.optimize_css(simple_definition) == "div{padding:1em 2em 3em 4em;}"
|
|
@ -0,0 +1,29 @@
|
||||||
|
# Copyright Sage Vaillancourt 2021
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
from flask import Flask
|
||||||
|
|
||||||
|
import undercover.routes
|
||||||
|
from undercover.css import optimize_css_file
|
||||||
|
|
||||||
|
|
||||||
|
def create_app(test_config=None):
|
||||||
|
optimize_css_file()
|
||||||
|
|
||||||
|
app = Flask(__name__, instance_relative_config=True)
|
||||||
|
app.config.from_mapping(
|
||||||
|
SECRET_KEY='dev',
|
||||||
|
DATABASE=os.path.join(app.instance_path, 'undercover.sqlite'),
|
||||||
|
)
|
||||||
|
|
||||||
|
if test_config is None:
|
||||||
|
app.config.from_pyfile('config.py', silent=True)
|
||||||
|
else:
|
||||||
|
app.config.from_mapping(test_config)
|
||||||
|
|
||||||
|
os.makedirs(app.instance_path, exist_ok=True)
|
||||||
|
|
||||||
|
app.register_blueprint(routes.writing_blueprint)
|
||||||
|
|
||||||
|
return app
|
|
@ -0,0 +1,22 @@
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
def optimize_css(css: str) -> str:
|
||||||
|
import re
|
||||||
|
minified_with_comments = "".join(list(map(
|
||||||
|
lambda line: re.sub(r'\s*([:{])\s*', r'\1', re.sub(r'^\s*', '', line)),
|
||||||
|
css.split('\n')
|
||||||
|
)))
|
||||||
|
return re.sub(r'/\*[\s\S]*?\*/', "", minified_with_comments)
|
||||||
|
|
||||||
|
|
||||||
|
def optimize_css_file():
|
||||||
|
root = os.path.dirname(os.getcwd())
|
||||||
|
static_dir = root + '/undercover/undercover/static/'
|
||||||
|
css = open(static_dir + 'styles.css', 'r').read()
|
||||||
|
minified = optimize_css(css)
|
||||||
|
minified_file = open(static_dir + 'styles_min.css', 'w')
|
||||||
|
minified_file.write(minified)
|
||||||
|
minified_file.close()
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
import bcrypt
|
|
||||||
import os
|
import os
|
||||||
import psycopg
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
import bcrypt
|
||||||
|
import psycopg
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Letter:
|
class Letter:
|
||||||
|
@ -118,22 +119,23 @@ def get_users() -> [UserWithHash]:
|
||||||
return map(lambda row: UserWithHash(row[0], row[1], row[2]), cur.fetchall())
|
return map(lambda row: UserWithHash(row[0], row[1], row[2]), cur.fetchall())
|
||||||
|
|
||||||
|
|
||||||
add_user("hash_man", "hashword")
|
if __name__ == "__main__":
|
||||||
print("Can pull correctly: " + str(login("hash_man", "hashword")))
|
add_user("hash_man", "hashword")
|
||||||
delete_user("hash_man")
|
print("Can pull correctly: " + str(login("hash_man", "hashword")))
|
||||||
# add_letter(1, "Dynamically-added", "This is a letter added from Python!")
|
delete_user("hash_man")
|
||||||
# edit_letter(3, "Dynamically edited!", "This letter was dynamically edited from Python!")
|
# add_letter(1, "Dynamically-added", "This is a letter added from Python!")
|
||||||
|
# edit_letter(3, "Dynamically edited!", "This letter was dynamically edited from Python!")
|
||||||
|
|
||||||
|
|
||||||
# for letter in get_user_letters(1):
|
# for letter in get_user_letters(1):
|
||||||
# print("\'" + letter.title + "\"" + ":")
|
# print("\'" + letter.title + "\"" + ":")
|
||||||
# print(" id: " + str(letter.id))
|
# print(" id: " + str(letter.id))
|
||||||
# print(" letter-data: " + letter.contents)
|
# print(" letter-data: " + letter.contents)
|
||||||
# print()
|
# print()
|
||||||
|
|
||||||
# for user in get_users():
|
# for user in get_users():
|
||||||
# print(user.email + ":")
|
# print(user.email + ":")
|
||||||
# print(" id: " + str(user.id))
|
# print(" id: " + str(user.id))
|
||||||
# print(" password: " + user.password_hash)
|
# print(" password: " + user.password_hash)
|
||||||
# print()
|
# print()
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
|
||||||
from flask import send_from_directory
|
from flask import send_from_directory
|
||||||
|
|
||||||
|
|
||||||
|
@ -21,7 +22,7 @@ def get_datetime():
|
||||||
root_dir = os.path.dirname(os.getcwd())
|
root_dir = os.path.dirname(os.getcwd())
|
||||||
proj_dir = root_dir + '/undercover/'
|
proj_dir = root_dir + '/undercover/'
|
||||||
output_dir = proj_dir + 'outputs/'
|
output_dir = proj_dir + 'outputs/'
|
||||||
base_tex_text = open(proj_dir + "/writing_templates/base.tex", "r").read()
|
base_tex_text = open(proj_dir + "/letter_templates/base.tex", "r").read()
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
|
@ -1,17 +1,17 @@
|
||||||
# Copyright Sage Vaillancourt 2021
|
# Copyright Sage Vaillancourt 2021
|
||||||
|
|
||||||
from flask import (Blueprint, render_template, request, make_response)
|
|
||||||
from wtforms import Form, StringField, TextAreaField, validators
|
|
||||||
import urllib.parse
|
|
||||||
|
|
||||||
from latty import CLData
|
|
||||||
import flaskr
|
|
||||||
import flaskr.db as db
|
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import threading
|
import threading
|
||||||
|
import urllib.parse
|
||||||
|
|
||||||
|
from flask import (Blueprint, render_template, request, make_response)
|
||||||
|
from wtforms import Form, StringField, TextAreaField, validators
|
||||||
|
|
||||||
|
import undercover.db as db
|
||||||
|
from undercover.latty import CLData
|
||||||
|
|
||||||
|
index_cache = None
|
||||||
writing_blueprint = Blueprint('writing', __name__,)
|
writing_blueprint = Blueprint('writing', __name__,)
|
||||||
|
|
||||||
|
|
||||||
|
@ -65,12 +65,13 @@ class CLForm(Form):
|
||||||
|
|
||||||
@writing_blueprint.route('/', methods=['GET'])
|
@writing_blueprint.route('/', methods=['GET'])
|
||||||
def index_get():
|
def index_get():
|
||||||
if flaskr.INDEX is None:
|
global index_cache
|
||||||
flaskr.INDEX = render_template(
|
if not index_cache:
|
||||||
|
index_cache = render_template(
|
||||||
'writing.jinja2',
|
'writing.jinja2',
|
||||||
form=CLForm()
|
form=CLForm()
|
||||||
)
|
)
|
||||||
return flaskr.INDEX
|
return index_cache
|
||||||
|
|
||||||
|
|
||||||
@writing_blueprint.route('/dbtest', methods=['GET'])
|
@writing_blueprint.route('/dbtest', methods=['GET'])
|
Loading…
Reference in New Issue