SHARE
TWEET

Untitled

a guest Dec 13th, 2016 29 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from flask import Flask, render_template, request, flash, redirect, url_for
  2. from datetime import datetime
  3. from forms import BoulderForm, HangboardForm
  4. from flask_sqlalchemy import SQLAlchemy
  5. from flask_login import LoginManager, UserMixin, login_user, logout_user, current_user, login_required
  6. from oauth import OAuthSignIn
  7.  
  8. app = Flask(__name__)
  9.  
  10. app.config["DEBUG"] = True
  11. app.config['SECRET_KEY'] = '~t\x86\xc9\x1ew\x8bOcX\x85O\xb6\xa2\x11kL\xd1\xce\x7f\x14<y\x9e'
  12. ###################################################################################################
  13.  
  14. SQLALCHEMY_DATABASE_URI = "mysql+mysqlconnector://{username}:{password}@{hostname}/{databasename}".format(
  15.     username="kampuswerk",
  16.     password="thecatsatonthemat",
  17.     hostname="kampuswerk.mysql.pythonanywhere-services.com",
  18.     databasename="kampuswerk$werkapp",
  19. )
  20. app.config["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_DATABASE_URI
  21. app.config["SQLALCHEMY_POOL_RECYCLE"] = 299
  22.  
  23. db = SQLAlchemy(app)
  24.  
  25. def flash_errors(form):
  26.     for field, errors in form.errors.items():
  27.         for error in errors:
  28.             flash(u"Error in the %s field - %s" % (
  29.                 getattr(form, field).label.text,
  30.                 error
  31.             ))
  32.  
  33.  
  34. ###################################################################################################
  35.  
  36. app.config['OAUTH_CREDENTIALS'] = {
  37.     'facebook': {
  38.         'id': '837483683053553',
  39.         'secret': 'bce2321650fbb754b955453e1d10b2f0'
  40.     },
  41.     'google': {
  42.         'id': '838769494183-f2e9i6s0ng8eps3075bfsmlgoo3e7hvk.apps.googleusercontent.com',
  43.         'secret': '51V_GgfoNr52oB0X9vhVXd3E'
  44.     },
  45.     'twitter': {
  46.         'id': 'dDu5qMg32Rsh94P89UCcQoLJO',
  47.         'secret': 'EB6LF33hBYjhKsiPqxofG1NN8PFSQ1i007zjdIvDekgCnkbkp7'
  48.     }
  49. }
  50.  
  51. lm = LoginManager(app)
  52. lm.login_view = 'index'
  53.  
  54.  
  55.  
  56. #Oauth decorators for social media login
  57. @lm.user_loader
  58. def load_user(id):
  59.     return User.query.get(int(id))
  60.  
  61.  
  62. @app.route('/', methods=["GET","POST"])
  63.  
  64.  
  65. def index():
  66.     return render_template('index.html')
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73. @app.route('/logout')
  74. def logout():
  75.     logout_user()
  76.     return redirect(url_for('index'))
  77.  
  78.  
  79. @app.route('/authorize/<provider>')
  80. def oauth_authorize(provider):
  81.     if not current_user.is_anonymous:
  82.         return redirect(url_for('index'))
  83.     oauth = OAuthSignIn.get_provider(provider)
  84.     return oauth.authorize()
  85.  
  86.  
  87. @app.route('/callback/<provider>')
  88. def oauth_callback(provider):
  89.     if not current_user.is_anonymous:
  90.         return redirect(url_for('index'))
  91.     oauth = OAuthSignIn.get_provider(provider)
  92.     social_id, username, email = oauth.callback()
  93.     if social_id is None:
  94.         flash('Authentication failed.')
  95.         return redirect(url_for('index'))
  96.     user = User.query.filter_by(social_id=social_id).first()
  97.     if not user:
  98.         user = User(social_id=social_id, nickname=username, email=email)
  99.         db.session.add(user)
  100.         db.session.commit()
  101.     login_user(user, True)
  102.     return redirect(url_for('index'))
  103.  
  104.  
  105. ######################################################################################################################
  106. class Comment(db.Model):
  107.  
  108.     __tablename__ = "comments"
  109.  
  110.     id = db.Column(db.Integer, primary_key=True)
  111.     content = db.Column(db.String(4096))
  112.  
  113.  
  114. class User(UserMixin, db.Model):
  115.     __tablename__ = 'users'
  116.     id = db.Column(db.Integer, primary_key=True)
  117.     social_id = db.Column(db.String(64), nullable=False, unique=True)
  118.     nickname = db.Column(db.String(64), nullable=False)
  119.     email = db.Column(db.String(64), nullable=True)
  120.     hangboardwerkouts = db.relationship('HangboardWerkout', backref='author', lazy='dynamic')
  121.     kampuswerkouts = db.relationship('KampusWerkout', backref='author', lazy='dynamic')
  122.  
  123.  
  124.  
  125. class HangboardWerkout(db.Model):
  126.  
  127.     __tablename__ = "hangboardwerkout"
  128.     id = db.Column(db.Integer, primary_key=True)
  129.     name = db.Column(db.String(4096))
  130.     holds_used = db.Column(db.Integer)
  131.     arm_used = db.Column(db.String(4096))
  132.     hangtime = db.Column(db.Integer)
  133.     resttime = db.Column(db.Integer)
  134.     reps = db.Column(db.Integer)
  135.     sets = db.Column(db.Integer)
  136.     setrest = db.Column(db.Integer)
  137.     weight_kg = db.Column(db.Integer)
  138.     timestamp = db.Column(db.DateTime)
  139.     user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
  140.  
  141.     def __init__(self, name, holds_used, reps, sets, setrest, arm_used, hangtime, resttime, weight_kg, timestamp, user_id):
  142.         self.name = name
  143.         self.holds_used = holds_used
  144.         self.arm_used = arm_used
  145.         self.hangtime = hangtime
  146.         self.resttime = resttime
  147.         self.reps = reps
  148.         self.sets = sets
  149.         self.setrest = setrest
  150.         self.weight_kg = weight_kg
  151.         self.timestamp = timestamp
  152.         self.user_id = user_id
  153.  
  154. class KampusWerkout(db.Model):
  155.     __tablename__ = "kampuswerkout"
  156.     id = db.Column(db.Integer, primary_key = True)
  157.     name = db.Column(db.String(4096))
  158.     hand = db.Column(db.String(4096))
  159.     rung1 = db.Column(db.Integer)
  160.     rung2 = db.Column(db.Integer)
  161.     rung3 = db.Column(db.Integer)
  162.     rung4 = db.Column(db.Integer)
  163.     rung5 = db.Column(db.Integer)
  164.     rung6 = db.Column(db.Integer)
  165.     rung7 = db.Column(db.Integer)
  166.     rung8 = db.Column(db.Integer)
  167.     rung9 = db.Column(db.Integer)
  168.     timestamp = db.Column(db.DateTime)
  169.     user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
  170.     def __init__(self, name, hand, rung1, rung2, rung3, rung4, rung5, rung6, rung7, rung8, rung9, timestamp, user_id):
  171.         self.name = name
  172.         self.hand = hand
  173.         self.rung1 = rung1
  174.         self.rung2 = rung2
  175.         self.rung3 = rung3
  176.         self.rung4 = rung4
  177.         self.rung5 = rung5
  178.         self.rung6 = rung6
  179.         self.rung7 = rung7
  180.         self.rung8 = rung8
  181.         self.rung9 = rung9
  182.         self.timestamp = timestamp
  183.         self.user_id = user_id
  184.  
  185.  
  186. class BoulderMoves(db.Model):
  187.  
  188.     __tablename__ = "bouldermoves"
  189.     id = db.Column(db.Integer, primary_key = True)
  190.     name = db.Column(db.String(4096))
  191.     numberofmoves = db.Column(db.Integer)
  192.     intensity = db.Column(db.Integer)
  193.     reps = db.Column(db.Integer)
  194.     werktime = db.Column(db.Integer)
  195.     resttime = db.Column(db.Integer)
  196.     grade = db.Column(db.String(4096))
  197.     sets = db.Column(db.Integer)
  198.     setrest = db.Column(db.Integer)
  199.     comments = db.Column(db.String(4096))
  200.     timestamp = db.Column(db.DateTime)
  201.     user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
  202.  
  203.     def __init__(self, name, numberofmoves, intensity, reps, werktime, resttime, grade, sets, comments, setrest, timestamp, user_id):
  204.         self.name = name
  205.         self.numberofmoves = numberofmoves
  206.         self.intensity = intensity
  207.         self.reps = reps
  208.         self.werktime = werktime
  209.         self.resttime = resttime
  210.         self.grade = grade
  211.         self.sets = sets
  212.         self.setrest = setrest
  213.         self.comments = comments
  214.         self.timestamp = timestamp
  215.         self.user_id = user_id
  216.  
  217.  
  218. #############################################
  219. ############# VIEWS #########################
  220. #############################################
  221. @app.route("/timerwerk", methods=["GET","POST"])
  222. def timer():
  223.     return render_template("timerwerk.html")
  224.  
  225. @app.route("/boulder", methods=["GET", "POST"])
  226. @login_required
  227.  
  228. def Boulder():
  229.     form = BoulderForm()
  230.     if form.validate_on_submit():
  231.         timestamp=datetime.utcnow()
  232.  
  233.         bouldermove = BoulderMoves(current_user.nickname, request.form['numberofmoves'], request.form['intensity'], request.form['reps'], request.form['werktime'], request.form['resttime'], request.form['comments'], request.form['grade'], request.form['sets'], request.form['setrest'], timestamp, current_user.id)
  234.  
  235.         #numberofmoves = BoulderMoves(numberofmoves=request.form["numberofmoves"])
  236.         #name = BoulderMoves(name=request.form["name"])
  237.         #db.session.add_all([numberofmoves, name])
  238.         db.session.add(bouldermove)
  239.         db.session.commit()
  240.         return render_template("timerwerk.html", hangtime=request.form['werktime'], resttime=request.form['resttime'], reps=request.form['reps'], sets = request.form['sets'], setrest=request.form['setrest'])
  241.  
  242.     else:
  243.         flash_errors(form)
  244.  
  245.     return render_template("boulder.html", title="Time to get strong", form=form, boulder=BoulderMoves.query.all())
  246. ##########################################################
  247.  
  248.  
  249. @app.route("/hangboard", methods=["GET", "POST"])
  250. @login_required
  251.  
  252. def Hangboard():
  253.     form = HangboardForm()
  254.     if form.validate_on_submit():
  255.         timestamp=datetime.utcnow()
  256.         werk = HangboardWerkout(current_user.nickname, request.form['holds_used'], request.form['reps'], request.form['sets'], request.form['setrest'], request.form['arm_used'], request.form['hangtime'], request.form['resttime'], request.form['weight_kg'], timestamp, current_user.id)
  257.         db.session.add(werk)
  258.         db.session.commit()
  259.         return render_template("timerwerk.html", hangtime=request.form['hangtime'], resttime=request.form['resttime'], reps=request.form['reps'], sets = request.form['sets'], setrest=request.form['setrest'], arm_used = request.form['arm_used'])
  260.     else:
  261.         flash_errors(form)
  262.     return render_template("hangboard.html", title="Time to get strong", form=form, hangboard=HangboardWerkout.query.all())
  263.  
  264.  
  265. #################################################################
  266.  
  267.  
  268.  
  269.  
  270. @app.route("/kampus", methods=["GET", "POST"])
  271. @login_required
  272.  
  273.  
  274. def Kampus():
  275.     if request.method == "POST":
  276.         timestamp=datetime.utcnow()
  277.         kampuswerkout = KampusWerkout(current_user.nickname, request.form['hand'], request.form['rung1'], request.form['rung2'], request.form['rung3'], request.form['rung4'], request.form['rung5'], request.form['rung6'], request.form['rung7'], request.form['rung8'], request.form['rung9'], timestamp, current_user.id)
  278.         db.session.add(kampuswerkout)
  279.         db.session.commit()
  280.         return render_template("kampus.html", kampus=KampusWerkout.query.all())
  281.     return render_template("kampus.html", kampus=KampusWerkout.query.all())
  282.  
  283.  
  284. ##############################################
  285. @app.route("/diary", methods=["GET", "POST"])
  286. def diary():
  287.     if request.method == "GET":
  288.         return render_template("diary.html", comments=Comment.query.all())
  289.  
  290.     comment = Comment(content=request.form["contents"])
  291.     db.session.add(comment)
  292.     db.session.commit()
  293.     return redirect(url_for('diary'))
  294. ############################################################
  295.  
  296.  
  297.  
  298. @app.route("/werktime", methods=["GET","POST"])
  299. def werktime():
  300.     return render_template("werktime.html")
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333. ## mongo details #####################################
  334. '''
  335. from pymongo import MongoClient
  336.  
  337. #connect
  338.  
  339. client = MongoClient('mongodb://monkeymoves:onelife999@ds054288.mlab.com:54288/campus')
  340.  
  341. db = client.campus
  342.  
  343. def connect():
  344.     connection = MongoClient("ds054288.mlab.com",54288)
  345.     handle = connection["campus"]
  346.     handle.authenticate("monkeymoves","onelife999")
  347.     return handle
  348. handle = connect()
  349.  
  350. @app.route("/kampus", methods=["GET", "POST"])
  351. def Kampus():
  352.     userinputs = [x for x in handle.mycollection.find()]
  353.  
  354.     return render_template("kampus.html", userinputs=userinputs)'''
  355.  
  356.  
  357.  
  358.  
  359.  
  360. ######################################################################################################################
  361. ''' from flask_script import Manager
  362. from flask_migrate import Migrate, MigrateCommand
  363.  
  364. migrate = Migrate(app, db)
  365. manager = Manager(app)
  366. manager.add_command('db', MigrateCommand) '''
  367. ######################################################################################################################
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top