a
    %Cqcò  ã                   @   s¦   d Z ddlZddlZddlZddlZddlZddlZddlZddlm	Z	m
Z
mZmZmZmZ e	ƒ Ze 
d¡edƒejdd„ ƒƒƒZe d¡edƒejdd„ ƒƒƒZdS )	zRoutes for edit/readings.é    N)ÚBottleÚrouteÚviewÚredirectÚpostÚrequestz/surveys/<name>/readingsÚreadingsc              
   C   sJ   t  | ¡\}}t  | ¡}dd„ |D ƒ}t| |t  | ¡||g tj d¡dS )z´Presents user with form to edit survey's readings or enter them for
    the first time if uploading. Readings are self-contained fieldsets that
    can be edited, added or removed.c                 S   s   g | ]}|d  ‘qS )Útype© )Ú.0Úreadingr
   r
   ú /var/www/vevn/routes/readings.pyÚ
<listcomp>   ó    z!edit_readings.<locals>.<listcomp>Ú	uploading©Úsurvey_nameÚsurveyÚmetadatar   Ú	fieldsetsÚremovedr   )ÚsurveyparserÚ
get_surveyÚget_readingsÚdictÚget_metadatar   ÚqueryÚget)Únamer   Ú_Zsurvey_readingsr   r
   r
   r   Úedit_readings   s    

ùr    c                 C   s  t  | ¡\}}t tj d¡¡}t tj d¡¡}g }t|ƒD ] \}}||vr>|dkrÈ| |tj d 	|¡¡tj d 	|¡¡tj d 	|¡¡tj d 	|¡¡tj 
d 	|¡¡tj 
d	 	|¡¡d
œ¡ q>|dksâ|dksâ|dkr| |tj d 	|¡¡tj d 	|¡¡tj 
d 	|¡¡tj d 	|¡¡ttj d 	|¡¡ƒtj d 	|¡¡tj d 	|¡¡tj d 	|¡¡tj d 	|¡¡tj d 	|¡¡ttj d 	|¡¡ƒdœ¡ t|d d ƒD ]J}tj d 	||¡¡|d d 	|¡< tj d 	||¡¡|d d 	|¡< q¸q>|dkr| |tj d 	|¡¡tj d 	|¡¡tj d 	|¡¡tj d 	|¡¡tj d  	|¡¡tj d 	|¡¡tj d 	|¡¡d!œ¡ q>|d"krê| |tj d 	|¡¡tj d 	|¡¡tj d 	|¡¡tj d# 	|¡¡d$œ¡ q>|d%kr>| |tj d 	|¡¡tj d 	|¡¡tj d 	|¡¡tj d  	|¡¡d&œ¡ q>tj d'¡}	|	d(kr˜tj d)d*| d+¡}
t ||
¡ tjjrŽtd,|  ƒ ntd-ƒ nV|	d.kr¶| tj d/¡¡ n8|	 d0¡d1 d2krä| t|	 d0¡d3 ƒ¡ n
|	d4krît| |t  | ¡|||tj d5¡d6S )7z?Apply and write changes to readings.json or add/remove reading.r   r   Zloyaltyzreading-{}-namezreading-{}-descriptionzreading-{}-scorenamezreading-{}-scoreszreading-{}-goodzreading-{}-bad)r	   r   ÚdescriptionZ	scorenameÚscoresÚgoodÚbadZ	agreementZ
efficiencyZcommunicationzreading-{}-agreement-questionszreading-{}-neitherzreading-{}-limitzreading-{}-agree-regexzreading-{}-disagree-regexz reading-{}-satisfaction-questionzreading-{}-satisfied-regexzreading-{}-disatisfied-regexzreading-{}-generics)r	   r   r!   zagreement-questionsÚneitherÚlimitzagree-regexzdisagree-regexzsatisfaction-questionúsatisfied-regexúdisatisfied-regexÚgenericséÿÿÿÿr)   zreading-{}-generic-question-{}zgeneric-question-{}zreading-{}-generic-plot-{}zgeneric-plot-{}Úsatisfactionzreading-{}-questionzreading-{}-plot)r	   r   r!   Úquestionr%   Úplotr'   r(   Z	wordcloudzreading-{}-words)r	   r   r!   r,   ÚwordsÚgeneric)r	   r   r!   r,   r-   ÚactionÚapplyÚdbÚsurveyszreadings.jsonz	/surveys/z/surveyszadd-readingznew-readingú-r   Úremoveé   Útemplater   r   )r   r   ÚutilsÚsafejsonloadsr   Úformsr   Ú	enumerateÚappendÚformatÚgetallÚintÚrangeÚosÚpathÚjoinÚsafejsondumpr   r   r   Úsplitr   r   )r   r   r   r   r   Znew_readingsÚiZfieldsetr/   r0   rB   r
   r
   r   r       s¤    ù
	ô"(
ø


û
û
	





ù)Ú__doc__rA   ÚjsonÚpandasÚsecurityr   Úshutilr8   Úbottler   r   r   r   r   r   r   Úrequire_adminr    r
   r
   r
   r   Ú<module>   s   8 