Skip to content

Commit

Permalink
Merge pull request #60 from avrae/remove_magic_numbers
Browse files Browse the repository at this point in the history
Use the existing constants instead of hardcoding limits
  • Loading branch information
Croebh committed Apr 28, 2023
2 parents 3346452 + 4114348 commit c695185
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions blueprints/customizations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from lib.auth import requires_auth
from lib.utils import jsonify
from workshop.constants import SNIPPET_SIZE_LIMIT, ALIAS_SIZE_LIMIT, UVAR_SIZE_LIMIT, GVAR_SIZE_LIMIT

customizations = Blueprint("customizations", __name__)

Expand Down Expand Up @@ -40,8 +41,8 @@ def alias_update(user, name):
return "Name cannot contain whitespace", 400
if name in current_app.rdb.jget("default_commands", []):
return "Alias is already built-in", 409
if len(data["commands"]) > 4000:
return "Alias commands must be less than 4KB", 400
if len(data["commands"]) > ALIAS_SIZE_LIMIT:
return f"Alias commands must be less than {ALIAS_SIZE_LIMIT:,} characters", 400

current_app.mdb.aliases.update_one(
{"owner": user.id, "name": name}, {"$set": {"commands": data["commands"]}}, upsert=True
Expand Down Expand Up @@ -77,8 +78,8 @@ def snippet_update(user, name):
return "Snippet cannot be blank", 400
if " " in name:
return "Name cannot contain whitespace", 400
if len(data["snippet"]) > 2000:
return "Snippet must be less than 2KB", 400
if len(data["snippet"]) > SNIPPET_SIZE_LIMIT:
return f"Snippet must be less than {SNIPPET_SIZE_LIMIT:,} characters", 400
if len(name) < 2:
return "Name must be at least 2 characters", 400

Expand Down Expand Up @@ -114,8 +115,8 @@ def uvar_update(user, name):
return "Missing value field", 400
if not data["value"]:
return "Value cannot be blank", 400
if len(data["value"]) > 4000:
return "Value must be less than 4KB", 400
if len(data["value"]) > UVAR_SIZE_LIMIT:
return f"Value must be less than {UVAR_SIZE_LIMIT:,} characters", 400

current_app.mdb.uvars.update_one({"owner": user.id, "name": name}, {"$set": {"value": data["value"]}}, upsert=True)
return "Uvar updated."
Expand Down Expand Up @@ -162,8 +163,8 @@ def gvar_new(user):
return "No data found", 400
if "value" not in data:
return "Missing value field", 400
if len(data["value"]) > 100000:
return "Gvars must be less than 100KB", 400
if len(data["value"]) > GVAR_SIZE_LIMIT:
return f"Gvars must be less than {GVAR_SIZE_LIMIT:,} characters", 400
key = str(uuid.uuid4())
gvar = {
"owner": user.id,
Expand Down Expand Up @@ -199,8 +200,8 @@ def gvar_update(user, key):
return "Gvar not found", 404
if gvar["owner"] != user.id and user.id not in gvar.get("editors", []):
return "You do not have permission to edit this gvar", 403
if len(data["value"]) > 100000:
return "Gvars must be less than 100KB", 400
if len(data["value"]) > GVAR_SIZE_LIMIT:
return f"Gvars must be less than {GVAR_SIZE_LIMIT:,} characters", 400
current_app.mdb.gvars.update_one({"key": key}, {"$set": {"value": data["value"]}})
return "Gvar updated."

Expand Down

0 comments on commit c695185

Please sign in to comment.