add create and get contest list api

This commit is contained in:
virusdefender 2017-01-26 12:33:00 +08:00
parent 681a88ea83
commit 33f8a6c5a3
11 changed files with 66 additions and 16 deletions

View File

@ -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

View File

@ -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)),
],

View File

@ -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)

25
contest/serializers.py Normal file
View File

@ -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

7
contest/urls/admin.py Normal file
View File

@ -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"),
]

View File

20
contest/views/admin.py Normal file
View File

@ -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)

View File

@ -6,3 +6,4 @@ otpauth
flake8-quotes
pytz
coverage
python-dateutil

View File

@ -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")),
]

View File

@ -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()