o
    @h'                     @   s  d dl Z d dlZd dlmZ d dlmZ d dlmZmZm	Z	m
Z
 d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dl m!Z! d dl"m#Z# d dl$m%Z% d dl&m'Z' d dl(m)Z) d dl*m+Z+ d dl,m-Z- d dl.m/Z/ d dl0m1Z1 d dl2m3Z3 d dl4m5Z5 d dl6m7Z7 d dl8m9Z9 d dl:m;Z; d dl<m=Z= d dl>m?Z? d d l@mAZA d d!lmBZB d d"lCmDZDmEZEmFZFmGZGmHZH d d#lImJZJ eA ZKed$efd%d&ZLee?jMe?jNe?jOeLd'd(ZPePQd)d*e	d+ed,e
fd-d.ZRePjSed/gd'd/gd/gd/gd0 ePjTed1d2gd3 ePjTed1d4gd3 ePjTed1d5gd3 ePjTed1d6gd3 ePjTed1d7gd3 ePjTe!d1d8gd3 ePjTe#d1d9gd3 ePjTe%d1d:gd3 ePjTe'd1d;gd3 ePjTe)d1d<gd3 ePjTe+d1d=gd3 ePjTe-d1d>gd3 ePjTe/d1d?gd3 ePjTe1d1d@gd3 ePjTe3d1dAgd3 ePjTe5d1dBgd3 ePjTe7d1dCgd3 ePjTe9d1dDgd3 ePjTe;d1dEgd3 ePjTe=d1dFgd3 ePUed*e	dGed,efdHdIZUePUed*e	dGed,efdJdKZVePUeWd*e	dGeWd,efdLdMZXePUeBd*e	dGeBd,efdNdOZYePUeZd*e	dGeZd,efdPdQZ[ePj\dRd'dSdTe]d,eJfdUdVZ^ePj_dWedXdYdXdZ ed[d\Z`ePad]d^d_ ZbeP\d`ee`fdae]fdbdcZcdS )d    N)Callable)asynccontextmanager)FastAPIHTTPExceptionRequestResponse)CORSMiddleware)JSONResponse)StaticFiles)ProgrammingError)Depends)OAuth2PasswordBearer)user_router)brand_router)segment_router)store_formate_router)student_router)simulation_router)group_router)customer_location_router)catchment_potential_router)network_planning_router)payment_router)excel_router)civil_router)display_racking_unit_router)carpentry_furniture_router)commercial_equipment_router)plumbing_router)trading_month_router)pre_operating_router)competitor_analysis_router)settings)MongoDBSingleton)OperationalError) handle_generic_exception_handlerhandle_programming_errorhttp_exception_error_handler handel_operational_error_handlerhandel_attribute_error_handler)CustomResponse_appc                 C  s   t }dV  dS )zInitialize the application.N)mongodb_instance)r+   _ r.   </var/www/html/RetailApiNew/retail-simulation-api/app/main.pylifespan3   s   
r0   T)titleversiondescriptionr0   debughttprequest	call_nextreturnc                    s6   t   }|| I dH }t   | }t||jd< |S )a  
    Add process time header to the response.

    Args:
        request (Request): The incoming request.
        call_next (Callable): The next callable in the middleware chain.

    Returns:
        Response: The response with added process time header.
    NzX-Process-Time)timestrheaders)r6   r7   
start_timeresponseprocess_timer.   r.   r/   add_process_time_headerE   s   r?   *)allow_originsallow_credentialsallow_methodsallow_headersexpose_headersz/FASTAPIUser)prefixtagsBrandSegmentzStore FormateStudent
SimulationGroupzCustomer LocationzCatchment PotentialzNetwork PlanningPaymentzDownload ExcelzCivil and Backroom WorkzDisplay and Racking UnitszCarpentry and FurniturezCommercial EquipmentPlumbingzTrading MonthszPre-Operating ExpenseszCompetitor Analysisexcc                    s   t | |S )zHandle HTTP exceptions.)r'   r6   rP   r.   r.   r/   exception_handlery   s   
rR   c                       t | |I dH S )z)Handle MySQL ProgrammingError exceptions.N)r&   rQ   r.   r.   r/   programming_error_handler      rT   c                    rS   )zHandle generic exceptions.N)r%   rQ   r.   r.   r/   generic_exception_handler   rU   rV   c                    s   t | |I d H N)r(   rQ   r.   r.   r/   operational_error_handler      rX   c                    s   t | |I d H S rW   )r)   rQ   r.   r.   r/   attribute_error_handler   rY   rZ   z/migrate)response_model_exclude_nonemessagec              
      s   zt ddddd| g t g d tddd	W S  t jy1 } ztd
t|dd }~w tyD } ztd
t|dd }~ww )Nalembicrevisionz--autogeneratez-mz"{}")r]   upgradehead1zMigration successful)statusr\   i  )status_codedetail)
subprocessrunformatr*   CalledProcessErrorr   r:   	Exception)r\   er.   r.   r/   run_migrations   s   rk   z/uploadsuploads)	directory)name/FASTAPI/student/login)tokenUrlstartupc                
   C   sX   ddddddddg id	g igd
ddddiidiidd	ddddiidiidt _d S )Nz3.0.0zMy APIz1.0.0z-API Documentation with User and Student Login)r1   r2   r3   z/some-protected-routegetuser_oauth2student_oauth2getSomeProtectedRoutez+A protected route for both user and student200r3   zSuccessful response)securityoperationIdsummary	responsessecuritySchemesoauth2passwordrp   ro   )typeflow)openapiinfopaths
components)appopenapi_schemar.   r.   r.   r/   
on_startup   s8   	r   z/student-protected-routestudentc                 C   s
   d| dS )Nz&This is a protected route for students)r\   r   r.   )r   r.   r.   r/   get_student_protected_route   s   
r   )dre   r9   collections.abcr   
contextlibr   fastapir   r   r   r   fastapi.middleware.corsr   fastapi.responsesr	   fastapi.staticfilesr
   sqlalchemy.excr   r   fastapi.securityr   app.api.user.routerr   app.api.brand.routerr   app.api.segment.routerr   app.api.store_formate.routerr   app.api.student.routerr   app.api.simulation.routerr   app.api.group.routerr    app.api.customer_location.routerr   "app.api.catchment_potential.routerr   app.api.network.routerr   app.api.payment.routerr   app.api.excel_download.routerr   app.api.civil.routerr   %app.api.display_racking_create.routerr   app.api.carpentry.routerr   #app.api.commercial_equipment.routerr   app.api.plumbing.routerr   app.api.trading_month.routerr   %app.api.pre_operating_expenses.routerr    "app.api.competitor_analysis.routerr!   
app.configr"   app.database.main.mongor#   r$   app.exceptionsr%   r&   r'   r(   r)   app.utils.schemas_utilsr*   r,   r0   PROJECT_NAMEAPI_VERSIONDESCRIPTIONr   
middlewarer?   add_middlewareinclude_routerrR   rT   ri   rV   rX   AttributeErrorrZ   rr   r:   rk   mountstudent_oauth2_schemeon_eventr   r   r.   r.   r.   r/   <module>   s    


5