mirror of
https://github.com/QingdaoU/OnlineJudge.git
synced 2024-12-29 08:32:08 +00:00
增加了公告发布成功的测试,以及公告分页的APIview
This commit is contained in:
parent
96c8d28c64
commit
844e970276
@ -1,8 +1,24 @@
|
||||
# coding=utf-8
|
||||
from rest_framework import serializers
|
||||
|
||||
from account.models import User
|
||||
from .models import Announcement
|
||||
|
||||
class AnnouncementSerializer(serializers.Serializer):
|
||||
|
||||
class CreateAnnouncementSerializer(serializers.Serializer):
|
||||
title = serializers.CharField(max_length=50)
|
||||
content = serializers.CharField(max_length=10000)
|
||||
|
||||
|
||||
class AnnouncementSerializer(serializers.ModelSerializer):
|
||||
|
||||
class UserSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ["username"]
|
||||
|
||||
created_by = UserSerializer()
|
||||
|
||||
class Meta:
|
||||
model = Announcement
|
||||
|
||||
|
@ -3,14 +3,25 @@ from django.core.urlresolvers import reverse
|
||||
|
||||
from rest_framework.test import APITestCase, APIClient
|
||||
|
||||
from account.models import User
|
||||
|
||||
|
||||
class AnnouncementAPITest(APITestCase):
|
||||
def setUp(self):
|
||||
self.client = APIClient()
|
||||
self.url = reverse("announcement_api")
|
||||
self.url = reverse("announcement_admin_api")
|
||||
user = User.objects.create(username="test")
|
||||
user.set_password("test")
|
||||
user.save()
|
||||
|
||||
def test_invalid_format(self):
|
||||
# todo 判断用户是否登录
|
||||
self.client.login(username="test", password="test")
|
||||
data = {"title": "test1"}
|
||||
response = self.client.post(self.url, data=data)
|
||||
self.assertEqual(response.data["code"], 1)
|
||||
|
||||
def test_success_announcement(self):
|
||||
self.client.login(username="test", password="test")
|
||||
data = {"title": "title0", "content": "content0"}
|
||||
response = self.client.post(self.url, data=data)
|
||||
self.assertEqual(response.data, {"code": 0, "data": u"公告发布成功!"})
|
@ -4,20 +4,19 @@ from rest_framework.views import APIView
|
||||
from utils.shortcuts import serializer_invalid_response, error_response, success_response
|
||||
|
||||
from account.models import User
|
||||
|
||||
from utils.shortcuts import paginate
|
||||
from .models import Announcement
|
||||
from .serializers import AnnouncementSerializer
|
||||
from .serializers import CreateAnnouncementSerializer, AnnouncementSerializer
|
||||
|
||||
|
||||
class AnnouncementAPIView(APIView):
|
||||
# todo 判断用户是否需要登录
|
||||
class AnnouncementAdminAPIView(APIView):
|
||||
def post(self, request):
|
||||
"""
|
||||
公告发布json api接口
|
||||
---
|
||||
request_serializer: AnnouncementSerializer
|
||||
request_serializer: CreateAnnouncementSerializer
|
||||
"""
|
||||
serializer = AnnouncementSerializer(data=request.DATA)
|
||||
serializer = CreateAnnouncementSerializer(data=request.DATA)
|
||||
if serializer.is_valid():
|
||||
data = serializer.data
|
||||
Announcement.objects.create(title=data["title"],
|
||||
@ -25,4 +24,15 @@ class AnnouncementAPIView(APIView):
|
||||
created_by=request.user)
|
||||
return success_response(u"公告发布成功!")
|
||||
else:
|
||||
return serializer_invalid_response(serializer)
|
||||
return serializer_invalid_response(serializer)
|
||||
|
||||
|
||||
class AnnouncementAPIView(APIView):
|
||||
def get(self, request):
|
||||
"""
|
||||
公告分页json api接口
|
||||
---
|
||||
request_serializer: AnnouncementSerializer
|
||||
"""
|
||||
announcement = Announcement.objects.all().order_by("last_update_time")
|
||||
return paginate(request, announcement, AnnouncementSerializer)
|
||||
|
@ -5,7 +5,7 @@ from django.views.generic import TemplateView
|
||||
|
||||
from account.views import UserLoginAPIView, UsernameCheckAPIView, UserRegisterAPIView, UserChangePasswordAPIView, \
|
||||
EmailCheckAPIView
|
||||
from announcement.views import AnnouncementAPIView
|
||||
from announcement.views import AnnouncementAPIView, AnnouncementAdminAPIView
|
||||
from admin.views import AdminTemplateView
|
||||
|
||||
urlpatterns = [
|
||||
@ -20,9 +20,11 @@ urlpatterns = [
|
||||
url(r'^api/change_password/$', UserChangePasswordAPIView.as_view(), name="user_change_password_api"),
|
||||
url(r'^api/username_check/$', UsernameCheckAPIView.as_view(), name="username_check_api"),
|
||||
url(r'^api/email_check/$', EmailCheckAPIView.as_view(), name="email_check_api"),
|
||||
url(r'^api/admin/announcement/$', AnnouncementAPIView.as_view(), name="announcement_api"),
|
||||
url(r'^api/admin/announcement/$', AnnouncementAdminAPIView.as_view(), name="announcement_admin_api"),
|
||||
url(r'^problem/(?P<problem_id>\d+)/$', "problem.views.problem_page", name="problem_page"),
|
||||
|
||||
url(r'^announcements/$', AnnouncementAPIView.as_view()),
|
||||
|
||||
url(r'^admin/contest/$', TemplateView.as_view(template_name="admin/contest/add_contest.html"), name="add_contest_page"),
|
||||
url(r'^problems/$', TemplateView.as_view(template_name="oj/problem/problem_list.html"), name="problem_list_page"),
|
||||
url(r'^admin/template/(?P<template_dir>\w+)/(?P<template_name>\w+).html', AdminTemplateView.as_view(), name="admin_template")
|
||||
|
Loading…
Reference in New Issue
Block a user