Skip to content

Commit

Permalink
gunicorn runner and updates
Browse files Browse the repository at this point in the history
  • Loading branch information
arihant2math committed Nov 6, 2023
1 parent 0c615f0 commit a37ba7e
Show file tree
Hide file tree
Showing 18 changed files with 155 additions and 119 deletions.
8 changes: 8 additions & 0 deletions gunicorn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from website.app import create_app
from website.config import BaseConfig


def app(db, secret_key):
BaseConfig.SECRET_KEY = secret_key
BaseConfig.SQLALCHEMY_DATABASE_URI = db
return create_app()
4 changes: 1 addition & 3 deletions website/bp/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ def not_found(e):
@errors_bp.app_errorhandler(500)
def error_500(e):
original_exception = e.original_exception
if isinstance(original_exception, SpaceTradersException): # TODO: Not for every issue though
flash(str(original_exception), "danger")
return redirect(url_for("local.select_token"))
flash("Error: " + str(original_exception), "danger")
resp = Response(render_template("error/500.html"))
resp.status_code = 500
return resp
24 changes: 9 additions & 15 deletions website/bp/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ def add_token(user):
return render_template("local/add_token.html")


@local_bp.route("/create-user-with-token/")
@local_bp.route("/add-existing-token/")
@minify_html
@login_required
def create_user_with_token(user):
return render_template("local/create_user_with_token.html")
def add_existing_token(user):
return render_template("local/add_existing_token.html")


@local_bp.route("/create-user-with-token-api/")
@local_bp.route("/add-existing-token-api/")
@login_required
def create_user_with_token_api(user):
def add_existing_token_api(user):
db.create_all()
user = User(token=request.args.get("token").strip(), active=False, user=user.id)
db.session.add(user)
Expand Down Expand Up @@ -79,14 +79,14 @@ def select_user_api(token_id, user):
return jsonify({})


@local_bp.route("/create-user-no-token/")
@local_bp.route("/create-token/")
@minify_html
@login_required
def create_user_no_token(user):
return render_template("local/create_user_no_token.html")
return render_template("local/create_token.html")


@local_bp.route("/create-user-no-token-api/")
@local_bp.route("/create-token-api/")
@login_required
def create_user_no_token_api(user):
db.create_all()
Expand All @@ -102,7 +102,7 @@ def create_user_no_token_api(user):
return jsonify({})


@local_bp.route("/delete-user-api/<token_id>")
@local_bp.route("/delete-token-api/<token_id>")
@login_required
def delete_user_api(token_id, user):
token = Token.query.filter_by(id=token_id).first()
Expand Down Expand Up @@ -176,9 +176,3 @@ def update_local_data(session):
}
json.dump(data_dict, open("./website/static/systems.json", "w"), indent=4)
return "Success<br><a href=\"/\">Back to the home page</a>"


@local_bp.route("/auth-sandbox/")
@token_required
def auth_sandbox(session):
return "Done"
43 changes: 22 additions & 21 deletions website/bp/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from autotraders.ship import Ship
from flask import *

from website.model import db, User, Automation
from website.model import db, User, Automation, Token
from website.session import get_session, anonymous_session
from website.wrappers import token_required, minify_html

Expand All @@ -27,7 +27,7 @@ def index(session):


