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

Migrated to postgresql #202

Merged
merged 73 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
3a50dca
[IN-DEV] Migrating to Postgres
dssecret Oct 12, 2023
9f64e10
[IN-DEV] Continue PG migration
dssecret Oct 22, 2023
da48692
Updated frontent to handle postgres database
dssecret Nov 9, 2023
354b43c
Black reformat
dssecret Nov 9, 2023
ed19800
Removed DD tracing in UI
dssecret Nov 9, 2023
43c08c5
Bug fix
dssecret Nov 12, 2023
5f87bcc
bug fix
dssecret Nov 12, 2023
df46f87
Bug fix
dssecret Nov 12, 2023
a03cb75
Bug fix
dssecret Nov 12, 2023
d2a8926
Bug fix
dssecret Nov 12, 2023
04362fc
Bug fix
dssecret Nov 12, 2023
481856d
Bug fix
dssecret Nov 12, 2023
1b95262
Replaced old skynet config keys
dssecret Nov 12, 2023
dc08414
Test db connection fix
dssecret Nov 12, 2023
75f6c32
Revert "Test db connection fix"
dssecret Nov 12, 2023
5e84a7b
Misc bug fixes
dssecret Nov 12, 2023
a2cc86b
Misc bug fixes
dssecret Nov 12, 2023
be49a23
bug fixes
dssecret Nov 13, 2023
96a9d0e
misc bug fixes
dssecret Nov 13, 2023
f8a211d
Bug fix
dssecret Nov 13, 2023
1b38cd1
Fixed server/faction permission check in faction banking commands
dssecret Nov 14, 2023
cb69355
Revert "Fixed server/faction permission check in faction banking comm…
dssecret Nov 15, 2023
05e5a83
Inline with origin/master
dssecret Nov 15, 2023
46fea78
Misc bug fixes & black reformat
dssecret Nov 15, 2023
3cf5b77
Bug fix
dssecret Nov 15, 2023
3802ac1
Bug fix
dssecret Nov 15, 2023
0c8b1e2
Bug fix
dssecret Nov 15, 2023
8d506b1
Bug fix
dssecret Nov 15, 2023
b85dff9
Discord auth bug fix
dssecret Nov 15, 2023
d65056f
Discord auth bug fix
dssecret Nov 15, 2023
89dea88
Fixed MissingKeyError handling
dssecret Nov 15, 2023
f46d08d
Bug fix test
dssecret Nov 15, 2023
3113a6f
Bug fix test
dssecret Nov 15, 2023
41496d3
Server/faction perm check fix
dssecret Nov 15, 2023
50491bf
Fixed faction/banking list of bankers
dssecret Nov 15, 2023
7a27dfa
Bug fix
dssecret Nov 15, 2023
60daebe
black reformat
dssecret Nov 15, 2023
e92bd1a
Fix faction guild permissions check
Nov 19, 2023
96f9b42
Fixed load_user when user.faction_id == null
Nov 19, 2023
98ad270
Fixed fulfilled withdrawal embeds showing as cancelled
Nov 19, 2023
be3316f
Fixed banking update SQL queries
Nov 19, 2023
9315cc6
bug fix
Nov 20, 2023
dd47705
Isort refactor
dssecret Nov 20, 2023
ccd2652
Fix banking auto-incrementation on wid
dssecret Nov 20, 2023
a11c504
User none bug fix
dssecret Nov 20, 2023
fee9241
bug fix
dssecret Nov 21, 2023
9b76974
Bug fix
dssecret Nov 21, 2023
f550c49
Replaced .save with atomic .update
dssecret Nov 22, 2023
f29532b
Miscellaneous minor bug fixes
dssecret Nov 27, 2023
6431294
Faction guild reference bug fix
dssecret Nov 27, 2023
e9e4d03
Misc bug fixes
dssecret Nov 29, 2023
4dd6c5a
Added ToS
dssecret Dec 4, 2023
daf4697
Added privacy policy
dssecret Dec 4, 2023
5b1de56
Bug fix
dssecret Dec 4, 2023
e897c7c
bug fix
dssecret Dec 7, 2023
40cb293
Bug fix
dssecret Dec 7, 2023
7d55583
Bug fix
dssecret Dec 7, 2023
04da960
Bug fixes
dssecret Dec 7, 2023
9e3eaf5
Removed second embed from assist request if empty
dssecret Dec 7, 2023
3dc6444
Bug fix
dssecret Dec 7, 2023
a14ea36
[IN-DEV] Adding support for full assists usersciprt
dssecret Dec 7, 2023
d95c8b2
Bug fix
dssecret Dec 7, 2023
a87222a
Added assist auto-deletion after 5 minutes
dssecret Dec 7, 2023
bad9932
bug fix
dssecret Dec 7, 2023
5bf3af8
Bug fix
dssecret Dec 7, 2023
77db767
Bug fix
dssecret Dec 7, 2023
8ed3fc8
Bug fix
dssecret Dec 8, 2023
59afaa3
Discord oauth test fix
dssecret Dec 8, 2023
7a148a2
Bug fix
dssecret Dec 12, 2023
facfebe
Added .flake8
dssecret Dec 12, 2023
9aa1c38
Fixed user not found in /balance
dssecret Dec 12, 2023
7390e81
Updated items slash commands to use new Notificaton model
dssecret Dec 13, 2023
fcfe4b4
Bug fix
dssecret Dec 13, 2023
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
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
root = true

