Skip to content

Commit

Permalink
Added html solutions and fixed media removal bug
Browse files Browse the repository at this point in the history
  • Loading branch information
dlareau committed Nov 3, 2020
1 parent 37585b1 commit fd313e1
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 29 deletions.
5 changes: 3 additions & 2 deletions huntserver/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class Meta:
fields = ('hunt', 'puzzle_name', 'puzzle_number', 'puzzle_id', 'answer', 'is_meta',
'doesnt_count', 'puzzle_page_type', 'puzzle_file', 'resource_file',
'solution_file', 'extra_data', 'num_required_to_unlock', 'unlock_type',
'points_cost', 'points_value')
'points_cost', 'points_value', 'solution_is_webpage', 'solution_resource_file')


class PuzzleAdmin(admin.ModelAdmin):
Expand All @@ -211,7 +211,8 @@ class Media:
(None, {
'fields': ('hunt', 'puzzle_name', 'answer', 'puzzle_number', 'puzzle_id', 'is_meta',
'doesnt_count', 'puzzle_page_type', 'puzzle_file', 'resource_file',
'solution_file', 'extra_data', 'unlock_type')
'solution_is_webpage', 'solution_file', 'solution_resource_file',
'extra_data', 'unlock_type')
}),
('Solve Unlocking', {
'classes': ('formset_border', 'solve_unlocking'),
Expand Down
24 changes: 24 additions & 0 deletions huntserver/migrations/0072_auto_20201103_1539.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 2.2.11 on 2020-11-03 20:39

from django.db import migrations, models
import huntserver.models


class Migration(migrations.Migration):

dependencies = [
('huntserver', '0071_auto_20201021_2143'),
]

operations = [
migrations.AddField(
model_name='puzzle',
name='solution_is_webpage',
field=models.BooleanField(default=False, help_text='Is this solution an html webpage?'),
),
migrations.AddField(
model_name='puzzle',
name='solution_resource_file',
field=models.FileField(blank=True, help_text='Puzzle solution resources, MUST BE A ZIP FILE.', storage=huntserver.models.PuzzleOverwriteStorage(), upload_to=huntserver.models.get_solution_file_path),
),
]
80 changes: 55 additions & 25 deletions huntserver/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ def clean(self, *args, **kwargs):
""" Overrides the standard clean method to ensure that only one hunt is the current hunt """
if(not self.is_current_hunt):
try:
old_instance = Hunt.objects.get(pk=self.pk)
if(old_instance.is_current_hunt):
old_obj = Hunt.objects.get(pk=self.pk)
if(old_obj.is_current_hunt):
raise ValidationError({'is_current_hunt':
["There must always be one current hunt", ]})
except ObjectDoesNotExist:
Expand Down Expand Up @@ -265,11 +265,19 @@ class Meta:
storage=PuzzleOverwriteStorage(),
blank=True,
help_text="Puzzle resources, MUST BE A ZIP FILE.")
solution_is_webpage = models.BooleanField(
default=False,
help_text="Is this solution an html webpage?")
solution_file = models.FileField(
upload_to=get_solution_file_path,
storage=PuzzleOverwriteStorage(),
blank=True,
help_text="Puzzle solution. MUST BE A PDF.")
solution_resource_file = models.FileField(
upload_to=get_solution_file_path,
storage=PuzzleOverwriteStorage(),
blank=True,
help_text="Puzzle solution resources, MUST BE A ZIP FILE.")
extra_data = models.CharField(
max_length=200,
blank=True,
Expand Down Expand Up @@ -300,22 +308,43 @@ class Meta:

# Overridden to delete old files on clear
def save(self, *args, **kwargs):
if(self.puzzle_file.name == ""):
old_instance = Puzzle.objects.get(pk=self.pk)
extension = old_instance.puzzle_file.name.split('.')[-1]
folder = "".join(old_instance.puzzle_file.name.split('.')[:-1])
if(extension == "zip"):
shutil.rmtree(os.path.join(settings.MEDIA_ROOT, folder), ignore_errors=True)
if os.path.exists(os.path.join(settings.MEDIA_ROOT, old_instance.puzzle_file.name)):
os.remove(os.path.join(settings.MEDIA_ROOT, old_instance.puzzle_file.name))
if(self.resource_file.name == ""):
old_instance = Puzzle.objects.get(pk=self.pk)
extension = old_instance.resource_file.name.split('.')[-1]
folder = "".join(old_instance.resource_file.name.split('.')[:-1])
if(extension == "zip"):
shutil.rmtree(os.path.join(settings.MEDIA_ROOT, folder), ignore_errors=True)
if os.path.exists(os.path.join(settings.MEDIA_ROOT, old_instance.resource_file.name)):
os.remove(os.path.join(settings.MEDIA_ROOT, old_instance.resource_file.name))
if(self.pk):
# TODO: Clean up this repetitive code
old_obj = Puzzle.objects.get(pk=self.pk)
if(self.puzzle_file.name == "" and old_obj.puzzle_file.name != ""):
full_name = os.path.join(settings.MEDIA_ROOT, old_obj.puzzle_file.name)
extension = old_obj.puzzle_file.name.split('.')[-1]
folder = "".join(old_obj.puzzle_file.name.split('.')[:-1])
if(extension == "zip"):
shutil.rmtree(os.path.join(settings.MEDIA_ROOT, folder), ignore_errors=True)
if os.path.exists(full_name):
os.remove(full_name)
if(self.resource_file.name == "" and old_obj.resource_file.name != ""):
full_name = os.path.join(settings.MEDIA_ROOT, old_obj.resource_file.name)
extension = old_obj.resource_file.name.split('.')[-1]
folder = "".join(old_obj.resource_file.name.split('.')[:-1])
if(extension == "zip"):
shutil.rmtree(os.path.join(settings.MEDIA_ROOT, folder), ignore_errors=True)
if os.path.exists(full_name):
os.remove(full_name)
if(self.solution_file.name == "" and old_obj.solution_file.name != ""):
full_name = os.path.join(settings.MEDIA_ROOT, old_obj.solution_file.name)
extension = old_obj.solution_file.name.split('.')[-1]
folder = "".join(old_obj.solution_file.name.split('.')[:-1])
if(extension == "zip"):
shutil.rmtree(os.path.join(settings.MEDIA_ROOT, folder), ignore_errors=True)
if os.path.exists(full_name):
os.remove(full_name)
old_name = old_obj.solution_resource_file.name
if(self.solution_resource_file.name == "" and old_name != ""):
full_name = os.path.join(settings.MEDIA_ROOT, old_obj.solution_resource_file.name)
extension = old_obj.solution_resource_file.name.split('.')[-1]
folder = "".join(old_obj.solution_resource_file.name.split('.')[:-1])
if(extension == "zip"):
shutil.rmtree(os.path.join(settings.MEDIA_ROOT, folder), ignore_errors=True)
if os.path.exists(full_name):
os.remove(full_name)

super(Puzzle, self).save(*args, **kwargs)

def serialize_for_ajax(self):
Expand Down Expand Up @@ -375,13 +404,14 @@ def __str__(self):
# Overridden to delete old files on clear
def save(self, *args, **kwargs):
if(self.resource_file.name == ""):
old_instance = Prepuzzle.objects.get(pk=self.pk)
extension = old_instance.resource_file.name.split('.')[-1]
folder = "".join(old_instance.resource_file.name.split('.')[:-1])
if(extension == "zip"):
shutil.rmtree(os.path.join(settings.MEDIA_ROOT, folder), ignore_errors=True)
if os.path.exists(os.path.join(settings.MEDIA_ROOT, old_instance.resource_file.name)):
os.remove(os.path.join(settings.MEDIA_ROOT, old_instance.resource_file.name))
old_obj = Prepuzzle.objects.get(pk=self.pk)
if(old_obj.resource_file.name != ""):
extension = old_obj.resource_file.name.split('.')[-1]
folder = "".join(old_obj.resource_file.name.split('.')[:-1])
if(extension == "zip"):
shutil.rmtree(os.path.join(settings.MEDIA_ROOT, folder), ignore_errors=True)
if os.path.exists(os.path.join(settings.MEDIA_ROOT, old_obj.resource_file.name)):
os.remove(os.path.join(settings.MEDIA_ROOT, old_obj.resource_file.name))
super(Prepuzzle, self).save(*args, **kwargs)


Expand Down
8 changes: 6 additions & 2 deletions huntserver/templates/puzzle.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@ <h4> {{ puzzle.solved_for.count }} teams have solved this puzzle so far.</h4>
</a>
{% elif puzzle.puzzle_page_type == puzzle.WEB_PUZZLE %}
<a class="btn btn-default" href='{{ PROTECTED_URL }}puzzles/{{ puzzle.puzzle_id }}/index.html' target="_blank">
Open separate page
Open as separate page
</a>
{% endif %}
{% if puzzle.hunt.is_public %}
{% if puzzle.solution_file != "" %}
{% if puzzle.solution_is_webpage and puzzle.solution_resource_file != "" %}
<a class="btn btn-default" href='{{ PROTECTED_URL }}solutions/{{ puzzle.puzzle_id }}_sol/index.html'>
View solution
</a>
{% elif not puzzle.solution_is_webpage and puzzle.solution_file != "" %}
<a class="btn btn-default" href='{{ PROTECTED_URL }}solutions/{{ puzzle.puzzle_id }}_sol.pdf'>
View solution
</a>
Expand Down

0 comments on commit fd313e1

Please sign in to comment.