def rich_format(s):
if "https://" in s: # TODO: Fix hack
if "https://" in s:
splt = s.split("https://")
new_s = f'{splt[0]}<a target="_blank" href="https://{splt[1]}">https://{splt[1]}</a>'
return new_s
Expand All @@ -38,16 +38,17 @@ def rich_format(s):
@minify_html
def settings():
db.create_all()
users = db.session.execute(db.select(User)).first()
# token = db.session.query(Token).filter_by().first() # TODO: Filter by user id
token = None
status = autotraders.get_status()
server_announcements = status.announcements
announcements = []
for server_announcement in server_announcements:
announcements.append(
server_announcement.title + " - " + rich_format(server_announcement.body)
)
if users is not None:
t = users[0].token
if token is not None:
t = token.token
else:
t = ""
return render_template(
Expand All @@ -60,22 +61,22 @@ def settings():


@main_bp.route("/settings-api/")
def settings_api():
users = db.session.query(User).filter_by(active=True).first()
if users is None:
resp = jsonify({"error": "No active user found."})
return resp
t = users[0].token
updated = []
input_token = request.args.get("token", t).strip(" ").strip('"').strip("'")
if input_token not in [t, "", " "]:
if len(input_token) < 5:
return jsonify({"error": "Token too short"})
else:
users[0].token = request.args.get("token", t)
db.session.commit()
updated.append("token")
return jsonify({"updated": updated})
def settings_api(): # TODO: This is a security risk, need to filter tokens instead (make sure to filter by current user id)
# users = db.session.query(User).filter_by(active=True).first()
# if users is None:
# resp = jsonify({"error": "No active user found."})
# return resp
# t = users[0].token
# updated = []
# input_token = request.args.get("token", t).strip(" ").strip('"').strip("'")
# if input_token not in [t, "", " "]:
# if len(input_token) < 5:
# return jsonify({"error": "Token too short"})
# else:
# users[0].token = request.args.get("token", t)
# db.session.commit()
# updated.append("token")
# return jsonify({"updated": updated})


@main_bp.route("/automations/")
Expand Down
4 changes: 2 additions & 2 deletions website/bp/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@map_bp.route("/map/")
@minify_html
def map_v3():
return render_template("map/map.html")
return render_template("map/map_v3.html")


@map_bp.route("/map-v4/")
Expand All @@ -28,7 +28,7 @@ def system_map(system, session):

@map_bp.route("/system-map-api/<system>")
@token_required
def system_map_api(system, session):
def system_map_api(system, session): # TODO: Allow no token
system_data = System(system, session)
j = {
"symbol": str(system_data.symbol),
Expand Down
52 changes: 22 additions & 30 deletions website/bp/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,50 +35,39 @@ def search(session):
if raw_query is None:
raw_query = ""
query, filters = read_query(raw_query)
should_query_systems = True
should_query_waypoints = True # TODO: Contracts
should_query_factions = True
should_query_ships = True
to_query = ["system", "waypoint", "faction", "ship", "contract"]
for is_filter in filters:
if is_filter.name == "is":
if is_filter.value == "ship":
should_query_factions = False
should_query_systems = False
should_query_waypoints = False
to_query = ["ship"]
elif is_filter.value == "faction":
should_query_systems = False
should_query_waypoints = False
should_query_ships = False
to_query = ["faction"]
elif is_filter.value == "map":
should_query_factions = False
should_query_ships = False
to_query = ["system", "waypoint"]
elif is_filter.value == "system":
should_query_waypoints = False
should_query_ships = False
should_query_factions = False
if should_query_systems:
to_query = ["system"]
elif is_filter.value == "waypoint":
to_query = ["waypoint", "system"]
elif is_filter.value == "contract":
to_query = ["contract"]
if "system" in to_query:
system_data = load_system_data()
else:
system_data = []
t1_2 = time.time()
if should_query_factions:
if "faction" in to_query:
faction_data = load_faction_data()
else:
faction_data = []
t1_3 = time.time()
unweighted_map = []
if should_query_ships:
ship_data = Ship.all(session)[1]
else:
ship_data = []
contract_data = Contract.all(session)[1]
t1_4 = time.time()
if should_query_systems:
if "system" in to_query:
for item in system_data:
if quick_weight(query, str(item.symbol)) > -0.1:
if check_filters_system(item, filters):
unweighted_map.append((item, weight(query, str(item.symbol))))
if should_query_waypoints:
if "waypoint" in to_query:
for waypoint in item.waypoints and (not fast_search or quick_weight(query, str(item.symbol)) > -0.1):
if quick_weight(query, str(waypoint.symbol)) > 0 and check_filters_waypoint(
waypoint, filters
Expand All @@ -87,22 +76,25 @@ def search(session):
(waypoint, weight(query, str(waypoint.symbol)))
)
t1_5 = time.time()
if should_query_factions:
if "faction" in to_query:
for item in faction_data:
if (
quick_weight(query, item.symbol) > -0.25 or quick_weight(query, item.name) > -0.25
) and check_filters_faction(item, filters):
unweighted_map.append((item, weight(query, str(item.symbol))))
t1_6 = time.time()
if should_query_ships:
if "ship" in to_query:
ship_data = Ship.all(session)[1]
for item in ship_data:
if quick_weight(query, item.symbol) > -0.25 and check_filters_ship(item, filters):
unweighted_map.append((item, weight(query, item.symbol)))
for item in contract_data:
if quick_weight(query, item.contract_id) > -0.7 and check_filters_contract(
if "contract" in to_query:
contract_data = Contract.all(session)[1]
for item in contract_data:
if quick_weight(query, item.contract_id) > -0.7 and check_filters_contract(
item, filters
):
unweighted_map.append((item, weight(query, str(item.contract_id))))
):
unweighted_map.append((item, weight(query, str(item.contract_id))))
amap = [
item for item, c in sorted(unweighted_map, key=lambda x: x[1], reverse=True) if c > -0.5
]
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ <h1>Automations <span class="badge bg-warning">WIP</span></h1>
</div>
<div class="row">
{% for automation in automations %}
<a href="/automation/{{ automation.id }}" class="card" style="width: 18rem;">
{# TODO: Fix manual margin #}
<a href="/automation/{{ automation.id }}" class="card" style="width: fit-content; margin: 17px 10px">
<div class="card-body">
<h5 class="card-title">{{ automation.name }}</h5>
<p class="card-text">{{ automation.description }}</p>
Expand Down
File renamed without changes.
6 changes: 2 additions & 4 deletions website/templates/faction/contract/contract_templates.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% import "macros.html" as macros %}

{% macro contract_card(contract) %}
{# TODO: Fix #}
{# TODO: Fix manual margin #}
<div class="card shadow" style="width: fit-content; margin: 17px 10px">
<div class="card-body">
<h5 class="card-title">{{ macros.contract_link(contract.contract_id) }}</h5>
Expand Down Expand Up @@ -56,9 +56,7 @@ <h2>Details</h2>
<div class="row gx-5 gy-3">
{% if contract.contract_type == "PROCUREMENT" %}
{% for delivery in contract.contract_data %}
<p><b>Deliver</b> {{ delivery.item.symbol }} to <a
href="/waypoint/{{ delivery.destination_symbol }}">{{ delivery.destination_symbol }}</a>
{# TODO: Icon for waypoint (above) #}
<p><b>Deliver</b> {{ delivery.item.symbol }} to {{ macros.waypoint_link(delivery.destination_symbol) }}
({{ delivery.item.fulfilled }}/{{ delivery.item.required }}).</p>
{% endfor %}
{% endif %}
Expand Down
37 changes: 37 additions & 0 deletions website/templates/landing.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{% extends 'base.html' %}
{% block title %}
Home
{% endblock %}
{% block body %}
<style>
.colored-background {
--bd-violet-rgb: 108, 20, 240;
--bd-accent-rgb: 255,228,132;
--bd-pink-rgb: 214,51,132;
background: linear-gradient(rgba(var(--bs-body-bg-rgb), 0.25),
rgba(var(--bs-body-bg-rgb), 0.75) 85%),
radial-gradient(ellipse at top left, rgba(var(--bs-primary-rgb), 0.7), transparent 60%),
radial-gradient(ellipse at bottom, rgba(var(--bs-success-rgb), 0.9), transparent 60%),
radial-gradient(ellipse at top right, rgba(var(--bd-accent-rgb), 0.7), transparent 60%),
radial-gradient(ellipse at bottom right, rgba(var(--bd-violet-rgb), 0.7), transparent 60%),
radial-gradient(ellipse at bottom left, rgba(var(--bd-pink-rgb), 0.7), transparent 60%);
}
</style>
<div class="colored-background">
<div class="container-fluid">
<div class="row justify-content-center py-4">
<div class="shadow-lg card" style="width: fit-content;">
<div class="card-body">
<div class="row justify-content-center text-center">
<h1>Homepage</h1>
</div>
<div class="row justify-content-center text-center">
<p></p>
</div>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
{% block navid %}home{% endblock %}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{% extends 'base.html' %}
{% block title %}
Create User
Add Existing Token
{% endblock %}
{% block body %}
<div class="container p-3">
<div class="row justify-content-center text-center">
<h1>Create User</h1>
<h1>Add Existing Token</h1>
</div>
<form class="form row g-3">
<div class="col-md-12">
Expand All @@ -16,11 +16,11 @@ <h1>Create User</h1>
<button class="btn btn-primary" type="button" onclick="create()">Save</button>
</div>
</form>
<a class="mt-3" href="/create-user-no-token/">Don't have a token? Create one!</a>
<a class="mt-3" href="/create-token/">Don't have a token? Create one!</a>
</div>
<script>
function create() {
fetch("/create-user-with-token-api?token=" + document.getElementById("token").value).then(
fetch("/add-existing-token-api?token=" + document.getElementById("token").value).then(
(_) => {
document.location.href = "/";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{% extends 'base.html' %}
{% block title %}
Create Token
Create New Token
{% endblock %}
{% block body %}
<div class="row justify-content-center text-center">
<h1>Create Token</h1>
<h1>Create New Token</h1>
</div>
<form class="form p-3">
<label class="label" for="symbol">Symbol:</label>
Expand All @@ -20,7 +20,7 @@ <h1>Create Token</h1>
<a href="/settings">the settings page</a>.</p>
<script>
function create() {
fetch("/create-user-no-token-api?symbol=" + $("#symbol").val() + "&faction=" + $("#faction").val() + "&email=" + $("#email").val()).then(
fetch("/create-token-api?symbol=" + $("#symbol").val() + "&faction=" + $("#faction").val() + "&email=" + $("#email").val()).then(
(_) => {
document.location.href = "/";
}
Expand Down
2 changes: 1 addition & 1 deletion website/templates/local/select_token.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ <h1 class="modal-title fs-5" id="helpModalTitle">Help</h1>
}

function deleteUser(user_id) {
fetch("/delete-user-api/" + user_id).then(
fetch("/delete-token-api/" + user_id).then(
(_) => {
document.location.href = "/";
}
Expand Down
File renamed without changes.
Loading

0 comments on commit a37ba7e

Please sign in to comment.