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)
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('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)
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)
def mutate_and_get_payload(cls, input, context, info): if hasattr(context.user, 'profile'): profile = context.user.profile else: profile = Profile() profile.user = context.user # update profile form = ProfileForm(input, instance=profile) if form.is_valid(): form.save() else: raise FormError(form.errors) return UpdateProfileMutation(profile=profile)
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)
def mutate_and_get_payload(cls, input, context, info): # check user check_superuser(context) parent_id = input.get('parent_id') parent = get_node(parent_id, context, info, Category) # create category form = CategoryForm(input) if form.is_valid(): new_category = form.save(commit=False) new_category.parent = parent new_category.save() else: raise FormError(form.errors) return CreateCategoryMutation(category=new_category)
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)
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): 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('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): # create user form = RegisterUserForm(input, initial={'is_active': False}) if form.is_valid(): user = form.save() else: raise FormError(form.errors) # set password password = form.cleaned_data['password'] user.set_password(password) user.save() # send activation email translation.activate('fa') from_email = settings.EMAIL_FROM to_email = user.email token = generate_token(user) subject = _("Activation Email") current_site = get_current_site(context) domain = current_site.domain link = 'http://' + domain + reverse('active', kwargs={'token': token}) context = { 'link': link, 'email': user.email, 'user': user, 'token': token, } text_body = loader.render_to_string("activation_email.txt", context) html_body = loader.render_to_string("activation_email.html", context) email_message = EmailMultiAlternatives(subject, text_body, from_email, [to_email]) email_message.attach_alternative(html_body, 'text/html') email_message.send() return RegisterUserMutation(user=user)
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)
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)