def sell(): ''' This function insert a book which the user sold to the database. Also, Checks if the user has enough creadits to pay to the system for this sale. ''' form = sellForm() if (request.method == 'POST') and form.validate_on_submit(): #DETERMINE IF USER HAVE ENOUGH CREDIT TO SELL THE BOOK #GET USER ID username = request.form['username'] user = User.query.filter_by(username = str(username)).first() userid = user.id #GET USER CREDITS user_credits = user.get_credit() #GET USER COMPLAINTS num_of_complaints = db.session.query(User_Complaints).filter_by(complained_id = userid).count() #CALCULATE CREDIT AFTER SALE temp_credit = user.credits - ((int(request.form['saleDuration'])) * 5) - ((int(request.form['saleDuration'])) * num_of_complaints) #UPDATE USER CREDITS if (temp_credit < 0): return render_template('no_credit.html') user.credits = temp_credit file = form.data.get('bookImage') tempBool = 0 b = Book() filename = ''.join(random.choice(string.ascii_letters+string.digits) for x in range(20)) if (str(request.files['bookImage']) == "<FileStorage: u'' ('application/octet-stream')>"): print "NO IMAGE" b.image_name = "noImage.jpg" else: #file = form.data.get('bookImage') file = request.files['bookImage'] file.filename = filename+".jpg" if file and allowed_file(file.filename): #UPLOAD FILE filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) b.image_name = filename #PUSH DATA TO DB b.title = request.form['title'] b.author = request.form['author'] b.isbn = request.form['isbn'] b.price = float(request.form['price']) b.saleDuration = int(request.form['saleDuration']) b.publisher = request.form['publisher'] b.numOfPages = int(request.form['numOfPages']) b.lang = request.form['lang'] b.condition = request.form['condition'] b.genre = request.form['genre'] b.bookType = request.form['bookType'] b.edition = int(request.form['edition']) b.information = request.form['information'] #tempBool=0 tempBool = False if (form.data.get('buyable') == True): #tempBool = 1; tempBool = True else: #tempBool = 0; tempBool = False b.buyable = tempBool if (tempBool == True): #if (tempBool == 1): b.buyout_price = float(request.form['buynowPrice']) # changing current_bid to price #b.current_bid=float(0) b.current_bid = b.price b.biddable= 1 b.starting_bid=float(request.form['price']) b.owner_id=int(userid) db.session.add(b) db.session.commit() return render_template('success.html') return render_template('sell.html', form=form)