uprawnienia do stacji i usuwanie firm

This commit is contained in:
Mateusz Gruszczyński
2026-07-13 15:55:37 +02:00
parent 31f10fc8d9
commit 6cfbfa658c
7 changed files with 61 additions and 14 deletions
+26 -8
View File
@@ -156,7 +156,13 @@ def fuel():
fueled_at = datetime.fromisoformat(request.form["fueled_at"]); wholesale = None; source = None
station_company_id = request.form.get("station_company_id", type=int)
station_company = db.session.get(FuelStationCompany, station_company_id) if station_company_id else None
price_region = station_company.selected_region if station_company and station_company.selected_region else CompanySettings.query.first().region
settings = vehicle.company or current_user.company or CompanySettings.query.first()
access_mode = settings.station_access_mode or "all_prefer_favorites"
if station_company and access_mode == "allowed_only" and station_company not in settings.allowed_stations:
response = error_response("Ta stacja nie jest dozwolona przez politykę firmy", 403); return response or redirect(url_for("main.fuel"))
if station_company and access_mode == "favorites_allowed_only" and station_company not in settings.allowed_stations and station_company not in settings.favorite_stations:
response = error_response("Ta stacja nie jest ulubiona ani dozwolona przez politykę firmy", 403); return response or redirect(url_for("main.fuel"))
price_region = station_company.selected_region if station_company and station_company.selected_region else settings.region
try:
p = fetch_orlen_price(request.form["fuel_type"], fueled_at.date(), price_region); wholesale=p["price_per_liter"]; source=p["source"]
except Exception: pass
@@ -165,20 +171,32 @@ def fuel():
vehicle.current_odometer = odometer; db.session.add(e); db.session.commit(); socketio.emit("fuel_added", {"vehicle": vehicle.name, "gross": round(e.gross,2)}); response = ok_response("Zapisano tankowanie", redirect=url_for("main.dashboard")); return response or redirect(url_for("main.dashboard"))
settings=current_user.company or CompanySettings.query.first()
allowed=list(settings.allowed_stations)
company_favorites=list(settings.favorite_stations)
access_mode=settings.station_access_mode or "all_prefer_favorites"
base_query=FuelStationCompany.query.filter_by(active=True)
stations=(base_query.filter(FuelStationCompany.id.in_([x.id for x in allowed])) if allowed else base_query).order_by(FuelStationCompany.station_count.desc(), FuelStationCompany.brand_name.asc()).all()
favorites=[x for x in current_user.favorite_stations if x in stations]
if access_mode == "allowed_only":
allowed_ids=[x.id for x in allowed]
stations=base_query.filter(FuelStationCompany.id.in_(allowed_ids)).all() if allowed_ids else []
elif access_mode == "favorites_allowed_only":
scope_ids={x.id for x in allowed}|{x.id for x in company_favorites}
stations=base_query.filter(FuelStationCompany.id.in_(scope_ids)).all() if scope_ids else []
else:
stations=base_query.all()
stations=sorted(stations,key=lambda x:(-x.station_count,x.display_name.lower()))
user_favorites=[x for x in current_user.favorite_stations if x in stations]
user_favorite_ids={x.id for x in current_user.favorite_stations}
favorites=list(user_favorites)
favorite_scope="user"
if not favorites and settings.favorite_stations:
favorites=[x for x in settings.favorite_stations if x in stations];favorite_scope="company"
if not favorites: favorites=sorted(stations,key=lambda x:(-x.station_count,x.company_name.lower()))[:10];favorite_scope="default"
if not favorites and access_mode in ("all_prefer_favorites", "favorites_allowed_only") and company_favorites:
favorites=[x for x in company_favorites if x in stations];favorite_scope="company"
if not favorites and access_mode == "all_prefer_favorites": favorites=stations[:10];favorite_scope="default"
favorites=sorted(favorites,key=lambda x:(-x.station_count,x.display_name.lower()))
favorite_ids={x.id for x in favorites}
top_stations=sorted((x for x in stations if x.id not in favorite_ids),key=lambda x:(-x.station_count,x.display_name.lower()))[:10]
proposed_ids=favorite_ids|{x.id for x in top_stations}
favorite_catalog=sorted(stations,key=lambda x:(-x.station_count,x.display_name.lower()))
favorite_catalog=sorted(stations,key=lambda x:(0 if x.id in user_favorite_ids else 1,-x.station_count,x.display_name.lower()))
other_stations=sorted((x for x in stations if x.id not in proposed_ids),key=lambda x:x.display_name.lower())
return render_template("fuel_form.html", vehicles=vehicles, stations=stations, favorite_stations=favorites, top_stations=top_stations, favorite_catalog=favorite_catalog, other_stations=other_stations, now=datetime.now().strftime("%Y-%m-%dT%H:%M"), restricted=bool(allowed), favorite_scope=favorite_scope, company=settings, fuel_cards=FuelCard.query.filter_by(active=True,company_id=settings.id).order_by(FuelCard.name).all())
return render_template("fuel_form.html", vehicles=vehicles, stations=stations, favorite_stations=favorites, top_stations=top_stations, favorite_catalog=favorite_catalog, other_stations=other_stations, now=datetime.now().strftime("%Y-%m-%dT%H:%M"), restricted=access_mode in ("allowed_only", "favorites_allowed_only"), access_mode=access_mode, user_favorite_ids=user_favorite_ids, favorite_scope=favorite_scope, company=settings, fuel_cards=FuelCard.query.filter_by(active=True,company_id=settings.id).order_by(FuelCard.name).all())
@main.route("/api/orlen-price")
@login_required