From 63eee102363e33ba05680d70f1518092b6fcc5ce Mon Sep 17 00:00:00 2001 From: Louis Fogel Date: Tue, 4 Nov 2014 12:27:24 -0500 Subject: [PATCH 1/2] Adds hint model to core. Fixes #32. --- goatctf/core/models.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/goatctf/core/models.py b/goatctf/core/models.py index bdbcaad..8935a56 100644 --- a/goatctf/core/models.py +++ b/goatctf/core/models.py @@ -108,3 +108,22 @@ class JoinRequest(models.Model): def cancel(self): self.delete() + +class Hint(models.Model): + """ + A hint is a bit of content that provides additional information about a + challenge. + """ + challenge = models.ForeignKey("Challenge") + created_date = models.DateTimeField(auto_now_add=True) + publish_date = models.DateTimeField(blank=True, null=False) + content_markdown = models.TextField() + content_html = models.TextField() + + def save(self, *args, **kwargs): + self.content_html = markdown.markdown(self.content_markdown) + if self.publish_date is None: + self.publish_date = self.created_date + + super(Hint, self).save(*args, **kwargs) + From 8a0c5391431ad0a82a5b4fd6c1ee6c35070319bb Mon Sep 17 00:00:00 2001 From: Louis Fogel Date: Tue, 4 Nov 2014 13:01:06 -0500 Subject: [PATCH 2/2] Adds admin and tests for Hint model. --- goatctf/core/admin.py | 6 +++++- goatctf/core/models.py | 8 ++++---- goatctf/tests/test_hint.py | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 goatctf/tests/test_hint.py diff --git a/goatctf/core/admin.py b/goatctf/core/admin.py index 6e8a13c..e5b976b 100644 --- a/goatctf/core/admin.py +++ b/goatctf/core/admin.py @@ -1,5 +1,5 @@ from django.contrib import admin -from core.models import Challenge, Team, Player, Solution +from core.models import Challenge, Hint, Team, Player, Solution from core.forms import ChallengeAdminForm @@ -7,7 +7,11 @@ class ChallengeAdmin(admin.ModelAdmin): fields = ['name', 'points', 'category', 'flag', 'description_markdown'] form = ChallengeAdminForm +class HintAdmin(admin.ModelAdmin): + fields = ['challenge', 'content_markdown', 'publish_date'] + admin.site.register(Challenge, ChallengeAdmin) +admin.site.register(Hint, HintAdmin) admin.site.register(Team) admin.site.register(Player) admin.site.register(Solution) diff --git a/goatctf/core/models.py b/goatctf/core/models.py index 8935a56..b292ef6 100644 --- a/goatctf/core/models.py +++ b/goatctf/core/models.py @@ -85,6 +85,7 @@ class Player(User): def has_gravatar(self): return has_gravatar(self.email) + class Solution(models.Model): """A solution is a record of a player's successful attempt of a challenge.""" challenge = models.ForeignKey("Challenge") @@ -116,14 +117,13 @@ class Hint(models.Model): """ challenge = models.ForeignKey("Challenge") created_date = models.DateTimeField(auto_now_add=True) - publish_date = models.DateTimeField(blank=True, null=False) + publish_date = models.DateTimeField(auto_now_add=True, editable=True) content_markdown = models.TextField() content_html = models.TextField() def save(self, *args, **kwargs): self.content_html = markdown.markdown(self.content_markdown) - if self.publish_date is None: - self.publish_date = self.created_date - super(Hint, self).save(*args, **kwargs) + def __str__(self): + return "{challenge} ({date})".format(challenge=self.challenge, date=self.publish_date) diff --git a/goatctf/tests/test_hint.py b/goatctf/tests/test_hint.py new file mode 100644 index 0000000..ea4b4ac --- /dev/null +++ b/goatctf/tests/test_hint.py @@ -0,0 +1,32 @@ +import pytest +from core.models import Challenge, Hint + + +@pytest.fixture +def challenge(): + c = Challenge(name="Get Crunk", category='be', points=100) + c.save() + return c + + +@pytest.mark.django_db +def test_auto_populate_publish_date(challenge): + hint = Hint(challenge=challenge, content_markdown='') + hint.save() + assert hint.publish_date is not None + + +@pytest.mark.django_db +def test_markdown_compiles_on_save(challenge): + hint = Hint(challenge=challenge) + hint.content_markdown = """# Title +## Subtitle + +Paragraph + +Another paragraph +""" + hint.save() + assert hint.content_html == ( + "

Title

\n

Subtitle

\n

Paragraph

\n

Another " + "paragraph

")