server端创建submission的基础逻辑

This commit is contained in:
virusdefender 2016-03-09 19:34:21 +08:00
parent 274924c509
commit d8625d99cc
8 changed files with 135 additions and 7 deletions

View File

@ -16,9 +16,11 @@ Including another URLconf
from django.conf.urls import url
from django.contrib import admin
from server.views import ProblemAPIView
from server.views import ProblemAPIView, SubmissionAPIView
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^problem/$', ProblemAPIView.as_view()),
url(r'^submission/$', SubmissionAPIView.as_view()),
]

View File

@ -1,6 +1,12 @@
# coding=utf-8
from django.contrib import admin
from .models import OJ, RobotUser, RobotStatusInfo, Problem, Submission
from .models import OJ, RobotUser, RobotStatusInfo, Problem, Submission, APIKey
class APIKeyAdmin(admin.ModelAdmin):
list_display = ["name", "create_time"]
admin.site.register(APIKey, APIKeyAdmin)
class OJAdmin(admin.ModelAdmin):
@ -34,4 +40,7 @@ class ProblemAdmin(admin.ModelAdmin):
admin.site.register(Problem, ProblemAdmin)
admin.site.register(Submission)
class SubmissionAdmin(admin.ModelAdmin):
list_display = ["api_key", "problem", "language", "result", "create_time", "status"]
admin.site.register(Submission, SubmissionAdmin)

View File

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.4 on 2016-03-09 09:06
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('server', '0002_auto_20160309_1656'),
]
operations = [
migrations.AddField(
model_name='problem',
name='origin_id',
field=models.CharField(blank=True, max_length=30, null=True),
),
]

View File

@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.1 on 2016-03-09 11:11
from __future__ import unicode_literals
import datetime
from django.db import migrations, models
import django.db.models.deletion
from django.utils.timezone import utc
import server.models
class Migration(migrations.Migration):
dependencies = [
('server', '0003_problem_origin_id'),
]
operations = [
migrations.AddField(
model_name='submission',
name='api_key',
field=models.ForeignKey(default=None, on_delete=django.db.models.deletion.CASCADE, to='server.APIKey'),
preserve_default=False,
),
migrations.AddField(
model_name='submission',
name='create_time',
field=models.DateTimeField(auto_now_add=True, default=datetime.datetime(2016, 3, 9, 11, 11, 38, 878862, tzinfo=utc)),
preserve_default=False,
),
migrations.AddField(
model_name='submission',
name='status',
field=models.IntegerField(choices=[(1, 'done'), (server.models.SubmissionStatus, 'crawling'), (3, 'failed')], default=1),
preserve_default=False,
),
]

View File

@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.1 on 2016-03-09 11:18
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('server', '0004_auto_20160309_1911'),
]
operations = [
migrations.AddField(
model_name='submission',
name='language',
field=models.IntegerField(choices=[('C', 0), ('CPP', 1), ('Java', 2)], default=1),
preserve_default=False,
),
]

View File

@ -2,6 +2,7 @@
from __future__ import unicode_literals
from django.db import models
from robots.utils import Language, Result
class OJ(models.Model):
@ -71,6 +72,7 @@ class ProblemStatus(models.Model):
class Problem(models.Model):
oj = models.ForeignKey(OJ)
url = models.URLField()
origin_id = models.CharField(max_length=30, blank=True, null=True)
submit_url = models.URLField(blank=True, null=True)
title = models.CharField(max_length=100, blank=True, null=True)
description = models.TextField(blank=True, null=True)
@ -99,13 +101,34 @@ class Problem(models.Model):
return self.oj.name + " - " + self.title
class SubmissionStatus(object):
done = 1
crawling = 2
failed = 3
class Submission(models.Model):
api_key = models.ForeignKey(APIKey)
problem = models.ForeignKey(Problem)
result = models.IntegerField()
robot_user = models.ForeignKey(RobotUser)
language = models.IntegerField(choices=((Language.C, "C", ), (Language.CPP, "CPP"), (Language.Java, "Java")))
result = models.IntegerField(choices=((Result.accepted, "accepted"),
(Result.wrong_answer, "wrong_answer"),
(Result.compile_error, "compiler_error"),
(Result.format_error, "format_error"),
(Result.memory_limit_exceeded, "memory_limit_exceeded"),
(Result.time_limit_exceeded, "time_limit_exceeded"),
(Result.runtime_error, "runtime_error"),
(Result.system_error, "system_error"),
(Result.waiting, "waiting")))
cpu_time = models.IntegerField()
memory = models.IntegerField()
info = models.TextField()
robot_user = models.ForeignKey(RobotUser)
create_time = models.DateTimeField(auto_now_add=True)
status = models.IntegerField(choices=((SubmissionStatus.done, "done"),
(SubmissionStatus, "crawling"),
(SubmissionStatus.failed, "failed")))
class Meta:
db_table = "submission"

View File

@ -1,9 +1,16 @@
# coding=utf-8
from rest_framework import serializers
from robots.utils import Language
from .models import Problem
class ProblemSerializer(serializers.ModelSerializer):
class Meta:
model = Problem
class CreateSubmissionSerializer(serializers.Serializer):
url = serializers.URLField()
language = serializers.ChoiceField(choices=[Language.C, Language.CPP, Language.Java])
code = serializers.CharField(max_length=10000)

View File

@ -6,7 +6,7 @@ from rest_framework.response import Response
from celery import states
from .serializers import ProblemSerializer
from .serializers import ProblemSerializer, CreateSubmissionSerializer
from .models import OJ, Problem, RobotUser, RobotStatusInfo, ProblemStatus
from .tasks import get_problem
@ -16,7 +16,7 @@ def error_response(error_reason):
def serializer_invalid_response(serializer):
for k, v in serializer.errors.iteritems():
for k, v in serializer.errors.items():
return error_response(k + " : " + v[0])
@ -48,6 +48,7 @@ class ProblemAPIView(APIView):
# 如果任务状态是成功,就更新数据库,并返回结果
if task.state == states.SUCCESS:
result = task.get()
problem.origin_id = result["id"]
problem.title = result["title"]
problem.submit_url = result["submit_url"]
problem.description = result["description"]
@ -88,3 +89,11 @@ class ProblemAPIView(APIView):
Problem.objects.create(oj=oj, url=url, status=ProblemStatus.crawling, task_id=task_id)
return success_response({"status": ProblemStatus.crawling})
class SubmissionAPIView(APIView):
def post(self, request):
serializer = CreateSubmissionSerializer(data=request.data)
if serializer.is_valid():
return success_response(serializer.data)
else:
return serializer_invalid_response(serializer)