def edit_news_category(request): form = EditNewsCategoryForm(request.POST) if form.is_valid(): pk = form.cleaned_data.get('pk') name = form.cleaned_data.get('name') try: NewCategory.objects.filter(pk=pk).update(name=name) return Response.response() except: return Response.params_error(message="该分类已经存在") else: return Response.params_error(message=form.get_errors())
def delete_news_category(request): pk = request.POST.get('pk') try: NewCategory.objects.filter(pk=pk).delete() return Response.response() except NewCategory.DoesNotExist: return Response.params_error(message="该分类已经存在")
def add_news_category(request): name = request.POST.get('name') exists = NewCategory.objects.filter(name=name).exists() if not exists: NewCategory.objects.create(name=name) return Response.response() else: return Response.params_error(message="该分类已经存在")
def public_comment(request): form = PublicCommentForm(request.POST) if form.is_valid(): news_id = form.cleaned_data.get('news_id') content = form.cleaned_data.get('content') news = News.objects.get(pk=news_id) comment = Comment.objects.create(content=content, news=news, author=request.user) serializer = CommentSerializer(comment) return Response.response(data=serializer.data) else: return Response.params_error(message=form.get_errors())
def post(self, request): form = WriteNewsForm(request.POST) if form.is_valid(): title = form.cleaned_data.get('title') desc = form.cleaned_data.get('desc') thumbnail = form.cleaned_data.get('thumbnail') content = form.cleaned_data.get('content') category_id = form.cleaned_data.get('category') category = NewCategory.objects.get(pk=category_id) News.objects.create(title=title, desc=desc, thumbnail=thumbnail, content=content, category=category, author=request.user) return Response.response() else: return Response.params_error(message=form.get_errors())
def login_view(request): form = LoginForm(request.POST) if form.is_valid(): telephone = form.cleaned_data.get('telephone') password = form.cleaned_data.get('password') remember = form.cleaned_data.get('remember') user = authenticate(request, username=telephone, password=password) if user: if user.is_active: login(request, user) if remember: request.session.set_expiry(None) else: request.session.set_expiry(0) return Response.response() else: return Response.unauth(message="账号被冻结!") else: return Response.params_error(message='手机或者密码错误') else: errors = form.get_errors() return Response.params_error(message=errors)
def register(request): global user form = RegisterForm(request.POST) if form.is_valid(): telephone = form.cleaned_data.get('telephone') username = form.cleaned_data.get('username') password = form.cleaned_data.get('password1') try: user = User.objects.create_user(telephone=telephone, username=username, password=password) except User.DoesNotExist: login(request, user) return Response.response() else: return Response.params_error(message=form.get_errors())
def post(self, request): form = EditNewsForm(request.POST) if form.is_valid(): if form.is_valid(): title = form.cleaned_data.get('title') desc = form.cleaned_data.get('desc') thumbnail = form.cleaned_data.get('thumbnail') content = form.cleaned_data.get('content') category_id = form.cleaned_data.get('category') pk = int(form.cleaned_data.get("pk")) category = NewCategory.objects.get(pk=category_id) News.objects.filter(pk=pk).update(title=title, desc=desc, thumbnail=thumbnail, content=content, category=category) return Response.response() else: return Response.params_error(message=form.get_errors())
def post(self, request): form = PubCourseForm(request.POST) if form.is_valid(): title = form.cleaned_data.get('title') category_id = form.cleaned_data.get('category_id') video_url = form.cleaned_data.get('video_url') cover_url = form.cleaned_data.get("cover_url") price = form.cleaned_data.get('price') duration = form.cleaned_data.get('duration') profile = form.cleaned_data.get('profile') teacher_id = form.cleaned_data.get('teacher_id') category = CourseCategory.objects.get(pk=category_id) teacher = Teacher.objects.get(pk=teacher_id) Course.objects.create(title=title, video_url=video_url, cover_url=cover_url, price=price, duration=duration, profile=profile, category=category, teacher=teacher) return Response.response() else: return Response.params_error(message=form.get_errors())
def course_token(request): # video:是视频文件的完整链接 file = request.GET.get('video') course_id = request.GET.get('course_id') if not CourseOrder.objects.filter( course_id=course_id, buyer=request.user, status=2).exists(): return Response.params_error(message='请先购买课程!') expiration_time = int(time.time()) + 2 * 60 * 60 USER_ID = settings.BAIDU_CLOUD_USER_ID USER_KEY = settings.BAIDU_CLOUD_USER_KEY # file=http://hemvpc6ui1kef2g0dd2.exp.bcevod.com/mda-igjsr8g7z7zqwnav/mda-igjsr8g7z7zqwnav.m3u8 extension = os.path.splitext(file)[1] media_id = file.split('/')[-1].replace(extension, '') # unicode->bytes=unicode.encode('utf-8')bytes key = USER_KEY.encode('utf-8') message = '/{0}/{1}'.format(media_id, expiration_time).encode('utf-8') signature = hmac.new(key, message, digestmod=hashlib.sha256).hexdigest() token = '{0}_{1}_{2}'.format(signature, USER_ID, expiration_time) return Response.response(data={'token': token})