U
    nZd%<                     @   s  d dl Z d dlZd dl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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 d1ddZd2ddZdd Zdd Zdd Zdd Zdd Zdd Zdd Z dddd d!d"d#d$d%d&d'd(d)Z!d*d+d'd(d,d-d.d/d0Z"dS )3    N)EXAM_PAGE_STATE_NOT_ATTENDED)QuizPurchaseWikiazmaBalanceHistoryInstituteBalanceHistoryUserBalanceHistory)Environment)QUESTION_TYPE_MULTIPLE_CHOICEQUESTION_TYPE_DESCRIPTIVE)	Blueprint)Sheet)QuestionScore)	QuizSheet)
logs_adderc           
         s   t | j}|d k	rjtjtj  d fddtdD }tj	j
|drnd fddtdD }q<nd }t|}tj	j|||| td|r|n| j|r|n| j|r|n| j|j |j d}	|	S )	N c                 3   s   | ]}t  V  qd S Nsecretschoice.0ialphabet &/var/www/wikiazma_server/quiz/utils.py	<genexpr>   s     z+create_quiz_sheet_helper.<locals>.<genexpr><   )entrance_tokenc                 3   s   | ]}t  V  qd S r   r   r   r   r   r   r      s     r   )r   userinstitute_studentquizstatelast_quizee_input_indexallowed_start_atallowed_finish_atduration_questions_sheet_solutions_sheet)r
   	from_dict
_blueprintstringascii_lettersdigitsjoinranger   objectsfilterr   Zfrom_blurprintcreater   startendr%   Zquestion_sheetto_dictsolutions_sheet)
r    r   r   r#   r$   r%   	blueprintr   sheet
quiz_sheetr   r   r   create_quiz_sheet_helper   s&     
  r9   c                 C   s  |d krt  }d |_||_|d |_|d |_| d krXd|_d}d}|d d }d}	nd|_d}d}| d D ]P}
d}|d	 D ]2}||
kr~d}||d
 kr|d7 }n|d7 } qq~|sntd qn|d d ||  }d}d}|d	 D ]"}||d
 kr|d7 }q|d7 }qd}|dkr4|t||d d  |d  }d}|jj	rj|dkrj|t||d d  |d  }|| }	|d kr|	n||_
||_||_||_|  |S )Nquestion_idindexTr   input_rulesmax_selectable_choicesFselected_choiceschoicescorrect_choices   zcritical error #4817341positive_scorenegative_score)r   	correctorr8   r:   question_indexblankprintminr    apply_negative_scoresscorer@   wrong_choicesblank_choicessave)
user_inputquestionsolutionr8   
correctionZforce_scorer@   rK   rL   Zquestion_scoreZuser_choiceZuser_choice_foundr   Ztotal_number_of_correct_choicesZtotal_number_of_wrong_choicesrB   rC   r   r   r   'correct_multiple_choice_question_helper)   sr    










rR   c                    s   g }| j d D ] |  d }| jd kr0d }n, fdd| jD }t|dkrX|d nd } d tkrt| || }|| q d tkr|d krtj	j
| d  d |d dd d d dd		}|| qtd
q|S )N	questionsr;   c                    s    g | ]}|d   d  kr|qS r;   r   )r   rN   rO   r   r   
<listcomp>u   s    z'auto_correct_helper.<locals>.<listcomp>r   question_typer:   T)	r8   rD   rE   r:   rF   r@   rK   rL   rJ   zUnknown question type)r&   get_solution_by_index_quizee_inputs_sheetlenr   rR   appendr	   r   r/   r1   
ValueError)r8   corrections_listrP   rN   user_inputsrQ   r   rU   r   auto_correct_helpern   s.    
    
r_   c                    s  i }d}| j d }t|dkr*dddg}g }|D ]}||d  q2|t| j d  g }t|D ]\}}d}	d}
d}d}d}d}d}d}| j d || ||d   D ]  fdd	|D }|s  d
S |jdd dd |d }||j7 }||j7 }|jd k	r|	|j7 }	|jd k	r*|
|j7 }
|jd k	r@||j7 }| d 7 }| d 7 }| d 7 }|d7 }q|	|
 | dkr|	d |
 d d |	|
 |  }|	d |	|
 |  }nd}d}|d |||||	|
