def stations_add(): if request.method == 'POST': # inputs assumed valid functions.add_station( functions.db_connect(), request.form.get('aws_id'), request.form.get('bom_number'), request.form.get('dfw_id'), request.form.get('station_name'), request.form.get('file_name'), request.files['scm'].read(), request.form.get('owner'), request.form.get('district'), request.form.get('lon'), request.form.get('lat'), request.form.get('elevation'), request.form.get('status'), request.form.get('deploydate'), request.form.get('message') ) return Response('<h2>Inserted<h2><p>Go to the <a href="/stations">list of stations</a> to view</p>', status=200, mimetype='text/html') else: conn = functions.db_connect() districts = functions.get_districts(conn) functions.db_disconnect(conn) return render_template('stations_add.html', districts=districts)
def confirm(): conn = functions.db_connect() cursor = conn.cursor(MySQLdb.cursors.DictCursor) cursor.execute('select * from unconfirmed_users') users = cursor.fetchall() for user in users: if user["confirmationcode"] == request.args.get('code'): new_id = functions.get_new_id("users", conn, cursor) sql = "insert users (email, username, passwordhash, salt, id) values ('%s', '%s', '%s', '%s', %d)" % ( user["email"], user["username"], user["passwordhash"], user["salt"], new_id) cursor.execute(sql) sql = "delete from unconfirmed_users where email = \"%s\"" % ( user["email"]) cursor.execute(sql) conn.commit() conn.close() flash("Account confirmed successfully!") return redirect(url_for('home')) conn.close() flash("Something went wrong with your account confirmation.") return redirect(url_for('home'))
def process_password_reset(): conn = functions.db_connect() cursor = conn.cursor(MySQLdb.cursors.DictCursor) username = request.form.get('username') reset_code = ''.join( secrets.choice(string.ascii_letters + string.digits) for i in range(32)) email = None cursor.execute("select * from users") users = cursor.fetchall() for user in users: if username == user['username']: email = user['email'] sql = 'update users set reset_code = "%s", reset_time = CURRENT_TIMESTAMP where username = "******"' % ( reset_code, username) cursor.execute(sql) functions.send_password_reset_email(email, reset_code) conn.commit() conn.close() flash("Email sent!") return redirect(url_for('home')) conn.close() flash("Username not found!") return redirect(url_for('forgotpassword'))
def get_minutes_data_as_csv(owner, day): """ Get yesterday's minutes data for an owner's stations as CSV :param owner: string :param day: datetime :return: string (CSV data) """ logging.debug("call get_minutes_data_as_csv(" + owner + ', ' + day.strftime('%Y-%m-%d') + ')') # get the data sql = """ SELECT dfw_id, DATE(stamp) AS d, TIME_FORMAT(TIME(stamp), '%H:%i') AS t, airT, appT, dp, rh, deltaT, soilT, gsr, Wmin, Wavg, Wmax, Wdir, rain, leaf, canT, canRH FROM tbl_data_minutes INNER JOIN tbl_stations ON tbl_data_minutes.aws_id = tbl_stations.aws_id WHERE owner = '""" + owner + """' AND DATE(stamp) = '""" + day.strftime('%Y-%m-%d') + """' AND dfw_id IS NOT NULL ORDER BY dfw_id, stamp;""" # write it as CSV to a string output = StringIO.StringIO() fieldnames = ['dfw_id', 'd', 't', 'airT', 'appT', 'dp', 'rh', 'deltaT', 'soilT', 'gsr', 'Wmin', 'Wavg', 'Wmax', 'Wdir', 'rain', 'leaf', 'canT', 'canRH'] csv_writer = csv.DictWriter(output, delimiter=',', lineterminator='\n', quotechar='"', quoting=csv.QUOTE_NONNUMERIC, fieldnames=fieldnames) conn = functions.db_connect() csv_writer.writerows(functions.db_query(conn, sql)) functions.db_disconnect(conn) # add header header_row = ','.join([ 'DfW ID', 'Date', 'Time', 'Ave AirTemp (AWS) (degC)', 'Ave AppTemp (degC)', 'Ave DewPoint (degC)', 'Ave Humidity (AWS) (%)', 'Ave DeltaT (degC)', 'Ave Soil Temperature (degC)', 'Ave GSR (W/m^2)', 'Min WndSpd (m/s)', 'Ave WndSpd (m/s)', 'Max WndSpd (m/s)', 'Ave WndDir (deg)', 'Total Rain (mm)', 'Ave LeafWet (% Wet)', 'Ave AirTemp (Canopy) (degC)', 'Ave Humidity (Canopy) (%)']) # return CSV data as string return header_row + '\n' + output.getvalue()
def job_calc_days(day): logging.debug('ran job_calc_days(' + day.strftime('%Y-%m-%d') + ')') sql = 'CALL proc_day_calcs("' + day.strftime('%Y-%m-%d') + '")' conn = functions.db_connect() functions.db_query(conn, sql) functions.db_disconnect(conn) return [True, 'job calc_days']
def edit_picture(): recipe_id = request.form.get('recipeID') url = request.form.get('pictureURL') print(recipe_id) print(url) conn = functions.db_connect() cursor = conn.cursor(MySQLdb.cursors.DictCursor) cursor.execute('update recipes set picture = "%s" where id = %s' % (url, recipe_id)) conn.commit() conn.close() return redirect(url_for('recipes') + "?id=" + recipe_id)
def job_check_values(day, minutes_or_days): logging.debug('ran job_check_minutes_values(' + day.strftime('%Y-%m-%d') + ',' + minutes_or_days + ')') if minutes_or_days == 'minutes': timestep = 'minutes' view = 'today' sql = functions.make_check_minutes_sql(day) else: # days timestep = 'days' view = '7days' sql = functions.make_check_days_sql(day) # get the data conn = functions.db_connect() rows = functions.db_query(conn, sql) functions.db_disconnect(conn) last_owner = '' last_owner_email = '' last_owner_html = '' admin_html = '' html_header = '<h4>Errors in ' + timestep + ' readings for ' + day.strftime('%Y-%m-%d') + ':</h4>\n' table_top = '<table>\n' table_header_owner = '\t<tr><th>aws_id</th><th>Variable</th><th>Message</th></tr>\n' table_header_admin = '\t<tr><th>aws_id</th><th>Variable</th><th>Message</th><th>Owner</th></tr>\n' table_bottom = '</table>\n' for row in rows: print row # if we have a new owner... if row['owner'] != last_owner: # if last owner was a real owner, send email if last_owner != '': msg = html_header + table_top + table_header_owner + last_owner_html + table_bottom functions.gmail_send([last_owner_email], timestep + ' data errors', 'message is in html', msg) # create new owner last_owner = row['owner'] last_owner_email = row['manager_email'] last_owner_html = '' last_owner_html += '\t<tr><td><a href="' + row['station_base_url'] + '?aws_id=' + row['aws_id'] + '&view=' + view + '">' + row['aws_id'] + '</a></td><td>' + row['var'] + '</td><td>' + row['msg'] + '</td></tr>\n' admin_html += '\t<tr><td><a href="' + row['station_base_url'] + '?aws_id=' + row['aws_id'] + '&view=' + view + '">' + row['aws_id'] + '</a></td><td>' + row['var'] + '</td><td>' + row['msg'] + '</td><td>' + last_owner + '</td></tr>\n' # send to the last owner msg = html_header + table_top + table_header_owner + last_owner_html + table_bottom functions.gmail_send([last_owner_email], timestep + ' data errors', 'message is in html', msg) # send the admin email (all stations) msg = html_header + table_top + table_header_admin + admin_html + table_bottom functions.gmail_send(settings.ERROR_MSG_RECEIVERS, timestep + ' data errors', 'message is in html', msg) return
def get_minutes_data_as_csv(owner, day): """ Get yesterday's minutes data for an owner's stations as CSV :param owner: string :param day: datetime :return: string (CSV data) """ logging.debug("call get_minutes_data_as_csv(" + owner + ', ' + day.strftime('%Y-%m-%d') + ')') # get the data sql = """ SELECT dfw_id, DATE(stamp) AS d, TIME_FORMAT(TIME(stamp), '%H:%i') AS t, airT, appT, dp, rh, deltaT, soilT, gsr, Wmin, Wavg, Wmax, Wdir, rain, leaf, canT, canRH FROM tbl_data_minutes INNER JOIN tbl_stations ON tbl_data_minutes.aws_id = tbl_stations.aws_id WHERE owner = '""" + owner + """' AND DATE(stamp) = '""" + day.strftime('%Y-%m-%d') + """' AND dfw_id IS NOT NULL ORDER BY dfw_id, stamp;""" # write it as CSV to a string output = StringIO.StringIO() fieldnames = [ 'dfw_id', 'd', 't', 'airT', 'appT', 'dp', 'rh', 'deltaT', 'soilT', 'gsr', 'Wmin', 'Wavg', 'Wmax', 'Wdir', 'rain', 'leaf', 'canT', 'canRH' ] csv_writer = csv.DictWriter(output, delimiter=',', lineterminator='\n', quotechar='"', quoting=csv.QUOTE_NONNUMERIC, fieldnames=fieldnames) conn = functions.db_connect() csv_writer.writerows(functions.db_query(conn, sql)) functions.db_disconnect(conn) # add header header_row = ','.join([ 'DfW ID', 'Date', 'Time', 'Ave AirTemp (AWS) (degC)', 'Ave AppTemp (degC)', 'Ave DewPoint (degC)', 'Ave Humidity (AWS) (%)', 'Ave DeltaT (degC)', 'Ave Soil Temperature (degC)', 'Ave GSR (W/m^2)', 'Min WndSpd (m/s)', 'Ave WndSpd (m/s)', 'Max WndSpd (m/s)', 'Ave WndDir (deg)', 'Total Rain (mm)', 'Ave LeafWet (% Wet)', 'Ave AirTemp (Canopy) (degC)', 'Ave Humidity (Canopy) (%)' ]) # return CSV data as string return header_row + '\n' + output.getvalue()
def reset_password(): reset_code = request.args.get('code') conn = functions.db_connect() cursor = conn.cursor(MySQLdb.cursors.DictCursor) cursor.execute('select * from users where reset_code = "%s"' % (reset_code)) user = cursor.fetchall() conn.close() username = user[0]['username'] session['usernameforpwreset'] = username session['codeforpwreset'] = reset_code return render_template('resetpassword.html')
def signup_status(): conn = functions.db_connect() cursor = conn.cursor(MySQLdb.cursors.DictCursor) email = request.form.get('email').lower() username = request.form.get('uname') password = request.form.get('psw') confirm_password = request.form.get('confirmpsw') if password != confirm_password: flash("Passwords did not match") return redirect(url_for('signup')) cursor.execute('select * from users') users = cursor.fetchall() if (len(users) > 0): for user in users: if (user['email'].lower() == email.lower() or user['username'] == username): flash("Email/username already in use.") return redirect(url_for('signup')) #hash the current password here salt = ''.join( secrets.choice(string.ascii_letters + string.digits) for i in range(32)) salted_pass = password + salt hex_dig = hashlib.sha256(salted_pass.encode('utf-8')).hexdigest() #insert new user into users table confirmation_code = ''.join( secrets.choice(string.ascii_letters + string.digits) for i in range(32)) sql = "insert unconfirmed_users (email, username, passwordhash, salt, confirmationcode) values ('%s', '%s', '%s', '%s', '%s')" % ( email, username, hex_dig, salt, confirmation_code) #sql = "insert users (email, username, passwordhash, salt, id) values ('%s', '%s', '%s', '%s', %d)" % (email, username, hex_dig, salt, new_id) cursor.execute(sql) conn.commit() conn.close() functions.send_confirmation_email(email, confirmation_code) flash("Check your email for the confirmation code!") return redirect(url_for('home'))
def finalizepasswordreset(): password = request.form.get('password') confirm_password = request.form.get('confirmpassword') username = session['usernameforpwreset'] code = session['codeforpwreset'] session.pop('usernameforpwreset', None) session.pop('codeforpwreset', None) if password == confirm_password: conn = functions.db_connect() cursor = conn.cursor(MySQLdb.cursors.DictCursor) cursor.execute('select * from users where username = "******"' % (username)) user = cursor.fetchall() if functions.longer_than_one_day(user[0]['reset_time']): flash("Password reset has expired. Try again.") cursor.execute( 'update users set reset_time = NULL, reset_code = NULL where username = "******"' % (username)) conn.commit() conn.close() return redirect(url_for('forgotpassword')) elif user[0]['reset_code'] == code: flash("Password reset successfully") salt = ''.join( secrets.choice(string.ascii_letters + string.digits) for i in range(32)) hashed_pass = functions.hash_password(password, salt) cursor.execute( 'update users set passwordhash = "%s", salt = "%s", reset_code = NULL, reset_time = NULL where username = "******"' % (hashed_pass, salt, username)) else: flash("Reset code did not match username") conn.commit() conn.close() return redirect(url_for('home')) else: flash("Passwords do not match.") return redirect(url_for('resetpassword'))
def loggedin(): redirect_to = redirect(url_for('login')) if request.method == 'POST': conn = functions.db_connect() cursor = conn.cursor(MySQLdb.cursors.DictCursor) username = request.form.get('uname') password = request.form.get('psw') cursor.execute("select * from users") conn.close() users = cursor.fetchall() for user in users: if user['username'] == username: if functions.check_password(user, password): session['username'] = escape(request.form.get('uname')) session['logged_in'] = True redirect_to = redirect(url_for('home')) else: flash('Invalid password') return redirect_to
def completesubmission(): conn = functions.db_connect() cursor = conn.cursor(MySQLdb.cursors.DictCursor) new_recipe_id = functions.get_new_id('recipes', conn, cursor) cursor.execute('select * from users where username = "******"' % (session['username'])) userid = cursor.fetchall()[0]['id'] #check to see if name is taken cursor.execute('select * from recipes where name = "%s"' % (request.form.get('recipeName'))) if (len(cursor.fetchall()) > 0): flash("That recipe name is already in use.") return redirect(url_for('submit')) #deal with categories (if we predefine categories, we can take this out) category = request.form.get("category") cursor.execute('select * from categories where name = "%s"' % (category)) categories = cursor.fetchall() if (len(categories) == 0): category_id = functions.get_new_id('categories', conn, cursor) cursor.execute('''insert categories (id, name) values (%s, "%s")''' % (category_id, category)) else: category_id = categories[0]['id'] #build record for recipe name = request.form.get("recipeName") prep_time = request.form.get("prepTime") cook_time = request.form.get("cookTime") description = request.form.get("description") cursor.execute('''insert recipes (id, name, preptime, cooktime, category, creatorid, description) values (%s, "%s", %s, %s, "%s", %s, "%s") ''' % (new_recipe_id, name, prep_time, cook_time, category_id, userid, description)) #build ingredients tables ingredient_counter = request.form.get('ingredientCounter') for i in range(0, int(ingredient_counter)): #make this a new function ingredient = request.form.get("ingredients[" + str(i) + "]") cursor.execute('select * from ingredients where name = "%s"' % (ingredient)) matching_ingredient = cursor.fetchall() if (len(matching_ingredient) == 0): #create new ingredient record ingredient_id = functions.get_new_id('ingredients', conn, cursor) cursor.execute('''insert ingredients (id, name) values (%s, "%s")''' % (ingredient_id, ingredient)) else: #ingredient already exists ingredient_id = matching_ingredient[0]['id'] cursor.execute( 'insert recipe_ingredients (recipe, ingredient) values (%s, %s)' % (new_recipe_id, ingredient_id)) #build instructions table instructions_counter = request.form.get('instructionCounter') for i in range(0, int(instructions_counter)): instruction = request.form.get("instructions[" + str(i) + "]") cursor.execute( '''insert instructions (recipe, instructionnumber, instruction) values (%s, %s, "%s")''' % (new_recipe_id, i, instruction)) conn.commit() conn.close() #eventually route user to the newly created recipe page return redirect(url_for('recipes') + "?id=%s" % (new_recipe_id))
def job_check_values(day, minutes_or_days): logging.debug('ran job_check_minutes_values(' + day.strftime('%Y-%m-%d') + ',' + minutes_or_days + ')') if minutes_or_days == 'minutes': timestep = 'minutes' view = 'today' sql = functions.make_check_minutes_sql(day) else: # days timestep = 'days' view = '7days' sql = functions.make_check_days_sql(day) # get the data conn = functions.db_connect() rows = functions.db_query(conn, sql) functions.db_disconnect(conn) last_owner = '' last_owner_email = '' last_owner_html = '' admin_html = '' html_header = '<h4>Errors in ' + timestep + ' readings for ' + day.strftime( '%Y-%m-%d') + ':</h4>\n' table_top = '<table>\n' table_header_owner = '\t<tr><th>aws_id</th><th>Variable</th><th>Message</th></tr>\n' table_header_admin = '\t<tr><th>aws_id</th><th>Variable</th><th>Message</th><th>Owner</th></tr>\n' table_bottom = '</table>\n' for row in rows: print row # if we have a new owner... if row['owner'] != last_owner: # if last owner was a real owner, send email if last_owner != '': msg = html_header + table_top + table_header_owner + last_owner_html + table_bottom functions.gmail_send([last_owner_email], timestep + ' data errors', 'message is in html', msg) # create new owner last_owner = row['owner'] last_owner_email = row['manager_email'] last_owner_html = '' last_owner_html += '\t<tr><td><a href="' + row[ 'station_base_url'] + '?aws_id=' + row[ 'aws_id'] + '&view=' + view + '">' + row[ 'aws_id'] + '</a></td><td>' + row[ 'var'] + '</td><td>' + row['msg'] + '</td></tr>\n' admin_html += '\t<tr><td><a href="' + row[ 'station_base_url'] + '?aws_id=' + row[ 'aws_id'] + '&view=' + view + '">' + row[ 'aws_id'] + '</a></td><td>' + row[ 'var'] + '</td><td>' + row[ 'msg'] + '</td><td>' + last_owner + '</td></tr>\n' # send to the last owner msg = html_header + table_top + table_header_owner + last_owner_html + table_bottom functions.gmail_send([last_owner_email], timestep + ' data errors', 'message is in html', msg) # send the admin email (all stations) msg = html_header + table_top + table_header_admin + admin_html + table_bottom functions.gmail_send(settings.ERROR_MSG_RECEIVERS, timestep + ' data errors', 'message is in html', msg) return
def stations(): conn = functions.db_connect() stations = functions.get_stations(conn) functions.db_disconnect(conn) return render_template('stations.html', stations=stations)
def job_check_latest_readings(): logging.debug('ran job_check_latest_readings()') sql = ''' SELECT m.aws_id, m.name, m.owner, m.manager_email, m.station_base_url FROM ( SELECT a.aws_id AS aws_id, a.name, a.owner, a.manager_email, a.station_base_url, b.aws_id AS other FROM ( SELECT aws_id, NAME, OWNER, manager_email, station_base_url FROM tbl_stations INNER JOIN tbl_owners ON tbl_stations.owner = tbl_owners.owner_id WHERE STATUS = 'on' ORDER BY aws_id) AS a LEFT JOIN (SELECT DISTINCT aws_id FROM tbl_data_minutes WHERE DATE(stamp) = CURDATE()) AS b ON a.aws_id = b.aws_id HAVING b.aws_id IS NULL) AS m ORDER BY OWNER, aws_id; ''' # get the data conn = functions.db_connect() rows = functions.db_query(conn, sql) functions.db_disconnect(conn) last_owner = '' last_owner_email = '' last_owner_html = '' admin_html = '' html_header = '<h4>Stations that are on but have failed to report today:</h4>\n' table_top = '<table>\n' table_header_owner = '\t<tr><th>aws_id</th><th>Name</th></tr>\n' table_header_admin = '\t<tr><th>aws_id</th><th>Name</th><th>Owner</th></tr>\n' table_bottom = '</table>\n' for row in rows: print row # if we have a new owner... if row['owner'] != last_owner: # if last owner was a real owner, send email if last_owner != '': msg = html_header + table_top + table_header_owner + last_owner_html + table_bottom functions.gmail_send([last_owner_email], 'stations failing to report today', 'message is in html', msg) # create new owner last_owner = row['owner'] last_owner_email = row['manager_email'] last_owner_html = '' last_owner_html += '\t<tr><td><a href="' + row['station_base_url'] + '?aws_id=' + row['aws_id'] + '">' + row['aws_id'] + '</a></td><td>' + row['name'] + '</td></tr>\n' admin_html += '\t<tr><td><a href="' + row['station_base_url'] + '?aws_id=' + row['aws_id'] + '">' + row['aws_id'] + '</a></td><td>' + row['name'] + '</td><td>' + last_owner + '</td></tr>\n' # send to the last owner msg = html_header + table_top + table_header_owner + last_owner_html + table_bottom functions.gmail_send([last_owner_email], 'stations failing to report today', 'message is in html', msg) # send the admin email (all stations) msg = html_header + table_top + table_header_admin + admin_html + table_bottom functions.gmail_send(settings.ERROR_MSG_RECEIVERS, 'stations failing to report today', 'message is in html', msg) return
import functions as func db = func.db_connect() cursor = db.cursor() print(func.list_subnets(cursor))
def station(aws_id): conn = functions.db_connect() station = functions.get_station(conn, aws_id) functions.db_disconnect(conn) return render_template('station.html', station=station)
import json from functions import db_connect, get_api_call, get_collection, insert_data url = "https://api.yelp.com/v3/businesses/search?term=restaurants&location=Dallas&limit=50" payload = {} response = get_api_call(url, payload) businesses = json.loads(response.text)['businesses'] client = db_connect() id_list = insert_data(client, 'Businesses', businesses) print(id_list) for id in id_list: url = "https://api.yelp.com/v3/businesses/" + str(id) + "/reviews" payload = {} response = get_api_call(url, payload) reviews = json.loads(response.text)['reviews'] id_list = insert_data(client, 'Reviews', reviews)
def job_check_latest_readings(): logging.debug('ran job_check_latest_readings()') sql = ''' SELECT m.aws_id, m.name, m.owner, m.manager_email, m.station_base_url FROM ( SELECT a.aws_id AS aws_id, a.name, a.owner, a.manager_email, a.station_base_url, b.aws_id AS other FROM ( SELECT aws_id, NAME, OWNER, manager_email, station_base_url FROM tbl_stations INNER JOIN tbl_owners ON tbl_stations.owner = tbl_owners.owner_id WHERE STATUS = 'on' ORDER BY aws_id) AS a LEFT JOIN (SELECT DISTINCT aws_id FROM tbl_data_minutes WHERE DATE(stamp) = CURDATE()) AS b ON a.aws_id = b.aws_id HAVING b.aws_id IS NULL) AS m ORDER BY OWNER, aws_id; ''' # get the data conn = functions.db_connect() rows = functions.db_query(conn, sql) functions.db_disconnect(conn) last_owner = '' last_owner_email = '' last_owner_html = '' admin_html = '' html_header = '<h4>Stations that are on but have failed to report today:</h4>\n' table_top = '<table>\n' table_header_owner = '\t<tr><th>aws_id</th><th>Name</th></tr>\n' table_header_admin = '\t<tr><th>aws_id</th><th>Name</th><th>Owner</th></tr>\n' table_bottom = '</table>\n' for row in rows: print row # if we have a new owner... if row['owner'] != last_owner: # if last owner was a real owner, send email if last_owner != '': msg = html_header + table_top + table_header_owner + last_owner_html + table_bottom functions.gmail_send([last_owner_email], 'stations failing to report today', 'message is in html', msg) # create new owner last_owner = row['owner'] last_owner_email = row['manager_email'] last_owner_html = '' last_owner_html += '\t<tr><td><a href="' + row[ 'station_base_url'] + '?aws_id=' + row['aws_id'] + '">' + row[ 'aws_id'] + '</a></td><td>' + row['name'] + '</td></tr>\n' admin_html += '\t<tr><td><a href="' + row[ 'station_base_url'] + '?aws_id=' + row['aws_id'] + '">' + row[ 'aws_id'] + '</a></td><td>' + row[ 'name'] + '</td><td>' + last_owner + '</td></tr>\n' # send to the last owner msg = html_header + table_top + table_header_owner + last_owner_html + table_bottom functions.gmail_send([last_owner_email], 'stations failing to report today', 'message is in html', msg) # send the admin email (all stations) msg = html_header + table_top + table_header_admin + admin_html + table_bottom functions.gmail_send(settings.ERROR_MSG_RECEIVERS, 'stations failing to report today', 'message is in html', msg) return