From 301e6ba377316c96668d0879e233bd90015936c9 Mon Sep 17 00:00:00 2001 From: Louis Fogel Date: Sun, 2 Nov 2014 21:10:53 -0500 Subject: [PATCH] Adds tastypie with resources for Players, Teams, Challenges and Solutions to core app. This is a partial fix for #28. --- goatctf/core/api.py | 43 +++++++++++++++++++++++++++++ goatctf/core/models.py | 8 +++++- goatctf/core/urls.py | 13 +++++++++ goatctf/goatctf/settings.py.example | 1 + goatctf/goatctf/urls.py | 3 +- requirements.txt | 1 + 6 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 goatctf/core/api.py create mode 100644 goatctf/core/urls.py diff --git a/goatctf/core/api.py b/goatctf/core/api.py new file mode 100644 index 0000000..624252b --- /dev/null +++ b/goatctf/core/api.py @@ -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'] + diff --git a/goatctf/core/models.py b/goatctf/core/models.py index 347c54a..00739c1 100644 --- a/goatctf/core/models.py +++ b/goatctf/core/models.py @@ -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") diff --git a/goatctf/core/urls.py b/goatctf/core/urls.py new file mode 100644 index 0000000..73e3491 --- /dev/null +++ b/goatctf/core/urls.py @@ -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)), +) diff --git a/goatctf/goatctf/settings.py.example b/goatctf/goatctf/settings.py.example index e7e2b84..522e972 100644 --- a/goatctf/goatctf/settings.py.example +++ b/goatctf/goatctf/settings.py.example @@ -37,6 +37,7 @@ INSTALLED_APPS = ( 'django.contrib.messages', 'django.contrib.staticfiles', 'core', + 'tastypie', ) MIDDLEWARE_CLASSES = ( diff --git a/goatctf/goatctf/urls.py b/goatctf/goatctf/urls.py index 5947ca0..517c155 100644 --- a/goatctf/goatctf/urls.py +++ b/goatctf/goatctf/urls.py @@ -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)), ) diff --git a/requirements.txt b/requirements.txt index be8e747..2c10e6f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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