Adds tastypie with resources for Players, Teams, Challenges and Solutions to core app. This is a partial fix for #28.

This commit is contained in:
Louis Fogel 2014-11-02 21:10:53 -05:00
parent d5f4159b8c
commit 301e6ba377
6 changed files with 67 additions and 2 deletions

43
goatctf/core/api.py Normal file
View File

@ -0,0 +1,43 @@
from django.contrib.auth.models import User
from tastypie import fields
from tastypie.resources import ModelResource
from core.models import Challenge, Team, Player, Solution
class PlayerResource(ModelResource):
full_name = fields.CharField('get_full_name')
gravatar_url = fields.FileField('get_gravatar_url')
has_gravatar = fields.BooleanField('has_gravatar')
class Meta:
queryset = Player.objects.all()
resource_name = 'player'
excludes = ['email', 'password', 'is_active', 'is_staff', 'is_superuser']
allowed_methods = ['get']
class TeamResource(ModelResource):
members = fields.ToManyField(PlayerResource, 'player_set')
class Meta:
queryset = Team.objects.all()
resource_name = 'team'
allowed_methods = ['get']
class ChallengeResource(ModelResource):
category_full = fields.CharField('get_category_display')
class Meta:
queryset = Challenge.objects.all()
resource_name = 'challenge'
allowed_methods = ['get']
class SolutionResource(ModelResource):
challenge = fields.ForeignKey(ChallengeResource, 'challenge')
player = fields.ForeignKey(PlayerResource, 'player')
class Meta:
queryset = Solution.objects.all()
resource_name = 'solution'
allowed_methods = ['get']

View File

@ -4,7 +4,7 @@ from django.db.utils import IntegrityError
import markdown
from core.settings import CHALLENGE_NAME_LENGTH, FLAG_LENGTH, TEAM_NAME_LENGTH
from django_gravatar.helpers import get_gravatar_url, has_gravatar
class Challenge(models.Model):
"""A challenge represents an individual problem to be solved."""
@ -73,6 +73,12 @@ class Player(User):
raise IntegrityError("Player must be a part of all created teams!")
super(Player, self).save(*args, **kwargs)
def get_gravatar_url(self, size=150):
return get_gravatar_url(self.email, size)
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")

13
goatctf/core/urls.py Normal file
View File

@ -0,0 +1,13 @@
from django.conf.urls import patterns, include, url
from tastypie.api import Api
from core import api
v1_api = Api(api_name='v1')
v1_api.register(api.ChallengeResource())
v1_api.register(api.PlayerResource())
v1_api.register(api.SolutionResource())
v1_api.register(api.TeamResource())
urlpatterns = patterns('',
(r'^api/', include(v1_api.urls)),
)

View File

@ -37,6 +37,7 @@ INSTALLED_APPS = (
'django.contrib.messages',
'django.contrib.staticfiles',
'core',
'tastypie',
)
MIDDLEWARE_CLASSES = (

View File

@ -1,10 +1,11 @@
from django.conf.urls import patterns, include, url
from django.contrib import admin
import core.urls
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'goatctf.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'', include(core.urls)),
url(r'^admin/', include(admin.site.urls)),
)

View File

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