def validate(self, data): store = get_or_raise(Store, slug=data['store_slug']) product = get_or_raise(Product, shopify_id=data['pk']) if (not product.store == store): raise ValidationError("{} does not belong to {}".format( product.title, store.url)) return data
def _fit_product(product_id, profile_id): """ Whether we have an authenticated profile or not, fitproduct process needs only profile_id and product_id """ product = get_or_raise(Product, shopify_id=product_id) profile = get_or_raise(Profile, id=profile_id) # first, if we do care about plan then `DONT_CARE_ABOUT_PLAN`==FALSE if not cfg.DONT_CARE_ABOUT_PLAN: # CHECK if additional values are added in product's variants if not product.ready_to_try: raise serializers.ValidationError( "{} is not ready to try yet :()".format(product.title)) # second, if we do care about ready_to_try then `DONT_CARE_ABOUT_READY_TO_TRY`==FALSE if not cfg.DONT_CARE_ABOUT_READY_TO_TRY: # check if product.gender doesn't match the plan if not product.covered_by_the_plan: raise serializers.ValidationError( "{} is not covered by the plan :()".format(product.title)) ''' IMPORTANT, we could design a plan scheme, Each store could only get only limited number of tries ''' data = fit_product(product=product, profile=profile) return data
def create(self, validated_data): token = validated_data['token'] hitproduct = get_or_raise(HitProduct, token=token) hitproduct.try_duration_seconds = hitproduct.try_duration_seconds + \ STILL_IN_FITTING_ROOM_TIME_INTERVAL hitproduct.save() return {'new token': hitproduct.token, 'product': hitproduct.product.shopify_id, 'try duration in seconds': hitproduct.try_duration_seconds}
def validate(self, data): # first get profile and product product_id = data['product'] self.context['profile'] = get_or_raise(Profile, id=self.context['profile_id']) self.context['product'] = get_or_raise(Product, shopify_id=product_id) try: # if they are not existed, i.e profile has not added the product, # to his closet, Validation is True obj = Product_Profile_Add_To_Closet.objects.get( product=self.context['product'], customer_profile=self.context['profile']) except: return data # if no exception has fired, that means it's already existed raise serializers.ValidationError( "{} has added {} to his closet".format(self.context['profile'], self.context['product']))
def create(self, validated_data): product_id = validated_data['product_id'] try: ''' With anonymous profile, please check UNIQUE contraints to make sure, there is no duplicated values. ''' profile = get_or_raise(Profile, **validated_data['profile']) except: # if there is no duplicated values then do create it profile = Profile.objects.create(**validated_data['profile']) return _fit_product(product_id, profile.id)
def get_queryset(self): store = get_or_raise(Store, slug=self.kwargs['store_slug']) if not (store.owner.user == self.request.user): raise PermissionDenied queryset = Product.objects.filter(store=store) if 'search' in self.request.query_params and self.request.query_params[ 'search']: queryset = queryset.filter( title__icontains=self.request.query_params['search']) if ('ready_to_try' in self.request.query_params and self.request.query_params['ready_to_try']): def str2bool(v): return v.lower() in ("yes", "true", "t", "1") pk_list = [ x.shopify_id for x in queryset if x.ready_to_try == str2bool( self.request.query_params['ready_to_try']) ] queryset = Product.objects.filter(shopify_id__in=pk_list) return queryset
def create(self, validated_data): product = get_or_raise(Product, shopify_id=validated_data['product']) hitproduct = HitProduct.objects.create(product=product) return {'token': hitproduct.token}
def has_permission(self, request, view): kwargs = request.__dict__['parser_context']['kwargs'] customer_slug, profile_id = kwargs['slug_slug'], kwargs['id_pk'] customer = get_or_raise(Customer, slug=customer_slug) profile = get_or_raise(Profile, id=profile_id) return profile.customer == customer