HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //home/arjun/projects/buyercall/buyercall/lib/__pycache__/util_sqlalchemy.cpython-310.pyc
o

�we3�@s�ddlZddlmZddlmZddlmZddlmZddl	m
Z
ddlmZddl
mZGd	d
�d
e�ZGdd�de
�ZGd
d�de�ZdS)�N)�inspect)�DateTime)�
TypeDecorator)�UUID)�SerializerMixin)�tzware_datetime)�dbc@s.eZdZdZedd�ZdZdd�Zdd�Zd	S)
�
AwareDateTimez�
    A DateTime type which can only store tz-aware DateTimes.

    Source:
      https://gist.github.com/inklesspen/90b554c864b99340747e
    T)�timezoneFcCs(t|tj�r|jdurtd�|���|S)Nz{!r} must be TZ-aware)�
isinstance�datetime�tzinfo�
ValueError�format)�self�value�dialect�r�?/home/arjun/projects/buyercall/buyercall/lib/util_sqlalchemy.py�process_bind_paramsz AwareDateTime.process_bind_paramcCsdS)NzAwareDateTime()r�rrrr�__repr__szAwareDateTime.__repr__N)	�__name__�
__module__�__qualname__�__doc__r�impl�cache_okrrrrrrr	s
r	c@seZdZedd�ffZdS)�CustomSerializerMixincCst|�S�N��str)�xrrr�<lambda> szCustomSerializerMixin.<lambda>N)rrrr�serialize_typesrrrrrs
�rc@s�eZdZeje�ded�Zeje�deed�Ze	dd��Z
e	gdfdd��Ze	d	d
��Zdd�Z
d
d�Zdd�Ze	dd��Ze	dd��Ze	dd��Ze	dd��Ze	dd��Zddd�ZdS)�
ResourceMixinF)�nullable�default)r&r'�onupdatecCs$||jjvrd}|dvrd}||fS)z�
        Sort a resource by a specific field and direction.

        :param field: Field name
        :type field: str
        :param direction: Direction
        :type direction: str
        :return: tuple
        �
created_on)�asc�descr*)�	__table__�columns)�cls�field�	directionrrr�sort_by.s
zResourceMixin.sort_by�csXttt����|dkr|j�|j��|�|��}dd�|D�}�r*�fdd�|D�}|S)a�
        Determine which IDs are to be modified.

        :param scope: Affect all or only a subset of items
        :type scope: str
        :param ids: List of ids to be modified
        :type ids: list
        :param omit_ids: Remove 1 or more IDs from the list
        :type omit_ids: list
        :param query: Search query (if applicable)
        :type query: str
        :return: list
        �all_search_resultscSsg|]}t|d��qS)rr )�.0�itemrrr�
<listcomp>Y�z5ResourceMixin.get_bulk_action_ids.<locals>.<listcomp>csg|]}|�vr|�qSrr)r4�id��omit_idsrrr6_r7)�list�mapr!�query�
with_entitiesr8�filter�search)r.�scope�idsr:r=rr9r�get_bulk_action_idsBsz!ResourceMixin.get_bulk_action_idscCs*|j�|j�|��jdd�}tj��|S)z�
        Delete 1 or more model instances.

        :param ids: List of ids to be deleted
        :type ids: list
        :return: Number of deleted instances
        F)�synchronize_session)r=r?r8�in_�deleter�session�commit)r.rB�delete_countrrr�bulk_deletecs
	�
zResourceMixin.bulk_deletecCstj�|�tj��|S)z?
        Save a model instance.

        :return: self
        )rrG�addrHrrrr�savers
zResourceMixin.savecCstj�|�tj��S)zY
        Delete a model instance.

        :return: db.session.commit()'s result
        )rrGrFrHrrrrrF}s
zResourceMixin.deletecsBtt���}�jj��}d��fdd�|D��}d|�jj|fS)Nz, c3s$�|]
}d|t�|d�fVqdS)z%s=%rN)�getattr)r4�nrrr�	<genexpr>�s�"z(ResourceMixin.__str__.<locals>.<genexpr>z<%s %s(%s)>)�hexr8r,�c�keys�join�	__class__r)r�obj_idr-�valuesrrr�__str__�szResourceMixin.__str__cC�<d}|rz|j�|j|k���}|j}W|SY|S|Sr)r=r?r8�first�sid)r.r8rZ�objrrr�get_sid_from_id���zResourceMixin.get_sid_from_idcCrXr)r=r?rZrYr8)r.rZr8r[rrr�get_id_from_sid�r]zResourceMixin.get_id_from_sidcKs|di|��}|��|S)Nr)rL)r.�paramsr[rrr�create�szResourceMixin.createcC�|r
|j�|j|k���SdSr)r=r?r8rY)r.�id_rrr�	get_by_id��zResourceMixin.get_by_idcCrar)r=r?rZrY)r.rZrrr�
get_by_sid�rdzResourceMixin.get_by_sidc
Kslzt�}|��D]
\}}t|||�q|��|r|WSdWSty5}zt|�WYd}~dSd}~ww)NTF)�locals�items�setattrrL�	Exception�print)r�
return_object�kwargsr_�k�v�errr�put�s��zResourceMixin.putN)F)rrrr�Columnr	rr)�
updated_on�classmethodr1rCrJrLrFrWr\r^r`rcrerprrrrr%$s@��
� 







r%)r�sqlalchemy.inspectionr�
sqlalchemyr�sqlalchemy.typesr�uuidr�sqlalchemy_serializerr�buyercall.lib.util_datetimer�buyercall.extensionsrr	rr%rrrr�<module>s