def new_item(): """ Create a new item :return string: JSON """ # Get and clean data title = clean(request.json.get('title')) model = clean(request.json.get('model')) description = clean(request.json.get('description')) brand = request.json.get('brand') price = request.json.get('price') author = g.user.id # Check data if len(title) < 5: return jsonify({'error': 'too short title, minimum 5 characters'}), 206 if len(model) < 2: return jsonify({'error': 'too short model, minimum 2 characters'}), 206 if len(description) < 5: return jsonify({'error': 'too short description, min 5 symbols'}), 206 # convert data to integer try: brand = int(brand) except TypeError: return jsonify({'error': 'invalid category type'}), 206 try: price = int(price) except TabError: return jsonify({'error': 'invalid price type'}), 206 # if brand les then 1 send error if brand < 1: return jsonify({'error': 'brand not found'}), 206 # Save data item = create_item(title, description, model, brand, author, price) return jsonify(item.serialize), 200
def new_car(): """ Create a new car :return mix: """ # POST request if request.method == 'POST' and request.form['csrf_token'] == csrf_token: # Get and clean data try: title = clean(request.form.get('title')) model = clean(request.form.get('model')) description = clean(request.form.get('description')) brand = clean(request.form.get('brand')) price = clean(request.form.get('price')) author = session.get('uid') except TypeError: flash('fields can\'t be empty', 'error') return render('catalog/new_car.html', brands=brands, csrf=csrf_token) # check data if len(title) < 5: flash('too short title, minimum 5 characters', 'error') return render('catalog/new_car.html', brands=brands, csrf=csrf_token) if len(model) < 2: flash('too short model, minimum 2 characters', 'error') return render('catalog/new_car.html', brands=brands, csrf=csrf_token) if len(description) < 5: flash('too short description, min 5 symbols', 'error') return render('catalog/new_car.html', brands=brands, csrf=csrf_token) # convert data to integer try: brand = int(brand) except TypeError: flash('invalid category type', 'error') return render('catalog/new_car.html', brands=brands, csrf=csrf_token) try: price = int(price) except TabError: flash('invalid price type', 'error') return render('catalog/new_car.html', brands=brands, csrf=csrf_token) # if brand les then 1 send error if brand < 1: flash('brand not found', 'error') return render('catalog/new_car.html', brands=brands, csrf=csrf_token) # Save data car = create_item(title, description, model, brand, author, price) # redirect user return redirect('/edit/car/%s' % car.id) return render('catalog/new_car.html', brands=brands, csrf=csrf_token)