o
    0<¹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!lBmCZC d d"lDmEZE d d#lFmGZG d d$lHmIZI d d%lJmKZK d d&lLmMZM d d'lNmOZO d d(lPmQZQ d d)lRmSZS d d*lTmUZU d d+lVmWZW d d,lXmYZY d d-lZm[Z[ d d.l\m]Z] d d/l^m_Z_ d d0l`maZa d d1lbmcZc d d2ldmeZe d d3lfmgZg d d4lhmiZi d d5ljmkZk d d6llmmZm d d7lnmoZo d d8lpmqZq d d9lrmsZs d d:ltmuZu d d;lvmwZw d d<lxmyZy d d=lzm{Z{ d d>l|m}Z} d d?l~mZ d d@l€mZ d dAl‚mƒZƒ d dBlm„Z„ d dCl…m†Z†m‡Z‡mˆZˆm‰Z‰mŠZŠ d dDl‹mŒZŒ eƒƒ ZedEefdFdG„ƒZŽeejejej‘eŽdHdIZ’e’ “dJ¡dKe	dLedMe
fdNdO„ƒZ”e’j•edPgdHdPgdPgdPgdQ e’j–edRdSgdT e’j–edRdUgdT e’j–edRdVgdT e’j–edRdWgdT e’j–edRdXgdT e’j–e!dRdYgdT e’j–e#dRdZgdT e’j–e%dRd[gdT e’j–e'dRd\gdT e’j–e)dRd]gdT e’j–e+dRd^gdT e’j–e-dRd_gdT e’j–e/dRd`gdT e’j–e1dRdagdT e’j–e3dRdbgdT e’j–e5dRdcgdT e’j–e7dRddgdT e’j–e9dRdegdT e’j–e;dRdfgdT e’j–e=dRdggdT e’j–e?dRdhgdT e’j–eAdRdigdT e’j–eCdRdjgdT e’j–eEdRdkgdT e’j–eGdRdlgdT e’j–eIdRdmgdT e’j–eKdRdngdT e’j–eMdRdogdT e’j–eOdRdpgdT e’j–eQdRdqgdT e’j–eSdRdrgdT e’j–eUdRdsgdT e’j–eWdRdtgdT e’j–eYdRdugdT e’j–e[dRdvgdT e’j–e]dRdwgdT e’j–e_dRdxgdT e’j–eadRdygdT e’j–ecdRdzgdT e’j–eedRd{gdT e’j–egdRd|gdT e’j–eidRd}gdT e’j–ekdRd~gdT e’j–emdRdgdT e’j–eodRd€gdT e’j–eqdRdgdT e’j–esdRd‚gdT e’j–eudRdƒgdT e’j–ewdRd„gdT e’j–eydRd…gdT e’j–e{dRd†gdT e’j–e}dRd‡gdT e’j–edRdˆgdT e’ —e¡dKe	d‰edMefdŠd‹„ƒZ—e’ —e¡dKe	d‰edMefdŒd„ƒZ˜e’ —e™¡dKe	d‰e™dMefdŽd„ƒZše’ —e„¡dKe	d‰e„dMefdd‘„ƒZ›e’ —eœ¡dKe	d‰eœdMefd’d“„ƒZe’jžd”dHd•d–eŸdMeŒfd—d˜„ƒZ e’j¡d™edšd›dšdœ eddžZ¢e’ £dŸ¡d d¡„ ƒZ¤e’ žd¢¡ee¢ƒfd£eŸfd¤d¥„ƒZ¥dS )¦é    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)Úcommercial_equipment_router)Úplumbing_router)Útrading_month_router)Úpre_operating_router)Úcompetitor_analysis_router)Údisplayboard_router)Úcompetitor_bench_marking_router)Úcarpentry_router)Úinfo_tech_router)Úvisual_merch_elements_router)Údepreciation_router)Úcarpentry_furniture_router)Ú#competitor_category_analyses_router)Úpre_select_category_router)Ú gross_margin_contribution_router)Ú#vendor_metrics_priority_list_router)Úvendor_scorecard_router)Úprivate_lablling_router)Ú#category_captinship_planning_router)Úadditional_installation_router)Úsales_estimate_router)Úrent_expenses_router)Ú"competitor_pricing_analysis_router)Ústore_formate_pricing_router)Úcorporate_store_router)Ú category_definition_excel_router)Ú%competitor_analysis_comparison_router)Úcorporate_concept_office_router)Úutility_router)Úoperating_expenses_router)Ústore_level_staff_router)Úcentralised_and_direct_router)Úcategory_wish_inventory_routre)Úshrinkage_areas_reasons_router)Úinventory_excel_router)Úonline_policies_router)Úsales_router)Úrecurring_operating_router)Údigital_asset_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)rL   Ú_© rO   ú#/var/www/html/RetailAPI/app/main.pyÚlifespanT   s   €
rQ   T)ÚtitleÚversionÚdescriptionrQ   Ú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)rW   rX   Ú
start_timeÚresponseÚprocess_timerO   rO   rP   Úadd_process_time_headerf   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Commercial EquipmentÚPlumbingzTrading MonthszPre-Operating ExpenseszCompetitor AnalysiszDisplay BoardzCompetitor Bench MarkingÚ	Carpentryz	Info TechzVisual Merchandising ElementsÚDepreciationÚ	FurniturezCompetitor Category AnalyseszPre Selected CategoryzGross Margin ContributionzVendor Metrics priority ListzVendor ScorecardzPrivate LabllingzCategory Captinship PlanningzAdditional InstallationzSales EstimatezRent ExpenseszCompetitor Pricing Analysisz Store Formate pricing StrategieszCorporate Storez Training Talent Management ExcelzCompetitor Analysis ComparisonzCorporate Concept OfficeÚUtilityzOperating ExpenseszStore Level StaffzCentralised and DirectzCategory Wish InventoryzShrinkage Areas ReasonszInventory ExcelzOnline Policies ExcelÚSaleszRecurring OperatingzDigital AssetÚexcc                 Ã   s   t | |ƒS )zHandle HTTP exceptions.)rH   ©rW   rv   rO   rO   rP   Úexception_handlerÀ   s   €
rx   c                 Ã   ó   t | |ƒI dH S )z)Handle MySQL ProgrammingError exceptions.N)rG   rw   rO   rO   rP   Úprogramming_error_handlerÆ   ó   €rz   c                 Ã   ry   )zHandle generic exceptions.N)rF   rw   rO   rO   rP   Úgeneric_exception_handlerÌ   r{   r|   c                 Ã   s   t | |ƒI d H ‚©N)rI   rw   rO   rO   rP   Úoperational_error_handlerÒ   ó   €r~   c                 Ã   s   t | |ƒI d H S r}   )rJ   rw   rO   rO   rP   Úattribute_error_handler×   r   r€   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ÚformatrK   ÚCalledProcessErrorr   r[   Ú	Exception)r‚   ÚerO   rO   rP   Úrun_migrationsÜ   s   €ÿ€€ÿr‘   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)rR   rS   rT   z/some-protected-routeÚgetÚuser_oauth2Ústudent_oauth2ÚgetSomeProtectedRoutez+A protected route for both user and studentÚ200rT   zSuccessful response)ÚsecurityÚoperationIdÚsummaryÚ	responsesÚsecuritySchemesÚoauth2Úpasswordr–   r•   )ÚtypeÚflow)ÚopenapiÚinfoÚpathsÚ
components)ÚappÚopenapi_schemarO   rO   rO   rP   Ú
on_startupù   s8   ýþÿùÿÿ	ÿÿþ÷ÿêr¬   z/student-protected-routeÚstudentc                 C   s
   d| dœS )Nz&This is a protected route for students)r‚   r­   rO   )r­   rO   rO   rP   Úget_student_protected_route/  s   
