更改表结构
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
from django.contrib import admin
|
||||
from .models import Comments
|
||||
# Register your models here.
|
||||
|
||||
admin.site.register(Comments)
|
||||
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class CommentsConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'apps.comments'
|
||||
@@ -0,0 +1,22 @@
|
||||
# Generated by Django 3.2.5 on 2022-04-15 14:15
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Comments',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('post_time', models.DateTimeField(verbose_name='发布时间')),
|
||||
('content', models.CharField(max_length=50, verbose_name='弹幕内容')),
|
||||
],
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,13 @@
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
|
||||
|
||||
class Comments(models.Model):
|
||||
class Meta:
|
||||
verbose_name_plural=u"弹幕内容"
|
||||
post_time = models.DateTimeField(verbose_name="发布时间")
|
||||
content = models.CharField(verbose_name="弹幕内容", max_length=50, blank=False)
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
from rest_framework import serializers
|
||||
from .models import *
|
||||
from utils.get_error_msg import get_error_msg
|
||||
|
||||
|
||||
class CommentsInfo(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Comments
|
||||
fields = ['id', 'content', 'post_time']
|
||||
|
||||
post_time = serializers.DateTimeField(label="发布时间", required=False)
|
||||
content = serializers.CharField(label="弹幕内容", max_length=50, required=True)
|
||||
|
||||
|
||||
def validate_content(self, value):
|
||||
ban = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')', "_", "-"]
|
||||
for i in ban:
|
||||
if i in value:
|
||||
raise serializers.ValidationError(code='40002', detail={'msg': get_error_msg(40002),
|
||||
"code": '40002'})
|
||||
|
||||
if len(value) > 50:
|
||||
raise serializers.ValidationError(code='40003', detail=get_error_msg(40003))
|
||||
elif len(value) == 0:
|
||||
raise serializers.ValidationError(code='40004', detail=get_error_msg(40004))
|
||||
|
||||
|
||||
return value
|
||||
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
@@ -0,0 +1,8 @@
|
||||
from django.urls import path
|
||||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
path('comment/', views.comments.as_view()),
|
||||
]
|
||||
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
import time, datetime
|
||||
from django.conf import settings
|
||||
import re
|
||||
from django.shortcuts import render
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.response import Response
|
||||
from .models import *
|
||||
from .serializers import CommentsInfo
|
||||
from django.utils import timezone
|
||||
from utils.get_error_msg import get_error_msg
|
||||
# Create your views here.
|
||||
|
||||
|
||||
class comments(APIView):
|
||||
|
||||
def get(self, request):
|
||||
data = {}
|
||||
queryset = Comments.objects.all()
|
||||
serializer = CommentsInfo(queryset, many=True)
|
||||
try:
|
||||
data['data'] = serializer.data
|
||||
except:
|
||||
data['msg'] = serializer.error_messages
|
||||
if len(data['data']) == 0:
|
||||
data['msg'] = get_error_msg(40005)
|
||||
data['code'] = 40005
|
||||
else:
|
||||
data['msg'] = get_error_msg(20000)
|
||||
data['code'] = 20000
|
||||
return Response(data=data)
|
||||
|
||||
|
||||
def post(self, request):
|
||||
data = {}
|
||||
serializer = CommentsInfo(data=request.data)
|
||||
if not serializer.is_valid(raise_exception=True):
|
||||
data['msg'] = serializer.error_messages
|
||||
data['code'] = 50000
|
||||
return Response(data=data)
|
||||
serializer.validated_data['post_time'] = timezone.now().replace(microsecond=0)
|
||||
serializer.save()
|
||||
data['data'] = serializer.validated_data
|
||||
data['msg'] = "success"
|
||||
data['code'] = 20000
|
||||
return Response(data=data)
|
||||
|
||||
Reference in New Issue
Block a user