Ejemplo n.º 1
0
def p_list(request):
    ## Récup des données ##
    p_list = Product.objects.all().order_by("ProductName")
    pt_list = ProductType.objects.all().order_by("Type")
    pst_list = ProductSubType.objects.all().order_by("SubType")
    pb_list = ProductBrand.objects.all().order_by("Name")
    #######################

    ## Search filter ##
    q = QueryDict(request.GET.get("q"))
    if q.__contains__("q_categorie"):
        q_categorie = q.getlist("q_categorie")
        p_list = Product.objects.filter(
            reduce(lambda x, y: x | y,
                   [Q(ProductType__Type=item) for item in q_categorie]))
    if q.__contains__("q_subtypeproduct"):
        q_subtypeproduct = q.getlist("q_subtypeproduct")
        p_list = p_list.filter(
            reduce(
                lambda x, y: x | y,
                [Q(ProductSubType__SubType=item)
                 for item in q_subtypeproduct]))
    if q.__contains__("q_brand"):
        q_brand = q.getlist("q_brand")
        p_list = p_list.filter(
            reduce(lambda x, y: x | y,
                   [Q(Brand__Name=item) for item in q_brand]))
    ###################
    ## Ajout du tri ##
    p_list.order_by("ProductName")
    # à développer avec tri dynamique
    ##################

    ## Gestion de la pagination ##
    paginator = Paginator(p_list, 12)
    page = request.GET.get('page')
    try:
        products = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        products = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        products = paginator.page(paginator.num_pages)
    ##############################
    return render(
        request, 'backoffice/p_list.html', {
            'products': products,
            'ptypes': pt_list,
            'psubtypes': pst_list,
            'pbrands': pb_list,
            'cart': Cart(request)
        })
    def put(self, request):
        """
        НУЖЕН ТОКЕН
        Здесь можно обновить любые данные, самое главное в теле запроса иметь id,
        а остальноное можно компоновать как захочется, то есть {id:1,location:'Livia'} или {id:1,vk:'token'}
        """
        req = QueryDict('', mutable=True)
        req.update(request.data)

        if req.__contains__('location'):
            loc = Location.objects.filter(
                title=request.data['location']).first()

            if loc:
                req.update({'location': LocationSerializer(loc).data['id']})
            else:
                locactionSerial = LocationSerializer(
                    data={'title': req.get('location')})
                if locactionSerial.is_valid():
                    locactionSerial.save()
                    req.update({'location': locactionSerial.data['id']})

        data = MoreDataSerializer(UserData.objects.get(
            pk=request.data['id']).id, data=req, partial=True)
        status = ''
        if data.is_valid():
            data.save()
            status = {'status': 'success'}
        else:
            status = {'status': 'Ошибка при введении данных'}
        return Response(data=status)