o
    [h2                     @   s  d dl Z d dlmZ d dlmZ d dlmZmZm	Z	 d dl
mZ d dlZd dlm  mZ d dlm  m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 d dlmZmZmZ d dl m!Z! d d	l m"Z"m#Z# d d
lm$Z$m%Z% d dl&m'Z' d dl(m)Z) d dl*m+Z+m,Z, d dl-m.Z. d dl/m0Z0 dd Z1G dd deZ2G dd deZ3G dd deZ4dd Z5dd Z6ddddZ7G dd  d eZ8G d!d" d"e.eZ9G d#d$ d$e.eZ:dS )%    N)core_gettext)HTTPEndpoint)HTMLResponseResponseRedirectResponse)Request)settings)	make_hashBotErrorGlobalState)NoResultFound)dbdbq)ParticipantSession)ParticipantVarsFromREST)	ROOM_DICT)ibis_loaderrender)GenericWaitPageMixin)
TurkClientc                   C   s   t tdddS )z
    this function exists because i'm not sure if Http response objects can be reused
    better to return 404 so browser bot client & tests can recognize it
    zSession is full.  status_code)r   _ r   r   ~/home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/venv/lib/python3.10/site-packages/otree/views/participant.py"no_participants_left_http_response   s   r   c                   @   s   e Zd ZdZdd ZdS )OutOfRangeNotificationz/OutOfRangeNotification/{code}c                 C   s\   |j d }tjt|d}|jr)|j}tj|jd}|r!d}t	|tj
|j|d tdi S )Ncoder   )participant_codezIFinished the last page, but the bot is still trying to submit more pages.)session_coder!   z!otree/OutOfRangeNotification.html)path_paramsr   
get_or_404r   Zis_browser_botsessionbrowser_botsZenqueue_next_post_datar   r
   Zsend_completion_messager   )selfrequestr   participantr%   Zhas_next_submissionmsgr   r   r   get)   s   

zOutOfRangeNotification.getN)__name__
__module____qualname__url_patternr+   r   r   r   r   r   &   s    r   c                   @      e Zd ZdZdefddZdS )InitializeParticipantz/InitializeParticipant/{code}r(   c                 C   sB   |j d }tjt|d}|jtjj}|	| |
 }t|S )z$anything essential should be done inr   r    )r#   r   r$   r   query_paramsr+   otree	constantsparticipant_labelZ
initializeZ_url_i_should_be_onr   )r'   r(   r   pplabelZ	first_urlr   r   r   r+   E   s   

zInitializeParticipant.getNr,   r-   r.   r/   r   r+   r   r   r   r   r1   A       r1   c                   @   r0   )
MTurkStartz/MTurkStart/{code}r(   c                 C   sB  |j d }tjt|d }| _|j}z
|d }|d }W n ty+   tddd Y S w |jd 	d	}|j
}|rf|sfttttj|ktj|ktj|k d u}	|	rZtd
S tjt||dd|d z| jjj|d }
W n# ty   | jjjddd }
|
st  Y S d|
_||
_Y nw ||
_t|
 ddS )Nr   r    ZassignmentIdZworkerIdz1URL is missing assignmentId or workerId parameterr   r   Zmturk_hit_settingsZgrant_qualification_idz'You have already accepted a related HIT   )ZQualificationTypeIdZWorkerIdZIntegerValue)use_sandbox)mturk_worker_idFvisitedidT.  ) r#   r   r$   r   r%   r2   KeyErrorr   configr+   Zmturk_use_sandboxr   r   joinfilterZmturk_qual_idr=   Zscalarr   Zassign_qualificationdictpp_set	filter_byoner   order_byfirstr   r?   Zmturk_assignment_idr   
_start_url)r'   r(   r   r%   GETZassignment_idZ	worker_idZqual_idr<   Zprevious_participationr6   r   r   r   r+   U   s\   

	


zMTurkStart.getNr8   r   r   r   r   r:   Q   r9   r:   c                 C   sF   | j }|rz	|j|d W S  ty   Y nw |jddd S )Nr7   Fr>   r@   )rG   rH   rI   r   rJ   rK   )r%   r7   qr   r   r   get_existing_or_new_participant   s   rP   c                 C   sX   d | j}||}|rtj|d S | jjddd }|r*|j||< |S d S )Nzsession_{}_participantr    Fr>   r@   )	formatr   r+   r   objects_filterrK   rG   rH   rJ   )r%   cookiesZcookie_namer   r)   r   r   r   !get_participant_with_cookie_check   s   

