diff --git a/problem/migrations/0003_auto_20150810_2233.py b/problem/migrations/0003_auto_20150810_2233.py new file mode 100644 index 00000000..709633fe --- /dev/null +++ b/problem/migrations/0003_auto_20150810_2233.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('problem', '0002_remove_problemtag_description'), + ] + + operations = [ + migrations.RenameField( + model_name='problem', + old_name='sample', + new_name='samples', + ), + ] diff --git a/problem/models.py b/problem/models.py index 3e236af3..b9a31813 100644 --- a/problem/models.py +++ b/problem/models.py @@ -6,7 +6,6 @@ from account.models import User class ProblemTag(models.Model): name = models.CharField(max_length=30) - # description = models.CharField(max_length=50) class Meta: db_table = "problem_tag" @@ -18,7 +17,7 @@ class AbstractProblem(models.Model): # 问题描述 HTML 格式 description = models.TextField() # 样例输入 可能会存储 json 格式的数据 - sample = models.TextField(blank=True) + samples = models.TextField(blank=True) # 测试用例id 这个id 可以用来拼接得到测试用例的文件存储位置 test_case_id = models.CharField(max_length=40) # 提示 diff --git a/problem/serizalizers.py b/problem/serizalizers.py index 66f3d75d..090e4c99 100644 --- a/problem/serizalizers.py +++ b/problem/serizalizers.py @@ -14,7 +14,6 @@ class ProblemSampleSerializer(serializers.ListField): class JSONField(serializers.Field): def to_representation(self, value): - print value, type(value) return json.loads(value) @@ -22,18 +21,18 @@ class CreateProblemSerializer(serializers.Serializer): title = serializers.CharField(max_length=50) description = serializers.CharField(max_length=10000) # [{"input": "1 1", "output": "2"}] - sample = ProblemSampleSerializer() + samples = ProblemSampleSerializer() test_case_id = serializers.CharField(max_length=40) source = serializers.CharField(max_length=30, required=False, default=None) time_limit = serializers.IntegerField() memory_limit = serializers.IntegerField() difficulty = serializers.IntegerField() - tags = serializers.ListField(child=serializers.IntegerField()) + tags = serializers.ListField(child=serializers.CharField(max_length=10)) hint = serializers.CharField(max_length=3000, required=False, default=None) class ProblemSerializer(serializers.ModelSerializer): - sample = JSONField() + samples = JSONField() class UserSerializer(serializers.ModelSerializer): class Meta: @@ -56,7 +55,7 @@ class EditProblemSerializer(serializers.Serializer): memory_limit = serializers.IntegerField() difficulty = serializers.IntegerField() tags = serializers.ListField(child=serializers.IntegerField()) - sample = ProblemSampleSerializer() + samples = ProblemSampleSerializer() hint = serializers.CharField(max_length=10000) visible = serializers.BooleanField() diff --git a/problem/tests.py b/problem/tests.py index 1b5f83ac..2ee84b2c 100644 --- a/problem/tests.py +++ b/problem/tests.py @@ -34,7 +34,7 @@ class ProblemAdminTest(APITestCase): "description": "des1", "test_case_id": "1", "source": "source1", - "sample": [{"input": "1 1", "output": "2"}], + "samples": [{"input": "1 1", "output": "2"}], "time_limit": "100", "memory_limit": "1000", "difficulty": "1", @@ -58,7 +58,7 @@ class ProblemAdminTest(APITestCase): description="des1", test_case_id="1", source="source1", - sample=[{"input": "1 1", "output": "2"}], + samples=[{"input": "1 1", "output": "2"}], time_limit=100, memory_limit=1000, difficulty=1, @@ -70,7 +70,7 @@ class ProblemAdminTest(APITestCase): "description": "des1", "test_case_id": "1", "source": "source1", - "sample": [{"input": "1 1", "output": "2"}], + "samples": [{"input": "1 1", "output": "2"}], "time_limit": "100", "memory_limit": "1000", "difficulty": "1", @@ -89,7 +89,7 @@ class ProblemAdminTest(APITestCase): description="des1", test_case_id="1", source="source1", - sample=[{"input": "1 1", "output": "2"}], + samples=[{"input": "1 1", "output": "2"}], time_limit=100, memory_limit=1000, difficulty=1, @@ -101,7 +101,7 @@ class ProblemAdminTest(APITestCase): "description": "des1", "test_case_id": "1", "source": "source1", - "sample": [{"input": "1 1", "output": "2"}], + "samples": [{"input": "1 1", "output": "2"}], "time_limit": "100", "memory_limit": "1000", "difficulty": "1", diff --git a/problem/views.py b/problem/views.py index 8cdf3282..f9677b1c 100644 --- a/problem/views.py +++ b/problem/views.py @@ -34,6 +34,7 @@ class ProblemTagAdminAPIView(APIView): return error_response(serializer) def get(self, request): + return success_response(ProblemTagSerializer(ProblemTag.objects.all(), many=True).data) keyword = request.GET.get("keyword", None) if not keyword: return error_response(u"参数错误") @@ -44,11 +45,11 @@ class ProblemTagAdminAPIView(APIView): def problem_page(request, problem_id): - # try: - # problem = Problem.objects.get(id=problem_id) - # except Problem.DoesNotExist: - # return render(request, "utils/error.html", {"error": u"题目不存在"}) - return render(request, "oj/problem/problem.html", {"problem": {}}) + try: + problem = Problem.objects.get(id=problem_id) + except Problem.DoesNotExist: + return render(request, "utils/error.html", {"error": u"题目不存在"}) + return render(request, "oj/problem/problem.html", {"problem": problem, "samples": json.loads(problem.samples)}) def problem_my_solutions_list_page(request, problem_id): @@ -75,15 +76,19 @@ class ProblemAdminAPIView(APIView): description=data["description"], test_case_id=data["test_case_id"], source=data["source"], - sample=json.dumps(data["sample"]), + samples=json.dumps(data["samples"]), time_limit=data["time_limit"], memory_limit=data["memory_limit"], difficulty=data["difficulty"], created_by=request.user, hint=data["hint"]) - tags = ProblemTag.objects.filter(id__in=data["tags"]) - problem.tags.add(*tags) + for tag in data["tags"]: + try: + tag = ProblemTag.objects.get(name=tag) + except ProblemTag.DoesNotExist: + tag = ProblemTag.objects.create(name=tag) + problem.tags.add(tag) return success_response(ProblemSerializer(problem).data) else: return serializer_invalid_response(serializer) @@ -110,7 +115,7 @@ class ProblemAdminAPIView(APIView): problem.time_limit = data["time_limit"] problem.memory_limit = data["memory_limit"] problem.difficulty = data["difficulty"] - problem.sample = json.dumps(data["sample"]) + problem.samples = json.dumps(data["samples"]) problem.hint = data["hint"] problem.visible = data["visible"]