def add_banner(request): form = AddBannerForm(request.POST) if form.is_valid(): priority = form.cleaned_data.get('priority') image_url = form.cleaned_data.get('image_url') link_to = form.cleaned_data.get('link_to') banner = Banner.objects.create(priority=priority, image_url=image_url, link_to=link_to) return Response.response(data={'banner_id': banner.pk}) else: return Response.response(message=form.get_errors())
def edit_banner(request): form = EditBannerForm(request.POST) if form.is_valid(): pk = form.cleaned_data.get('pk') priority = form.cleaned_data.get('priority') image_url = form.cleaned_data.get('image_url') link_to = form.cleaned_data.get('link_to') Banner.objects.filter(pk=pk).update(priority=priority, image_url=image_url, link_to=link_to) return Response.response() else: return Response.response(message=form.get_errors())
def qntoken(request): ak = settings.QINIU_ACCESS_KEY sk = settings.QINIU_SECRET_KEY bucket = settings.QINIU_BUCKET_NAME q = qiniu.Auth(ak, sk) token = q.upload_token(bucket) return Response.response(data={'token': token})
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 upload_file(request): file = request.FILES.get('file') name = file.name with open(os.path.join(settings.MEDIA_ROOT, name), 'wb') as fp: for chunk in file.chunks(): fp.write(chunk) url = request.build_absolute_uri(settings.MEDIA_URL + name) return Response.response(data={'url': url})
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 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 course_order_key(request): goodsname = request.POST.get("goodsname") istype = request.POST.get("istype") notify_url = request.POST.get("notify_url") orderid = request.POST.get("orderid") price = request.POST.get("price") return_url = request.POST.get("return_url") token = 'e6110f92abcb11040ba153967847b7a6' uid = '49dc532695baa99e16e01bc0' orderuid = str(request.user.pk) key = md5((goodsname + istype + notify_url + orderid + orderuid + price + return_url + token + uid).encode("utf-8")).hexdigest() return Response.response(data={"key": key})
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 new_list(request): page = int(request.GET.get('p', 1)) category_id = int(request.GET.get('category_id', 0)) start = (page - 1) * settings.ONE_PAGE_NEWS_COUNT end = start + settings.ONE_PAGE_NEWS_COUNT if category_id == 0: news = News.objects.select_related('category', 'author').all()[start:end] else: news = News.objects.select_related( 'category', 'author').filter(category__id=category_id)[start:end] serializer = NewsSerializers(news, many=True) data = serializer.data return Response.response(data=data)
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 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 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})
def sms_captcha(request): telephone = request.GET.get('telephone') code = ''.join([str(random.randint(0, 9)) for i in range(0, 4)]) cache.set(telephone, code, 5 * 60) print(f"{telephone} {code}") return Response.response()
def notify_view(request): orderid = request.POST.get('orderid') PayInfoOrder.objects.filter(pk=orderid).update(status=2) return Response.response()
def banner_list(request): banners = Banner.objects.all() serialize = BannerSerializer(banners, many=True) return Response.response(data=serialize.data)
def delete_banner(request): banner_id = request.POST.get('banner_id') Banner.objects.filter(pk=banner_id).delete() return Response.response()
def delete_news(request): news_id = request.POST.get('news_id') News.objects.filter(pk=news_id).delete() return Response.response()