rT   )rS   c                C   s>   |du r
t | |}nt| |}|sdS d|_|r|| |S )z>pass request.session as an arg if you want to get/set a cookieNT)rP   rT   r?   Z	set_label)r%   r7   rS   r)   r   r   r   participant_or_none_if_exceeded   s   

rU   c                   @   r0   )JoinSessionAnonymouslyz/join/{anonymous_code}r(   c                 C   sF   |j d }tjt|d}|jd}t||d}|st S t|	 S )Nanonymous_code)Z_anonymous_coder5   rN   )
r#   r   r$   r   r2   r+   rU   r   r   rL   )r'   r(   rW   r%   r7   r)   r   r   r   r+      s   
zJoinSessionAnonymously.getNr8   r   r   r   r   rV      r9   rV   c                   @   s.   e Zd ZdZdefddZdd Zdd Zd	S )
AssignVisitorToRoomz/room/{room_name}r(   c                 C   sl  |j d }|| _zt| j }W n ty   tddd Y S w |jdd}|jr[|r4d}|| v}nd}d}|s?|rF|j	sFt
d	d
|iS |j	r[|jd}|t|kr[tdddS | }|d u rtj | _tj| j|| jd| _t
dt| tdtddS |rd }	n|j}	t|||	d}
|
st S |rtj| j|d }|r|
j|j t| t |
! S )N	room_namezInvalid room specified in urlr   r   r5    FTzotree/RoomInputLabel.htmlinvalid_labelhashz^Invalid hash parameter. use_secure_urls is True, so you must use the participant-specific URL.)rY   r5   tab_unique_idzotree/WaitPageRoom.htmlPlease waitz!Waiting for your session to beginview
title_textZ	body_text)r7   rS   )rY   r5   )"r#   rY   r   rB   r   r2   r+   Zhas_participant_labelsZget_participant_labelsZuse_secure_urlsr   r	   Zget_sessionr3   commonZrandom_chars_join_coder]   channel_utilsZroom_participant_path_socket_urlrF   r   r%   rU   r   r   rR   rK   varsupdater   deleter   rL   )r'   r(   rY   roomr7   Zmissing_labelr[   r\   r%   rS   r)   Zpassed_varsr   r   r   r+      sx   
	
zAssignVisitorToRoom.getc                 K   s
   d| j iS )Nrh   )rY   )r'   kwargsr   r   r   get_context_data%  s   
z$AssignVisitorToRoom.get_context_datac                 C   s   | j S )N)rd   r'   r   r   r   
socket_url(  s   zAssignVisitorToRoom.socket_urlN)r,   r-   r.   r/   r   r+   rj   rl   r   r   r   r   rX      s
    LrX   c                   @   s$   e Zd ZdZdZdd Zdd ZdS )BrowserBotStartLinkzushould i move this to another module?
    because the rest of these views are accessible without password login.
    z/browser_bot_startc                 C   s~   t j}|r3ztj|d}W n	 ty   Y nw |jjddd }|s(t	 S d|_
t| ddS t| dd	d
}td|S )Nr    Fr>   r@   TrA   r   r^   z)Waiting for browser bots session to beginr_   zotree/WaitPage.html)r   Z"browser_bots_launcher_session_coder   Zobjects_getr   rG   rH   rJ   rK   r   r?   r   rL   rF   r   )r'   r(   r"   r%   r)   ctxr   r   r   r+   5  s&   
zBrowserBotStartLink.getc                 C   s   dS )Nz/browser_bot_wait/r   rk   r   r   r   rl   P  s   zBrowserBotStartLink.socket_urlN)r,   r-   r.   __doc__r/   r+   rl   r   r   r   r   rm   ,  s
    rm   );timeZ
otree.i18nr   r   Zstarlette.endpointsr   Zstarlette.responsesr   r   r   Zstarlette.requestsr   datetimeZotree.bots.browserZbotsZbrowserr&   Zotree.channels.utilsZchannelsutilsrc   Zotree.commonr3   Zotree.constantsZotree.modelsZotree.views.adminZotree.views.cbvZotree.views.mturkr   r	   r
   r   Zotree.databaser   r   r   r   r   Zotree.models_concreter   Z
otree.roomr   Zotree.templatingr   r   Zotree.views.abstractr   Zotree.mturk_clientr   r   r   r1   r:   rP   rT   rU   rV   rX   rm   r   r   r   r   <module>   sD    N
W