U
    Ih<                     @   sd   d dl mZ d dlmZmZ d dlmZmZ d dlZd dl	m	Z	 d dl
Z
e
eZG dd dZdS )    )MongoClient)ConnectionFailureServerSelectionTimeoutError)MONGODB_URI
MONGODB_DBN)datetimec                   @   s8   e Zd Zdd ZdddZdd Zdd	 ZdddZdS )MongoHandlerc              
   C   s   z<t tdd| _| j  | jt | _| jj| _t	d W nB t
tfk
r~ } z tdt|  tdW 5 d }~X Y nX d S )Ni  )ZserverSelectionTimeoutMSz!Successfully connected to MongoDBzFailed to connect to MongoDB: zDCould not connect to MongoDB. Please check your connection settings.)r   r   clientZserver_infor   db
test_cases
collectionloggerinfor   r   errorstr	Exception)selfe r   @/var/www/html/testcasegenerator.evdpl.com/utils/mongo_handler.py__init__   s    

zMongoHandler.__init__Nc              
   C   s   zDt t }||t ||i d}| j| td|  |W S  t	k
r } z t
dt |  t	dW 5 d}~X Y nX dS )z+Save test case data and generate unique URL)_id	test_data
created_aturl_keyitem_idstatusz&Successfully saved test case with ID: zError saving test case: z$Failed to save test case to databaseN)r   uuiduuid4r   utcnowr   Z
insert_oner   r   r   r   )r   r   r   	unique_iddocumentr   r   r   r   save_test_case   s    zMongoHandler.save_test_casec              
   C   s  z| j d|i}|s.td|  W dS td| d|  d}|rd|krd|kr| j d|idd	| |ii d
}td|  d}d|krt|d trd
}td|  |r|d }d}t|D ]~\}	}
|
	dd}||krtd|  | j d|idd|	 d|ii}|sL| j d|idd	| |ii d
} qXq|szt
d| d|  W dS W d
S d|krd|d kr|d d }d }d|kr|d}t|dkr|d  d|d  d|d  }td|  t|D ]\}	}
|
	d|
	dd}|
	d|
	dd}|r||krtd|  | j d|idd |	 d!|ii}|s| j d|idd	| |ii |jdkrtd"|   W d
S |rX|rX||krXtd#| d$|  | j d|idd |	 d!|ii}|s4| j d|idd	| |ii |jdkrXtd%|   W d
S |r||krtd& | j d|idd |	 d!|ii}|r|s| j d|idd	| |ii |jdkrtd'|   W d
S qt|D ]\}	}
|
	d(|ks|
	d)|krtd*|	  |
	d|
	dd}| j d|idd |	 d!|ii}|r|s| j d|idd	| |ii |jdk  W S qt
d+| d|  W dS t
d,| d- W dS W n> tk
r } ztd.t|  W Y dS d }~X Y nX d S )/Nr   z No document found with url_key: Fz/Updating status for test case with identifier 'z' in document ./$setzstatus.Tz-Updated central status dictionary for title: r   z Shared view update detected for Title z"Found shared view match by title: z
test_data.z.StatuszNo test case found with title 'z' in shared view document r   _   r         zExtracted UI identifier: titleContentcontentzFound match in title: ztest_data.test_cases.z.statusz/Successfully updated status by title match for zFound match for UI identifier z in title: z7Successfully updated status by UI identifier match for zFound match in contentz1Successfully updated status by content match for test_case_idzTest Case IDzFound direct ID match at index zNo test case found matching 'z	Document z has no test casesz!Error updating test case status: )r   find_oner   r   r   
update_one
isinstancelist	enumerategetwarningsplitlenZmodified_countr   r   )r   r   r/   r   docZtitle_foundis_shared_viewr   foundidxtcr,   resultZui_identifierpartsr.   r   r   r   r   update_test_case_status*   s    

 
 
z$MongoHandler.update_test_case_statusc              
   C   sn   z*| j d|i}|s&td|  |W S  tk
rh } z tdt|  tdW 5 d}~X Y nX dS )z"Retrieve test case data by URL keyr    No test case found for URL key: zError retrieving test case: z*Failed to retrieve test case from databaseN)r   r0   r   r6   r   r   r   )r   r   r>   r   r   r   r   get_test_case   s    zMongoHandler.get_test_caseFc           
   
   C   s  z| j d|i}|s.td|  W dS d|krLtd|d   n
td d|krt|d trt|d D ]B\}}|dd	}|d
d	}|rxtd| d| d| d qxn|d|kr:d|d kr:t|d d D ]R\}}|d|dd	}|d
|dd	}|rtd| d| d| d qd|krx|d rx|sxtdt	|d  d |d W S i }d|krt|d trtd |d D ]6}d|kr|dd	}|d
d	}|r|||< qnld|krNd|d krNtd |d d D ]<}|d|dd	}|d
|dd	}|r|||< q|rtdt	| d|  | j 
d|idd|ii tdt	| d|  |W S  tk
r }	 ztdt|	  W Y dS d}	~	X Y nX dS )zRetrieve all status values for test cases in a document
        
        Args:
            url_key: The unique URL key for the document
            force_refresh: If True, forces a direct database query to get fresh data
        r   rA   Nr   zSTATUS DICT in MongoDB: z"NO STATUS DICT in MongoDB documentr   r&   r'   StatuszSHARED VIEW TC[z
]: Title='z', Status=''r   r,   zMAIN VIEW TC[zFound z# status values in status dictionaryz.Building status values from shared view formatz,Building status values from main view formatz%UPDATING status dict in MongoDB with z	 values: r%   z
Returning z status values for z*Error retrieving test case status values: )r   r0   r   r6   r   r2   r3   r4   r5   r8   r1   r   r   r   )
r   r   force_refreshr>   ir=   r,   r   status_valuesr   r   r   r   get_test_case_status_values   sd    
" 




z(MongoHandler.get_test_case_status_values)N)F)__name__
__module____qualname__r   r"   r@   rB   rH   r   r   r   r   r   
   s   
 )r   )Zpymongor   Zpymongo.errorsr   r   config.settingsr   r   r   r   logging	getLoggerrI   r   r   r   r   r   r   <module>   s   
