Accept Merge Request #37 : (hohoTT-dev -> dev)

Merge Request: 增加了公告发布成功的测试,以及公告分页的APIview
Created By: @hohoTT
Accepted By: @hohoTT
URL: https://coding.net/u/virusdefender/p/qduoj/git/merge/37
This commit is contained in:
hohoTT 2015-08-06 16:30:17 +08:00
commit bc874883c7
7 changed files with 82 additions and 17 deletions

View File

@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('account', '0003_user_email'),
]
operations = [
migrations.RemoveField(
model_name='user',
name='admin_group',
),
migrations.AddField(
model_name='user',
name='admin_type',
field=models.IntegerField(default=0),
),
]

View File

@ -17,8 +17,9 @@ class Migration(migrations.Migration):
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('title', models.CharField(max_length=50)),
('description', models.TextField()),
('content', models.TextField()),
('create_time', models.DateTimeField(auto_now_add=True)),
('last_update_time', models.DateTimeField(auto_now=True)),
('visible', models.BooleanField(default=True)),
('created_by', models.ForeignKey(to=settings.AUTH_USER_MODEL)),
],

View File

@ -7,12 +7,14 @@ from account.models import User
class Announcement(models.Model):
# 标题
title = models.CharField(max_length=50)
# 公告的描述 HTML 格式
description = models.TextField()
# 公告的内容 HTML 格式
content = models.TextField()
# 创建时间
create_time = models.DateTimeField(auto_now_add=True)
# 这个公告是谁创建的
created_by = models.ForeignKey(User)
# 最后更新时间
last_update_time = models.DateTimeField(auto_now=True)
# 是否可见 false的话相当于删除
visible = models.BooleanField(default=True)

View File

@ -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)
description = serializers.CharField(max_length=10000)
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

View File

@ -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"公告发布成功!"})

View File

@ -4,25 +4,35 @@ 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"],
description=data["description"],
content=data["content"],
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)

View File

@ -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")