r®   )¦r‹   rZ   Ú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.commercial_equipment.routerr   Úapp.api.plumbing.routerr   Úapp.api.trading_month.routerr   Ú%app.api.pre_operating_expenses.routerr   Ú"app.api.competitor_analysis.routerr    Úapp.api.display_board.routerr!   Ú'app.api.competitor_bench_marking.routerr"   Úapp.api.carpentry.routerr#   Úapp.api.info_tech.routerr$   Ú,app.api.visual_merchandising_elements.routerr%   Úapp.api.depreciation.routerr&   Úapp.api.furniture.routerr'   Ú+app.api.competitor_category_analyses.routerr(   Ú$app.api.pre_selected_category.routerr)   Ú(app.api.gross_margin_contribution.routerr*   Ú+app.api.vendor_metrics_priority_list.routerr+   Úapp.api.vendor_scorecard.routerr,   Úapp.api.private_lablling.routerr-   Ú+app.api.category_captinship_planning.routerr.   Ú)app.api.addtional_installation_fix.routerr/   Úapp.api.sales_estimate.routerr0   Úapp.api.rent_expenses.routerr1   Ú*app.api.competitor_pricing_analysis.routerr2   Ú/app.api.store_formate_pricing_strategies.routerr3   Úapp.api.corporate_store.routerr4   Ú(app.api.category_definition_excel.routerr5   Ú-app.api.competitor_analysis_comparison.routerr6   Ú'app.api.corporate_concept_office.routerr7   Úapp.api.utilitys.routerr8   Ú!app.api.operating_expenses.routerr9   Ú&app.api.store_level_staff_needs.routerr:   Ú%app.api.centralised_and_direct.routerr;   Ú&app.api.category_wish_inventory.routerr<   Ú&app.api.shrinkage_areas_reasons.routerr=   Úapp.api.inventory_excel.routerr>   Úapp.api.online_policies.routerr?   Úapp.api.sales.routerr@   Ú+app.api.recurring_operating_expenses.routerrA   Ú(app.api.digital_asset_acquisition.routerrB   Ú
app.configrC   Úapp.database.main.mongorD   rE   Úapp.exceptionsrF   rG   rH   rI   rJ   Úapp.utils.schemas_utilsrK   rM   rQ   ÚPROJECT_NAMEÚAPI_VERSIONÚDESCRIPTIONrª   Ú
middlewarer`   Úadd_middlewareÚinclude_routerrx   rz   r   r|   r~   ÚAttributeErrorr€   r˜   r[   r‘   ÚmountÚstudent_oauth2_schemeÚon_eventr¬   r®   rO   rO   rO   rP   Ú<module>   sF   ù
ú

5ÿÿ