[*]
end_of_line = lf
insert_final_new_line = true

[*.{js,py,lua,txt}]
charset = utf-8

[*.{js,py}]
indent_style = space
indent_size = 4

[*.html]
indent_style = space
indent_size = 4
16 changes: 16 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[flake8]
exclude = ./.tox,./.git,__pycache__,./venv,./docs
max-line-length = 120
extend-ignore =
# E203: Black autoformatting
E203,
# E266: multiple # in comment
E266,
# E402: invocation of ddtrace and other packages before normal packages
E402,
# E501: line too long
E501,
# F401: ?
F401,
# E712: peewee query (? == True)
E712
62 changes: 36 additions & 26 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,12 @@
import flask
from flask_cors import CORS
from flask_login import LoginManager, current_user
from mongoengine import connect
from peewee import JOIN, DoesNotExist
from tornium_commons import Config, rds
from tornium_commons.formatters import commas, rel_time, torn_timestamp
from tornium_commons.models import FactionModel
from tornium_commons.models import Faction

config = Config().load()

if not hasattr(sys, "_called_from_test"):
connect(
db="Tornium",
username=config["username"],
password=config["password"],
host=f'mongodb://{config["host"]}',
connect=False,
)
config = Config.from_json()

import utils

Expand Down Expand Up @@ -94,21 +85,22 @@ def init__app():
from skynet import mod as skynet_mod

app = flask.Flask(__name__)
if config["secret"] is None:
if config.flask_secret is None:
app.secret_key = config.regen_secret()
else:
app.secret_key = config["secret"]
app.secret_key = config.flask_secret

app.config["REMEMBER_COOKIE_DURATION"] = 604800
app.config["SESSION_COOKIE_SECURE"] = True
app.config["SESSION_COOKIE_HTTPONLY"] = True
app.config["SESSION_COOKIE_SAMESITE"] = "Lax"
app.config["SESSION_COOKIE_SAMESITE"] = "strict"
app.config["SESSION_COOKIE_DOMAIN"] = config.flask_domain

CORS(
app,
resources={
r"/api/*": {"origins": "*"},
r"/*": {"origins": config["domain"]},
r"/*": {"origins": config.flask_domain},
},
supports_credentials=True,
)
Expand Down Expand Up @@ -148,9 +140,9 @@ def init__app():

@login_manager.user_loader
def load_user(user_id):
from models.user import User
from models.user import AuthUser

return User(user_id)
return AuthUser.select().join(Faction, JOIN.LEFT_OUTER).where(AuthUser.tid == user_id).first()


@login_manager.unauthorized_handler
Expand All @@ -173,16 +165,24 @@ def refresh_needed():

@app.template_filter("reltime")
def relative_time(s):
if s is None:
return ""
elif isinstance(s, datetime.datetime):
return rel_time(s)

return rel_time(datetime.datetime.fromtimestamp(s))


