
    PIiT                     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	 d dl
Z
 G d de      Zy)    )FacebookBadObjectError)FacebookAdsApiCursorFacebookRequest)AbstractObject)ObjectParserNc                   4    e Zd ZdZd fd	Z fdZd Zd Zd Ze	dd       Z
d Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z	 	 	 	 	 	 d dZ	 	 	 	 	 	 d dZ	 	 	 	 	 	 d dZ	 	 	 	 	 d!dZd Z	 	 	 ddZeZ	 	 	 	 	 d"dZ	 	 	 d#dZ ddZ!d Z" xZ#S )$AbstractCrudObjectz
    Extends AbstractObject and implements methods to create, read, update,
    and delete.
    Attributes:
        parent_id: The object's parent's id. (default None)
        api: The api instance associated with this object. (default None)
    c                     t         t        |           |xs t        j                         | _        i | _        |d}t        j                  |       || _	        || j                  d<   d| _        y)ay  Initializes a CRUD object.
        Args:
            fbid (optional): The id of the object ont the Graph.
            parent_id (optional): The id of the object's parent.
            api (optional): An api object which all calls will go through. If
                an api object is not specified, api calls will revert to going
                through the default api.
        Nz<parent_id as a parameter of constructor is being deprecated.idT)superr
   __init__r   get_default_api_api_changesloggingwarning
_parent_id_data_include_summary)selffbid	parent_idapiwarning_message	__class__s        |/var/www/webhook.estador.de/web/meta/bot/venv/lib/python3.12/site-packages/facebook_business/adobjects/abstractcrudobject.pyr   zAbstractCrudObject.__init__   se     	 $02;>99;	!&OOOO,#

4 $    c                     || j                   vs| j                   |   |k7  r|| j                  |<   t        t        |   ||       dt        |       v r| j                  ||       | S )z?Sets an item in this CRUD object while maintaining a changelog._setitem_trigger)r   r   r   r
   __setitem__dirr    )r   keyvaluer   s      r   r!   zAbstractCrudObject.__setitem__3   sa     djj DJJsOu$<!&DMM# $3C?T*!!#u-r   c                 V    | j                   |= | j                  j                  |d        y N)r   r   pop)r   r#   s     r   __delitem__zAbstractCrudObject.__delitem__>   s!    JJsO#t$r   c                     t        || j                        xrE | j                         xr3 |j                         xr! | j                         |j                         k(  S )z4Two objects are the same if they have the same fbid.)
isinstancer   get_idr   others     r   __eq__zAbstractCrudObject.__eq__B   sR     udnn- , KKM, $lln, KKMU\\^+		
r   c                 &    | j                  |       S r&   )r.   r,   s     r   __ne__zAbstractCrudObject.__ne__O   s    ;;u%%%r   c                    |xs t        j                         }t        |xs i       }| j                  ||       dj	                  t        t        |            |d<   |j                  ddg|      }g }|j                         j                         D ]1  \  }} | ||      }	|	j                  |       |j                  |	       3 |S )N,idsGET/params)r   )r   r   dict_assign_fields_to_paramsjoinmapstrcalljsonitems	_set_dataappend)
clsr3   r7   fieldsr   responseresultr   dataobjs
             r   
get_by_idszAbstractCrudObject.get_by_idsR   s    5^335fl#$$VV4S#/u88E  

 "--///1 	JD$d$CMM$MM#	 r   c                     t        | d      r/t        | j                  d      r| | j                  j                     S | d   S )z8Returns the object's fbid if set. Else, it returns None.Fieldr   )hasattrrJ   r   r   s    r   r+   zAbstractCrudObject.get_idf   s8    &-dG&<U\A]tDJJMM"mcghlcmmr   c                 v    d}t        j                  |       	 | j                  xs t        j                         S )Nparent_id is being deprecated.)r   r   r   r   get_default_account_idr   r   s     r   get_parent_idz AbstractCrudObject.get_parent_idk   s.    :(/I."G"G"IIr   c                     | j                   S )z=
        Returns the api associated with the object.
        )r   rL   s    r   get_apizAbstractCrudObject.get_apiq   s     yyr   c                     | j                  | j                  j                        s"t        d| j                  j
                  z        | j                         S )z~Returns the fbid of the object.
        Raises:
            FacebookBadObjectError if the object does not have an id.
        z)%s object needs an id for this operation.)getrJ   r   r   r   __name__r+   rL   s    r   get_id_assuredz!AbstractCrudObject.get_id_assuredw   sH    
 xx

