def mutate_and_get_payload(cls, input, context, info):
        # check user
        check_superuser(context)

        category_field_id = input.get('id')
        category_field = get_node(
            category_field_id, context, info, CategoryField)
        if not category_field:
            raise ResponseError(
                "Invalid Category Field",
                code='invalid_category_field')

        category_id = input.get('category_id')
        category = get_node(category_id, context, info, Category)
        if not category:
            raise ResponseError(
                "Invalid Category",
                code='invalid_category')

        # update category field
        form = CategoryFieldForm(input, instance=category_field)
        if form.is_valid():
            form.save()
            category_field.category = category
            category_field.save()
        else:
            raise FormError(form.errors)

        return UpdateCategoryFieldMutation(category_field=category_field)
Exemple #2
0
def add_attrs(primary_attrs, category):
    attrs = {}
    limit_character = 100
    if primary_attrs:
        for i in range(0, primary_attrs.__len__()):
            name = primary_attrs[i].get('name')
            value = primary_attrs[i].get('value', '')
            if value and len(value) > limit_character:
                raise ResponseError("Number of value characters is exceeded",
                                    code='invalid_attrs')
            category_field = CategoryField.objects.filter(
                name__exact=name).first()
            if not category_field or category != category_field.category:
                raise ResponseError("Invalid Category field",
                                    code='invalid_category_field')
            else:
                title = category_field.title
                type_ = category_field.type
                order = category_field.order
                option = category_field.option
                attrs[name] = {
                    "title": title,
                    "type": type_,
                    "order": order,
                    "option": option,
                    "value": value
                }
    json_attrs = json.dumps(attrs)
    return json_attrs
Exemple #3
0
    def mutate_and_get_payload(cls, input, context, info):
        user = context.user

        picture_id = input.get('id')
        picture = get_node(picture_id, context, info, Picture)
        if not picture:
            raise ResponseError("Invalid Picture", code='invalid_picture')

        if not picture.product.owner.validate_user(user):
            raise ResponseError("Invalid Owner", code='invalid_owner')

        media_id = input.get('picture_id')
        media = get_node(media_id, context, info, Media)

        if not media or not media.identity.validate_user(user):
            raise ResponseError("Invalid Media", code='invalid_media')

        # update picture
        picture.picture = media

        form = PictureForm(input, instance=picture)
        if form.is_valid():
            form.save()
        else:
            raise FormError(form.errors)

        return UpdateProductPictureMutation(picture=picture)
    def mutate_and_get_payload(cls, input, context, info):
        user = context.user

        product_id = input.get('id')
        product = get_node(product_id, context, info, Product)
        if not product:
            raise ResponseError("Invalid Product", code='invalid_product')

        if not product.owner.validate_user(user):
            raise ResponseError("Invalid Owner", code='invalid_owner')

        category_id = input.get('category_id')
        category = get_node(category_id, context, info, Category)
        if not category or not category.creatable:
            raise ResponseError("Invalid Category", code='invalid_category')

        # update product
        form = ProductForm(input, instance=product)
        form.data['attrs'] = add_attrs(input.get('attrs'), category)
        form.data['custom_attrs'] = add_custom_attrs(input.get('custom_attrs'))
        if form.is_valid():
            product = form.save(commit=False)
            product.category = category
            product.save()
        else:
            raise FormError(form.errors)

        return UpdateProductMutation(product=product)
Exemple #5
0
    def mutate_and_get_payload(cls, input, context, info):
        user = context.user

        product_id = input.get('product_id')
        product = get_node(product_id, context, info, Product)
        if not product:
            raise ResponseError("Invalid Product", code='invalid_product')

        if not product.owner.validate_user(user):
            raise ResponseError("Invalid Owner", code='invalid_owner')

        media_id = input.get('picture_id')
        media = get_node(media_id, context, info, Media)

        if not media or not media.identity.validate_user(user):
            raise ResponseError("Invalid Media", code='invalid_media')

        # create picture
        form = PictureForm(input, context.FILES)
        if form.is_valid():
            new_picture = form.save(commit=False)
            new_picture.product = product
            new_picture.picture = media
            new_picture.save()
        else:
            raise FormError(form.errors)

        return CreateProductPictureMutation(picture=new_picture)
Exemple #6
0
    def mutate_and_get_payload(cls, input, context, info):
        user = context.user

        picture_id = input.get('id')
        picture = get_node(picture_id, context, info, Picture)
        if not picture:
            raise ResponseError("Invalid Picture", code='invalid_picture')

        if not picture.product.owner.validate_user(user):
            raise ResponseError("Invalid Owner", code='invalid_owner')

        # delete product
        picture.delete()

        return DeleteProductPictureMutation(deleted_id=picture_id)
Exemple #7
0
    def mutate_and_get_payload(cls, input, context, info):
        user = context.user

        price_id = input.get('id', None)
        price = get_node(price_id, context, info, Price)
        if not price:
            raise ResponseError("Invalid Price", code='invalid_price')

        if not price.product.owner.validate_user(user):
            raise ResponseError("Invalid Owner", code='invalid_owner')

        # delete price
        price.delete()

        return DeletePriceMutation(deleted_id=price_id)
Exemple #8
0
    def mutate_and_get_payload(cls, input, context, info):
        user = context.user

        price_id = input.get('id')
        price = get_node(price_id, context, info, Price)
        if not price:
            raise ResponseError("Invalid Price", code='invalid_price')

        if not price.product.owner.validate_user(user):
            raise ResponseError("Invalid Owner", code='invalid_owner')

        # update price
        form = PriceForm(input, instance=price)
        if form.is_valid():
            form.save()
        else:
            raise FormError(form.errors)

        return UpdatePriceMutation(price=price)
