def get(self, request): try: # Check for shop shop = request.GET.get('shop') check_shop(shop) model = Newsletter.objects.filter(shop=shop).order_by('-created') objs, pagination_data = pagination( model=model, page=request.GET.get('page') or 1, admin=request.GET.get('admin') or True ) return custom_response( message="emails fetched", status=status.HTTP_200_OK, data=NewsletterSerializer(objs, many=True).data, pagination=pagination_data, filters={} ) except Exception as error: return custom_response( message=str(error), status=status.HTTP_500_INTERNAL_SERVER_ERROR, )
def create(self, **kwargs): if 'shop' not in kwargs or not kwargs['shop']: raise ValueError('Shop is required') check_shop(kwargs['shop']) obj = self.model(**kwargs) obj.save() return obj
def get(self, request): try: shop = request.GET.get('shop') check_shop(shop) categories = Category.objects.get_all(shop=shop) return custom_response(message='Categories fetched', data=CategorySerializer(categories, many=True).data, status=status.HTTP_200_OK) except Exception as e: return custom_response( message=str(e), status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def get(self, request, **kwargs): try: active = request.GET.get('active') # Check for shop shop = request.GET.get('shop') check_shop(shop) if active: obj = Product.objects.get(pk=kwargs['pk'], active=bool(active), shop=shop) else: obj = Product.objects.get(pk=kwargs['pk'], shop=shop) return custom_response(message='Product fetched', data=ProductSerializer(obj).data, status=status.HTTP_200_OK) except Exception as error: return custom_response(message=str(error), status=status.HTTP_400_BAD_REQUEST)
def post(self, request): shop = request.GET.get('shop') try: obj = check_shop(shop) return custom_response( status=status.HTTP_200_OK, message='Store OK', data=obj ) except Exception as e: return custom_response( status=status.HTTP_400_BAD_REQUEST, message=str(e) )
def get(self, request): try: sort_by_options = [ { 'id': 1, 'name': 'Latest' }, { 'id': 2, 'name': 'Price Ascending' }, { 'id': 3, 'name': 'Price Descending' }, ] data = {} active = request.GET.get('active') category = request.GET.get('category') sort_by = request.GET.get('sort_by') # Check for shop shop = request.GET.get('shop') override_limit = int( request.GET.get('limit')) if request.GET.get('limit') else 0 check_shop(shop) data['shop'] = shop if active: data['active'] = bool(active) if category: data['category__category__pk'] = int(category) order_by = '-modified' if sort_by: if int(sort_by) == 1: order_by = '-modified' if int(sort_by) == 2: order_by = 'price' if int(sort_by) == 3: order_by = '-price' model = Product.objects.filter(**data).order_by(order_by) objs, pagination_data = pagination(model=model, page=request.GET.get('page') or 1, admin=request.GET.get('admin') or False, override_limit=override_limit) categories_options = Category.objects.filter( shop=shop).order_by('name') return custom_response( message="products fetched", status=status.HTTP_200_OK, data=ProductSerializer(objs, many=True).data, sort_by_options=sort_by_options, categories_options=CategorySerializer(categories_options, many=True).data, pagination=pagination_data, filters={ "category": category, "sort_by": sort_by }) except Exception as error: return custom_response( message=str(error), status=status.HTTP_500_INTERNAL_SERVER_ERROR, )