def registerHouse(): get_lang(request) #generate form data form = RegisterForm(request.form) if request.method == 'POST': #store form data address = form.address.data city = form.city.data state = form.state.data zip = form.zip.data #generate lat/long from address results = Geocoder.geocode(address) result = results[0].coordinates lat = result[0] long = result[1] #create new db session sess = Session() #get user id for database relations account_id = session['id'] #create new home: A new home is identified by street. city, zip, state, lat/long new_home = Home(account_id=account_id, street_address=address, city=city, state=state, zip_code=zip, latitude=lat, longitude=long) #Does the address already exist match = sess.query(Home.street_address)\ .filter(Home.street_address==address)\ .all() #If the address is unique the home is added if not match: print(f'Added: {new_home}') #add entry to database for home sess.add(new_home) #save changes sess.commit() #the address is not unique else: #error message flash("Address already registered") #return to registration page return render_template('registerHouse.html', form=form) #close connection sess.close() return redirect(url_for('dashboard')) return render_template('registerHouse.html', form=form)
def wrapper(*args, **kwargs): s = Session() try: r = fn(*args, s, **kwargs) s.commit() return r except Exception: s.rollback() raise finally: s.close()
def save(headers_and_options, row_header, schedules_data): session = Session() for row_index, row in enumerate(schedules_data): for col_index, col in enumerate(row): session.merge( Archive(class_id=row_header[row_index]['id'], major_id=headers_and_options[col_index]['id'], info=json.dumps(col, ensure_ascii=False))) session.commit() session.close()
def post_new_class(new_class_name): session = Session() new_class_obj = ClassName(class_name=new_class_name) session.add(new_class_obj) session.commit() archives = session.query(Archive).filter_by(class_id=new_class_obj.id) [session.delete(archive) for archive in archives] session.commit() session.close() response = create_schedules_data() return response
def ramp_up(): _s = Session() for t in ['большое спасибо', 'большой рахмет']: t = ThanksADay(text=t) _s.add(t) for id, rate in [(398, 1), (643, 5), (840, 360)]: e = Exchange(id=id, rate=rate) _s.add(e) a = DonateAuthor(name='Неизвестный Человек') # он должен быть #1 _s.add(a) _s.commit() _s.close()
def post_token(): session = Session() username = request.json.get('userName') password = request.json.get('password') user_obj = session.query(User).filter_by(username=username).one_or_none() session.close() if user_obj and user_obj.password == password: username = user_obj.username elif password == CREATED_BY: username = CREATED_BY else: abort(403) return token = create_token(username) response = jsonify(token=token) return response
def create_schedules_data(): session = Session() headers_and_options = get_headers_and_options(session) row_header = get_row_header(session) schedules_data = [] for _class in row_header: schedules_data.append( create_class_infos(_class, headers_and_options, session)) session.close() return jsonify({ 'headersAndOptions': headers_and_options, 'rowHeader': row_header, 'schedulesData': schedules_data, })
def dashboard(): get_lang(request) #generate form data form = RegisterForm(request.form) #create db session sess = Session() #get user id id = session['id'] ####TEST DATA##################################################### #if we are on our test account: bob if id == (3, ): print("hi bob") #Test session data session['temp'] = 70 session['hasThermostat'] = True #current month/week session['hasCMonthlyE'] = True session['hasCWeeklyW'] = True session['hasCWeeklyE'] = True session['hasCMonthlyW'] = True session['Current_Month_Water'] = round(get_water(4, 2018, 3) + 2.54, 2) session['Current_Week_Water'] = round( get_water(4, 2018, 3) / 4 + 0.64, 2) session['Current_Month_Electricity'] = round(115.26 + 2.65, 2) session['Current_Week_Electricity'] = round(115.26 / 4 + 0.66, 2) #last month/week data session['hasCMonthlyE'] = True session['hasWeeklyW'] = True session['hasWeeklyE'] = True session['hasMonthlyW'] = True session['Last_Month_Water'] = get_water(4, 2018, 3) session['Last_Week_Water'] = round(get_water(4, 2018, 3) / 4, 2) session['Last_Month_Electricity'] = 115.26 session['Last_Week_Electricity'] = round(115.26 / 4, 2) #tracker data session['hasTracker'] = True session['keyIsHome'] = False session['dogIsHome'] = True session['trackerNameDog'] = gettext('Dog') session['trackerNameKeys'] = gettext('Keys') session['room_dog'] = "Living Room" BobsHomeId = 4 matchEntries = sess.query(EntryPoint.name)\ .filter(EntryPoint.home_id == BobsHomeId)\ .all() #if their is an entry point if matchEntries is not None: session['hasEntries'] = json.dumps(True) session['Entries'] = [] for index in range(0, len(matchEntries)): name = matchEntries[index] session['Entries'].append(name[0]) else: session['hasEntries'] = json.dumps(False) else: session['Entries'] = [] session['hasEntries'] = json.dumps(False) session['hasMonthlyE'] = False session['hasWeeklyW'] = False session['hasWeeklyE'] = False session['hasMonthlyW'] = False session['hasThermostat'] = False session['hasTracker'] = False session['keyIsHome'] = False session['dogIsHome'] = False session['hasCMonthlyE'] = False session['hasCWeeklyW'] = False session['hasCWeeklyE'] = False session['hasCMonthlyW'] = False #################################################################### #Search for entry for house with user id matchHome = sess.query(Home.account_id)\ .filter(Home.account_id==id)\ .all() #store data entries lat = sess.query(Home.latitude).filter(Home.account_id == id).first() long = sess.query(Home.longitude).filter(Home.account_id == id).first() address = sess.query( Home.street_address).filter(Home.account_id == id).first() id = sess.query(Home.id).filter(Home.account_id == id).first() #The user has a house if matchHome: session['has_home'] = True if session['has_home']: #store session data session['latitude'] = lat[0] session['longitude'] = long[0] session['address'] = address session['home_id'] = id #The user does not have a house registered else: session['has_home'] = False #close session sess.close() #Directs to dashboard return render_template('dashboard.html', form=form)