def post(self): # NAME if not request.form: return responseJSON(None, "Sin parametros", 403) if not "username" in request.form: return responseJSON(None, "Parametros invalidos (username)", 403) # Check for name not null if len(request.form['username']) < 1: return responseJSON(None, "Nombre invalido", 403) # PASSWORD if not "password" in request.form: return responseJSON(None, "Parametros invalidos (password)", 403) if len(request.form['password']) < 1: return responseJSON(None, "Constraseña invalida", 403) # ROLE if not "rol" in request.form: return responseJSON(None, "Parametros invalidos (rol)", 403) if len(request.form['rol']) < 1: return responseJSON(None, "Rol invalida", 403) # MAIL if not "email" in request.form: return responseJSON(None, "Parametros invalidos (email)", 403) if len(request.form['email']) < 1: return responseJSON(None, "Email invalida", 403) # Add user u = User(request.form['username'], request.form['password'], request.form['rol'], request.form['email']) db.session.add(u) db.session.commit() return responseJSON("userToJSON(p)", None, 200)
def delete(self, id): if id: category = Category.query.get(id) if not category: return responseJSON(None,"Parametros invalidos (id)",403) db.session.delete(category) db.session.commit() return responseJSON("Categoría eliminada",None,200) return responseJSON(None,"Id invalido",403)
def delete(self, id): if id: user = User.query.get(id) if not user: return responseJSON(None, "Parametros invalidos (id)", 403) db.session.delete(user) db.session.commit() return responseJSON("Usuario eliminado", None, 200) return responseJSON(None, "Id invalido", 403)
def delete(self, id): if id: product = Product.query.get(id) alert = Alert(product.name+" ha sido eliminado",0) if not product: return responseJSON(None,"Parametros invalidos (id)",403) db.session.add(alert) db.session.delete(product) db.session.commit() return responseJSON("Prodcuto eliminado",None,200) return responseJSON(None,"Id invalido",403)
def get(self,id=None): if id: category = Category.query.get(id) if not category: return responseJSON(None,"Parametros invalidos (id)",403) else: res = categoryToJSON(category) else: categories = Category.query.all() res=[] for p in categories: res.append(categoryToJSON(p)) return responseJSON(res,None,200)
def get(self, id=None): if id: user = User.query.get(id) if not user: return responseJSON(None, "Parametros invalidos (id)", 403) else: res = userToJSON(user) else: users = User.query.all() res = [] for p in users: res.append(userToJSON(p)) return responseJSON(res, None, 200)
def get(self,id=None): if id: product = Product.query.get(id) if not product: return responseJSON(None,"Parametros invalidos (id)",403) else: res = productToJSON(product) else: products = Product.query.all() res=[] for p in products: res.append(productToJSON(p)) return responseJSON(res,None,200)
def put(self, id): if id: category = Category.query.get(id) if not category: return responseJSON(None,"Parametros invalidos (id)",403) # NAME if not request.form: return responseJSON(None,"Sin parametros (name)",403) if not "name" in request.form: return responseJSON(None,"Parametros invalidos (name)",403) # Check for name not null if len(request.form['name']) < 1: return responseJSON(None,"Nombre invalido",403) # DESCRIPTION if not "description" in request.form: return responseJSON(None,"Parametros invalidos (description)",403) if len(request.form['description']) < 1: return responseJSON(None,"Descripcción invalida",403) # Create category category.name = request.form['name'] category.description = request.form['description'] db.session.add(category) db.session.commit() return responseJSON(categoryToJSON(category),None,200) return responseJSON(None,"Id invalido",403)
def post(self): # NAME if not request.form: return responseJSON(None,"Sin parametros (name)",403) if not "name" in request.form: return responseJSON(None,"Parametros invalidos (name)",403) # Check for name not null if len(request.form['name']) < 1: return responseJSON(None,"Nombre invalido",403) # DESCRIPTION if not "description" in request.form: return responseJSON(None,"Parametros invalidos (description)",403) if len(request.form['description']) < 1: return responseJSON(None,"Descripcción invalida",403) # Add category c = Category(request.form['name'],request.form['description'],0) db.session.add(c) db.session.commit() return responseJSON("categoryToJSON(c)",None,200)
def put(self, id): if id: u = User.query.get(id) if not u: return responseJSON(None, "Parametros invalidos (id)", 403) # NAME if not request.form: return responseJSON(None, "Sin parametros", 403) if not "username" in request.form: return responseJSON(None, "Parametros invalidos (username)", 403) # Check for name not null if len(request.form['username']) < 1: return responseJSON(None, "Nombre invalido", 403) # PASSWORD if not "password" in request.form: return responseJSON(None, "Parametros invalidos (password)", 403) if len(request.form['password']) < 1: return responseJSON(None, "Constraseña invalida", 403) # ROLE if not "rol" in request.form: return responseJSON(None, "Parametros invalidos (rol)", 403) if len(request.form['rol']) < 1: return responseJSON(None, "Rol invalida", 403) # MAIL if not "email" in request.form: return responseJSON(None, "Parametros invalidos (email)", 403) if len(request.form['email']) < 1: return responseJSON(None, "Email invalida", 403) # Create user u.username = request.form['username'] u.password = request.form['password'] u.rol = request.form['rol'] u.email = request.form['email'] db.session.add(u) db.session.commit() return responseJSON(userToJSON(u), None, 200) return responseJSON(None, "Id invalido", 403)
def put(self, id): if id: product = Product.query.get(id) if not product: return responseJSON(None,"Parametros invalidos (id)",403) # NAME if not request.form: return responseJSON(None,"Sin parametros (name)",403) if not "name" in request.form: return responseJSON(None,"Parametros invalidos (name)",403) # Check for name not null if len(request.form['name']) < 1: return responseJSON(None,"Nombre invalido",403) # SKU if not "sku" in request.form: return responseJSON(None,"Parametros invalidos (sku)",403) if len(request.form['sku']) < 1: return responseJSON(None,"SKU invalido",403) # DESCRIPTION if not "description" in request.form: return responseJSON(None,"Parametros invalidos (description)",403) if len(request.form['description']) < 1: return responseJSON(None,"Descripcción invalida",403) # BRAND if not "brand" in request.form: return responseJSON(None,"Parametros invalidos (brand)",403) if len(request.form['brand']) < 1: return responseJSON(None,"Marca invalida",403) # PRICE if not "price" in request.form: return responseJSON(None,"Sin parametros (price)",403) # Cast price to float try: float(request.form['price']) # Price > 0 if float(request.form['price']) <= 0: return responseJSON(None,"Precio invalido",403) except ValueError: return responseJSON(None,"Parametros invalidos (price)",403) # CATEGORY if not "category_id" in request.form: return responseJSON(None,"Sin parametros (category_id)",403) # Cast to int try: int(request.form['category_id']) except ValueError: return responseJSON(None,"Parametros invalidos (category_id)",403) #Check for changes if product.sku != request.form['sku']: alertSku=Alert(product.name+" cambio el sku a "+request.form['sku'],0) db.session.add(alertSku) db.session.commit() if product.name != request.form['name']: alertName=Alert(product.name+" cambio el nombre a "+request.form['name'],0) db.session.add(alertName) db.session.commit() if product.description != request.form['description']: alertDesc=Alert(product.name+" cambio ls descripcción a "+request.form['description'],0) db.session.add(alertDesc) db.session.commit() if product.brand != request.form['brand']: alertBrand=Alert(product.name+" cambio la marca a "+request.form['brand'],0) db.session.add(alertBrand) db.session.commit() if product.price != request.form['price']: alertPrice=Alert(product.name+" cambio el precio a $"+request.form['price'],0) db.session.add(alertPrice) db.session.commit() if product.category_id != request.form['category_id']: alertCategory=Alert(product.name+" cambio la categoría a "+request.form['category_id'],0) db.session.add(alertCategory) db.session.commit() # Create product product.sku = request.form['sku'] product.name = request.form['name'] product.description = request.form['description'] product.brand = request.form['brand'] product.price = request.form['price'] product.category_id = request.form['category_id'] product.file = request.form['file'] db.session.add(product) db.session.commit() return responseJSON(productToJSON(product),None,200) return responseJSON(None,"Id invalido",403)
def post(self): # NAME if not request.form: return responseJSON(None,"Sin parametros (name)",403) if not "name" in request.form: return responseJSON(None,"Parametros invalidos (name)",403) # Check for name not null if len(request.form['name']) < 1: return responseJSON(None,"Nombre invalido",403) # SKU if not "sku" in request.form: return responseJSON(None,"Parametros invalidos (sku)",403) if len(request.form['sku']) < 1: return responseJSON(None,"SKU invalido",403) # DESCRIPTION if not "description" in request.form: return responseJSON(None,"Parametros invalidos (description)",403) if len(request.form['description']) < 1: return responseJSON(None,"Descripcción invalida",403) # BRAND if not "brand" in request.form: return responseJSON(None,"Parametros invalidos (brand)",403) if len(request.form['brand']) < 1: return responseJSON(None,"Marca invalida",403) # PRICE if not "price" in request.form: return responseJSON(None,"Sin parametros (price)",403) # Cast price to float try: float(request.form['price']) # Price > 0 if float(request.form['price']) <= 0: return responseJSON(None,"Precio invalido",403) except ValueError: return responseJSON(None,"Parametros invalidos (price)",403) # CATEGORY if not "category_id" in request.form: return responseJSON(None,"Sin parametros (category_id)",403) # Cast to int try: int(request.form['category_id']) except ValueError: return responseJSON(None,"Parametros invalidos (category_id)",403) # Add product p = Product(request.form['sku'],request.form['name'],request.form['description'],request.form['brand'],request.form['price'],request.form['category_id'],request.form['file'],0) alert = Alert(request.form['name']+" ha sido añadido.",0) db.session.add(p) db.session.add(alert) db.session.commit() return responseJSON(productToJSON(p),None,200)