コード例 #1
0
ファイル: restful.py プロジェクト: tinkerer93/Stella
 def get(self):
     """Returns all prices for gas station at particular date (if date is omit at max date with info)."""
     try:
         longitude = float(request.args.get("longitude"))
         latitude = float(request.args.get("latitude"))
     except ValueError:
         return make_response_json({
             'status': 'fail',
             'reason': 'wrong longitude or latitude'
         })
     companies = gasStationInfo(longitude, latitude)
     if len(companies) > 0:
         company_name = companies[0]['name']
         company_address = companies[0]['adress']
         try:
             get_date_param(request, 'date_of_price')
         except ValueError:
             return make_response_json({
                 'status': 'fail',
                 'reason': 'wrong date'
             })
         result = query_by_station_current_date(
             session, company_name, company_address,
             get_date_param(request, 'date_of_price'))
         result_dict = helpers.query_to_dict(result)
     else:
         result_dict = {}
     return make_response_json({'status': 'ok', 'data': result_dict})
コード例 #2
0
ファイル: ui.py プロジェクト: tinkerer93/Stella
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)
コード例 #3
0
 def get(self):
     """Returns average prices for all gas station at particular date (if date is omit at max date with info)."""
     try:
         get_date_param(request, 'date_of_price')
     except ValueError:
         return make_response_json({
             'status': 'fail',
             'reason': 'wrong date'
         })
     result = query_avg_all_stations(session, get_date_param(request, 'date_of_price'))
     result_dict = helpers.query_to_dict(result)
     return make_response_json({'status': 'ok', 'data': result_dict})
コード例 #4
0
def avg_price():
    if request.headers['Content-Type'] == 'application/json':
        request_data = request.get_json()
    date_of_price = request_data["date_of_price"]
    result = query_avg_all_stations(session, date_of_price)
    result_dict = helpers.query_to_dict(result)
    json_data = json.dumps(result_dict,
                           default=helpers.to_serializable,
                           ensure_ascii=False)
    resp = make_response(json_data)
    resp.mimetype = 'application/json'
    return resp
コード例 #5
0
 def get(self):
     """Returns all prices for period (if dates are omit for last 10 days)."""
     try:
         get_date_param(request, 'date_from')
         get_date_param(request, 'date_to')
     except ValueError:
         return make_response_json({
             'status': 'fail',
             'reason': 'wrong date'
         })
     result = query_all_price_period(session, get_date_param(request, 'date_from'),
                                     get_date_param(request, 'date_to'))
     result_dict = helpers.query_to_dict(result)
     return make_response_json({'status': 'ok', 'data': result_dict})
コード例 #6
0
 def get(self):
     """Returns the minimum price for fuel at particular date (if date is omit at max date with info)."""
     if not request.args.get("fuel_type"):
         return make_response_json({
             'status': 'fail',
             'reason': 'no fuel type provided'
         })
     try:
         get_date_param(request, 'date_of_price')
     except ValueError:
         return make_response_json({
             'status': 'fail',
             'reason': 'wrong date'
         })
     result = query_by_station_min_price(session, request.args.get("fuel_type"),
                                         get_date_param(request, 'date_of_price'))
     result_dict = helpers.query_to_dict(result)
     return make_response_json({'status': 'ok', 'data': result_dict})
コード例 #7
0
def price_by_day():
    if request.headers['Content-Type'] == 'application/json':
        request_data = request.get_json()
    long = request_data["longitude"]
    lat = request_data["latitude"]
    date_of_price = request_data["date_of_price"]
    company = MetaDataFromCoordinates(lat, long)
    company_name = company.get_name()
    company_address = company.get_address()
    result = query_by_station_current_date(session, company_name,
                                           company_address, date_of_price)
    result_dict = helpers.query_to_dict(result)
    json_data = json.dumps(result_dict,
                           default=helpers.to_serializable,
                           ensure_ascii=False)
    resp = make_response(json_data)
    resp.mimetype = 'application/json'
    return resp
コード例 #8
0
 def get(self):
     """Returns average prices for fuel at period (if dates are omit for last 10 days)."""
     if not request.args.get("fuel_type"):
         return make_response_json({
             'status': 'fail',
             'reason': 'no fuel type provided'
         })
     try:
         get_date_param(request, 'date_from')
         get_date_param(request, 'date_to')
     except ValueError:
         return make_response_json({
             'status': 'fail',
             'reason': 'wrong date'
         })
     result = query_avg_price_period(session, request.args.get("fuel_type"),
                                     get_date_param(request, 'date_from'), get_date_param(request, 'date_to'))
     result_dict = helpers.query_to_dict(result)
     return make_response_json({'status': 'ok', 'data': result_dict})
コード例 #9
0
ファイル: ui.py プロジェクト: AntDu/Stella
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)