[#5] Add Challenge to admin interface

This commit is contained in:
Ian Adam Naval 2014-10-31 20:27:03 -04:00
parent 2d061c6f46
commit 35af9c5ff8
7 changed files with 57 additions and 3 deletions

View File

@ -1,3 +1,10 @@
from django.contrib import admin
from core.models import Challenge
from core.forms import ChallengeAdminForm
# Register your models here.
class ChallengeAdmin(admin.ModelAdmin):
fields = ['points', 'category', 'flag', 'description_markdown']
form = ChallengeAdminForm
admin.site.register(Challenge, ChallengeAdmin)

8
goatctf/core/forms.py Normal file
View File

@ -0,0 +1,8 @@
from django import forms
from django.contrib.admin.widgets import AdminTextareaWidget
class ChallengeAdminForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(ChallengeAdminForm, self).__init__(*args, **kwargs)
self.fields['description_markdown'].widget = AdminTextareaWidget()

View File

@ -1,16 +1,23 @@
from django.contrib.auth.models import User
from django.db import models
from core.settings import TEAM_NAME_LENGTH, FLAG_LENGTH
import markdown
from core.settings import CHALLENGE_NAME_LENGTH, FLAG_LENGTH, TEAM_NAME_LENGTH
class Challenge(models.Model):
"""A challenge represents an individual problem to be solved."""
name = models.CharField(max_length=CHALLENGE_NAME_LENGTH)
points = models.IntegerField()
category = models.CharField(max_length=2)
flag = models.CharField(max_length=FLAG_LENGTH)
description_markdown = models.TextField()
description_html = models.TextField()
def save(self, *args, **kwargs):
self.description_html = markdown.markdown(self.description_markdown)
super(Challenge, self).save(*args, **kwargs)
class Team(models.Model):
"""A team is a collection of players."""

View File

@ -1,2 +1,3 @@
FLAG_LENGTH = 256
TEAM_NAME_LENGTH = 64
CHALLENGE_NAME_LENGTH = 128

2
goatctf/tests/pytest.ini Normal file
View File

@ -0,0 +1,2 @@
[pytest]
DJANGO_SETTINGS_MODULE=goatctf.settings

View File

@ -0,0 +1,26 @@
import pytest
from core.models import Challenge
@pytest.fixture
def challenge():
return Challenge()
@pytest.mark.django_db
def test_markdown_compiles_on_save(challenge):
challenge.name = "Test Challenge"
challenge.points = 0
challenge.category = ""
challenge.flag = ""
challenge.description_markdown = """# Title
## Subtitle
Paragraph
Another paragraph
"""
challenge.save()
assert challenge.description_html == (
"<h1>Title</h1>\n<h2>Subtitle</h2>\n<p>Paragraph</p>\n<p>Another "
"paragraph</p>")

View File

@ -1,2 +1,5 @@
Django==1.7.1
django-gravatar2==1.1.4
django-gravatar2==1.1.4
Markdown==2.5.1
pytest==2.6.4
pytest-django==2.7.0