예제 #1
0
    def get(self, request):
        public_key = get_header(self.request)
        token = get_header(self.request, 'MARKET-TOKEN')

        logger.info("public_key:%s,token:%s", public_key, token)
        # FIXME query current user following tags,query top 10 products with tags
        queryset = Product.objects.filter(status=0)[0:10]
        serializer = YouMayLikeProductSerializer(queryset, many=True)
        products = serializer.data

        # fill username and rating for each product,return to client
        product_list = []
        for p in products:
            pk = p['owner_address']
            u = WalletUser.objects.get(public_key=pk)

            p['username'] = '' if not u else u.username
            # query average rating from SummaryComment table
            comment, _ = SummaryComment.objects.get_or_create(
                market_hash=p['msg_hash'])
            sale_status, _ = ProductSaleStatus.objects.get_or_create(
                market_hash=p['msg_hash'])
            p['avg_rating'] = 1 if not comment else comment.avg_rating
            p['sales_number'] = 0 if not sale_status else sale_status.sales_number
            product_list.append(p)
        return create_success_data_response(product_list)
예제 #2
0
 def check_permission(self, request):
     public_key = get_header(request)
     token = get_header(request, HTTP_MARKET_TOKEN)
     logger.debug('public_key:%s,token:%s' % (public_key, token))
     if public_key is None:
         return False
     return Token.objects.filter(public_key__exact=public_key).filter(
         key__exact=token).exists()
예제 #3
0
 def post(self, request):
     public_key = get_header(self.request)
     tag = request.data['tag']
     logger.debug("delete tag:%s for public_key:%s" % (tag, public_key))
     MyTag.objects.filter(public_key=public_key,
                          tag=request.data['tag']).delete()
     return create_success_response()
예제 #4
0
 def post(self, request):
     public_key = get_header(self.request)
     seller_public_key = request.data['seller_public_key']
     logger.debug("delete seller_public_key:%s for public_key:%s" %
                  (seller_public_key, public_key))
     MySeller.objects.filter(public_key=public_key,
                             seller_public_key=seller_public_key).delete()
     return create_success_response()
예제 #5
0
 def get(self, request):
     public_key = get_header(self.request)
     queryset = MyTag.objects.filter(public_key=public_key).order_by('-id')
     page_set = PageNumberPagination().paginate_queryset(queryset=queryset,
                                                         request=request,
                                                         view=self)
     serializer = MyTagSerializer(page_set, many=True)
     return create_success_data_response(serializer.data)
예제 #6
0
    def post(self, request):
        public_key = get_header(self.request)
        tag = request.data['tag']
        logger.debug("public_key:%s tag:%s" % (public_key, tag))

        obj, _ = MyTag.objects.get_or_create(public_key=public_key,
                                             tag=request.data['tag'])
        return create_success_response()
예제 #7
0
    def post(self, request):
        public_key = get_header(self.request)
        seller_public_key = request.data['seller_public_key']
        logger.debug("public_key:%s seller_public_key:%s" %
                     (public_key, seller_public_key))

        obj, _ = MySeller.objects.get_or_create(
            public_key=public_key, seller_public_key=seller_public_key)
        return create_success_response()
예제 #8
0
 def my_products(self, request):
     pk = get_header(request)
     if not pk:
         return Response(data=[])
     queryset = Product.objects.filter(owner_address=pk)
     queryset = queryset.order_by('-created')
     serializer = ProductSerializer(queryset, many=True)
     data = serializer.data
     return Response(data=data)