@app.template_filter("tcttime")
def tct_time(s):
return torn_timestamp(int(s))
return torn_timestamp(s)


@app.template_filter("commas")
def commas_filter(s):
if s is None:
return ""

return commas(int(s))


Expand All @@ -203,12 +203,12 @@ def join_list(iter: typing.Iterable[str], and_seperator=False):

@app.template_filter("faction")
def faction_filter(tid):
faction: typing.Optional[FactionModel] = FactionModel.objects(tid=int(tid)).first()

if faction is None or faction.name in ("", None):
try:
faction = Faction.select(Faction.name).where(Faction.tid == tid).get()
except DoesNotExist:
return f"N/A {tid}"
else:
return f"{faction.name} [{faction.tid}]"

return f"{faction.name} [{tid}]"


@app.before_request
Expand Down Expand Up @@ -253,9 +253,19 @@ def after_request(response: flask.Response):
client_token = secrets.token_urlsafe()

redis_client.set(
f"tornium:token:api:{client_token}", f"{int(time.time())}|{current_user.tid}", nx=True, ex=300
f"tornium:token:api:{client_token}",
f"{int(time.time())}|{current_user.tid}",
nx=True,
ex=300,
)

response.set_cookie("token", client_token, max_age=300, secure=True, httponly=True, samesite="Strict")
response.set_cookie(
"token",
client_token,
max_age=300,
secure=True,
httponly=True,
samesite="Strict",
)

return response
5 changes: 3 additions & 2 deletions commands/commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
"faction/banking/withdraw",
"faction/data/faction",
"notify/notify",
"stats/chainlist",
"stats/stat",
"stocks/stocks",
"user/user"
],
"disabled": []
"disabled": [
"stats/chainlist"
]
}
12 changes: 11 additions & 1 deletion controllers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,21 @@ def index():
return render_template("index.html", extensions=extensions)


@mod.route("/terms")
def terms():
return render_template("terms.html")


@mod.route("/privacy")
def privacy():
return render_template("privacy.html")


@mod.route("/settings")
@fresh_login_required
@token_required(setnx=True)
def settings(*args, **kwargs):
if current_user.key == "":
if current_user.key in ("", None):
obfuscated_key = "Not Set"
else:
obfuscated_key = current_user.key[:6] + "*" * 10
Expand Down
31 changes: 25 additions & 6 deletions controllers/adminroutes.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,30 @@ def load_user(tid):
return (
render_template("admin/load_user.html"),
200,
{"Cache-Control": "no-cache, no-store, must-revalidate", "Pragma": "no-cache", "Expires": "0"},
{
"Cache-Control": "no-cache, no-store, must-revalidate",
"Pragma": "no-cache",
"Expires": "0",
},
)

passphrase = request.form.get("passphrase")

if passphrase is None:
return (
render_template(
"errors/error.html", title="Invalid Passphrase", error="Your security passphrase was incorrect."
"errors/error.html",
title="Invalid Passphrase",
error="Your security passphrase was incorrect.",
),
401,
)
elif Config()["admin-passphrase"] != hashlib.sha512(passphrase.encode("utf-8")).hexdigest():
elif Config.from_cache()["admin-passphrase"] != hashlib.sha512(passphrase.encode("utf-8")).hexdigest():
return (
render_template(
"errors/error.html", title="Invalid Passphrase", error="Your security passphrase was incorrect."
"errors/error.html",
title="Invalid Passphrase",
error="Your security passphrase was incorrect.",
),
401,
)
Expand All @@ -61,12 +69,23 @@ def load_user(tid):
user = OverrideUser(tid)
except ValueError:
return (
render_template("errors/error.html", title="Unknown User", error="This user is not found in the database."),
render_template(
"errors/error.html",
title="Unknown User",
error="This user is not found in the database.",
),
400,
)

if user.key == "":
return render_template("errors/error.html", title="Invalid User", error="This user is not signed in."), 400
return (
render_template(
"errors/error.html",
title="Invalid User",
error="This user is not signed in.",
),
400,
)

if user.discord_id not in (0, "", None):
send_dm.delay(
Expand Down
Loading
Loading