Skip to content

Commit

Permalink
More code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Dillon Lareau committed Feb 12, 2020
1 parent 1289b40 commit c5bf37c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 35 deletions.
33 changes: 16 additions & 17 deletions huntserver/hunt_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import logging
logger = logging.getLogger(__name__)

DT_FORMAT = "%Y-%m-%dT%H:%M:%S.%fZ"


def protected_static(request, file_path):
"""
Expand Down Expand Up @@ -228,10 +230,9 @@ def puzzle_view(request, puzzle_id):
submission_list = [render_to_string('puzzle_sub_row.html', {'submission': s})]

try:
date_query = Submission.objects.latest('modified_date').modified_date
last_date = date_query.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
last_date = Submission.objects.latest('modified_date').modified_date.strftime(DT_FORMAT)
except Submission.DoesNotExist:
last_date = timezone.now().strftime('%Y-%m-%dT%H:%M:%S.%fZ')
last_date = timezone.now().strftime(DT_FORMAT)

# Send back rendered response for display
context = {'submission_list': submission_list, 'last_date': last_date}
Expand All @@ -243,18 +244,17 @@ def puzzle_view(request, puzzle_id):
return HttpResponseNotFound('access denied')

# Find which objects the user hasn't seen yet and render them to HTML
last_date = datetime.strptime(request.GET.get("last_date"), '%Y-%m-%dT%H:%M:%S.%fZ')
last_date = datetime.strptime(request.GET.get("last_date"), DT_FORMAT)
last_date = last_date.replace(tzinfo=tz.gettz('UTC'))
submissions = Submission.objects.filter(modified_date__gt=last_date)
submissions = submissions.filter(team=team, puzzle=puzzle)
submission_list = [render_to_string('puzzle_sub_row.html', {'submission': submission})
for submission in submissions]

try:
date_query = Submission.objects.latest('modified_date').modified_date
last_date = date_query.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
last_date = Submission.objects.latest('modified_date').modified_date.strftime(DT_FORMAT)
except Submission.DoesNotExist:
last_date = timezone.now().strftime('%Y-%m-%dT%H:%M:%S.%fZ')
last_date = timezone.now().strftime(DT_FORMAT)

context = {'submission_list': submission_list, 'last_date': last_date}
return HttpResponse(json.dumps(context))
Expand All @@ -275,10 +275,9 @@ def puzzle_view(request, puzzle_id):
submissions = puzzle.submission_set.filter(team=team).order_by('pk')
form = AnswerForm()
try:
date_query = Submission.objects.latest('modified_date').modified_date
last_date = date_query.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
last_date = Submission.objects.latest('modified_date').modified_date.strftime(DT_FORMAT)
except Submission.DoesNotExist:
last_date = timezone.now().strftime('%Y-%m-%dT%H:%M:%S.%fZ')
last_date = timezone.now().strftime(DT_FORMAT)
context = {'form': form, 'pages': list(range(puzzle.num_pages)), 'puzzle': puzzle,
'submission_list': submissions, 'PROTECTED_URL': settings.PROTECTED_URL,
'last_date': last_date, 'team': team}
Expand Down Expand Up @@ -313,9 +312,9 @@ def puzzle_hint(request, puzzle_id):

try:
last_hint = Hint.objects.latest('last_modified_time')
last_date = last_hint.last_modified_time.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
last_date = last_hint.last_modified_time.strftime(DT_FORMAT)
except Hint.DoesNotExist:
last_date = timezone.now().strftime('%Y-%m-%dT%H:%M:%S.%fZ')
last_date = timezone.now().strftime(DT_FORMAT)

# Send back rendered response for display
context = {'hint_list': hint_list, 'last_date': last_date,
Expand All @@ -326,17 +325,17 @@ def puzzle_hint(request, puzzle_id):
elif request.is_ajax():

# Find which objects the user hasn't seen yet and render them to HTML
last_date = datetime.strptime(request.GET.get("last_date"), '%Y-%m-%dT%H:%M:%S.%fZ')
last_date = datetime.strptime(request.GET.get("last_date"), DT_FORMAT)
last_date = last_date.replace(tzinfo=tz.gettz('UTC'))
hints = Hint.objects.filter(last_modified_time__gt=last_date)
hints = hints.filter(team=team, puzzle=puzzle)
hint_list = [render_to_string('hint_row.html', {'hint': hint}) for hint in hints]

try:
last_hint = Hint.objects.latest('last_modified_time')
last_date = last_hint.last_modified_time.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
last_date = last_hint.last_modified_time.strftime(DT_FORMAT)
except Hint.DoesNotExist:
last_date = timezone.now().strftime('%Y-%m-%dT%H:%M:%S.%fZ')
last_date = timezone.now().strftime(DT_FORMAT)

context = {'hint_list': hint_list, 'last_date': last_date,
'num_available_hints': team.num_available_hints}
Expand All @@ -350,9 +349,9 @@ def puzzle_hint(request, puzzle_id):
hints = team.hint_set.filter(puzzle=puzzle).order_by('pk')
try:
last_hint = Hint.objects.latest('last_modified_time')
last_date = last_hint.last_modified_time.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
last_date = last_hint.last_modified_time.strftime(DT_FORMAT)
except Hint.DoesNotExist:
last_date = timezone.now().strftime('%Y-%m-%dT%H:%M:%S.%fZ')
last_date = timezone.now().strftime(DT_FORMAT)
context = {'form': form, 'pages': list(range(puzzle.num_pages)), 'puzzle': puzzle,
'hint_list': hints, 'last_date': last_date, 'team': team}
return render(request, 'puzzle_hint.html', context)
Expand Down
2 changes: 1 addition & 1 deletion huntserver/info_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

def index(request):
""" Main landing page view, mostly static with the exception of hunt info """
curr_hunt = Hunt.objects.get(is_current_hunt=True)
curr_hunt = Hunt.objects.get(is_current_hunt=6)
return render(request, "index.html", {'curr_hunt': curr_hunt})


Expand Down
14 changes: 14 additions & 0 deletions huntserver/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,15 @@ def unlock_hints(self):
self.num_available_hints = models.F('num_available_hints') + num_hints
self.save()

def reset(self):
self.unlocked.clear()
self.unlock_set.all().delete()
self.solved.clear()
self.solve_set.all().delete()
self.submission_set.all().delete()
self.num_available_hints = 0
self.save()

def __str__(self):
return str(self.size) + " (" + self.location + ") " + self.short_name

Expand Down Expand Up @@ -509,6 +518,11 @@ def respond(self):
self.response_text = response
self.save()

def update_response(self, text):
self.response_text = text
self.modified_date = timezone.now()
self.save()

def __str__(self):
return self.submission_text

Expand Down
27 changes: 10 additions & 17 deletions huntserver/staff_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
from .forms import SubmissionForm, UnlockForm, EmailForm, HintResponseForm
from .utils import download_puzzle, download_zip

DT_FORMAT = '%Y-%m-%dT%H:%M:%S.%fZ'


def add_apps_to_context(context, request):
context['available_apps'] = admin.site.get_app_list(request)
Expand All @@ -37,13 +39,11 @@ def queue(request):
return HttpResponse(status=400)
response = form.cleaned_data['response']
s = Submission.objects.get(pk=form.cleaned_data['sub_id'])
s.response_text = response
s.modified_date = timezone.now()
s.save()
s.update_response(response)
submissions = [s]

elif request.is_ajax():
last_date = datetime.strptime(request.GET.get("last_date"), '%Y-%m-%dT%H:%M:%S.%fZ')
last_date = datetime.strptime(request.GET.get("last_date"), DT_FORMAT)
last_date = last_date.replace(tzinfo=tz.gettz('UTC'))
submissions = Submission.objects.filter(modified_date__gt=last_date)
submissions = submissions.exclude(team__location="DUMMY")
Expand Down Expand Up @@ -80,10 +80,9 @@ def queue(request):

form = SubmissionForm()
try:
date_query = Submission.objects.latest('modified_date').modified_date
last_date = date_query.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
last_date = Submission.objects.latest('modified_date').modified_date.strftime(DT_FORMAT)
except Submission.DoesNotExist:
last_date = timezone.now().strftime('%Y-%m-%dT%H:%M:%S.%fZ')
last_date = timezone.now().strftime(DT_FORMAT)
submission_list = [render_to_string('queue_row.html', {'submission': submission},
request=request)
for submission in submissions]
Expand Down Expand Up @@ -464,13 +463,7 @@ def control(request):
return redirect('huntserver:hunt_management')
if(request.POST["action"] == "reset"):
for team in teams:
team.unlocked.clear()
team.unlock_set.all().delete()
team.solved.clear()
team.solve_set.all().delete()
team.submission_set.all().delete()
team.num_available_hints = 0
team.save()
team.reset()
return redirect('huntserver:hunt_management')

if(request.POST["action"] == "getpuzzles"):
Expand Down Expand Up @@ -531,7 +524,7 @@ def staff_hints_text(request):
hints = [h]

elif request.is_ajax():
last_date = datetime.strptime(request.GET.get("last_date"), '%Y-%m-%dT%H:%M:%S.%fZ')
last_date = datetime.strptime(request.GET.get("last_date"), DT_FORMAT)
last_date = last_date.replace(tzinfo=tz.gettz('UTC'))
hints = Hint.objects.filter(last_modified_time__gt=last_date)

Expand All @@ -550,9 +543,9 @@ def staff_hints_text(request):

try:
time_query = Hint.objects.latest('last_modified_time').last_modified_time
last_date = time_query.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
last_date = time_query.strftime(DT_FORMAT)
except Hint.DoesNotExist:
last_date = timezone.now().strftime('%Y-%m-%dT%H:%M:%S.%fZ')
last_date = timezone.now().strftime(DT_FORMAT)

hint_list = []
for hint in hints:
Expand Down

0 comments on commit c5bf37c

Please sign in to comment.