&(;..))* 
 {{}r   c                     d}t        j                  |       | j                         s"t        d| j                  j
                  z        | j                         S )zReturns the object's parent's fbid.
        Raises:
            FacebookBadObjectError if the object does not have a parent id.
        rN   z/%s object needs a parent_id for this operation.)r   r   rQ   r   r   rV   rP   s     r   get_parent_id_assuredz(AbstractCrudObject.get_parent_id_assured   sT    
 ;(!!#(A..))* 
 !!##r   c                 n    | j                         }|s"t        d| j                  j                  z        |S )ztReturns the fbid of the object.
        Raises:
            FacebookBadObjectError if get_api returns None.
        z%s does not yet have an associated api object.
Did you forget to instantiate an API session with: FacebookAdsApi.init(app_id, app_secret, access_token))rS   r   r   rV   )r   r   s     r   get_api_assuredz"AbstractCrudObject.get_api_assured   s@    
 lln(H ..))*  
r   c                 J    i | _         d| j                  v r| j                  d= | S )Nfilename)r   r   rL   s    r   _clear_historyz!AbstractCrudObject._clear_history   s&    #

:&r   c                     t        t        |      D ]&  }||   | |<   | j                  j                  |d       ( || _        | S )zo
        Sets object's data as if it were read from the server.
        Warning: Does not log changes.
        N)r;   r<   r   r'   _json)r   rF   r#   s      r   r@   zAbstractCrudObject._set_data   sJ    
 sD> 	)CS	DI MMc4(		)
 
r   c                 8    | j                  | j                        S )z
        Returns a dictionary of property names mapped to their values for
        properties modified from their original values.
        )export_valuer   rL   s    r   export_changed_dataz&AbstractCrudObject.export_changed_data   s    
   //r   c                 "    | j                         S )zU
        Deprecated. Use export_all_data() or export_changed_data() instead.
        )rc   rL   s    r   export_datazAbstractCrudObject.export_data   s     ''))r   c                 4    | | j                   j                  = | S )zClears the object's fbid.)rJ   r   rL   s    r   clear_idzAbstractCrudObject.clear_id   s    r   c                 $    | j                         fS )z6Returns the node's relative path as a tuple of tokens.)rW   rL   s    r   get_node_pathz AbstractCrudObject.get_node_path   s    ##%''r   c                 @    dj                  | j                               S )z#Returns the node's path as a tuple.r5   )r:   ri   rL   s    r   get_node_path_stringz'AbstractCrudObject.get_node_path_string   s    xx**,--r   c           
      T    d}t        j                  |        j                         r"t        d j                  j
                  z        dt               vr"t        d j                  j
                  z        |si n|j                         }|j                   j                                d}t         d      r" j                   j                         d      }nKt         j                         d	 j                          j                    j                  t#         
            }|j%                  |       |j'                  |       | fd}	fd}
|j)                  ||	|
      S |j+                         } j-                  |j.                          j1                           S )a  Creates the object by calling the API.
        Args:
            batch (optional): A FacebookAdsApiBatch object. If specified,
                the call will be added to the batch.
            params (optional): A mapping of request parameters where a key
                is the parameter name and its value is a string or an object
                which can be JSON-encoded.
            files (optional): An optional mapping of file names to binary open
                file objects. These files will be attached to the request.
            success (optional): A callback function which will be called with
                the FacebookResponse of this call if the call succeeded.
            failure (optional): A callback function which will be called with
                the FacebookResponse of this call if the call failed.
        Returns:
            self if not a batch call.
            the return value of batch.add if a batch call.
        zO`remote_create` is being deprecated, please update your code with new function.z#This %s object was already created.get_endpointz Cannot create object of type %s.N
