Skip to content

Commit 4fc52e3

Browse files
committed
Lovin' the Django admin command interface
1 parent e0f25e2 commit 4fc52e3

File tree

8 files changed

+98
-44
lines changed

8 files changed

+98
-44
lines changed

core/management/__init__.py

Whitespace-only changes.

core/management/commands/__init__.py

Whitespace-only changes.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from django.core.management.base import BaseCommand
2+
3+
from core.models import CorrectSubmission, Team, Problem
4+
5+
6+
class Command(BaseCommand):
7+
help = 'Clears the solves for a specific problem and takes away points.'
8+
9+
def add_arguments(self, parser):
10+
parser.add_argument('problem', type=str, help='name of problem to clear points for')
11+
12+
def handle(self, *args, **options):
13+
problem = Problem.objects.get(name=args['problem'])
14+
15+
for team in Team.objects.all():
16+
if problem in team.solved.all():
17+
team.solved.remove(problem)
18+
19+
correct = CorrectSubmission.objects.get(team=team, problem=problem)
20+
for submission in CorrectSubmission.objects.filter(team=team):
21+
if submission.time >= correct.time:
22+
submission.new_score -= problem.value
23+
submission.save()
24+
25+
correct.delete()
26+
27+
team.score -= problem.value
28+
team.save()
29+
30+
problem.solves = 0
31+
problem.save()

core/management/commands/fixscores.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from django.core.management.base import BaseCommand
2+
3+
from core.models import CorrectSubmission, Team
4+
5+
6+
class Command(BaseCommand):
7+
help = 'Recalculates all team scores in event of scoring error based on CorrectSubmission objects.'
8+
9+
def handle(self, *args, **options):
10+
i = 0
11+
12+
for team in Team.objects.all():
13+
score = 0
14+
15+
team.solved
16+
team.clear()
17+
18+
for submission in CorrectSubmission.objects.all().filter(team=team).order_by('time'):
19+
score += submission.problem.value
20+
submission.new_score = score
21+
submission.save()
22+
team.solved.add(submission.problem)
23+
24+
team.score = score
25+
team.save()
26+
27+
i += 1
28+
print("Fixed score for team {%d}/{%d}" % (i, Team.objects.count()))

core/management/commands/resetctf.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from django.core.management.base import BaseCommand
2+
from django.contrib.auth.models import User
3+
4+
from core.models import Problem, ProblemUpdate, CorrectSubmission, IncorrectSubmission, Team, UserProfile
5+
6+
class Command(BaseCommand):
7+
help = 'Resets the CTF game for a new year.'
8+
9+
def handle(self, *args, **options):
10+
def yesno(prompt, default=True):
11+
yes = ['yes', 'ye', 'y']
12+
no = ['no', 'n']
13+
14+
answer = input(prompt + " [Y/n] ")
15+
while answer.lower() not in yes and answer.lower() not in no and answer != "":
16+
answer = input(prompt + " [Y/n] ")
17+
18+
if answer == "":
19+
return default
20+
elif answer.lower() in yes:
21+
return True
22+
else:
23+
return False
24+
25+
if yesno("Delete problems, updates, and submissions?"):
26+
Problem.objects.all().delete()
27+
ProblemUpdate.objects.all().delete()
28+
CorrectSubmission.objects.all().delete()
29+
IncorrectSubmission.objects.all().delete()
30+
31+
if yesno("Delete teams?"):
32+
Team.objects.all().delete()
33+
34+
if yesno("Delete users?", default=False):
35+
User.objects.all().delete()
36+
UserProfile.objects.all().delete()

core/scripts/clear_points.py

Lines changed: 0 additions & 20 deletions
This file was deleted.

core/scripts/fix_scores.py

Lines changed: 0 additions & 22 deletions
This file was deleted.

learn/management/commands/updatelearn.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
import json
77
import markdown2
88

9+
910
class Command(BaseCommand):
10-
help = 'Update learning modules by reading from directory.'
11+
help = 'Updates learning modules by reading from directory.'
1112

1213
def add_arguments(self, parser):
13-
parser.add_argument('module-dir', type=str)
14+
parser.add_argument('module-dir', type=str, help='path to learn repository')
1415

1516
def handle(self, *args, **options):
1617
modules = {}

0 commit comments

Comments
 (0)