Best Python code snippet using fMBT_python
schema.py
Source:schema.py
1import graphene2from graphene_django.types import DjangoObjectType3from graphql_relay.node.node import from_global_id4from Apps.products.models import Products5import datetime6class ProductsType(DjangoObjectType):7 class Meta:8 model = Products9class CreateProduct(graphene.Mutation):10 products = graphene.Field(lambda: ProductsType)11 class Arguments:12 productName = graphene.String()13 productCode = graphene.String()14 brandsId = graphene.Int()15 def mutate(self, info, productName, productCode, brandsId):16 products = Products(product_name=productName,17 product_code=productCode,18 brands_id=brandsId,19 product_status=True,20 product_creation=datetime.datetime.now(),)21 products.save()22 return CreateProduct(products=products)23class UpdateProducts(graphene.Mutation):24 products = graphene.Field(lambda: ProductsType)25 class Arguments:26 productsId = graphene.Int()27 productName = graphene.String()28 productCode = graphene.String()29 brandsId = graphene.Int()30 def mutate(self, info, productsId, productName, productCode, brandsId):31 products = Products.objects.get(pk=productsId)32 products.product_name = productName33 products.product_code = productCode34 products.product_status = True35 products.product_creation = datetime.datetime.now()36 products.brands_id = brandsId37 products.save()38 return UpdateProducts(products=products)39class DeleteProducts(graphene.Mutation):40 products = graphene.Field(lambda: ProductsType)41 class Arguments:42 productsId = graphene.Int()43 def mutate(self, info, productsId):44 products = Products.objects.get(pk=productsId)45 products.delete()46 return DeleteProducts(products=products)47class Query(graphene.ObjectType):48 ProductsAll = graphene.List(ProductsType)49 ProductsOne = graphene.Field(ProductsType, product_id=graphene.Int())50 def resolve_ProductsOne(self, info, **kwargs):51 product_id = kwargs.get('product_id')52 if product_id is not None:53 return Products.objects.get(pk=product_id)54 else:55 return None56 def resolve_ProductsAll(self, info):57 return Products.objects.all()58class Mutations():59 create_product = CreateProduct.Field()60 update_product = UpdateProducts.Field()61 delete_product = DeleteProducts.Field()...
app.py
Source:app.py
1from flask import Flask , app, jsonify, request23app= Flask(__name__)45from products import products67@app.route('/products', methods=['GET'])8def getProducts():9 return jsonify({"Products": products})1011@app.route('/products/<string:product_name>')12def getProduct(product_name):13 productsFound= [product for product in products if product['name'] == product_name] 14 if (len(productsFound )>0):15 return jsonify({"product": productsFound[0]})16 return jsonify({"message": "Product not found"})1718@app.route('/products', methods=['POST'])19def addProduct():20 new_product = {21 "name": request.json['name'], 22 "price": request.json['price'],23 "quantity": request.json['quantity']24 }25 products.append(new_product)26 return jsonify({"message": "Product Added Succesfully", "products": products})2728@app.route('/products/<string:product_name>', methods=['PUT'])29def editProduct(product_name):30 productsFound= [product for product in products if product['name'] == product_name]31 if (len(productsFound )>0):32 productsFound[0]['name'] = request.json['name'],33 productsFound[0]['price'] = request.json['price'],34 productsFound[0]['quantity'] = request.json['quantity']35 return jsonify({36 "message": "Product Updated",37 "product": productsFound[0]})38 return jsonify({"message": "Product not found"})3940@app.route('/products/<string:product_name>', methods=['DELETE'])41def deleteProduct(product_name):42 productsFound= [product for product in products if product['name'] == product_name]43 if (len(productsFound )>0):44 products.remove(productsFound[0])45 return jsonify({"message": "Product Deleted",46 "Products": products})47 return jsonify({"message": "Product not found"})4849 505152if __name__ =='__main__':
...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!