SHARE
TWEET

Untitled

a guest Dec 13th, 2016 74 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': '83838769494183-f2e9i6s0ng8eps3075bfsmlgoo3e7hvk.apps.googleusercontent.com7483683053553',
  43.         'secret': 'bce23251V_GgfoNr52oB0X9vhVXd3E1650fbb754b955453e1d10b2f0'
  44.     },
  45.     'twitter': {
  46.         'id': 'dDu5qMg32Rsh94P89UCcQoLJO',
  47.         'secret': 'EB6LF33hBYjhKsiPqxofG1NN8PFSQ1i007zjdIvDekgCnkbkp7'
  48.     }
  49. }
  50.  
  51. lm = LoginManager(app)
  52. lm.login_view = 'index'
  53.  
  54.  
  55. #Oauth decorators for social media login
  56. @lm.user_loader
  57. def load_user(id):
  58.     return User.query.get(int(id))
  59.  
  60.  
  61. @app.route('/', methods=["GET","POST"])
  62.  
  63.  
  64. def index():
  65.     return render_template('index.html')
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72. @app.route('/logout')
  73. def logout():
  74.     logout_user()
  75.     return redirect(url_for('index'))
  76.  
  77.  
  78. @app.route('/authorize/<provider>')
  79. def oauth_authorize(provider):
  80.     if not current_user.is_anonymous:
  81.         return redirect(url_for('index'))
  82.     oauth = OAuthSignIn.get_provider(provider)
  83.     return oauth.authorize()
  84.  
  85.  
  86. @app.route('/callback/<provider>')
  87. def oauth_callback(provider):
  88.     if not current_user.is_anonymous:
  89.         return redirect(url_for('index'))
  90.     oauth = OAuthSignIn.get_provider(provider)
  91.     social_id, username, email = oauth.callback()
  92.     if social_id is None:
  93.         flash('Authentication failed.')
  94.         return redirect(url_for('index'))
  95.     user = User.query.filter_by(social_id=social_id).first()
  96.     if not user:
  97.         user = User(social_id=social_id, nickname=username, email=email)
  98.         db.session.add(user)
  99.         db.session.commit()
  100.     login_user(user, True)
  101.     return redirect(url_for('index'))
  102.  
  103.  
  104. ######################################################################################################################
  105. class Comment(db.Model):
  106.  
  107.     __tablename__ = "comments"
  108.  
  109.     id = db.Column(db.Integer, primary_key=True)
  110.     content = db.Column(db.String(4096))
  111.  
  112.  
  113. class User(UserMixin, db.Model):
  114.     __tablename__ = 'users'
  115.     id = db.Column(db.Integer, primary_key=True)
  116.     social_id = db.Column(db.String(64), nullable=False, unique=True)
  117.     nickname = db.Column(db.String(64), nullable=False)
  118.     email = db.Column(db.String(64), nullable=True)
  119.     hangboardwerkouts = db.relationship('HangboardWerkout', backref='author', lazy='dynamic')
  120.     kampuswerkouts = db.relationship('KampusWerkout', backref='author', lazy='dynamic')
  121.  
  122.  
  123.  
  124. class HangboardWerkout(db.Model):
  125.  
  126.     __tablename__ = "hangboardwerkout"
  127.     id = db.Column(db.Integer, primary_key=True)
  128.     name = db.Column(db.String(4096))
  129.     holds_used = db.Column(db.Integer)
  130.     arm_used = db.Column(db.String(4096))
  131.     hangtime = db.Column(db.Integer)
  132.     resttime = db.Column(db.Integer)
  133.     reps = db.Column(db.Integer)
  134.     sets = db.Column(db.Integer)
  135.     setrest = db.Column(db.Integer)
  136.     weight_kg = db.Column(db.Integer)
  137.     timestamp = db.Column(db.DateTime)
  138.     user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
  139.  
  140.     def __init__(self, name, holds_used, reps, sets, setrest, arm_used, hangtime, resttime, weight_kg, timestamp, user_id):
  141.         self.name = name
  142.         self.holds_used = holds_used
  143.         self.arm_used = arm_used
  144.         self.hangtime = hangtime
  145.         self.resttime = resttime
  146.         self.reps = reps
  147.         self.sets = sets
  148.         self.setrest = setrest
  149.         self.weight_kg = weight_kg
  150.         self.timestamp = timestamp
  151.         self.user_id = user_id
  152.  
  153. class KampusWerkout(db.Model):
  154.     __tablename__ = "kampuswerkout"
  155.     id = db.Column(db.Integer, primary_key = True)
  156.     name = db.Column(db.String(4096))
  157.     hand = db.Column(db.String(4096))
  158.     rung1 = db.Column(db.Integer)
  159.     rung2 = db.Column(db.Integer)
  160.     rung3 = db.Column(db.Integer)
  161.     rung4 = db.Column(db.Integer)
  162.     rung5 = db.Column(db.Integer)
  163.     rung6 = db.Column(db.Integer)
  164.     rung7 = db.Column(db.Integer)
  165.     rung8 = db.Column(db.Integer)
  166.     rung9 = db.Column(db.Integer)
  167.     timestamp = db.Column(db.DateTime)
  168.     user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
  169.     def __init__(self, name, hand, rung1, rung2, rung3, rung4, rung5, rung6, rung7, rung8, rung9, timestamp, user_id):
  170.         self.name = name
  171.         self.hand = hand
  172.         self.rung1 = rung1
  173.         self.rung2 = rung2
  174.         self.rung3 = rung3
  175.         self.rung4 = rung4
  176.         self.rung5 = rung5
  177.         self.rung6 = rung6
  178.         self.rung7 = rung7
  179.         self.rung8 = rung8
  180.         self.rung9 = rung9
  181.         self.timestamp = timestamp
  182.         self.user_id = user_id
  183.  
  184.  
  185. class BoulderMoves(db.Model):
  186.  
  187.     __tablename__ = "bouldermoves"
  188.     id = db.Column(db.Integer, primary_key = True)
  189.     name = db.Column(db.String(4096))
  190.     numberofmoves = db.Column(db.Integer)
  191.     intensity = db.Column(db.Integer)
  192.     reps = db.Column(db.Integer)
  193.     werktime = db.Column(db.Integer)
  194.     resttime = db.Column(db.Integer)
  195.     grade = db.Column(db.String(4096))
  196.     sets = db.Column(db.Integer)
  197.     setrest = db.Column(db.Integer)
  198.     comments = db.Column(db.String(4096))
  199.     timestamp = db.Column(db.DateTime)
  200.     user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
  201.  
  202.     def __init__(self, name, numberofmoves, intensity, reps, werktime, resttime, grade, sets, comments, setrest, timestamp, user_id):
  203.         self.name = name
  204.         self.numberofmoves = numberofmoves
  205.         self.intensity = intensity
  206.         self.reps = reps
  207.         self.werktime = werktime
  208.         self.resttime = resttime
  209.         self.grade = grade
  210.         self.sets = sets
  211.         self.setrest = setrest
  212.         self.comments = comments
  213.         self.timestamp = timestamp
  214.         self.user_id = user_id
  215.  
  216.  
  217. #############################################
  218. ############# VIEWS #########################
  219. #############################################
  220. @app.route("/timerwerk", methods=["GET","POST"])
  221. def timer():
  222.     return render_template("timerwerk.html")
  223.  
  224. @app.route("/boulder", methods=["GET", "POST"])
  225. @login_required
  226.  
  227. def Boulder():
  228.     form = BoulderForm()
  229.     if form.validate_on_submit():
  230.         timestamp=datetime.utcnow()
  231.  
  232.         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)
  233.  
  234.         #numberofmoves = BoulderMoves(numberofmoves=request.form["numberofmoves"])
  235.         #name = BoulderMoves(name=request.form["name"])
  236.         #db.session.add_all([numberofmoves, name])
  237.         db.session.add(bouldermove)
  238.         db.session.commit()
  239.         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'])
  240.  
  241.     else:
  242.         flash_errors(form)
  243.  
  244.     return render_template("boulder.html", title="Time to get strong", form=form, boulder=BoulderMoves.query.all())
  245. ##########################################################
  246.  
  247.  
  248. @app.route("/hangboard", methods=["GET", "POST"])
  249. @login_required
  250.  
  251. def Hangboard():
  252.     form = HangboardForm()
  253.     if form.validate_on_submit():
  254.         timestamp=datetime.utcnow()
  255.         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)
  256.         db.session.add(werk)
  257.         db.session.commit()
  258.         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'])
  259.     else:
  260.         flash_errors(form)
  261.     return render_template("hangboard.html", title="Time to get strong", form=form, hangboard=HangboardWerkout.query.all())
  262.  
  263.  
  264. #################################################################
  265.  
  266.  
  267.  
  268.  
  269. @app.route("/kampus", methods=["GET", "POST"])
  270. @login_required
  271.  
  272.  
  273. def Kampus():
  274.     if request.method == "POST":
  275.         timestamp=datetime.utcnow()
  276.         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)
  277.         db.session.add(kampuswerkout)
  278.         db.session.commit()
  279.         return render_template("kampus.html", kampus=KampusWerkout.query.all())
  280.     return render_template("kampus.html", kampus=KampusWerkout.query.all())
  281.  
  282.  
  283. ##############################################
  284. @app.route("/diary", methods=["GET", "POST"])
  285. def diary():
  286.     if request.method == "GET":
  287.         return render_template("diary.html", comments=Comment.query.all())
  288.  
  289.     comment = Comment(content=request.form["contents"])
  290.     db.session.add(comment)
  291.     db.session.commit()
  292.     return redirect(url_for('diary'))
  293. ############################################################
  294.  
  295.  
  296.  
  297. @app.route("/werktime", methods=["GET","POST"])
  298. def werktime():
  299.     return render_template("werktime.html")
  300.  
  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. ## mongo details #####################################
  333. '''
  334. from pymongo import MongoClient
  335.  
  336. #connect
  337.  
  338. client = MongoClient('mongodb://monkeymoves:onelife999@ds054288.mlab.com:54288/campus')
  339.  
  340. db = client.campus
  341.  
  342. def connect():
  343.     connection = MongoClient("ds054288.mlab.com",54288)
  344.     handle = connection["campus"]
  345.     handle.authenticate("monkeymoves","onelife999")
  346.     return handle
  347. handle = connect()
  348.  
  349. @app.route("/kampus", methods=["GET", "POST"])
  350. def Kampus():
  351.     userinputs = [x for x in handle.mycollection.find()]
  352.  
  353.     return render_template("kampus.html", userinputs=userinputs)'''
  354.  
  355.  
  356.  
  357.  
  358.  
  359. ######################################################################################################################
  360. ''' from flask_script import Manager
  361. from flask_migrate import Migrate, MigrateCommand
  362.  
  363. migrate = Migrate(app, db)
  364. manager = Manager(app)
  365. manager.add_command('db', MigrateCommand) '''
  366. ######################################################################################################################
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