diff --git a/announcement/serializers.py b/announcement/serializers.py index 3878e142..a195b2ea 100644 --- a/announcement/serializers.py +++ b/announcement/serializers.py @@ -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 + diff --git a/announcement/tests.py b/announcement/tests.py index f73805e9..f337a9c7 100644 --- a/announcement/tests.py +++ b/announcement/tests.py @@ -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"公告发布成功!"}) \ No newline at end of file diff --git a/announcement/views.py b/announcement/views.py index e3766e86..b01af7f0 100644 --- a/announcement/views.py +++ b/announcement/views.py @@ -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) \ No newline at end of file + 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) diff --git a/oj/urls.py b/oj/urls.py index 82ecafb2..2586cb62 100644 --- a/oj/urls.py +++ b/oj/urls.py @@ -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\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\w+)/(?P\w+).html', AdminTemplateView.as_view(), name="admin_template")