예제 #9
0
    def post(self, request):
        public_key = get_header(self.request)
        logger.info("public_key:%s" % public_key)
        data = request.data
        try:
            msg_seq = WalletMsgSequence.objects.get(public_key=public_key)
            msg_seq.seq = msg_seq.seq + 1
        except WalletMsgSequence.DoesNotExist:
            msg_seq = WalletMsgSequence(
                seq=0,
                public_key=public_key,
                user=WalletUser.objects.get(public_key=public_key))
        now = timezone.now()
        product = Product(data)
        product.seq = msg_seq.seq
        product.owner_address = data['owner_address']
        product.title = data['title']
        product.ptype = data['ptype']
        product.description = data['description']
        product.price = data['price']
        product.created = now
        product.start_date = data['start_date']
        product.end_date = data['end_date']
        product.signature = data['signature']
        product.owner_address = data['owner_address']

        signature_source = product.get_signature_source()
        is_valid_sign = is_valid_signature(public_key, signature_source,
                                           product.signature)
        if not is_valid_sign:
            logger.error("invalid_signature")
            return create_invalid_response()
        # generate msg hash
        try:
            market_hash_source = product.get_msg_hash_source()
            product.msg_hash = generate_market_hash(market_hash_source)
            data._mutable = True
            data['msg_hash'] = product.msg_hash
            data['seq'] = msg_seq.seq
            request.FIELS['cover_image']
            # data['cover_image'] = request.data['cover_image']
        except Exception as e:
            logger.exception()
            raise e
        serializer = ProductSerializer(data=data)
        try:
            if serializer.is_valid(raise_exception=True):
                msg_seq.save()
                user = WalletUser.objects.get(public_key=public_key)
                serializer.save(owner=user)
                return create_success_data_response(
                    {'market_hash': product.msg_hash})
        except Exception as e:
            logger.exception()
            raise e
예제 #10
0
    def get(self, request):
        public_key = get_header(self.request)
        seller_list = MySeller.objects.filter(public_key=public_key)
        keyword = ','.join(x.seller_public_key for x in seller_list)
        logger.debug('keyword:%s' % keyword)

        queryset = Product.objects.filter(status=0).filter(
            Q(owner_address=keyword))
        page_set = PageNumberPagination().paginate_queryset(queryset=queryset,
                                                            request=request,
                                                            view=self)
        serializer = ProductSerializer(page_set, many=True)
        return create_success_data_response(serializer.data)
예제 #11
0
    def get(self, request):
        public_key = get_header(self.request)
        logger.info("public_key:%s" % public_key)
        params = request.query_params
        keyword = params.get('keyword')
        if keyword is not None and len(keyword) != 0:
            logger.debug("keyword is %s" % keyword)
            queryset = Product.objects.filter(owner_address=public_key).filter(
                Q(title__contains=keyword) | Q(description__contains=keyword)
                | Q(tags__contains=keyword))
        else:
            queryset = Product.objects.filter(owner_address=public_key)

        serializer = ProductSerializer(queryset, many=True)
        return Response(data=serializer.data)
예제 #12
0
    def get(self, request):
        public_key = get_header(self.request)
        tag_list = MyTag.objects.filter(public_key=public_key).order_by('-id')
        logger.debug('taglist:%s' % tag_list)
        keyword = ','.join(x.tag for x in tag_list)

        logger.debug("keyword is %s" % keyword)

        queryset = Product.objects.filter(status=0).filter(
            Q(tags__contains=keyword))

        page_set = PageNumberPagination().paginate_queryset(queryset=queryset,
                                                            request=request,
                                                            view=self)
        serializer = ProductSerializer(page_set, many=True)
        return create_success_data_response(serializer.data)
예제 #13
0
    def increase_product_sales_quantity(self, request):
        public_key = get_header(self.request)
        market_hash = request.data['market_hash']
        logger.debug("public_key:%s market_hash:%s" %
                     (public_key, market_hash))

        obj, created = SalesQuantity.objects.get_or_create(
            market_hash=request.data['market_hash'])
        if not created:
            serializer = ProductSalesQuantitySerializer(obj, data=request.data)
            if not serializer.is_valid(raise_exception=True):
                return create_invalid_response()

            logger.debug("update product number")
            obj = serializer.update(obj, request.data)

        logger.debug("obj.quantity:%s" % obj.quantity)
        return create_success_data_response(obj.quantity)
예제 #14
0
    def update_product_status(self, request, status):
        public_key = get_header(self.request)
        logger.debug("public_key:%s status:%s" % (public_key, status))

        try:
            product = Product.objects.get(owner_address=public_key,
                                          msg_hash=request.data['market_hash'])
        except Product.DoesNotExist:
            return create_invalid_response()
        data = request.data
        data['status'] = status
        serializer = ProductUpdateSerializer(product, data=data)
        if not serializer.is_valid(raise_exception=True):
            return create_invalid_response()
        logger.debug("update product status")
        serializer.update(product, data)
        response = create_success_response()
        return response
예제 #15
0
 def perform_create(self, serializer):
     public_key = get_header(self.request)
     logger.debug("create product for public_key:%s" % public_key)
     serializer.save(owner=WalletUser.objects.get(public_key=public_key))