Skip to content

Commit

Permalink
bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
arihant2math committed Nov 5, 2023
1 parent 2679ed7 commit 9d4f057
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 96 deletions.
14 changes: 14 additions & 0 deletions website/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from website.bp.system import system_bp
from website.config import BaseConfig
from website.model import db
from website.session import get_user


def create_app():
Expand Down Expand Up @@ -39,4 +40,17 @@ def favicon():
mimetype="image/vnd.microsoft.icon",
)

class UserTemplate:
def __init__(self, auth):
self.is_authenticated = auth

@app.context_processor
def inject_user():
user = get_user()
if user is not None:
user = UserTemplate(True)
else:
user = UserTemplate(False)
return dict(user=user)

return app
58 changes: 25 additions & 33 deletions website/bp/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,55 +10,47 @@
auth_bp = Blueprint("auth", __name__)


@auth_bp.route("/register")
@auth_bp.route("/register/")
def register():
return render_template("auth/register.html")


@auth_bp.route("/register-api")
@auth_bp.post("/register-api/")
def register_api():
if request.method == "POST":
data_json = json.loads(request.data)
username = data_json["username"]
password = data_json["password"]
requested_user = db.session.query(User).filter_by(username=username).first()
if requested_user is None:
user = User(username=username, password=werkzeug.security.generate_password_hash(password))
db.session.add(user)
db.session.commit()
login_session(user)
return jsonify({"success": True})
else:
abort(401)
username = request.form["username"]
password = request.form["password"]
requested_user = db.session.query(User).filter_by(username=username).first()
if requested_user is None:
user = User(username=username, password=werkzeug.security.generate_password_hash(password))
db.session.add(user)
db.session.commit()
login_session(user.username)
return jsonify({"success": True})
else:
abort(405)
abort(401)


@auth_bp.route("/login")
@auth_bp.route("/login/")
def login():
return render_template("auth/login.html")


@auth_bp.route("/login-api")
@auth_bp.post("/login-api/")
def login_api():
if request.method == "POST":
data_json = json.loads(request.data)
username = data_json["username"]
password = data_json["password"]
requested_user = db.session.query(User).filter_by(username=username).first()
if requested_user is None:
abort(404)
else:
if werkzeug.security.check_password_hash(requested_user.password, password):
login_session(requested_user)
return jsonify({"success": True})
else:
return jsonify({"success": False})
username = request.form["username"]
password = request.form["password"]
requested_user = db.session.query(User).filter_by(username=username).first()
if requested_user is None:
abort(404)
else:
abort(405)
if werkzeug.security.check_password_hash(requested_user.password, password):
login_session(requested_user.username)
return jsonify({"success": True})
else:
return jsonify({"success": False})


@auth_bp.route("/logout")
@auth_bp.route("/logout/")
@login_required
def logout(user):
logout_session()
Expand Down
2 changes: 1 addition & 1 deletion website/bp/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ 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_user"))
return redirect(url_for("local.select_token"))
resp = Response(render_template("error/500.html"))
resp.status_code = 500
return resp
13 changes: 9 additions & 4 deletions website/bp/faction.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@
from flask import *

from website.paginated_return import paginated_return
from website.session import get_session, anonymous_session
from website.wrappers import token_required, minify_html

faction_bp = Blueprint("faction", __name__)


@faction_bp.route("/factions/")
@minify_html
@token_required
def factions(session):
def factions():
session = get_session()
if session is None:
session = anonymous_session()
page = int(request.args.get("page", default=1))
factions = Faction.all(session)
new_li = paginated_return(factions, page)
Expand All @@ -23,8 +26,10 @@ def factions(session):

@faction_bp.route("/faction/<symbol>/")
@minify_html
@token_required
def faction(symbol, session):
def faction(symbol):
session = get_session()
if session is None:
session = anonymous_session()
light_background = {}.get(symbol, "")
dark_background = {}.get(symbol, "")
force_dark = {"VOID": True}.get(symbol, False)
Expand Down
14 changes: 7 additions & 7 deletions website/bp/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
local_bp = Blueprint("local", __name__)


@local_bp.route("/create-user/")
@local_bp.route("/add-token/")
@minify_html
@login_required
def create_user(user):
def add_token(user):
return render_template("local/create_user.html")


Expand All @@ -41,13 +41,13 @@ def create_user_with_token_api(user):
return jsonify({})


@local_bp.route("/select-user/")
@local_bp.route("/select-token/")
@minify_html
@login_required
def select_user(user):
def select_token(user):
if db.session.query(User).count() == 0:
flash("No users found, please create one", "info")
return redirect(url_for("local.create_user"))
flash("No tokens found, please create one", "info")
return redirect(url_for("local.add_token"))

class MockAgent:
def __init__(self, token, id, active):
Expand All @@ -64,7 +64,7 @@ def __init__(self, token, id, active):
users.append(a)
except Exception as e:
users.append(MockAgent(user.token, user.id, user.active))
return render_template("local/select_user.html", users=users)
return render_template("local/select_token.html", users=users)


@local_bp.route("/select-user-api/<token_id>")
Expand Down
13 changes: 9 additions & 4 deletions website/bp/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
check_filters_contract,
check_filters_faction, quick_weight,
)
from website.session import get_session, anonymous_session
from website.wrappers import token_required, minify_html

main_bp = Blueprint("main", __name__)
Expand Down Expand Up @@ -106,8 +107,10 @@ def automation(i):