api_createTpendingPOSTreuse_objectnode_idmethodendpointr   target_classresponse_parserc                 z    j                  | j                                j                          r	 |        y y r&   )r@   r>   r^   rD   r   successs    r   callback_successz:AbstractCrudObject.remote_create.<locals>.callback_success  s1    x}}/##%H% r   c                     r	 |        y y r&    rD   failures    r   callback_failurez:AbstractCrudObject.remote_create.<locals>.callback_failure      H% r   requestr|   r   )r   r   r+   r   r   rV   r"   	TypeErrorcopyupdateexport_all_datarK   rn   rY   r   rm   r   r   
add_params	add_filesadd_requestexecuter@   r`   r^   )r   batchr   filesr7   r|   api_versionr   r   r}   r   rD   s   ` `  `      r   remote_createz AbstractCrudObject.remote_create   s   4 l(;;=(5..))*  T*>"nn556 7 7 "v{{}d**,-4&ood&@&@&BDoQG%224**,II!^^ ,!%!	G 	6"% && $$(( %   (HNN8>>*!Kr   c           
          d}t        j                  |       t        |xs i       }t         d      r j	                  d      }n=t         j                         dd j                   j                  t                     }|j                  |       |j                  |       |! fd	}	fd
}
|j                  ||	|
      }|S |j                           S )a  Reads the object by calling the API.
        Args:
            batch (optional): A FacebookAdsApiBatch object. If specified,
                the call will be added to the batch.
            fields (optional): A list of fields to read.
            params (optional): A mapping of request parameters where a key
                is the parameter name and its value is a string or an object
                which can be JSON-encoded.
            files (optional): An optional mapping of file names to binary open
                file objects. These files will be attached to the request.
            success (optional): A callback function which will be called with
                the FacebookResponse of this call if the call succeeded.
            failure (optional): A callback function which will be called with
                the FacebookResponse of this call if the call failed.
        Returns:
            self if not a batch call.
            the return value of batch.add if a batch call.
        zM`remote_read` is being deprecated, please update your code with new function.api_getTro   r4   r5   rr   rt   c                 Z    j                  | j                                r	 |        y y r&   )r@   r>   r{   s    r   r}   z8AbstractCrudObject.remote_read.<locals>.callback_successZ  s%    x}}/H% r   c                     r	 |        y y r&   r   r   s    r   r   z8AbstractCrudObject.remote_read.<locals>.callback_failure`  r   r   r   )r   r   r8   rK   r   r   rW   r   r   r   r   
add_fieldsr   r   )r   r   r   rC   r7   r|   r   r   r   r}   r   
batch_calls   ` `  `      r   remote_readzAbstractCrudObject.remote_read+  s    6 j(fl#4#ll4l0G%++-II!^^ ,!%!	G 	6"6"&& **(( + J
 ??$DKr   c           
      D    d}t        j                  |       |si n|j                         }|j                   j	                                 j                  |       t         d      r j                  d      }n=t         j                         dd j                   j                  t                     }|j                  |       |j                  |       |! fd	}	fd
}
|j                  ||	|
      }|S |j!                           j#                           S )a  Updates the object by calling the API with only the changes recorded.
        Args:
            batch (optional): A FacebookAdsApiBatch object. If specified,
                the call will be added to the batch.
            params (optional): A mapping of request parameters where a key
                is the parameter name and its value is a string or an object
                which can be JSON-encoded.
            files (optional): An optional mapping of file names to binary open
                file objects. These files will be attached to the request.
            success (optional): A callback function which will be called with
                the FacebookResponse of this call if the call succeeded.
            failure (optional): A callback function which will be called with
                the FacebookResponse of this call if the call failed.
        Returns:
            self if not a batch call.
            the return value of batch.add if a batch call.
        zO`remote_update` is being deprecated, please update your code with new function.
api_updateTro   rq   r5   rr   rt   c                 <    j                          r	 |        y y r&   )r^   r{   s    r   r}   z:AbstractCrudObject.remote_update.<locals>.callback_success  s    ##%H% r   c                     r	 |        y y r&   r   r   s    r   r   z:AbstractCrudObject.remote_update.<locals>.callback_failure  r   r   r   )r   r   r   r   rc   r@   rK   r   r   rW   r   r   r   r   r   r   r   r^   )r   r   r   r   r7   r|   r   r   r   r}   r   r   s   ` `  `      r   remote_updatez AbstractCrudObject.remote_updatep  s   4 l(!v{{}d..01v4&oodo3G%++-II!^^ ,!%!	G 	6"% && **(( + J
 OO!Kr   c                 n    d}t        j                  |       t         d      r j                  d      }n't	         j                         dd j                        }|j                  |       |! fd}fd	}	|j                  |||	
      }
|
S |j                           j                           S )a8  Deletes the object by calling the API with the DELETE http method.
        Args:
            batch (optional): A FacebookAdsApiBatch object. If specified,
                the call will be added to the batch.
            params (optional): A mapping of request parameters where a key
                is the parameter name and its value is a string or an object
                which can be JSON-encoded.
            success (optional): A callback function which will be called with
                the FacebookResponse of this call if the call succeeded.
            failure (optional): A callback function which will be called with
                the FacebookResponse of this call if the call failed.
        Returns:
            self if not a batch call.
            the return value of batch.add if a batch call.
        zO`remote_delete` is being deprecated, please update your code with new function.
