def comany_and_address(lat, long): # md = MetaDataFromCoordinates(lat, long) # return md.get_name(), md.get_address() with session_scope() as session: fc = session.query(FuelCompany).first() gs = (session.query(GasStation).filter( GasStation.fuel_company_connection == fc).first()) return fc.fuel_company_name, gs.address
def sign_up(): if current_user.is_authenticated: return redirect(url_for('ui.prices')) form = SignUpForm(meta={'csrf': False}) if form.validate_on_submit(): user = User(username=form.username.data) user.set_password(form.password.data) with session_scope() as session: session.add(user) session.commit() flash("Registration successful. Please, sign in now.") return redirect(url_for('auth.sign_in')) return render_template('auth/register.html', form=form)
def sign_in(): if current_user.is_authenticated: return redirect(url_for('ui.prices')) form = SignInForm(meta={'csrf': False}) if form.validate_on_submit(): with session_scope() as session: user = (session.query(User).filter( User.username == form.username.data).first()) if user is None or not user.check_password(form.password.data): flash("Invalid username and/or password") return redirect(url_for('auth.sign_in')) login_user(user, remember=form.remember_me.data) next_page = request.args.get('next') if not next_page or url_parse(next_page).netloc != '': next_page = url_for('ui.prices') return redirect(next_page) return render_template('auth/login.html', form=form)
def fixture(): """fixture""" from database.models import Fuel, FuelCompany from database.queries import session_scope with session_scope() as session: for company_name in ['Юкон', 'Брсм']: session.add(FuelCompany(fuel_company_name=company_name)) session.commit() for fuel, is_premium in [('95+', False), ('95E', True), ('92', False), ('ДП+', False), ('ДП', False), ('ГАЗ', False), ('95', False)]: session.add(Fuel(fuel_type=fuel, is_premium=is_premium)) session.commit()
def prices(): with session_scope() as session: price_list = query_to_dict(query_all_price_period(session)) price_list = list(dict(price_list).values()) date_to_str = lambda d: f'{d.month}/{d.day}/{d.year}' attr_getter = lambda x: (x['fuel_company_name'], date_to_str(x['date_of_price'])) companies_and_dates = map(attr_getter, price_list) d = {k: [] for k in dict.fromkeys(companies_and_dates)} fields = ['date_of_price', 'fuel_company_name', 'fuel_type', 'price'] for record in price_list: date, company, fuel_type, price = [record[f] for f in fields] date = date_to_str(date) d[(company, date)].append((fuel_type, price)) price_list = [{ 'fuel_company_name': company, 'fuel': [{ 'fuel_type': d, 'price': p } for d, p in fuels], 'date_of_price': date } for (company, date), fuels in d.items()] companies_list = list_fuel_company_names(session) gas_stations_query = query_to_dict(query_all_gas_stations(session)) gas_stations = [] for i in gas_stations_query.values(): gas_stations.append((i['fuel_company_name'], i['address'])) gas_stations = sorted(gas_stations) form = SendPhotoForm(meta={'csrf': False}) form.company.choices = [(item, item) for item in companies_list] form.gas_station.choices = [(item, item) for item in gas_stations] if form.validate_on_submit(): photo = form.photo.data filename = secure_filename(photo.filename) upload_image_to_dbx(photo, ('/telegram_files/' + filename)) flash('Thanks for uploading photo') return redirect(url_for('ui.prices')) return render_template('main/main.html', price_list=price_list, form=form)
def load_user(uid): with session_scope() as session: return session.query(User).filter(User.id == uid).first()
def prices(): price_list = [ { 'name': 'Okko', 'fuel': [('A-92 ', 27.00), ('A-95 ', 28.00), ('A-95+', 30.00), ('propane', 12.00)], 'datetime': '03.03.19 12:45', }, { 'name': 'WOG', 'fuel': [('A-92 ', 25.00), ('A-95 ', 26.00), ('A-95+', 28.00), ('propane', 10.00)], 'datetime': '02.03.19 13:18', }, { 'name': 'Okko', 'fuel': [('A-92 ', 27.00), ('A-95 ', 28.00), ('A-95+', 30.00), ('propane', 12.00)], 'datetime': '03.03.19 12:45', }, { 'name': 'WOG', 'fuel': [('A-92 ', 25.00), ('A-95 ', 26.00), ('A-95+', 28.00), ('propane', 10.00)], 'datetime': '02.03.19 13:18', }, { 'name': 'Okko', 'fuel': [('A-92 ', 27.00), ('A-95 ', 28.00), ('A-95+', 30.00), ('propane', 12.00)], 'datetime': '03.03.19 12:45', }, { 'name': 'WOG', 'fuel': [('A-92 ', 25.00), ('A-95 ', 26.00), ('A-95+', 28.00), ('propane', 10.00)], 'datetime': '02.03.19 13:18', }, { 'name': 'Okko', 'fuel': [('A-92 ', 27.00), ('A-95 ', 28.00), ('A-95+', 30.00), ('propane', 12.00)], 'datetime': '03.03.19 12:45', }, { 'name': 'WOG', 'fuel': [('A-92 ', 25.00), ('A-95 ', 26.00), ('A-95+', 28.00), ('propane', 10.00)], 'datetime': '02.03.19 13:18', }, { 'name': 'Okko', 'fuel': [('A-92 ', 27.00), ('A-95 ', 28.00), ('A-95+', 30.00), ('propane', 12.00)], 'datetime': '03.03.19 12:45', }, { 'name': 'WOG', 'fuel': [('A-92 ', 25.00), ('A-95 ', 26.00), ('A-95+', 28.00), ('propane', 10.00)], 'datetime': '02.03.19 13:18', }, ] with session_scope() as session: price_list = query_to_dict(query_all_price_period(session)) price_list = list(dict(price_list).values()) date_to_str = lambda d: f'{d.month}/{d.day}/{d.year}' attr_getter = lambda x: (x['fuel_company_name'], date_to_str(x['date_of_price'])) companies_and_dates = map(attr_getter, price_list) d = {k: [] for k in dict.fromkeys(companies_and_dates)} fields = ['date_of_price', 'fuel_company_name', 'fuel_type', 'price'] for record in price_list: date, company, fuel_type, price = [record[f] for f in fields] date = date_to_str(date) d[(company, date)].append((fuel_type, price)) price_list = [{'fuel_company_name': company, 'fuel': [{'date': d, 'price': p} for d, p in fuels], 'date_of_price': date} for (company, date), fuels in d.items()] form = SendPhotoForm(meta={'csrf': False}) if form.validate_on_submit(): photo = form.photo.data filename = secure_filename(photo.filename) upload_image_to_dbx(photo, ('/telegram_files/' + filename)) flash('Thanks for uploading photo') return redirect(url_for('ui.prices')) return render_template('main/main.html', price_list=price_list, form=form)
def validate_username(self, username): with session_scope() as session: user = (session.query(User).filter( User.username == username.data).first()) if user is not None: raise ValidationError('User with such name already exists.')