Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for puzzle content and solution file uploads #20

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions puzzle_editing/migrations/0004_auto_20210619_0414.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Generated by Django 3.1.8 on 2021-06-19 04:14
from django.db import migrations
from django.db import models

import puzzle_editing.models


class Migration(migrations.Migration):

dependencies = [
("puzzle_editing", "0003_auto_20210208_0427"),
]

operations = [
migrations.AddField(
model_name="puzzle",
name="uploaded_content",
field=models.FileField(
blank=True,
help_text="An uploaded file of the puzzle itself. It may be a text file, PDF, etc. or it may be a zip file containing an index.html page as well as additional assets.",
null=True,
upload_to=puzzle_editing.models.Puzzle.uploaded_content_path,
),
),
migrations.AddField(
model_name="puzzle",
name="uploaded_solution",
field=models.FileField(
blank=True,
help_text="An uploaded file of the puzzle solution. It may be a text file, PDF, etc. or it may be a zip file containing an index.html page as well as additional assets.",
null=True,
upload_to=puzzle_editing.models.Puzzle.uploaded_solution_path,
),
),
]
37 changes: 34 additions & 3 deletions puzzle_editing/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import os
import random
import time
from enum import Enum

import django.urls as urls
Expand Down Expand Up @@ -259,10 +262,37 @@ def get_transitions(self):
),
default=3,
)

content = models.TextField(
blank=True, help_text="The puzzle itself. An external link is fine."
blank=True,
help_text="The puzzle itself. An external link is fine.",
)

def uploaded_content_path(instance, filename):
_, ext = os.path.splitext(filename.lower())
return f"uploaded_content/{instance.id}/{int(time.time())}-{random.randrange(1000000)}{ext}"

uploaded_content = models.FileField(
upload_to=uploaded_content_path,
null=True,
blank=True,
help_text="An uploaded file of the puzzle itself. It may be a text file, PDF, etc. or it may be a zip file containing an index.html page as well as additional assets.",
)

solution = models.TextField(
blank=True,
)

def uploaded_solution_path(instance, filename):
_, ext = os.path.splitext(filename.lower())
return f"uploaded_solution/{instance.id}/{int(time.time())}-{random.randrange(1000000)}{ext}"

uploaded_solution = models.FileField(
upload_to=uploaded_solution_path,
null=True,
blank=True,
help_text="An uploaded file of the puzzle solution. It may be a text file, PDF, etc. or it may be a zip file containing an index.html page as well as additional assets.",
)
solution = models.TextField(blank=True)

def get_emails(self, exclude_emails=()):
emails = set(self.authors.values_list("email", flat=True))
Expand Down Expand Up @@ -352,7 +382,8 @@ class StatusSubscription(models.Model):
"""An indication to email a user when any puzzle enters this status."""

status = models.CharField(
max_length=status.MAX_LENGTH, choices=status.DESCRIPTIONS.items(),
max_length=status.MAX_LENGTH,
choices=status.DESCRIPTIONS.items(),
)
user = models.ForeignKey(User, on_delete=models.CASCADE)

Expand Down
17 changes: 13 additions & 4 deletions puzzle_editing/templates/puzzle.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{% load user_list %}
{% load comment_list %}
{% load markdown %}
{% load upload_url %}
{% block title %}
{{ puzzle.spoiler_free_title }}
{% endblock %}
Expand Down Expand Up @@ -161,26 +162,34 @@ <h2>Summary/Description <a href="{% url 'puzzle_edit' puzzle.id %}" class="edit-
</table>

<h2>Puzzle <button type="button" class="toggle-show" data-target="content-form" data-rehide="Hide editing">Edit</button></h2>
<form method="post" id="content-form" class="hidden">
<form method="post" id="content-form" class="hidden" enctype="multipart/form-data">
{% csrf_token %}
{{ content_form }}
<table>
{{ content_form.as_table }}
</table>
<input type="submit" name="edit_content" value="Submit">
</form>
{% if puzzle.has_postprod %}<b>Since the puzzle has been postprodded, <a href="https://FIXME.example.com/{{puzzle.postprod.slug}}/">the postprodded puzzle</a> should be used rather than this field.</b>{% endif %}
{% if puzzle.content %}
{{ puzzle.content|markdown }}
{% elif puzzle.uploaded_content %}
Link to puzzle content: <a href="{{ puzzle.uploaded_content|upload_url }}" target="_blank">{{ puzzle.uploaded_content|upload_url }}</a>
{% else %}
<div class="empty">(no puzzle yet)</div>
{% endif %}
<h2>Solution <button type="button" class="toggle-show" data-target="solution-form" data-rehide="Hide editing">Edit</button></h2>
<form method="post" id="solution-form" class="hidden">
<form method="post" id="solution-form" class="hidden" enctype="multipart/form-data">
{% csrf_token %}
{{ solution_form }}
<table>
{{ solution_form.as_table }}
</table>
<input type="submit" name="edit_solution" value="Submit">
</form>
{% if puzzle.has_postprod %}<b>Since the puzzle has been postprodded, <a href="https://FIXME.example.com/{{puzzle.postprod.slug}}/solution/">the postprodded solution</a> should be used rather than this field.</b>{% endif %}
{% if puzzle.solution %}
{{ puzzle.solution|markdown }}
{% elif puzzle.uploaded_solution %}
Link to solution: <a href="{{ puzzle.uploaded_solution|upload_url }}" target="_blank">{{ puzzle.uploaded_solution|upload_url }}</a>
{% else %}
<div class="empty">(no solution yet)</div>
{% endif %}
Expand Down
3 changes: 3 additions & 0 deletions puzzle_editing/templates/testsolve_one.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{% load user_list %}
{% load comment_list %}
{% load markdown %}
{% load upload_url %}
{% block title %}
Testsolving {{ session.puzzle }}
{% endblock %}
Expand Down Expand Up @@ -96,6 +97,8 @@ <h2>Puzzle</h2>
{% if session.puzzle.has_postprod %}<b>Since the puzzle has been postprodded, <a href="https://postprod.hidden.institute/pppzzlvwr21/{{session.puzzle.postprod.slug}}/">the postprodded puzzle</a> should be used rather than this field.</b>{% endif %}
{% if session.puzzle.content %}
{{ session.puzzle.content|markdown }}
{% elif session.puzzle.uploaded_content %}
Link to puzzle content: <a href="{{ session.puzzle.uploaded_content|upload_url }}" target="_blank">{{ session.puzzle.uploaded_content|upload_url }}</a>
{% else %}
<div class="empty">(no puzzle yet)</div>
{% endif %}
Expand Down
10 changes: 10 additions & 0 deletions puzzle_editing/templatetags/upload_url.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django import template

from ..utils import get_upload_url

register = template.Library()


@register.filter
def upload_url(file_field):
return get_upload_url(file_field)
15 changes: 15 additions & 0 deletions puzzle_editing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@
from puzzle_editing.models import PuzzlePostprod


def extract_uploaded_zip_in_place(zip_path):
dir_path = os.path.splitext(zip_path)[0]
if not os.path.isdir(dir_path):
os.mkdir(dir_path)
with ZipFile(zip_path, "r") as f:
f.extractall(dir_path)


def get_upload_url(file_field):
_, ext = os.path.splitext(file_field.path)
if ext == ".zip":
return file_field.url[:-4] + "/"
return file_field.url


def get_latest_zip(pp):
try:
repo = git.Repo.init(settings.HUNT_REPO)
Expand Down
Loading