无限极分类
commentfunc
data = [
{"cat_id": 1, "name": "上海", "parent_id": 0},
{"cat_id": 3, "name": "青浦", "parent_id": 1},
{"cat_、id": 4, "name": "浦东", "parent_id": 1},
{"cat_id": 2, "name": "张江", "parent_id": 4},
{"cat_id": 5, "name": "北京", "parent_id": 0},
{"cat_id": 6, "name": "昌平", "parent_id": 5},
]
def get_tree(data):
lists = []
tree = {}
for i in data:
tree[i["cat_id"]] = i
for item in data:
if not item["parent_id"]:
# lists.append(tree[item["cat_id"]])
lists.append(item)
else:
if "children" not in tree[item["parent_id"]]:
tree[item["parent_id"]]["children"] = []
# tree[item["parent_id"]]["children"].append(tree[item["cat_id"]])
tree[item["parent_id"]]["children"].append(item)
return lists
res = []
def get_son(data, level=0, parent_id=0, is_clear=True):
if is_clear:
res.clear()
#假设传来的分类为1 ,现在找索引分类id为1 的子孙,但是如果不加下面这一行代码,就会遗漏自己本身
if parent_id:
res1.append(parent_id)
for item in data:
if item['parent_id'] == parent_id:
item['level'] = level
res.append(item)
get_son(data, level=level + 1, parent_id=item['cat_id'], is_clear=False)
return res
res1=[]
def get_all_son(data,parent_id,is_clear=True):
#如果第一次进来就清空
if is_clear:
res1.clear()
for item in data:
if item["parent_id"]==parent_id:
res1.append(item["cat_id"])
get_all_son(data,parent_id=item["cat_id"],is_clear=False)
return res1
# 上海
# -青浦
# -浦东
# --张江
# 北京
# -昌平
views
User
from rest_framework.views import APIView
from rest_framework.response import Response
from app01 import models
from app01.wx import WxLogin
import time, hashlib
from django.core.cache import cache
class Login(APIView):
def post(self, request):
param = request.data
if param.get("code"):
data = WxLogin.get_openid(param.get("code"))
if data:
val = data["openid"] + "&" + data["session_key"]
key = str(int(time.time())) + data["session_key"]
md5 = hashlib.md5()
md5.update(key.encode("utf-8"))
key = md5.hexdigest()
cache.set(key, val)
has_user = models.Wxuser.objects.filter(openid=data["openid"]).first()
if not has_user:
models.Wxuser.objects.create(openid=data["openid"])
return Response({"code": 200, "msg": "ok", "data": {"login_key": key}})
else:
return Response({"code": 201, "msg": "无效的code"})
else:
return Response({"code": 202, "msg": "缺少参数"})
Banner
from rest_framework.views import APIView
from rest_framework.response import Response
from app01 import models
from app01.my_ser import Banner_ser
class List(APIView):
def post(self, request):
data = models.Banner.objects.filter(is_show=True).order_by("-w_order")
data = Banner_ser.Banner_ser(instance=data, many=True, context={"request": request}).data
print("list",data)
return Response({
"code": 200,
"msg": "ok",
"data": data
})
Goods
from rest_framework.views import APIView
from rest_framework.response import Response
from app01 import models
from app01.my_ser import Goods_ser, Category_ser
from app01.comment import func
class Hotlist(APIView):
def post(self, request):
data = models.Product.objects.filter(disabled=True).order_by("-buy_count", "-w_order")
data = Goods_ser.Good_ser(instance=data, many=True, context={"request": request}).data
print("hotgoods/list", data)
return Response({"code": 200, "msg": "ok", "data": data})
class List(APIView):
def post(self, request):
param = request.data
if param.get("category_id"):
data = models.Category.objects.filter(is_show=True)
data = Category_ser.Category_ser(instance=data, many=True, context={"request": request}).data
all_id = func.get_all_son(data, param.get("category_id"))
# 查出分类属于all_id的所有值
data = models.Product.objects.filter(disabled=True, product_id__in=all_id).order_by("-w_order")
data = Goods_ser.Good_ser(instance=data, many=True, context={"request": request}).data
print("goods/list",data)
return Response({"code": 200, "msg": "ok", "data": data})
else:
return Response({"code": 201, "msg": "缺少参数"})
Category
from rest_framework.views import APIView
from rest_framework.response import Response
from app01 import models
from app01.my_ser import Category_ser
class List(APIView):
def post(self,request):
data=models.Category.objects.filter(is_show=True)
data=Category_ser.Category_ser(instance=data,many=True,context={"request":request}).data
print("Category/list",data)
return Response({"code":200,"msg":"ok","data":data})
my_ser
Banner_ser
from rest_framework import serializers
from app01 import models
class Banner_ser(serializers.ModelSerializer):
product_id=serializers.IntegerField(source="product.product_id")
image_url=serializers.ImageField(source="image.image_url")
class Meta:
model = models.Banner
fields = "__all__"
Category_ser
from rest_framework import serializers
from app01 import models
class Category_ser(serializers.ModelSerializer):
product_id = serializers.IntegerField(source="product.product_id")
parent_id = serializers.SerializerMethodField()
def get_parent_id(self, obj):
if obj.parent_id is None:
return 0
else:
return obj.parent_id
class Meta:
model = models.Category
fields = "__all__"
Goods_ser
from rest_framework import serializers
from app01 import models
class Good_ser(serializers.ModelSerializer):
image_url = serializers.ImageField(source="image.image_url")
class Meta:
model = models.Product
fields = "__all__"