U
    Jc                     @   sr   d dl Z d dlmZ d dlmZ d dlmZ e eZ	dd Z
dd Zd	d
 Zdd Zdd Zdd Zdd ZdS )    N)BytesIO)Image)Filec                 C   s*   d| krt |  n
t |  t|  dS )z
    This function is responsible for printing some logs to terminal.
    it takes a content and prints it to the terminal
    CRITICALN)loggerwarningerrorprint)content r   '/var/www/wikiazma_server/utils/utils.py
logs_adder
   s    
r   c                 C   s   ddddd}| | dS )z
    This function converts the incoming sort_by parameter to an attribute of Institute table,
    so we can order the Institute records based on that attribute.
    namez-name
created_at-created_at)inst_name_ascinst_name_descinst_created_ascinst_created_descNgetsort_byZswitcherr   r   r   institute_sort_by_converter   s    r   c                 C   s   ddddddd}| | dS )	z
    This function converts the incoming sort_by parameter to an attribute of Access table,
    so we can order the Access records based on that attribute.
    Zinstitute_namez-institute_nameZinstitute_created_atz-institute_created_atZuser__emailz-user__email)r   r   r   r   	email_asc
email_descNr   r   r   r   r   access_sort_by_converter$   s    r   c                 C   s&   ddddddddd	d
d
}| | dS )z
    This function converts the incoming sort_by parameter to an attribute of InstituteStudent table,
    so we can order the InstituteStudent records based on that attribute.
    
first_namez-first_name	last_namez
-last_nameemailz-emailZ"referer_institute_student_identityz#-referer_institute_student_identityidz-id)
Zfirst_name_ascZfirst_name_descZlast_name_ascZlast_name_descr   r   Z&referer_institute_student_identity_ascZ'referer_institute_student_identity_descZid_ascZid_descNr   r   r   r   r   #institute_student_sort_by_converter4   s    r!   c                 C   s   ddddd}| | dS )z
    This function converts the incoming sort_by parameter to an attribute of PublicResource table,
    so we can order the PublicResource records based on that attribute.
    titlez-titler   r   )Z	title_ascZ
title_descZcreated_at_ascZcreated_at_descNr   r   r   r   r   "public_resources_sort_by_converterH   s    r#   c                 C   sD   |  dr| dd S |  dr0d| dd  S | dkr<dS | S d S )	NZ_ascr   Z_desc-random?)endswith)r   r   r   r   sort_by_translatorV   s    

r*   c                 C   sZ  t | D}|j}|j}d }||krR||krR|}t|| | }|||f}n2||kr||kr|}t|| | }|||f}|rt }| jdd 	 }	|	dkrd}	|j
||	d t|| jd}
|
W  5 Q R  S t | b}t }| jdd 	 }|dkrd}|j
||d t|| jd}|W  5 Q R  W  5 Q R  S Q R X W 5 Q R X d S )N.ZJPGZJPEG)format)r   )r   openheightwidthintZresizer   r   splituppersaver   )imageZmax_side_sizeZopened_imageZheight_originalZwidth_originalZresized_imager/   r0   Zopened_image_ioZcompressed_formatZcompressed_imageZduplicate_imageZduplicate_image_ioZimage_formatZduplicate_image_filer   r   r   compress_imagea   s>    	
 r6   )loggingior   PILr   django.core.filesr   	getLogger__name__r   r   r   r   r!   r#   r*   r6   r   r   r   r   <module>   s   
