Commit b901b7f1 authored by danmcquillan's avatar danmcquillan

lab-15

parent 2a9aa3b8
import pymysql
import datetime
class DBHelper:
def __init__(self):
self.db = pymysql.connect(host='localhost',
user='root',
passwd='lol1649',
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=%s"
with self.db.cursor() as cursor:
cursor.execute(query, twit_id)
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 \
(%s,%s);"
with self.db.cursor() as cursor:
cursor.execute(query, (twit,1))
return self.db.commit()
def update_twit(self,twit,twit_id):
query = "update twits set twit=%s where twit_id=%s"
with self.db.cursor() as cursor:
cursor.execute(query,(twit,twit_id))
return self.db.commit()
def delete_twit(self,twit_id):
query = "delete from twits where twit_id=%s"
with self.db.cursor() as cursor:
cursor.execute(query, twit_id)
return self.db.commit()
def check_password(self,username,password):
query = "select user_id from users where username = %s and password=%s;"
with self.db.cursor() as cursor:
cursor.execute(query,(username,password))
return cursor.fetchone()
# This method retrieves the next row of a query result set and
# returns a single sequence, or None if no more rows are available.
# By default, the returned tuple consists of data returned by the
# MySQL server, converted to Python objects
# https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-fetchone.html
import pymysql
import datetime
import hashlib
class DBHelper:
def __init__(self):
self.db = pymysql.connect(host='localhost',
user='mytwits_user',
passwd='mytwits_password',
db='mytwits')
def get_user(self, user_id):
query = "select * from users where user_id=%s"
with self.db.cursor() as cursor:
cursor.execute(query,user_id)
return cursor.fetchone()
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=%s"
with self.db.cursor() as cursor:
cursor.execute(query, twit_id)
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,user_id):
query = "insert into twits (twit,user_id) values \
(%s,%s);"
with self.db.cursor() as cursor:
cursor.execute(query, (twit,user_id))
return self.db.commit()
def update_twit(self,twit,twit_id):
query = "update twits set twit=%s where twit_id=%s"
with self.db.cursor() as cursor:
cursor.execute(query,(twit,twit_id))
return self.db.commit()
def delete_twit(self,twit_id):
query = "delete from twits where twit_id=%s"
with self.db.cursor() as cursor:
cursor.execute(query, twit_id)
return self.db.commit()
def check_password(self,username,password):
query = "select user_id, salt, hashed from users where username = %s;"
with self.db.cursor() as cursor:
cursor.execute(query,(username))
user = cursor.fetchone()
if user:
user_id, salt, hashed = user
if hashlib.sha512((salt + password).encode('utf-8')).hexdigest() == hashed:
return user_id
return None
import pymysql
import datetime
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=%s"
with self.db.cursor() as cursor:
cursor.execute(query, twit_id)
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 \
(%s,%s);"
with self.db.cursor() as cursor:
cursor.execute(query, (twit,1))
return self.db.commit()
def update_twit(self,twit,twit_id):
query = "update twits set twit=%s where twit_id=%s"
with self.db.cursor() as cursor:
cursor.execute(query,(twit,twit_id))
return self.db.commit()
def delete_twit(self,twit_id):
query = "delete from twits where twit_id=%s"
with self.db.cursor() as cursor:
cursor.execute(query, twit_id)
return self.db.commit()
def check_password(self,username,password):
query = "select user_id from users where username = %s and password=%s;"
with self.db.cursor() as cursor:
cursor.execute(query,(username,password))
return cursor.fetchone()
# This method retrieves the next row of a query result set and
# returns a single sequence, or None if no more rows are available.
# By default, the returned tuple consists of data returned by the
# MySQL server, converted to Python objects
# https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-fetchone.html
import pymysql
import datetime
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=%s"
with self.db.cursor() as cursor:
cursor.execute(query, twit_id)
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,user_id):
query = "insert into twits (twit,user_id) values \
(%s,%s);"
with self.db.cursor() as cursor:
cursor.execute(query, (twit,user_id))
return self.db.commit()
def update_twit(self,twit,twit_id):
query = "update twits set twit=%s where twit_id=%s"
with self.db.cursor() as cursor:
cursor.execute(query,(twit,twit_id))
return self.db.commit()
def delete_twit(self,twit_id):
query = "delete from twits where twit_id=%s"
with self.db.cursor() as cursor:
cursor.execute(query, twit_id)
return self.db.commit()
def check_password(self,username,password):
query = "select user_id from users where username = %s and password=%s;"
with self.db.cursor() as cursor:
user_id = None
cursor.execute(query,(username,password))
return cursor.fetchone()
import pymysql
import datetime
class DBHelper:
def __init__(self):
self.db = pymysql.connect(host='localhost',
user='mytwits_user',
passwd='mytwits_password',
db='mytwits')
def get_user(self, user_id):
query = "select * from users where user_id=%s"
with self.db.cursor() as cursor:
cursor.execute(query,user_id)
return cursor.fetchone()
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=%s"
with self.db.cursor() as cursor:
cursor.execute(query, twit_id)
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,user_id):
query = "insert into twits (twit,user_id) values \
(%s,%s);"
with self.db.cursor() as cursor:
cursor.execute(query, (twit,user_id))
return self.db.commit()
def update_twit(self,twit,twit_id):
query = "update twits set twit=%s where twit_id=%s"
with self.db.cursor() as cursor:
cursor.execute(query,(twit,twit_id))
return self.db.commit()
def delete_twit(self,twit_id):
query = "delete from twits where twit_id=%s"
with self.db.cursor() as cursor:
cursor.execute(query, twit_id)
return self.db.commit()
def check_password(self,username,password):
query = "select user_id from users where username = %s and password=%s;"
with self.db.cursor() as cursor:
user_id = None
cursor.execute(query,(username,password))
return cursor.fetchone()
from flask_wtf import FlaskForm
from wtforms import StringField
from wtforms import HiddenField
from wtforms import PasswordField
from wtforms import SubmitField
from wtforms import validators
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 loginForm(FlaskForm):
username = StringField('username', validators = [validators.DataRequired()])
password = PasswordField('password', validators =[validators.DataRequired()])
password2 = PasswordField('password2', validators=[validators.DataRequired(),
validators.EqualTo('password', message='Passwords must match')])
submit = SubmitField('submit', [validators.DataRequired()])
from flask_wtf import FlaskForm
from wtforms import StringField
from wtforms import HiddenField
from wtforms import PasswordField
from wtforms import SubmitField
from wtforms import validators
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 loginForm(FlaskForm):
username = StringField('username', validators = [validators.DataRequired()])
password = PasswordField('password', validators =[validators.DataRequired()])
password2 = PasswordField('password2', validators=[validators.DataRequired(),
validators.EqualTo('password', message='Passwords must match')])
submit = SubmitField('submit', [validators.DataRequired()])
from flask_wtf import FlaskForm
from wtforms import StringField
from wtforms import HiddenField
from wtforms import PasswordField
from wtforms import SubmitField
from wtforms import validators
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 loginForm(FlaskForm):
username = StringField('username', validators = [validators.DataRequired()])
password = PasswordField('password', validators =[validators.DataRequired()])
password2 = PasswordField('password2', validators=[validators.DataRequired(),
validators.EqualTo('password', message='Passwords must match')])
submit = SubmitField('submit', [validators.DataRequired()])
from flask_wtf import FlaskForm
from wtforms import StringField
from wtforms import HiddenField
from wtforms import PasswordField
from wtforms import SubmitField
from wtforms import validators
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 loginForm(FlaskForm):
username = StringField('username', validators = [validators.DataRequired()])
password = PasswordField('password', validators =[validators.DataRequired()])
password2 = PasswordField('password2', validators=[validators.DataRequired(),
validators.EqualTo('password', message='Passwords must match')])
submit = SubmitField('submit', [validators.DataRequired()])
from flask_wtf import FlaskForm
from wtforms import StringField
from wtforms import HiddenField
from wtforms import PasswordField
from wtforms import SubmitField
from wtforms import validators
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 loginForm(FlaskForm):
username = StringField('username', validators = [validators.DataRequired()])
password = PasswordField('password', validators =[validators.DataRequired()])
password2 = PasswordField('password2', validators=[validators.DataRequired(),
validators.EqualTo('password', message='Passwords must match')])
submit = SubmitField('submit', [validators.DataRequired()])
from flask import Flask, render_template
from flask import request, make_response
import feedparser
import datetime
app = Flask(__name__)
RSS_FEEDS = { 'bbc': 'http://feeds.bbci.co.uk/news/rss.xml',
'aljazeera' : 'https://www.aljazeera.com/xml/rss/all.xml',
'ap':\
'http://hosted2.ap.org/atom/APDEFAULT/cae69a7523db45408eeb2b3a98c0c9c5'}
@app.route("/")
def headlines():
publication =''
if request.cookies.get('publication'):
publication = request.cookies.get('publication')
if request.args.get('publication'):
publication = request.args.get('publication')
print publication
if not publication or publication.lower() not in RSS_FEEDS:
publication = "bbc"
else:
publication = publication.lower()
print(publication)
feed = feedparser.parse(RSS_FEEDS[publication])
articles = feed['entries']
response =\
make_response(render_template('get_headlines.html',articles=articles))
expires = datetime.datetime.now() + datetime.timedelta(days=1)
response.set_cookie("publication", publication, expires=expires)
return response
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
from flask import session, flash, abort
from vs_url_for import vs_url_for
from forms_10 import addTwitForm, editTwitForm, loginForm
from dbhelper_10 import DBHelper
from flask_login import LoginManager, login_required
from flask_login import login_user, logout_user
from user import User
login_manager = LoginManager()
app = Flask(__name__)
db = DBHelper()
login_manager.init_app(app)
app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'
@login_manager.user_loader
def load_user(user_id):
result = db.get_user(user_id)
if result:
return User(user_id)
@app.route('/')
def index():
twits = db.get_all_twits()
return render_template("mytwits_mysql_6.html", twits=twits)
@app.route('/add_twit', methods = ['GET', 'POST'])
@login_required
def add_twit():
form = addTwitForm()
if form.validate_on_submit():
twit = form.twit.data
db.add_twit(twit,session['user_id'])
return redirect(vs_url_for('index'))
return render_template('add_twit_mysql_6.html',form=form)
@app.route('/edit_twit', methods = ['GET', 'POST'])
@login_required
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_6.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_6.html',form=form)
@app.route('/delete_twit', methods = ['GET', 'POST'])
@login_required
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'))
@app.route('/login', methods = ['GET', 'POST'])
def login():
form = loginForm()
if form.validate_on_submit():
password = form.password.data
username = form.username.data
user_id = db.check_password(username,password)
if user_id:
user = User(user_id)
login_user(user)
flash('login successful!')
return redirect(vs_url_for('index'))
else:
flash('login unsuccessful!')
return render_template('login.html',form=form)
@app.route('/logout')
def logout():
# remove the username from the session if it's there
logout_user()
return redirect(vs_url_for('index'))
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='root',
passwd='lol1649',
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=%s"
with self.db.cursor() as cursor:
cursor.execute(query, twit_id)
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 \
(%s,%s);"
with self.db.cursor() as cursor:
cursor.execute(query, (twit,1))
return self.db.commit()
def update_twit(self,twit,twit_id):
query = "update twits set twit=%s where twit_id=%s"
with self.db.cursor() as cursor:
cursor.execute(query,(twit,twit_id))
return self.db.commit()
def delete_twit(self,twit_id):
query = "delete from twits where twit_id=%s"
with self.