||||d}|| qf||d< t	dd |D t	dd |D t	dd |D t	dd |D t	dd |D t	dd |D t	dd |D |d}||d< |||d  fS )!Nr   groupsr   )namestart_atrb   rS   rA   c                    s   g | ]}|j  d  kr|qS rT   )rE   )r   itemrU   r   r   rV      s      z/generate_report_card_helper.<locals>.<listcomp>)NNNc                 S   s   | j S r   )modified_at)rc   r   r   r   <lambda>       z-generate_report_card_helper.<locals>.<lambda>T)keyreverserB   rC   r%      d   ra   )ra   positive_scoresnegative_scores	durationsquestions_countr@   rK   rL   rJ   percent_with_negativespercent_without_negativesc                 s   s   | ]}|d  V  qdS )rk   Nr   r   groupr   r   r   r      s     z.generate_report_card_helper.<locals>.<genexpr>c                 s   s   | ]}|d  V  qdS )rl   Nr   rq   r   r   r   r      s     c                 s   s   | ]}|d  V  qdS )rm   Nr   rq   r   r   r   r      s     c                 s   s   | ]}|d  V  qdS )rn   Nr   rq   r   r   r   r      s     c                 s   s   | ]}|d  V  qdS )r@   Nr   rq   r   r   r   r      s     c                 s   s   | ]}|d  V  qdS )rK   Nr   rq   r   r   r   r      s     c                 s   s   | ]}|d  V  qdS )rL   Nr   rq   r   r   r   r      s     )rk   rl   rm   rn   r@   rK   rL   rJ   overallrk   )
r&   rZ   r[   	enumeratesortrJ   r@   rK   rL   sum)r8   r]   report_cardtotal_scorer`   Zquestion_start_atsrr   Zreport_card_groupsr;   r@   rK   rL   rk   rl   rm   rn   rJ   Ztarget_correctionsZselected_correctionro   rp   Zreport_card_group_itemrs   r   rU   r   generate_report_card_helper   s    
"








     ry   c                 C   s   | r2| j }| j}| j}|j| }||k r,|}|}n>tjjdd\}}	|j}
|j}|j	}|j| }||
k rl|
}|}|j| | }|j
rd}|jrd}t|j| | }t|j}|| }|||||fS )NT)
active_envr   )best_min_wage_feebest_wage_factorbest_tax_factorpricer   r/   get_or_createdefault_min_wage_feedefault_wage_factordefault_tax_factor	wage_freevax_freemathfloorceil)r   r   r    Zuser_min_wage_feeZuser_wage_factorZuser_tax_factorwage
tax_factorrz   _Zactive_env_min_wage_feeZactive_env_wage_factorZactive_env_tax_factortax
total_costinstitute_sharewikiazma_sharer   r   r   price_calculator_helper   s2    

r   c                 C   s4   t  }t ||t| |d}|j|j |_|  d S )N)amountreason_meta_datareasonr    r   )r   get_last_wikiazma_balancer   remaining_balancer   rM   )r    r   r   r   last_wikiazma_balance_historyZnew_wikiazma_balance_historyr   r   r   &quiz_purchase_wikiazma_balance_handler  s     r   c                 C   s6   t | }t | ||t|d}|j|j |_|  d S )N)	instituter   r   r   r    )r   get_last_institute_balancer   r   r   rM   )r   r    r   r   last_institute_balance_historynew_balance_historyr   r   r   'quiz_purchase_institute_balance_handler  s    
 r   c                 C   s^   t | }t | ||t|d}|j|j |_|  |jdk rZtd| d|j  td d S )N)r   r   r   r   r    r   zamount=z(, new_balance_history.remaining_balance=zcritical error # 94166237225)r   get_last_user_balancer   r   r   rM   rG   )r   r    r   r   last_user_balance_historyr   r   r   r   "quiz_purchase_user_balance_handler  s    


r   c                 C   sv   t | d |\}}}}}|dkrrt|j||dt|jid t|dt|ji| |d t| |dt|jid| d d S )Nr   quiz_sheet_id)r   r   r    r   )r    r   r   r   )r   r    r   r   )r   r   r   str
referer_idr   r   )r   r    r8   r   r   r   r   r   r   r   r   quiz_purchase_handler&  s    
 r   ra   z-name
created_atz-created_atrd   z-modified_atrn   z-questions_countr2   z-startr%   z	-duration)name_asc	name_desccreated_at_asccreated_at_descmodified_at_ascmodified_at_descquestions_count_ascquestions_count_descZ	start_ascZ
start_descduration_ascduration_descentrance_datez-entrance_datefinal_scorez-final_scorer!   z-state)Zentrance_date_ascZentrance_date_descr   r   Zfinal_score_ascZfinal_score_descZ	state_ascZ
state_desc)NNNNN)NN)#r   r   r*   exam.modelsr   payment.modelsr   r   r   r   plan.modelsr   question.modelsr   r	   Zquiz.blueprint.blueprintr
   Zquiz.blueprint.sheetr   quiz.modelsr   r   utils.utilsr   r9   rR   r_   ry   r   r   r   r   r   list_quiz_sort_by_switcher list_quiz_sheet_sort_by_switcherr   r   r   r   <module>   sR     

EY&        