enroll app修改

This commit is contained in:
ygm1881
2022-04-21 21:38:06 +08:00
parent 1c8a148c84
commit 16b2e3cf8e
28 changed files with 336 additions and 11 deletions
View File
+5
View File
@@ -0,0 +1,5 @@
from django.contrib import admin
from .models import Comments
# Register your models here.
admin.site.register(Comments)
+6
View File
@@ -0,0 +1,6 @@
from django.apps import AppConfig
class CommentsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'comments'
+22
View File
@@ -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='弹幕内容')),
],
),
]
View File
+11
View File
@@ -0,0 +1,11 @@
from django.db import models
# Create your models here.
class Comments(models.Model):
post_time = models.DateTimeField(verbose_name="发布时间")
content = models.CharField(verbose_name="弹幕内容", max_length=50, blank=False)
+25
View File
@@ -0,0 +1,25 @@
from rest_framework import serializers
from .models import *
class CommentsInfo(serializers.ModelSerializer):
class Meta:
model = Comments
fields = ['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('非法字符')
if len(value) > 50:
raise serializers.ValidationError("弹幕过长")
elif len(value) == 0:
raise serializers.ValidationError("输入不能为空")
return value
+3
View File
@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.
+9
View File
@@ -0,0 +1,9 @@
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('comment/', views.comments.as_view()),
]
+45
View File
@@ -0,0 +1,45 @@
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
# 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'] = 'error'
data['code'] = "40000"
else:
data['msg'] = "success"
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'] = "40000"
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)