增加了公告的API接口和相应的API测试
This commit is contained in:
0
announcement/__init__.py
Normal file
0
announcement/__init__.py
Normal file
3
announcement/admin.py
Normal file
3
announcement/admin.py
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
from django.contrib import admin
|
||||||
|
|
||||||
|
# Register your models here.
|
||||||
0
announcement/migrations/__init__.py
Normal file
0
announcement/migrations/__init__.py
Normal file
20
announcement/models.py
Normal file
20
announcement/models.py
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
# coding=utf-8
|
||||||
|
from django.db import models
|
||||||
|
|
||||||
|
from account.models import User
|
||||||
|
|
||||||
|
|
||||||
|
class Announcement(models.Model):
|
||||||
|
# 标题
|
||||||
|
title = models.CharField(max_length=50)
|
||||||
|
# 公告的描述 HTML 格式
|
||||||
|
description = models.TextField()
|
||||||
|
# 创建时间
|
||||||
|
create_time = models.DateTimeField(auto_now_add=True)
|
||||||
|
# 这个公告是谁创建的
|
||||||
|
created_by = models.ForeignKey(User)
|
||||||
|
# 是否可见 false的话相当于删除
|
||||||
|
visible = models.BooleanField(default=True)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
db_table = "announcement"
|
||||||
8
announcement/serializers.py
Normal file
8
announcement/serializers.py
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
# coding=utf-8
|
||||||
|
from rest_framework import serializers
|
||||||
|
|
||||||
|
|
||||||
|
class AnnouncementSerializer(serializers.Serializer):
|
||||||
|
title = serializers.CharField(max_length=50)
|
||||||
|
description = serializers.CharField(max_length=10000)
|
||||||
|
|
||||||
16
announcement/tests.py
Normal file
16
announcement/tests.py
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
# coding=utf-8
|
||||||
|
from django.core.urlresolvers import reverse
|
||||||
|
|
||||||
|
from rest_framework.test import APITestCase, APIClient
|
||||||
|
|
||||||
|
|
||||||
|
class AbstractAnnouncementAPITest(APITestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.client = APIClient()
|
||||||
|
self.url = reverse("announcement_api")
|
||||||
|
|
||||||
|
def test_invalid_format(self):
|
||||||
|
# todo 判断用户是否登录
|
||||||
|
data = {"title": "test1"}
|
||||||
|
response = self.client.post(self.url, data=data)
|
||||||
|
self.assertEqual(response.data["code"], 1)
|
||||||
28
announcement/views.py
Normal file
28
announcement/views.py
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
# coding=utf-8
|
||||||
|
from rest_framework.views import APIView
|
||||||
|
|
||||||
|
from utils.shortcuts import serializer_invalid_response, error_response, success_response
|
||||||
|
|
||||||
|
from account.models import User
|
||||||
|
|
||||||
|
from .models import Announcement
|
||||||
|
from .serializers import AnnouncementSerializer
|
||||||
|
|
||||||
|
|
||||||
|
class AnnouncementAPIView(APIView):
|
||||||
|
# todo 判断用户是否需要登录
|
||||||
|
def post(self, request):
|
||||||
|
"""
|
||||||
|
公告发布json api接口
|
||||||
|
---
|
||||||
|
request_serializer: AnnouncementSerializer
|
||||||
|
"""
|
||||||
|
serializer = AnnouncementSerializer(data=request.DATA)
|
||||||
|
if serializer.is_valid():
|
||||||
|
data = serializer.data
|
||||||
|
Announcement.objects.create(title=data["title"],
|
||||||
|
description=data["description"],
|
||||||
|
created_by=request.user)
|
||||||
|
return success_response(u"公告发布成功!")
|
||||||
|
else:
|
||||||
|
return serializer_invalid_response(serializer)
|
||||||
@@ -4,6 +4,7 @@ from django.contrib import admin
|
|||||||
from django.views.generic import TemplateView
|
from django.views.generic import TemplateView
|
||||||
|
|
||||||
from account.views import UserLoginAPIView, UsernameCheckAPIView, UserRegisterAPIView, UserChangePasswordAPIView
|
from account.views import UserLoginAPIView, UsernameCheckAPIView, UserRegisterAPIView, UserChangePasswordAPIView
|
||||||
|
from announcement.views import AnnouncementAPIView
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url("^$", TemplateView.as_view(template_name="oj/index.html"), name="index_page"),
|
url("^$", TemplateView.as_view(template_name="oj/index.html"), name="index_page"),
|
||||||
@@ -14,6 +15,7 @@ urlpatterns = [
|
|||||||
url(r'^api/register/$', UserRegisterAPIView.as_view(), name="user_register_api"),
|
url(r'^api/register/$', UserRegisterAPIView.as_view(), name="user_register_api"),
|
||||||
url(r'^api/change_password/$', UserChangePasswordAPIView.as_view(), name="user_change_password_api"),
|
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/username_check/$', UsernameCheckAPIView.as_view(), name="username_check_api"),
|
||||||
|
url(r'^api/admin/announcement/$', AnnouncementAPIView.as_view(), name="announcement_api"),
|
||||||
url(r'^problem/(?P<problem_id>\d+)/$', "problem.views.problem_page", name="problem_page"),
|
url(r'^problem/(?P<problem_id>\d+)/$', "problem.views.problem_page", name="problem_page"),
|
||||||
|
|
||||||
url(r'^admin/contest/$', TemplateView.as_view(template_name="admin/contest/add_contest.html"), name="add_contest_page"),
|
url(r'^admin/contest/$', TemplateView.as_view(template_name="admin/contest/add_contest.html"), name="add_contest_page"),
|
||||||
|
|||||||
Reference in New Issue
Block a user