Skip to content

Commit

Permalink
Lovin' the Django admin command interface
Browse files Browse the repository at this point in the history
  • Loading branch information
singerng committed Jul 13, 2016
1 parent e0f25e2 commit 4fc52e3
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 44 deletions.
Empty file added core/management/__init__.py
Empty file.
Empty file.
31 changes: 31 additions & 0 deletions core/management/commands/clearpoints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from django.core.management.base import BaseCommand

from core.models import CorrectSubmission, Team, Problem


class Command(BaseCommand):
help = 'Clears the solves for a specific problem and takes away points.'

def add_arguments(self, parser):
parser.add_argument('problem', type=str, help='name of problem to clear points for')

def handle(self, *args, **options):
problem = Problem.objects.get(name=args['problem'])

for team in Team.objects.all():
if problem in team.solved.all():
team.solved.remove(problem)

correct = CorrectSubmission.objects.get(team=team, problem=problem)
for submission in CorrectSubmission.objects.filter(team=team):
if submission.time >= correct.time:
submission.new_score -= problem.value
submission.save()

correct.delete()

team.score -= problem.value
team.save()

problem.solves = 0
problem.save()
28 changes: 28 additions & 0 deletions core/management/commands/fixscores.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from django.core.management.base import BaseCommand

from core.models import CorrectSubmission, Team


class Command(BaseCommand):
help = 'Recalculates all team scores in event of scoring error based on CorrectSubmission objects.'

def handle(self, *args, **options):
i = 0

for team in Team.objects.all():
score = 0

team.solved
team.clear()

for submission in CorrectSubmission.objects.all().filter(team=team).order_by('time'):
score += submission.problem.value
submission.new_score = score
submission.save()
team.solved.add(submission.problem)

team.score = score
team.save()

i += 1
print("Fixed score for team {%d}/{%d}" % (i, Team.objects.count()))
36 changes: 36 additions & 0 deletions core/management/commands/resetctf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from django.core.management.base import BaseCommand
from django.contrib.auth.models import User

from core.models import Problem, ProblemUpdate, CorrectSubmission, IncorrectSubmission, Team, UserProfile

class Command(BaseCommand):
help = 'Resets the CTF game for a new year.'

def handle(self, *args, **options):
def yesno(prompt, default=True):
yes = ['yes', 'ye', 'y']
no = ['no', 'n']

answer = input(prompt + " [Y/n] ")
while answer.lower() not in yes and answer.lower() not in no and answer != "":
answer = input(prompt + " [Y/n] ")

if answer == "":
return default
elif answer.lower() in yes:
return True
else:
return False

if yesno("Delete problems, updates, and submissions?"):
Problem.objects.all().delete()
ProblemUpdate.objects.all().delete()
CorrectSubmission.objects.all().delete()
IncorrectSubmission.objects.all().delete()

if yesno("Delete teams?"):
Team.objects.all().delete()

if yesno("Delete users?", default=False):
User.objects.all().delete()
UserProfile.objects.all().delete()
20 changes: 0 additions & 20 deletions core/scripts/clear_points.py

This file was deleted.

22 changes: 0 additions & 22 deletions core/scripts/fix_scores.py

This file was deleted.

5 changes: 3 additions & 2 deletions learn/management/commands/updatelearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
import json
import markdown2


class Command(BaseCommand):
help = 'Update learning modules by reading from directory.'
help = 'Updates learning modules by reading from directory.'

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

def handle(self, *args, **options):
modules = {}
Expand Down

0 comments on commit 4fc52e3

Please sign in to comment.