api_deleteTro   DELETEr5   )ru   rv   rw   r   c                 <    j                          r	 |        y y r&   )rg   r{   s    r   r}   z:AbstractCrudObject.remote_delete.<locals>.callback_success  s    H% r   c                     r	 |        y y r&   r   r   s    r   r   z:AbstractCrudObject.remote_delete.<locals>.callback_failure  r   r   r   )r   r   rK   r   r   rW   r   r   r   r   rg   )r   r   r   r7   r|   r   r   r   r}   r   r   s   ` ` `      r   remote_deletez AbstractCrudObject.remote_delete  s    . l(4&oodo3G%++-II	G 	6"&& **(( + J
 OOMMOKr   c                     d}t        j                  |       | j                         r | j                  |i |S  | j                  |i |S )zz
        Calls remote_create method if object has not been created. Else, calls
        the remote_update method.
        zM`remote_save` is being deprecated, please update your code with new function.)r   r   r+   r   r   )r   argskwargsr   s       r   remote_savezAbstractCrudObject.remote_save  sO    
 j(;;=%4%%t6v66%4%%t6v66r   c                     dt        |       vsdt        | j                        vr"t        d| j                  j                  z        | j                  d| j                  j                  i|||      S )NStatusarchivedz!Cannot archive object of type %s.status)r7   r   r   r|   )r"   r   r   r   rV   rn   r   )r   r   r   r|   s       r   remote_archivez!AbstractCrudObject.remote_archive  sw     3t9$
#dkk:J(J?"nn556 7 7$++..   
 	
r   c                 P    | }t        ||||||      }|r|j                          |S )z
        Returns Cursor with argument self as source_object and
        the rest as given __init__ arguments.
        Note: list(iterate_edge(...)) can prefetch all the objects.
        )rC   r7   include_summaryrw   )r   load_next_page)	r   target_objects_classrC   r7   fetch_first_pager   rw   source_objectcursors	            r   iterate_edgezAbstractCrudObject.iterate_edge  s;      +
 !!#r   c                    ddl m} 	 | }| j                  |||||      }	|r|	S |si }nt        |      }| j                  j                  ||       ||j                         }| j                         j                  d| j                         |f|      j                         }
 |       }d|
v r|
d   |
d<   |j                  |
       |S )Nr   )AdReportRun)r   r   rq   r6   report_run_idr   )'facebook_business.adobjects.adreportrunr   r   r8   r   r9   rm   r[   r=   rW   r>   r@   )r   r   rC   r7   is_asyncr   rw   r   synchronoussynchronous_iteratorrD   rE   s               r   iterate_edge_asyncz%AbstractCrudObject.iterate_edge_async1  s     	H	  #l#00 (+  1  
 ''F&\F//?
 +88:H'')..  "H- / 
 $&	 	 h&%o6HTN"r   c                 p    |si n|j                         }d|d<   | j                  ||||      D ]  }|c S  y)z
        Returns first object when iterating over Cursor with argument
        self as source_object and the rest as given __init__ arguments.
        1limit)rC   r7   rw   N)r   r   )r   r   rC   r7   rw   rG   s         r   edge_objectzAbstractCrudObject.edge_objectj  sS    
 "v{{}w$$ 	 % 
 	C J	 r   c                 2    | j                   st        d      y )Nz)Api call cannot be made if api is not set)r   r   rL   s    r   assure_callzAbstractCrudObject.assure_call|  s    yy(;= = r   )NNN)NNNNNN)NNNNN)NNTTN)NNFTN)$rV   
__module____qualname____doc__r   r!   r(   r.   r0   classmethodrH   r+   rQ   rS   rW   rY   r[   r^   r@   rc   re   rg   ri   rk   r   r   r   r   r   r   saver   r   r   r   __classcell__)r   s   @r   r
   r
      s.   %*	%
&  &n
J$$0*
(. Oj AN DT 8z
7 	
& D
 6 ?CHL$(7r$=r   r
   )facebook_business.exceptionsr   facebook_business.apir   r   r   *facebook_business.adobjects.abstractobjectr   (facebook_business.adobjects.objectparserr   r   r
   r   r   r   <module>r      s.     F A j	= j	=r   