def get_one(self, id=None, slug=None): if not id and not slug: raise InvalidRequestParams('Must pass atleast code or slug!') params = dict(deleted=False) if id: params['id'] = id else: params['slug'] = slug product = Sim.objects(**params).first() if not product: raise InvalidRequestParams('Product not found!') product_output = product.output() print('product_output', product_output) image_id = product.image_id if image_id: image = Image.objects(id=image_id).first() print('image', image) product_output['image'] = image.url return product.output()
def get(self, id=None, slug=None): if not id and not slug: raise InvalidRequestParams('Must pass atleast code or slug!') params = dict( deleted=False ) if id: params['id'] = id else: params['slug'] = slug product = Product.objects(**params).first() if not product: raise InvalidRequestParams('Product not found!') result = product.output() for index, image_id in enumerate(product.images): image = Image.objects(id=image_id).first() result['images'][index] = image.url if product.promotion_package_id: promotion_package = PromotionPackage.objects(id=product.promotion_package_id).first() result['sale_off_value'] = promotion_package.sale_off_value return result
def post(self): file = request.files.get('file') if not file: raise InvalidRequestParams('Missing file!') params = self.parse_request_params() return image_bl.create(file=file, **params)
def parse_request_params(self): req_method = request.method input_schema = getattr(self, '%s_INPUT_SCHEMA' % req_method, None) param_location = getattr(self, '%s_PARAM_LOCATION' % req_method, None) if not param_location: return abort(405) raw_params = getattr(request, param_location) or {} if req_method == 'GET': try: raw_params = json_util.loads(raw_params.get('params') or '{}') except Exception: raise InvalidRequestParams(payload=raw_params) if not input_schema: return raw_params errors = input_schema.validate(raw_params) if errors: raise InvalidRequestParams(message=errors) return input_schema.dump(raw_params).data
def create(self, name, object_type=None): if self._is_name_duplicate(name=name): raise NameAlreadyExists banner = Banner() banner.name = name if object_type: if object_type not in OBJECT_TYPE_MAP.keys(): raise InvalidRequestParams('Invalid banner object_type!') banner.object_type = object_type banner.create() return banner.output()
def get_one(self, id=None, slug=None): if not id and not slug: raise InvalidRequestParams('Must pass atleast id or slug!') if id: post = self._get_record_by_id(model=Post, id=id) else: post = Post.objects(slug=slug).first() if not post: return dict() result = post.output() for index, image_id in enumerate(post.images): image = Image.objects(id=image_id).first() result['images'][index] = image.url return result
def update(self, id, name=None, object_type=None): banner = self._get_record_by_id(id=id, model=Banner) update_params = dict() if name: if self._is_name_duplicate(name=name, id=id): raise NameAlreadyExists update_params['name'] = name if object_type: if object_type not in OBJECT_TYPE_MAP.keys(): raise InvalidRequestParams('Invalid banner object_type!') update_params['object_type'] = object_type if update_params: update_params['updated_by'] = g.user.id banner.patch(update_params=update_params) return dict(success=True)
def update(self, id, name=None, parent_id=None): category = self._get_record_by_id(model=Category, id=id) update_params = dict() if name: if self.is_name_duplicate(name=name, id=id): raise NameAlreadyExists update_params['name'] = name if parent_id: parent = Category.objects(parent_id__exists=False, id=parent_id).first() if not parent: raise InvalidRequestParams('Parent not found!') update_params['parent_id'] = parent_id if update_params: category.patch(update_params=update_params) return dict(success=True)
def update(self, id, name=None, price=None, default_image_id=None, code=None, status=None, sizes=None, promotion_package_id=None): product = self._get_record_by_id(model=Product, id=id) update_params = dict() if name: update_params['name'] = name if price is not None: update_params['price'] = price if sizes is not None: update_params['sizes'] = sizes if status: update_params['status'] = status if code: if self._is_code_duplicate(code=code, id=id): raise DuplicateCode update_params['code'] = code if promotion_package_id: self._get_record_by_id(model=PromotionPackage, id=promotion_package_id) update_params['promotion_package_id'] = promotion_package_id if default_image_id: if default_image_id not in product.images: raise InvalidRequestParams('Default image id not in product images!') update_params['default_image_id'] = default_image_id if update_params: product.patch(update_params=update_params) return dict(success=True)
def download(self, path): if not os.path.exists(path): raise InvalidRequestParams('File not found!', payload=path) return send_file(path, as_attachment=True)