Exemple #9
0
    def mutate_and_get_payload(cls, input, context, info):
        user = context.user

        comment_id = input.get('id')
        comment = get_node(comment_id, context, info, Comment)
        if not comment or comment.user != user:
            raise ResponseError("Invalid Comment", code='invalid_comment')

        # delete comment
        comment.delete()

        return DeleteCommentMutation(deleted_id=comment_id)
Exemple #10
0
    def mutate_and_get_payload(cls, input, context, info):
        user = context.user

        product_id = input.get('product_id')
        product = get_node(product_id, context, info, Product)
        if not product:
            raise ResponseError("Invalid Product", code='invalid_product')

        if not product.owner.validate_user(user):
            raise ResponseError("Invalid Owner", code='invalid_owner')

        # create price
        form = PriceForm(input)
        if form.is_valid():
            new_price = form.save(commit=False)
            new_price.product = product
            new_price.save()
        else:
            raise FormError(form.errors)

        return CreatePriceMutation(price=new_price)
    def mutate_and_get_payload(cls, input, context, info):
        # check user
        check_superuser(context)

        category_id = input.get('id', None)
        category = get_node(category_id, context, info, Category)
        if not category:
            raise ResponseError("Invalid Category", code='invalid_category')

        # delete category
        category.delete()

        return DeleteCategoryMutation(deleted_id=category_id)
Exemple #12
0
    def mutate_and_get_payload(cls, input, context, info):
        user = context.user

        owner_id = input.get('owner_id')
        owner = get_node(owner_id, context, info, Identity)
        if not owner or not owner.validate_user(user):
            raise ResponseError("Invalid Owner", code='invalid_owner')

        category_id = input.get('category_id')
        category = get_node(category_id, context, info, Category)
        if not category or not category.creatable:
            raise ResponseError("Invalid Category", code='invalid_category')

        # create product
        form = ProductForm(input)
        if form.is_valid():
            new_product = form.save(commit=False)
            new_product.owner = owner
            new_product.category = category
            new_product.save()
        else:
            raise FormError(form.errors)
        return CreateProductMutation(product=new_product)
Exemple #13
0
def add_custom_attrs(primary_custom_attrs):
    custom_attrs = {}
    limit_array = 50
    limit_character = 100
    if primary_custom_attrs:
        if primary_custom_attrs.__len__() < limit_array:
            for i in range(0, primary_custom_attrs.__len__()):
                key = primary_custom_attrs[i].get('name')
                value = primary_custom_attrs[i].get('value')
                if key and value:
                    if len(key) > limit_character or len(
                            value) > limit_character:
                        raise ResponseError(
                            "Number of characters exceeds the limit",
                            code='invalid_custom_attrs')
                    custom_attrs[key] = value
                else:
                    raise ResponseError("Key or value is empty",
                                        code='invalid_custom_attrs')
        else:
            raise ResponseError("Number of array elements is exceeded",
                                code='invalid_custom_attrs')
    json_custom_attrs = json.dumps(custom_attrs)
    return json_custom_attrs
Exemple #14
0
    def mutate_and_get_payload(cls, input, context, info):
        user = context.user

        comment_id = input.get('id')
        comment = get_node(comment_id, context, info, Comment)
        if not comment or comment.user != user:
            raise ResponseError("Invalid Comment", code='invalid_comment')

        # update comment
        form = CommentForm(input, instance=comment)
        if form.is_valid():
            form.save()
        else:
            raise FormError(form.errors)

        return UpdateCommentMutation(comment=comment)
Exemple #15
0
    def mutate_and_get_payload(cls, input, context, info):
        user = context.user
        old_password = input.get('old_password')
        new_password = input.get('new_password')

        if user.has_usable_password():
            if not user.check_password(old_password):
                raise ResponseError("Invalid Password",
                                    code='invalid_old_password')

        # TODO password validator
        # change password
        user.set_password(new_password)
        user.save()

        return ChangePasswordMutation(success=True)
Exemple #16
0
    def mutate_and_get_payload(cls, input, context, info):
        user = context.user

        product_id = input.get('product_id')
        product = get_node(product_id, context, info, Product)
        if not product:
            raise ResponseError("Invalid Product", code='invalid_product')

        # create comment
        form = CommentForm(input)
        if form.is_valid():
            new_comment = form.save(commit=False)
            new_comment.product = product
            new_comment.user = user
            new_comment.save()
        else:
            raise FormError(form.errors)

        return CreateCommentMutation(comment=new_comment)
    def mutate_and_get_payload(cls, input, context, info):
        # check user
        check_superuser(context)

        category_id = input.get('category_id')
        category = get_node(category_id, context, info, Category)
        if not category:
            raise ResponseError(
                "Invalid Category",
                code='invalid_category')

        # create category field
        form = CategoryFieldForm(input)
        if form.is_valid():
            new_category_field = form.save(commit=False)
            new_category_field.category = category
            new_category_field.save()
        else:
            raise FormError(form.errors)

        return CreateCategoryFieldMutation(category_field=new_category_field)
    def mutate_and_get_payload(cls, input, context, info):
        # check user
        check_superuser(context)

        category_id = input.get('id', None)
        category = get_node(category_id, context, info, Category)
        if not category:
            raise ResponseError("Invalid Category", code='invalid_category')

        parent_id = input.get('parent_id')
        parent = get_node(parent_id, context, info, Category)

        # update category
        form = CategoryForm(input, instance=category)
        if form.is_valid():
            form.save()
            category.parent = parent
            category.save()
        else:
            raise FormError(form.errors)

        return UpdateCategoryMutation(category=category)
Exemple #19
0
def check_superuser(context):
    user = context.user
    if not user.is_superuser:
        raise ResponseError("This user is not super user",
                            code=' not_access_permission')