From 33f8a6c5a3397244b32ab20e3d1f73bed21d58e6 Mon Sep 17 00:00:00 2001 From: virusdefender Date: Thu, 26 Jan 2017 12:33:00 +0800 Subject: [PATCH] add create and get contest list api --- announcement/serializers.py | 10 ++-------- contest/migrations/0001_initial.py | 7 +++---- contest/models.py | 6 ++---- contest/serializers.py | 25 +++++++++++++++++++++++++ contest/{views.py => urls/__init__.py} | 0 contest/urls/admin.py | 7 +++++++ contest/views/__init__.py | 0 contest/views/admin.py | 20 ++++++++++++++++++++ deploy/requirements.txt | 1 + oj/urls.py | 1 + utils/api/_serializers.py | 5 +++++ 11 files changed, 66 insertions(+), 16 deletions(-) create mode 100644 contest/serializers.py rename contest/{views.py => urls/__init__.py} (100%) create mode 100644 contest/urls/admin.py create mode 100644 contest/views/__init__.py create mode 100644 contest/views/admin.py diff --git a/announcement/serializers.py b/announcement/serializers.py index 3aabb08b..eab1e741 100644 --- a/announcement/serializers.py +++ b/announcement/serializers.py @@ -1,6 +1,6 @@ from account.models import User from utils.api import serializers -from utils.api._serializers import DateTimeTZField +from utils.api._serializers import DateTimeTZField, UsernameSerializer from .models import Announcement @@ -14,13 +14,7 @@ class CreateAnnouncementSerializer(serializers.Serializer): class AnnouncementSerializer(serializers.ModelSerializer): create_time = DateTimeTZField() last_update_time = DateTimeTZField() - - class UserSerializer(serializers.ModelSerializer): - class Meta: - model = User - fields = ["username"] - - created_by = UserSerializer() + created_by = UsernameSerializer() class Meta: model = Announcement diff --git a/contest/migrations/0001_initial.py b/contest/migrations/0001_initial.py index ae0f9ae3..6bc91a9e 100644 --- a/contest/migrations/0001_initial.py +++ b/contest/migrations/0001_initial.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Generated by Django 1.9.12 on 2017-01-25 04:50 +# Generated by Django 1.9.12 on 2017-01-26 04:32 from __future__ import unicode_literals from django.conf import settings @@ -35,16 +35,15 @@ class Migration(migrations.Migration): name='Contest', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('title', models.CharField(max_length=40, unique=True)), + ('title', models.CharField(max_length=40)), ('description', utils.models.RichTextField()), ('real_time_rank', models.BooleanField()), ('password', models.CharField(blank=True, max_length=30, null=True)), - ('contest_type', models.CharField(max_length=36)), ('rule_type', models.CharField(max_length=36)), ('start_time', models.DateTimeField()), ('end_time', models.DateTimeField()), ('create_time', models.DateTimeField(auto_now_add=True)), - ('last_updated_time', models.DateTimeField(auto_now=True)), + ('last_update_time', models.DateTimeField(auto_now=True)), ('visible', models.BooleanField(default=True)), ('created_by', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), ], diff --git a/contest/models.py b/contest/models.py index ceb47bf1..93dcbdf1 100644 --- a/contest/models.py +++ b/contest/models.py @@ -24,19 +24,17 @@ class ContestRuleType(object): class Contest(models.Model): - title = models.CharField(max_length=40, unique=True) + title = models.CharField(max_length=40) description = RichTextField() # show real time rank or cached rank real_time_rank = models.BooleanField() password = models.CharField(max_length=30, blank=True, null=True) - # enum of ContestType - contest_type = models.CharField(max_length=36) # enum of ContestRuleType rule_type = models.CharField(max_length=36) start_time = models.DateTimeField() end_time = models.DateTimeField() create_time = models.DateTimeField(auto_now_add=True) - last_updated_time = models.DateTimeField(auto_now=True) + last_update_time = models.DateTimeField(auto_now=True) created_by = models.ForeignKey(User) # 是否可见 false的话相当于删除 visible = models.BooleanField(default=True) diff --git a/contest/serializers.py b/contest/serializers.py new file mode 100644 index 00000000..26ebe3be --- /dev/null +++ b/contest/serializers.py @@ -0,0 +1,25 @@ +from utils.api import serializers, DateTimeTZField, UsernameSerializer + +from .models import ContestRuleType, Contest + + +class CreateConetestSeriaizer(serializers.Serializer): + title = serializers.CharField(max_length=128) + description = serializers.CharField() + start_time = serializers.DateTimeField() + end_time = serializers.DateTimeField() + rule_type = serializers.ChoiceField(choices=[ContestRuleType.ACM, ContestRuleType.OI]) + password = serializers.CharField(allow_blank=True, max_length=32) + visible = serializers.BooleanField() + real_time_rank = serializers.BooleanField() + + +class ContestSerializer(serializers.ModelSerializer): + start_time = DateTimeTZField() + end_time = DateTimeTZField() + create_time = DateTimeTZField() + last_update_time = DateTimeTZField() + created_by = UsernameSerializer() + + class Meta: + model = Contest diff --git a/contest/views.py b/contest/urls/__init__.py similarity index 100% rename from contest/views.py rename to contest/urls/__init__.py diff --git a/contest/urls/admin.py b/contest/urls/admin.py new file mode 100644 index 00000000..75eb8f06 --- /dev/null +++ b/contest/urls/admin.py @@ -0,0 +1,7 @@ +from django.conf.urls import url + +from ..views.admin import ContestAPI + +urlpatterns = [ + url(r"^contest", ContestAPI.as_view(), name="contest_api"), +] diff --git a/contest/views/__init__.py b/contest/views/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/contest/views/admin.py b/contest/views/admin.py new file mode 100644 index 00000000..f2b93b78 --- /dev/null +++ b/contest/views/admin.py @@ -0,0 +1,20 @@ +import dateutil.parser +from utils.api import APIView, validate_serializer +from ..serializers import CreateConetestSeriaizer, ContestSerializer +from ..models import Contest + + +class ContestAPI(APIView): + @validate_serializer(CreateConetestSeriaizer) + def post(self, request): + data = request.data + data["start_time"] = dateutil.parser.parse(data["start_time"]) + data["end_time"] = dateutil.parser.parse(data["end_time"]) + data["created_by"] = request.user + print(data) + Contest.objects.create(**data) + print(request.data) + return self.success() + + def get(self, request): + return self.success(ContestSerializer(Contest.objects.all(), many=True).data) diff --git a/deploy/requirements.txt b/deploy/requirements.txt index ae085536..ae2a33cc 100644 --- a/deploy/requirements.txt +++ b/deploy/requirements.txt @@ -6,3 +6,4 @@ otpauth flake8-quotes pytz coverage +python-dateutil diff --git a/oj/urls.py b/oj/urls.py index ab1353ce..bdcc1467 100644 --- a/oj/urls.py +++ b/oj/urls.py @@ -8,4 +8,5 @@ urlpatterns = [ url(r"^api/admin/", include("conf.urls.admin")), url(r"^api/", include("problem.urls.oj")), url(r"^api/admin/", include("problem.urls.admin")), + url(r"^api/admin/", include("contest.urls.admin")), ] diff --git a/utils/api/_serializers.py b/utils/api/_serializers.py index 00a3b8cc..0ffa767e 100644 --- a/utils/api/_serializers.py +++ b/utils/api/_serializers.py @@ -18,3 +18,8 @@ class DateTimeTZField(serializers.DateTimeField): class IDOnlySerializer(serializers.Serializer): id = serializers.IntegerField() + + +class UsernameSerializer(serializers.Serializer): + id = serializers.IntegerField() + username = serializers.CharField()