Skip to content

Commit

Permalink
Refactor: Add get_user_belts util
Browse files Browse the repository at this point in the history
  • Loading branch information
ConnorNelson committed Nov 30, 2023
1 parent d2c0da8 commit e851ade
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
17 changes: 2 additions & 15 deletions dojo_plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from .config import DOJO_HOST, bootstrap
from .utils import unserialize_user_flag, render_markdown
from .utils.discord import get_discord_user, get_discord_roles, add_role, send_message
from .utils.dojo import get_user_belts
from .pages.dojos import dojos, dojos_override
from .pages.dojo import dojo
from .pages.workspace import workspace
Expand All @@ -45,24 +46,10 @@ def solve(cls, user, team, challenge, request):
if not discord_user:
return

# TODO: Get this from dojo.yml
belts = {
"CSE 365 - Spring 2023": "Orange Belt",
"CSE 466 - Fall 2022": "Yellow Belt",
"CSE 494 - Spring 2023": "Blue Belt",
}
discord_roles = get_discord_roles()

dojos = Dojos.query.join(DojoChallenges).filter(Dojos.official, DojoChallenges.challenge == challenge)
for dojo in dojos:
belt = belts.get(dojo.name)
if not belt:
continue
if not dojo.completed(user):
continue
for belt in get_user_belts(user):
if discord_roles.get(belt) in discord_user["roles"]:
continue

user_mention = f"<@{discord_user['user']['id']}>"
message = f"{user_mention} earned their {belt}! :tada:"
print(message, flush=True)
Expand Down
2 changes: 1 addition & 1 deletion dojo_plugin/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def solves(self, **kwargs):
return DojoChallenges.solves(dojo=self, **kwargs)

def completed(self, user):
return self.solves(user=user).count() == len(self.challenges)
return self.solves(user=user, ignore_visibility=True, ignore_admins=False).count() == len(self.challenges)

def is_admin(self, user=None):
if user is None:
Expand Down
16 changes: 16 additions & 0 deletions dojo_plugin/utils/dojo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,3 +397,19 @@ def get_current_dojo_challenge(user=None):
DojoChallenges.dojo == Dojos.from_id(container.labels.get("dojo.dojo_id")).first())
.first()
)


def get_user_belts(user):
# TODO: Get this from dojo.yml
belts = {
"Orange Belt": ["cse365-s2023", "cse365-f2023"],
"Yellow Belt": ["cse466-f2022", "cse466-f2023"],
"Blue Belt": ["cse494-s2023"],
}
result = []
for belt, dojo_ids in belts.items():
dojos = Dojos.query.filter(Dojos.official, Dojos.id.in_(dojo_ids))
if not any(dojo.completed(user) for dojo in dojos):
break
result.append(belt)
return result

0 comments on commit e851ade

Please sign in to comment.