Commit 87fe9b1c authored by danmcquillan's avatar danmcquillan

lab update

parent 7aaebb59
from flask import Flask, request
from flask import render_template
from flask import redirect, url_for
import pymongo
class DBHelper:
def __init__(self):
client = pymongo.MongoClient()
self.db = client['mytwits']
def get_all_twits(self):
return self.db.twits.find().sort('created_at',pymongo.ASCENDING)
app = Flask(__name__)
db = DBHelper()
@app.route('/')
def index():
twits = db.get_all_twits()
return render_template("mytwits_mongo_1.html", twits=twits)
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0',port=8000)
from flask import Flask, request
from flask import render_template
from flask import redirect, url_for
import pymongo
import datetime
from vs_url_for import vs_url_for
class DBHelper:
def __init__(self):
client = pymongo.MongoClient()
self.db = client['mytwits']
def get_all_twits(self):
return self.db.twits.find().sort('created_at',pymongo.DESCENDING)
def add_twit(self,twit):
return self.db.twits.insert({'twit':twit,'username':'dan1','created_at': datetime.datetime.utcnow()})
app = Flask(__name__)
db = DBHelper()
@app.route('/')
def index():
twits = db.get_all_twits()
return render_template("mytwits_mongo_2.html", twits=twits)
@app.route('/add_twit', methods = ['GET', 'POST'])
def add_twit():
if request.form.get('twit'):
db.add_twit(request.form['twit'])
return redirect(vs_url_for('index'))
return render_template('add_twit_mongo_2.html')
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0',port=8000)
from flask import Flask, request
from flask import render_template
from flask import redirect, url_for
import pymongo
import datetime
from flask_wtf import FlaskForm
from wtforms import StringField
from wtforms import SubmitField
from wtforms import validators
from vs_url_for import vs_url_for
class addTwitForm(FlaskForm):
twit = StringField('twit', validators = [validators.DataRequired()])
submit = SubmitField('submit', [validators.DataRequired()])
class DBHelper:
def __init__(self):
client = pymongo.MongoClient()
self.db = client['mytwits']
def get_all_twits(self):
return self.db.twits.find().sort('created_at',pymongo.DESCENDING)
def add_twit(self,twit):
return self.db.twits.insert({'twit':twit,'username':'dan1','created_at': datetime.datetime.utcnow()})
app = Flask(__name__)
db = DBHelper()
app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'
@app.route('/')
def index():
twits = db.get_all_twits()
return render_template("mytwits_mongo_3.html", twits=twits)
@app.route('/add_twit', methods = ['GET', 'POST'])
def add_twit():
form = addTwitForm()
if form.validate_on_submit():
twit = form.twit.data
db.add_twit(twit)
return redirect(vs_url_for('index'))
return render_template('add_twit_mongo_3.html',form=form)
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0',port=8000)
from flask import Flask, request
from flask import render_template
from flask import redirect, url_for
import pymysql
class DBHelper:
def __init__(self):
self.db = pymysql.connect(host='localhost',
user='mytwits_user',
passwd='mytwits_password',
db='mytwits')
def get_all_twits(self):
query = "select u.username, t.twit, t.created_at from twits t, users u where t.user_id=u.user_id order by t.created_at desc;"
with self.db.cursor() as cursor:
cursor.execute(query)
return cursor.fetchall() # The method fetches all (or all remaining) rows of a query result set and returns a list of tuples
app = Flask(__name__)
db = DBHelper()
@app.route('/')
def index():
twits = db.get_all_twits()
return render_template("mytwits_mysql_1.html", twits=twits)
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0',port=8000)
from flask import Flask, request
from flask import render_template
from flask import redirect, url_for
import pymysql
from vs_url_for import vs_url_for
class DBHelper:
def __init__(self):
self.db = pymysql.connect(host='localhost',
user='mytwits_user',
passwd='mytwits_password',
db='mytwits')
def get_all_twits(self):
query = "select u.username, t.twit, t.created_at from twits t, users u where t.user_id=u.user_id order by t.created_at desc;"
with self.db.cursor() as cursor:
cursor.execute(query)
return cursor.fetchall() # The method fetches all (or all remaining) rows of a query result set and returns a list of tuples
def add_twit(self,twit):
query = "insert into twits (twit,user_id) values \
('{}','{}');".format(twit,1)
print(query)
with self.db.cursor() as cursor:
cursor.execute(query)
return self.db.commit()
app = Flask(__name__)
db = DBHelper()
@app.route('/')
def index():
twits = db.get_all_twits()
return render_template("mytwits_mysql_2.html", twits=twits)
@app.route('/add_twit', methods = ['GET', 'POST'])
def add_twit():
if request.form.get('twit'):
db.add_twit(request.form['twit'])
return redirect(vs_url_for('index'))
return render_template('add_twit_mysql_2.html')
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0',port=8000)
from flask import Flask, request
from flask import render_template
from flask import redirect, url_for
import pymysql
import datetime
from flask_wtf import FlaskForm
from wtforms import StringField
from wtforms import SubmitField
from wtforms import validators
from vs_url_for import vs_url_for
class addTwitForm(FlaskForm):
twit = StringField('twit', validators = [validators.DataRequired()])
submit = SubmitField('submit', [validators.DataRequired()])
class DBHelper:
def __init__(self):
self.db = pymysql.connect(host='localhost',
user='mytwits_user',
passwd='mytwits_password',
db='mytwits')
def get_all_twits(self):
query = "select u.username, t.twit, t.created_at from twits t, users u where t.user_id=u.user_id order by t.created_at desc;"
with self.db.cursor() as cursor:
cursor.execute(query)
return cursor.fetchall() # The method fetches all (or all remaining) rows of a query result set and returns a list of tuples
def add_twit(self,twit):
query = "insert into twits (twit,user_id) values \
('{}','{}');".format(twit,1)
print(query)
with self.db.cursor() as cursor:
cursor.execute(query)
return self.db.commit()
app = Flask(__name__)
db = DBHelper()
app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'
@app.route('/')
def index():
twits = db.get_all_twits()
return render_template("mytwits_mysql_3.html", twits=twits)
@app.route('/add_twit', methods = ['GET', 'POST'])
def add_twit():
form = addTwitForm()
if form.validate_on_submit():
twit = form.twit.data
db.add_twit(twit)
return redirect(vs_url_for('index'))
return render_template('add_twit_mysql_3.html',form=form)
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0',port=8000)
from flask import Flask, request
from flask import render_template
from flask import redirect, url_for
import pymysql
import datetime
from flask_wtf import FlaskForm
from wtforms import StringField
from wtforms import HiddenField
from wtforms import SubmitField
from wtforms import validators
from vs_url_for import vs_url_for
class addTwitForm(FlaskForm):
twit = StringField('twit', validators = [validators.DataRequired()])
submit = SubmitField('submit', [validators.DataRequired()])
class editTwitForm(FlaskForm):
twit = StringField('twit', validators = [validators.DataRequired()])
twit_id = HiddenField('twit_id')
submit = SubmitField('submit', [validators.DataRequired()])
class DBHelper:
def __init__(self):
self.db = pymysql.connect(host='localhost',
user='mytwits_user',
passwd='mytwits_password',
db='mytwits')
def get_all_twits(self):
query = "select u.username, t.twit_id, t.twit, t.created_at from twits t, users u where t.user_id=u.user_id order by t.created_at desc;"
with self.db.cursor() as cursor:
cursor.execute(query)
return cursor.fetchall() # The method fetches all (or all remaining) rows of a query result set and returns a list of tuples
def get_twit(self,twit_id):
query = "select twit from twits where twit_id={}".format(twit_id)
with self.db.cursor() as cursor:
cursor.execute(query)
return cursor.fetchone()
# more detals about cursor.fetchone at
# https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-fetchone.html
def add_twit(self,twit):
query = "insert into twits (twit,user_id) values \
('{}','{}');".format(twit,1)
with self.db.cursor() as cursor:
cursor.execute(query)
return self.db.commit()
def update_twit(self,twit,twit_id):
query = "update twits set twit='{}' where twit_id='{}'"\
.format(twit,twit_id)
with self.db.cursor() as cursor:
cursor.execute(query)
return self.db.commit()
app = Flask(__name__)
db = DBHelper()
app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'
@app.route('/')
def index():
twits = db.get_all_twits()
return render_template("mytwits_mysql_4.html", twits=twits)
@app.route('/add_twit', methods = ['GET', 'POST'])
def add_twit():
form = addTwitForm()
if form.validate_on_submit():
twit = form.twit.data
db.add_twit(twit)
return redirect(vs_url_for('index'))
return render_template('add_twit_mysql_4.html',form=form)
@app.route('/edit_twit', methods = ['GET', 'POST'])
def edit_twit():
form = editTwitForm()
if request.args.get('id'):
twit_id = request.args.get('id')
twit = db.get_twit(twit_id)
form.twit.data = twit[0]
form.twit_id.data = twit_id
return render_template('edit_twit_mysql_4.html',form=form,twit=twit)
if form.validate_on_submit():
twit = form.twit.data
twit_id = form.twit_id.data
db.update_twit(twit,twit_id)
return redirect(vs_url_for('index'))
return render_template('edit_twit_mysql_4.html',form=form)
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0',port=8000)
from flask import Flask, request
from flask import render_template
from flask import redirect, url_for
import pymysql
import datetime
from flask_wtf import FlaskForm
from wtforms import StringField
from wtforms import HiddenField
from wtforms import SubmitField
from wtforms import validators
from vs_url_for import vs_url_for
class addTwitForm(FlaskForm):
twit = StringField('twit', validators = [validators.DataRequired()])
submit = SubmitField('submit', [validators.DataRequired()])
class editTwitForm(FlaskForm):
twit = StringField('twit', validators = [validators.DataRequired()])
twit_id = HiddenField('twit_id')
submit = SubmitField('submit', [validators.DataRequired()])
class DBHelper:
def __init__(self):
self.db = pymysql.connect(host='localhost',
user='mytwits_user',
passwd='mytwits_password',
db='mytwits')
def get_all_twits(self):
query = "select u.username, t.twit_id, t.twit, t.created_at from twits t, users u where t.user_id=u.user_id order by t.created_at desc;"
with self.db.cursor() as cursor:
cursor.execute(query)
return cursor.fetchall() # The method fetches all (or all remaining) rows of a query result set and returns a list of tuples
def get_twit(self,twit_id):
query = "select twit from twits where twit_id={}".format(twit_id)
with self.db.cursor() as cursor:
cursor.execute(query)
return cursor.fetchone()
# more detals about cursor.fetchone at
# https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-fetchone.html
def add_twit(self,twit):
query = "insert into twits (twit,user_id) values \
('{}','{}');".format(twit,1)
with self.db.cursor() as cursor:
cursor.execute(query)
return self.db.commit()
def update_twit(self,twit,twit_id):
query = "update twits set twit='{}' where twit_id='{}'"\
.format(twit,twit_id)
with self.db.cursor() as cursor:
cursor.execute(query)
return self.db.commit()
def delete_twit(self,twit_id):
query = "delete from twits where twit_id='{}'"\
.format(twit_id)
with self.db.cursor() as cursor:
cursor.execute(query)
return self.db.commit()
app = Flask(__name__)
db = DBHelper()
app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'
@app.route('/')
def index():
twits = db.get_all_twits()
return render_template("mytwits_mysql_5.html", twits=twits)
@app.route('/add_twit', methods = ['GET', 'POST'])
def add_twit():
form = addTwitForm()
if form.validate_on_submit():
twit = form.twit.data
db.add_twit(twit)
return redirect(vs_url_for('index'))
return render_template('add_twit_mysql_4.html',form=form)
@app.route('/edit_twit', methods = ['GET', 'POST'])
def edit_twit():
form = editTwitForm()
if request.args.get('id'):
twit_id = request.args.get('id')
twit = db.get_twit(twit_id)
form.twit.data = twit[0]
form.twit_id.data = twit_id
return render_template('edit_twit_mysql_4.html',form=form,twit=twit)
if form.validate_on_submit():
twit = form.twit.data
twit_id = form.twit_id.data
db.update_twit(twit,twit_id)
return redirect(vs_url_for('index'))
return render_template('edit_twit_mysql_4.html',form=form)
@app.route('/delete_twit', methods = ['GET', 'POST'])
def delete_twit():
if request.args.get('id'):
twit_id = request.args.get('id')
twit = db.delete_twit(twit_id)
return redirect(vs_url_for('index'))
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0',port=8000)
{% extends "base.html" %}
{% block body %}
<div class="jumbotron">
<div class="form-horizontal">
<form method="post" action="/add_twit">
<p>twit<input type='text' name='twit'>
<p><input type='submit' value='post twit'>
</form>
</div>
</div>
{% endblock %}
{% extends "base.html" %}
{% block body %}
<div class="jumbotron">
<div >
<form class="form-horizontal" method="post" action="/add_twit">
{{ form.csrf_token }}
<div class='form-group'>
<div class='col-md-6'>
{% if form.twit.errors %}
<ul class='errors'>
{% for error in form.twit.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
{{ form.twit(class='form-control',placeholder='twit') }}
</div>
</div>
<div class='form-group'>
<div class='col-md-6'>
{{ form.submit(class='btn btn-primary btn-block') }}
</div>
</div>
</form>
</div>
</div>
{% endblock %}
{% extends "base.html" %}
{% block body %}
<div class="jumbotron">
<div class="form-horizontal">
<form method="post" action="/add_twit">
<p>twit<input type='text' name='twit'>
<p><input type='submit' value='post twit'>
</form>
</div>
</div>
{% endblock %}
{% extends "base.html" %}
{% block body %}
<div class="jumbotron">
<div >
<form class="form-horizontal" method="post" action="/add_twit">
{{ form.csrf_token }}
<div class='form-group'>
<div class='col-md-6'>
{% if form.twit.errors %}
<ul class='errors'>
{% for error in form.twit.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
{{ form.twit(class='form-control',placeholder='twit') }}
</div>
</div>
<div class='form-group'>
<div class='col-md-6'>
{{ form.submit(class='btn btn-primary btn-block') }}
</div>
</div>
</form>
</div>
</div>
{% endblock %}
{% extends "base.html" %}
{% block body %}
<div class="jumbotron">
<div >
<form class="form-horizontal" method="post" action="/add_twit">
{{ form.csrf_token }}
<div class='form-group'>
<div class='col-md-6'>
{% if form.twit.errors %}
<ul class='errors'>
{% for error in form.twit.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
{{ form.twit(class='form-control',placeholder='twit') }}
</div>
</div>
<div class='form-group'>
<div class='col-md-6'>
{{ form.submit(class='btn btn-primary btn-block') }}
</div>
</div>
</form>
</div>
</div>
{% endblock %}
{% extends "base.html" %}
{% block body %}
<div class="jumbotron">
<div >
<form class="form-horizontal" method="post" action="/add_twit">
{{ form.csrf_token }}
<div class='form-group'>
<div class='col-md-6'>
{% if form.twit.errors %}
<ul class='errors'>
{% for error in form.twit.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
{{ form.twit(class='form-control',placeholder='twit') }}
</div>
</div>
<div class='form-group'>
<div class='col-md-6'>
{{ form.submit(class='btn btn-primary btn-block') }}
</div>
</div>
</form>
</div>
</div>
{% endblock %}
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">