from sqlalchemy.exc import IntegrityError from sqlalchemy.orm import Session from db import get_db from models import PermissionSet from models.metrics import MeterSnapshot, HeatMeterSnapshot, \ ElectricityMeterSnapshot, MeterType, Meter from permissions import has_permission from request_models import create_pagination_model from request_models.metrics_requests import MeterSnapshotModel, AddMeterSnapshotModel, ChangeMeterSnapshotModel, \ AddAutoMeterSnapshotModel from routes import metrics_router from utils import paginate @metrics_router.get("/meter-snapshots/", status_code=200, response_model=create_pagination_model(MeterSnapshotModel)) async def get_meter_snapshots(request: Request, db: Session = Depends(get_db)): has_permission(request, PermissionSet.MeterSnapshotRead.value) return paginate( db=db, db_model=MeterSnapshot, serializer=MeterSnapshotModel, request=request ) @metrics_router.post("/meter-snapshots/", status_code=201, response_model=MeterSnapshotModel) async def add_meter_snapshot(request: Request, body: AddMeterSnapshotModel, db: Session = Depends(get_db), ): has_permission(request, PermissionSet.MeterSnapshotEdit.value) snapshot_dict = body.dict() snapshot_dict['automatic'] = False
response_model=HeadcountModel) async def get_headcount(db: Session = Depends(get_db)): buildings: List[Building] = db.query(Building).all() model = HeadcountModel() for building in buildings: model.living += building.living_quantity studying = building.studying_daytime + building.studying_evening_time + building.studying_part_time working = building.working_help + building.working_science + building.working_teachers model.studying += studying model.personnel += working return model @metrics_router.get("/locations/", status_code=200, response_model=create_pagination_model(LocationModel)) async def get_locations(request: Request, db: Session = Depends(get_db)): has_permission(request, PermissionSet.LocationRead.value) return paginate(db=db, db_model=Location, serializer=LocationModel, request=request) @metrics_router.post("/locations/", status_code=201, response_model=LocationModel) async def add_location(request: Request, body: AddLocationModel, db: Session = Depends(get_db)): has_permission(request, PermissionSet.LocationEdit.value)
from auth_api import get_user from db import get_db from models import PermissionSet from models.location import Building, BuildingType from permissions import has_permission from request_models import create_pagination_model from request_models.location_requests import BuildingModel, AddBuildingModel, ChangeBuildingModel, BuildingTypeModel, \ AddBuildingTypeModel, BuildingTypeCountModel from routes import metrics_router from utils import paginate, apply_filtering @metrics_router.get("/building-types/", status_code=200, response_model=create_pagination_model(BuildingTypeModel)) async def get_building_types( request: Request, db: Session = Depends(get_db), ): has_permission(request, PermissionSet.BuildingTypeRead.value) return paginate(db=db, db_model=BuildingType, serializer=BuildingTypeModel, request=request) @metrics_router.get( "/building-types/count/", status_code=200, response_model=create_pagination_model(BuildingTypeCountModel))
from sqlalchemy.exc import IntegrityError from sqlalchemy.orm import Session from db import get_db from models import PermissionSet from models.metrics import EnvironmentalReading from permissions import has_permission from request_models import create_pagination_model from request_models.metrics_requests import EnvironmentalReadingModel, \ AddEnvironmentalReadingModel, ChangeEnvironmentalReadingModel from routes import metrics_router from utils import paginate @metrics_router.get("/rooms/environmental-readings/", status_code=200, response_model=create_pagination_model(EnvironmentalReadingModel)) async def get_environmental_readings(request: Request, db: Session = Depends(get_db)): has_permission(request, PermissionSet.RoomRead.value) return paginate( db=db, db_model=EnvironmentalReading, serializer=EnvironmentalReadingModel, request=request ) @metrics_router.post("/rooms/environmental-readings/", status_code=201, response_model=EnvironmentalReadingModel) async def add_environmental_reading(request: Request, body: AddEnvironmentalReadingModel, db: Session = Depends(get_db), ): has_permission(request, PermissionSet.RoomEdit.value) environmental_reading = EnvironmentalReading(**body.dict())
from auth_api import get_user from db import get_db from models import PermissionSet from models.location import ResponsibleUser from permissions import has_permission from request_models import create_pagination_model from request_models.location_requests import ResponsibleUserModel, AddResponsibleUserModel, ChangeResponsibleUserModel from routes import metrics_router from utils import apply_filtering @metrics_router.get( "/responsible_users/", status_code=200, response_model=create_pagination_model(ResponsibleUserModel)) async def get_responsible_users(request: Request, db: Session = Depends(get_db)): has_permission(request, PermissionSet.BuildingRead.value) result_models, count, page_number = apply_filtering( db=db, db_model=ResponsibleUser, request=request) items = [{ 'id': user.id, 'rank': user.rank, 'building_id': user.building_id, 'user': get_user(user.user_id) } for user in result_models] return { 'total_size': count, 'page_number': page_number,
from sqlalchemy.orm import Session from db import get_db from models import PermissionSet from models.location import Room from permissions import has_permission from request_models import create_pagination_model from request_models.location_requests import RoomModel, \ AddRoomModel, ChangeRoomModel from routes import metrics_router from utils import paginate @metrics_router.get("/rooms/", status_code=200, response_model=create_pagination_model(RoomModel)) async def get_rooms(request: Request, db: Session = Depends(get_db)): has_permission(request, PermissionSet.RoomRead.value) return paginate(db=db, db_model=Room, serializer=RoomModel, request=request) @metrics_router.post("/rooms/", status_code=201, response_model=RoomModel) async def add_room(request: Request, body: AddRoomModel, db: Session = Depends(get_db)): has_permission(request, PermissionSet.RoomEdit.value) room = Room(**body.dict()) db.add(room)
from sqlalchemy.exc import IntegrityError from sqlalchemy.orm import Session from db import get_db from models import PermissionSet from models.location import Floor, FloorPlanItem, FloorItemType, Room from models.metrics import Meter from permissions import has_permission from request_models import create_pagination_model from request_models.location_requests import FloorModel, AddFloorModel, ChangeFloorModel, AddFloorPlanItemModel, \ FloorPlanItemModel from routes import metrics_router from utils import paginate @metrics_router.get("/floors/", status_code=200, response_model=create_pagination_model(FloorModel)) async def get_floors(request: Request, db: Session = Depends(get_db)): has_permission(request, PermissionSet.FloorRead.value) return paginate( db=db, db_model=Floor, serializer=FloorModel, request=request ) @metrics_router.post("/floors/", status_code=201, response_model=FloorModel) async def add_floor(request: Request, body: AddFloorModel, db: Session = Depends(get_db)): has_permission(request, PermissionSet.FloorEdit.value) floor = Floor(**body.dict()) db.add(floor)