广告小程序后端开发(8.发布免费广告或店铺,以及上传图片)

1.在apps目录下新建目录utils,在utils目录下新建auth.py文件

from users.models import UserProfile
from rest_framework import exceptions


class Authtication(object):
    def authenticate(self,request):
        # 验证是否已经登录,函数名必须为:authenticate
        token = request._request.POST.get(token)
        # print(token)
        token_obj=UserProfile.objects.filter(token=token).first()
        if not token_obj:
            raise exceptions.AuthenticationFailed(用户认证失败。)
        #在rest_framework内部会将以下两个元素赋值到request,以供后续使用
        return (token_obj.nickName,token_obj)
    def authenticate_header(self,request):
        #这个函数可以没内容,但是必须要有
        pass

2.修改用户表内的UserProfile内的用户记录中的token字段,因为新建的超级用户的token字段是null,如果不加入一些token数据,一旦前端没有发送token数据,就会被误认为是超级用户。以后新建的员工用户,也是要加入一些token的初始数据的。

广告小程序后端开发(8.发布免费广告或店铺,以及上传图片)

3.在apps/ad/views.py中新建创建广告或店铺的类CreateAdOrShop,以及上传图片类UpLoadImg

from django.shortcuts import render,HttpResponse
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.renderers import JSONRenderer,BrowsableAPIRenderer
from .serializers import CategoryModelSerializer,GoodModelSerializer,PrizeModelSerializer,ImagesModelSerializer
from .models import Category,Good,Images,Prize
from utils.auth import Authtication
from users.models import UserProfile,Area
# Create your views here.

#……

class CreateAdOrShop(APIView):
    """
    创建ad或者shop
    """
    renderer_classes = [JSONRenderer]  # 渲染器
    authentication_classes = [Authtication,]

    def post(self,request):
        # print(‘微信名:‘,request.user)
        token=request.POST.get(token)
        tabcur=request.POST.get(tabcur)
        tabcur = int(tabcur) + 1
        title=request.POST.get(title)
        desc=request.POST.get(desc)
        mobile=request.POST.get(mobile)
        address=request.POST.get(address)
        c1=request.POST.get(c1)
        c2=request.POST.get(c2)
        area3=request.POST.get(area)
        # print(token,tabcur,title,desc,mobile,address,c1,c2,area3)
        if token and tabcur and title and desc and mobile and address and c1 and c2 and area3:
            user=UserProfile.objects.filter(token=token).first()
            cat1=Category.objects.filter(name=c1).first()
            cat2=Category.objects.filter(name=c2,parent_category=cat1).first()
            area=Area.objects.filter(name=area3).first()

            new_good=Good()
            new_good.title=title
            new_good.desc=desc
            new_good.mobile=mobile
            new_good.address=address
            new_good.user=user
            new_good.category=cat2
            new_good.area=area
            new_good.type=tabcur
            new_good.save()

            re=GoodModelSerializer(new_good)
            return Response(re.data)
        else:
            return HttpResponse(404)


class UpLoadImg(APIView):
    """
    上传图片
    """
    renderer_classes = [JSONRenderer]  # 渲染器
    authentication_classes = [Authtication, ]

    def post(self, request):
        id=int(request.POST.get(id))
        good=Good.objects.filter(id=id).first()
        new_img=Images()
        new_img.image=request.FILES.get(file)
        new_img.good=good
        new_img.save()

        return HttpResponse(200)

4.在apps/ad/urls.py中配置路由:

from django.urls import path
from .views import GetCategory,CreateAdOrShop,UpLoadImg

urlpatterns = [
    path(getcategory/,GetCategory.as_view()),
    path(createadorshop/,CreateAdOrShop.as_view()),
    path(uploadimg/,UpLoadImg.as_view())
]

 

广告小程序后端开发(8.发布免费广告或店铺,以及上传图片)

上一篇:PHP和js判断访问设备是否是微信浏览器实例


下一篇:[转]Java 动态代理机制分析及扩展