price = quote['price'] value = round(shares * price, 2) user = Users.query.get(session.get('user_id')) user.cash += value record = Records(symbol=quote['symbol'], company_name=quote['name'], transact_type="sell", shares=int('-' + str(shares)), price=price, user_id=user.id) db.session.add(record) db.session.commit() flash('Sold') return redirect(url_for('index')) def errorhandler(e): """Handle error""" if not isinstance(e, HTTPException): e = InternalServerError() return apology(e.name, e.code) # Listen for errors for code in default_exceptions: app.errorhandler(code)(errorhandler)
result = word_count(text, delimiter) elif (operationname == 'charactercount'): operation_text = 'Character Count' result = character_count(text) else: operation_text = 'Most Frequent 5 Words' result = frequent_five_words(text, delimiter) return render_template('result.html', title='Result', operation=operation_text, input=text, result=result) app.errorhandler(403) def forbidden_error(error): return render_template('403.html'), 403 @app.errorhandler(404) def not_found_error(error): return render_template('404.html'), 404 @app.errorhandler(500) def internal_error(error): return render_template('500.html'), 500
def login(): """Render the website's login user page.""" secret= app.config['TOKEN_SECRET'] email= request.json['email'] password= request.json['password'] payload={"email": email,"password": password} encoded_token=jwt.encode({'logon_info': payload}, secret, algorithm='HS256' user = Person.query.filter_by(email_address=email, password=password).first() if user is not None: login_user(user) # Sends back the information along with the token generated response = jsonify(information={"error":"null","data":{'user':{'id':user.id,'email': user.email_address,'fname':user.first_name, 'lname': user.last_name, 'Authorization_token':encoded_token},"message":"Success"}}) else: response = jsonify({"error":"1","data":{},"message":'failed'}) response.status_code = 401 return response @app.route('/api/users/<userid>/wishlist', methods=["GET","POST"]) @login_required def apiadd(userid): if request.method == "POST": new_wish= Wish(wish_url=request.json['url'], user_id=userid , wish_descript=request.json['description'], wish_title=request.json['title'], thumbnail=request.json['image'], added=str(datetime.now())); db.session.add(new_wish) db.session.commit() response = jsonify({"error":"null","data":{},"message":"Success"}) return response else: user = Person.query.filter_by(id=userid).first() userwishes = Wish.query.filter_by(user_id=userid) wishlist = [] for wish in userwishes: wishlist.append({'id':wish.wish_id,'title': wish.wish_title,'description':wish.wish_descript,'url':wish.wish_url,'thumbnail':wish.thumbnail, 'added': wish.added}) if(len(wishlist)>0): response = jsonify({"error":"null","data":{"wishes":wishlist}}) else: response = jsonify({"error":"1","data":{}}) return response @app.route('/api/thumbnails', methods=['POST']) def thumbnail(): url = request.json['url'] imagelist = get_images(url) for each in imagelist: if not each.lower().endswith(('.png', '.jpg', '.jpeg')): imagelist.remove(each) imagelist= list(set(imagelist)); output = jsonify(thumbnails= imagelist) return output def get_images(url): result = requests.get(url) soup = BeautifulSoup(result.text, "html.parser") imgs=[] image = "%s" for img in soup.findAll("img", src=True): link = image % urlparse.urljoin(url, img["src"]) imgs+=[link] return imgs @app.route('/api/users/<userid>/wishlist/<itemid>', methods=['POST']) def deletewish(userid,itemid): item_id= request.json['itemid'] #because based on the db the wish id and the person/userid are always the same deleted_wish= Wish.query.filter_by(user_id=userid,wish_id= itemid).first() # use session.delete here instead of add db.session.delete(deleted_wish) db.session.commit() response = jsonify({"error":"null","data":{},"message":"Success"}) return response @app.route('/about/') def about(): """Render the website's about page.""" return render_template('about.html') # user_loader callback. This callback is used to reload the user object from # the user ID stored in the session @login_manager.user_loader def load_user(id): return Person.query.get(int(id)) # ### # # The functions below should be applicable to all Flask apps. # ### @app.route('/share/', methods = ['POST']) def send_email(): firstname = request.json['firstname'] lastname = request.json['lastname'] user= Person.query.filter_by(id=request.json['userid']).first() name = user.first_name userid= user.id from_addr = '*****@*****.**' to_addr = request.json['email'] msg = MIMEMultipart() msg['From'] = from_addr msg['To'] = to_addr msg['Subject'] = str(name) + " has shared their wishlist with you" body = "Member " + str(name) + " from Wishlstproject.com has shared their wishlist with you!! https://www.google.com/search?q=cats+meme&client=firefox-b&noj=1&source=lnms&tbm=isch&sa=X&ved=0ahUKEwizk8GiyMXTAhWBKiYKHc0NAh0Q_AUICigB&biw=1366&bih=669" msg.attach(MIMEText(body, 'plain')) #from_name = 'wishlistproject' #to_name = request.json['name'] #subject = 'Someone has shared their wishlist with you' ### message = 'Member '+ username + 'from Wishlstproject.com has shared his/her wishlist with you!! /n http://info3180-project2-shadain94.c9users.io/api/users/' + userid + '/wishlist' #message = "test" #message_to_send = message.format(from_name, from_addr, to_name, to_addr, subject, message) # Credentials (if needed) username = '******' password = '******' # The actual mail send server = smtplib.SMTP('smtp.gmail.com:587') server.ehlo() server.starttls() server.login(username, password) text = msg.as_string() server.sendmail(from_addr, to_addr, text) server.quit() response = jsonify({"error":"null", "message":"Success"}) return response def timeinfo(entry): day = time.strftime("%a") date = time.strftime("%d") if (date <10): date = date.lstrip('0') month = time.strftime("%b") year = time.strftime("%Y") return day + ", " + date + " " + month + " " + year @app.route('/<file_name>.txt') def send_text_file(file_name): """Send your static text file.""" file_dot_text = file_name + '.txt' return app.send_static_file(file_dot_text) @app.after_request def add_header(response): """ Add headers to both force latest IE rendering engine or Chrome Frame, and also to cache the rendered page for 10 minutes. """ response.headers['X-UA-Compatible'] = 'IE=Edge,chrome=1' response.headers['Cache-Control'] = 'public, max-age=0' return response @app.errorhandler(404) def page_not_found(error): """Custom 404 page.""" return render_template('404.html'), 404 if __name__ == '__main__': app.run(debug=True,host="0.0.0.0",port="8080")
__author__ = 'troviln' from flask import Flask, jsonify, request, abort, render_template, flash, redirect, url_for from app import app, db from auth import auth from model import Goods, User, Reviews, Photo, Order import datetime app.errorhandler(404) def not_found(error=None): message = { 'status': 404, 'message': 'Not Found: ' + request.url, } resp = jsonify(message) resp.status_code = 404 return resp # @app.route('/api/users', methods=['POST']) # def users_endpoint(page=1): # if request.method == 'POST': # post request # # row = Users.create(**request.json) # query = Users.select().where( # Users.id == row.id, # Users.name == row.name # ) # data = [i.serialize for i in query]