def videos_create(): request_body = request.get_json() if invalid_video_data(request_body): return {"message": "Invalid Request"}, 400 title = request_body.get("title") release_date = request_body.get("release_date") total_inventory = request_body.get("total_inventory") available_inventory = total_inventory video = Video(title=title, release_date=release_date, total_inventory=total_inventory, available_inventory=available_inventory) video.save() return { "id": video.id, }, 201
def post(self, req): # 获取信息 videoName = req.POST.get('videoName') image = req.POST.get('image') videoType = req.POST.get('videoType') videoSource = req.POST.get('videoSource') nation = req.POST.get('nation') desc = req.POST.get('desc') userID = req.session.get(SESSION_NAME) exists = User.objects.filter(pk=userID).exists() if not exists: return redirect('{}?error={}'.format(reverse('external_video'), '用户不存在')) user = User.objects.filter(pk=userID)[0] # 如果有该填写的区域为空 if not all([videoName, image, videoType, videoSource, nation, desc]): return redirect('{}?error={}'.format(reverse('external_video'), '请确保所有内容均被正确填写')) # 如果枚举类型错误 if not checkEnum([VideoType, VideoSource, Nation], [videoType, videoSource, nation]): return redirect('{}?error={}'.format(reverse('external_video'), '种类、国家或视频源选择错误')) # 都无问题后存入DB video = Video( user=user, videoName=videoName, image=image, videoType=videoType, source=videoSource, nation=nation, desc=desc ) video.save() return redirect(reverse('external_video'))