83 lines
3.0 KiB
Python
83 lines
3.0 KiB
Python
from functools import wraps
|
|
|
|
from flask import jsonify, request
|
|
from flask_login import current_user, login_required
|
|
|
|
from ..extensions import db
|
|
from ..models import CompanySettings, FuelStationCompany, User, Vehicle
|
|
|
|
|
|
def response(data=None, message=None, status=200):
|
|
payload = {"ok": status < 400}
|
|
if message:
|
|
payload["message"] = message
|
|
if data is not None:
|
|
payload["data"] = data
|
|
return jsonify(payload), status
|
|
|
|
|
|
def fail(message, status=400):
|
|
return response(message=message, status=status)
|
|
|
|
|
|
def role_required(*allowed):
|
|
def decorator(function):
|
|
@wraps(function)
|
|
@login_required
|
|
def wrapped(*args, **kwargs):
|
|
if current_user.role not in allowed:
|
|
return fail("Brak uprawnień", 403)
|
|
return function(*args, **kwargs)
|
|
return wrapped
|
|
return decorator
|
|
|
|
|
|
def payload():
|
|
return request.get_json(silent=True) or request.form
|
|
|
|
|
|
def boolv(data, key):
|
|
value = data.get(key)
|
|
return value in (True, 1, "1", "true", "on", "yes")
|
|
|
|
|
|
def accessible_vehicles():
|
|
if current_user.role == "admin":
|
|
return Vehicle.query.order_by(Vehicle.name).all()
|
|
ids = {vehicle.id for vehicle in current_user.owned_vehicles + current_user.shared_vehicles}
|
|
return Vehicle.query.filter(Vehicle.id.in_(ids)).order_by(Vehicle.name).all() if ids else []
|
|
|
|
|
|
def user_dict(user):
|
|
return {
|
|
"id": user.id, "name": user.name, "email": user.email, "role": user.role,
|
|
"active": user.active, "fuel_card_id": user.fuel_card_id,
|
|
"fuel_card": user.fuel_card.name if user.fuel_card else None,
|
|
"company_id": user.company_id, "company": user.company.name if user.company else None,
|
|
}
|
|
|
|
|
|
def vehicle_dict(vehicle):
|
|
return {
|
|
"id": vehicle.id, "name": vehicle.name, "make": vehicle.make, "model": vehicle.model,
|
|
"registration": vehicle.registration, "fuel_type": vehicle.fuel_type,
|
|
"owner": user_dict(vehicle.owner), "drivers": [user_dict(x) for x in vehicle.drivers],
|
|
"has_fuel_card": bool(vehicle.fuel_card_id), "fuel_card_id": vehicle.fuel_card_id,
|
|
"fuel_card": vehicle.fuel_card.name if vehicle.fuel_card else None,
|
|
"current_odometer": vehicle.current_odometer,
|
|
}
|
|
|
|
|
|
def station_dict(station):
|
|
settings = CompanySettings.query.first()
|
|
return {
|
|
"id": station.id, "brand_name": station.display_name, "company_name": station.company_name,
|
|
"nip": station.nip, "regon": station.regon, "brands": station.station_name_list,
|
|
"regions": station.region_list, "selected_region": station.selected_region,
|
|
"station_count": station.station_count, "has_petrol": station.has_petrol,
|
|
"has_diesel": station.has_diesel, "has_lpg": station.has_lpg,
|
|
"use_orlen_last_price": station.use_orlen_last_price, "active": station.active,
|
|
"favorite": station in current_user.favorite_stations if current_user.is_authenticated else False,
|
|
"allowed": station in settings.allowed_stations if settings else True,
|
|
}
|