a ,"?g@sddlZddlZddlZddlZddlmZmZddlmZej Z ej Z ej ZejZejZddZddZdd Zd d Zd d ZddZddZddZddZddZddZddZdS)N)datetimezone)Configc CsNztjtttd}|WStyH}ztd|WYd}~dSd}~00dS)N)Zapi_urlz?An error occured while instantiating the Contentful CDA client.) contentfulClientSPACE_IDCDA_ACCESS_TOKEN CDA_API_HOST ExceptionloggingerrorclienterE/home/cmiss/Jenkins/workspace/SPARC-API-DEV/app/metrics/contentful.pyinit_cf_cda_clients rc CsHztt}|WStyB}ztd|WYd}~dSd}~00dS)Nz?An error occured while instantiating the Contentful CMA client.)contentful_managementrCMA_ACCESS_TOKENr r r r rrrinit_cf_cma_client s rcCs|ddi}|jS)N content_typeZ sparcAward)entriestotal)rresponserrrget_funded_projects_count,srcCst}||S)N)rentry)idrrrrget_cda_client_entry3src Cs<dtjdtjdtjd|d }tj|d}|dS)Nhttps:///spaces/z*/environments/master/entries?access_token=&content_type= &limit=999urlitemsrCTF_CMA_API_HOST CTF_SPACE_IDCTF_CMA_ACCESS_TOKENrequestsgetjsonZcontent_type_idr#rrrrget_all_entries8s$ r-c Cs:dtjdtjdtjd|d }t|}|dS)Nrrz1/environments/master/public/entries?access_token=r r!r$r%r,rrrget_all_published_entries>s$ r.cCs4dtjdtjd|dtj}t|}|S)Nrrz$/environments/master/public/entries/?access_token=r%rr#rrrrget_cma_published_entryDs" r1cCs6dtjdtjd|dtj}tj|d}|S)Nrr/environments/master/entries/r/r"r%r0rrr get_cma_entryJs" r3cCs`t|dd}dtjdtjd|}dtjddt|t|d }tj|||d }|S) Nsysversionrrr2Bearer z-application/vnd.contentful.management.v1+jsonzapplication/json) Authorizationz Content-TypeAcceptzX-Contentful-Content-TypeX-Contentful-Version)headersr#r+) r3rr&r'r(strr)putr+)rrdatar5r#hedrrrr update_entry_using_json_responseRsr?cCsFdtjdtjd|d}dtjt|d}tj||d}|S)Nrrr2z /publishedr6)r7r9)r:r#)rr&r'r(r;r)r<r+)rr5r#r>rrrr publish_entryfsr@cCs<t|dddkr8|dd}d|dvo6d|dvSdS)Nr$rfeaturedDatasetsfieldsdateToClearFeaturedDatasetsF)len)result featured_datarrr_have_featured_datasetsts rHc Csdtjdtjd}ddtji}ddd}tj|||d }g}|jtjjkr| }t |r|d d }|d d }|d d}|durt }t |} | |krg}|S)Nrrz/environments/master/entriesr7r6Zhomepagez:fields.featuredDatasets,fields.dateToClearFeaturedDatasets)rselect)r:paramsr#r$rrCrDrB)rCTF_CDA_API_HOSTr'CTF_CDA_ACCESS_TOKENr)r* status_codecodesokr+rHrtoday fromisoformat) r#r>qrZfeatured_datasets json_datarGZ date_to_clearZtime_nowZexpiration_timerrrget_featured_datasets|s0     rT)r rrr)datetimerr app.configrrKr r&Z CMA_API_HOSTrLrr(rr'rrrrrr-r.r1r3r?r@rHrTrrrrs,