@main_bp.route("/agents/")
@minify_html
@token_required
def agents(session):
def agents():
session = get_session()
if session is None:
session = anonymous_session()
page = int(request.args.get("page", default=1))
agents_list = Agent.all(session, page)
li = {1}
Expand Down Expand Up @@ -144,8 +147,10 @@ def agents(session):

@main_bp.route("/agent/<symbol>/")
@minify_html
@token_required
def agent(symbol, session):
def agent(symbol):
session = get_session()
if session is None:
session = anonymous_session()
return render_template("agent/agent.html", agent=Agent(session, symbol))


Expand Down
16 changes: 10 additions & 6 deletions website/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@

def get_session():
if "username" in session:
user = db.session.query(Token).filter_by(active=True, username=session["username"]).first()
user = db.session.query(Token).filter_by(active=True, user=session["username"]).first()
if user is None:
user = db.session.query(Token, username=session["username"]).first()
user = db.session.query(Token).filter_by(user=session["username"]).first()
if user is None:
raise ValueError("No token not found")
return None
user.active = True
db.session.commit()
return asession.AutoTradersSession(user.token)
else:
raise ValueError("User not logged in")
return None


def get_user():
if "username" in session:
user = db.session.query(Token).filter_by(username=session["username"]).first()
user = db.session.query(Token).filter_by(user=session["username"]).first()
return user
else:
raise ValueError("User not logged in")
return None


def login_session(username):
Expand All @@ -34,3 +34,7 @@ def login_session(username):
def logout_session():
del session["username"]
session["logged_in"] = False


def anonymous_session():
return asession.AutoTradersSession()
31 changes: 21 additions & 10 deletions website/templates/auth/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,30 @@
Login
{% endblock %}
{% block body %}
<div class="row justify-content-center text-center">
<h1>Login</h1>
<div class="container">
<div class="row justify-content-center text-center">
<h1>Login</h1>
</div>
<form class="form p-3">
<div class="form-floating">
<input id="username" class="form-control" required>
<label for="username">Username</label>
</div>
<div class="form-floating">
<input id="password" class="form-control" type="password" required>
<label for="password">Password</label>
</div>
<button class="btn btn-primary" type="button" onclick="register()">Login</button>
</form>
</div>
<form class="form p-3">
<label class="label" for="username">Username:</label>
<input id="username" class="form-control" required>
<label class="label" for="password">Password:</label>
<input id="password" class="form-control" type="password" required>
<button class="btn btn-primary" type="button" onclick="register()">Login</button>
</form>
<script>
function register() {
$.post("/login-api/", {"username": $("#username").val(), "password": $("#password").val()}).then(function () {
$.post("/login-api/",
{
username: $("#username").val(),
password: $("#password").val()
}
).then(function () {
window.location.href = "/"
});
}
Expand Down
31 changes: 21 additions & 10 deletions website/templates/auth/register.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,30 @@
Register
{% endblock %}
{% block body %}
<div class="row justify-content-center text-center">
<h1>Register</h1>
<div class="container">
<div class="row justify-content-center text-center">
<h1>Register</h1>
</div>
<form class="form p-3">
<div class="form-floating">
<input id="username" class="form-control" required>
<label for="username">Username</label>
</div>
<div class="form-floating">
<input id="password" class="form-control" type="password" required>
<label for="password">Password</label>
</div>
<button class="btn btn-primary" type="button" onclick="register()">Register</button>
</form>
</div>
<form class="form p-3">
<label class="label" for="username">Username:</label>
<input id="username" class="form-control" required>
<label class="label" for="password">Password:</label>
<input id="password" class="form-control" type="password" required>
<button class="btn btn-primary" type="button" onclick="register()">Register</button>
</form>
<script>
function register() {
$.post("/register-api/", {"username": $("#username").val(), "password": $("#password").val()}).then(function () {
$.post("/register-api/",
{
username: $("#username").val(),
password: $("#password").val()
}
).then(function () {
window.location.href = "/"
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% extends 'base.html' %}
{% block title %}
Select User
Select Token
{% endblock %}
{% block body %}
<div class="container p-3">
Expand Down Expand Up @@ -30,7 +30,7 @@ <h1>Select User</h1>
<p>No users saved, you should create one ...</p>
{% endif %}
</div>
<a class="btn btn-primary m-3" role="button" href="/create-user"><i class="bi bi-person-plus-fill"></i> Create User</a>
<a class="btn btn-primary m-3" role="button" href="/add-token"><i class="bi bi-person-plus-fill"></i> Add Token</a>
<button type="button" class="btn btn-secondary m-3" data-bs-toggle="modal" data-bs-target="#helpModal"><i class="bi bi-question-circle"></i> Help</button>

<!-- Modal -->
Expand All @@ -42,12 +42,12 @@ <h1 class="modal-title fs-5" id="helpModalTitle">Help</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
This page allows you to select a user to use. The rest of the website depends on the "Active User".
To make a user active, click the "Make Active" button on the user you want to use, the highlighted user is the current active user.
This page allows you to select a token to use. The rest of the website depends on the "Active Token".
To make a token active, click the "Make Active" button on the token you want to use, the highlighted token is the current active token.
If you have been redirected to this page, then either
<ol>
<li>There is no active user (in which case create an user or make one active)</li>
<li>The current user is invalid, delete it and create a new one.</li>
<li>There is no active token (in which case create an token or make one active)</li>
<li>The current token is invalid, delete it and create a new one.</li>
</ol>
</div>
<div class="modal-footer">
Expand Down
Loading

0 comments on commit 9d4f057

Please sign in to comment.