o
    l~i3                     @   s   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	m
Z
 d dlmZ d ZdZddgZe d	Zg d
Zdd Zdd Zdd Zdd Zdd ZG dd dejZG dd de
jZdS )    N)forms)MaxValueValidatorMinValueValidatorRegexValidator)
connectionmodels)gettext_lazyl    HexadecimalFieldHexIntegerFieldz^(([0-9A-f])|(0x[0-9A-f]))+$)zdjango.db.backends.postgresqlz&django.db.backends.postgresql_psycopg2z&django.contrib.gis.db.backends.postgiszdjango.db.backends.sqlite3c                   C   s   t jd tv S )NENGINE)r   settings_dictsigned_integer_engines r   r   X/var/www/newdalilibackend/backend/venv/lib/python3.10/site-packages/fcm_django/fields.py_using_signed_storage      r   c                 C      t dt d| d S )NQqr   structunpackpackvaluer   r   r   _signed_to_unsigned_integer      r   c                 C   r   )Nr   r   r   r   r   r   r   r   _unsigned_to_signed_integer   r   r   c                 C   s
   t | dS )N   )intr   r   r   r   _hex_string_to_unsigned_integer#   s   
r    c                 C   s   t | dS )NL)hexrstripr   r   r   r   _unsigned_integer_to_hex_string'   r   r$   c                       s,   e Zd ZdZ fddZ fddZ  ZS )r	   z<
    A form field that accepts only hexadecimal numbers
    c                    s*   t ttddg| _t j|i | d S )Nz Enter a valid hexadecimal numberinvalid)r   hex_re_default_validatorssuper__init__)selfargskwargs	__class__r   r   r*   0   s   zHexadecimalField.__init__c                    s2   |rt |tstjdv rt|}ttj| |S )N)mysqlsqlite)	
isinstancestrr   vendorr$   r)   r   	CharFieldprepare_valuer+   r   r.   r   r   r6   6   s   
zHexadecimalField.prepare_value)__name__
__module____qualname____doc__r*   r6   __classcell__r   r   r.   r   r	   +   s    c                       s`   e Zd ZdZeeeegZ fddZ	dd Z
dd Zdd	 Z fd
dZ fddZ  ZS )r
   a  
    This field stores a hexadecimal *string* of up to 64 bits as an unsigned integer
    on *all* backends including postgres.

    Reasoning: Postgres only supports signed bigints. Since we don't care about
    signedness, we store it as signed, and cast it to unsigned when we deal with
    the actual value (with struct)

    On sqlite and mysql, native unsigned bigint types are used. In all cases, the
    value we deal with in python is always in hex.
    c                    s0   |j d }d|v rdS d|v rdS t j|dS )Nr   r0   zbigint unsignedr1   zUNSIGNED BIG INT)r   )r   r)   db_type)r+   r   enginer.   r   r   r=   S   s   
zHexIntegerField.db_typec                 C   s8   |du s|dkr
dS t |trt|}t rt|}|S )z9Return the integer value to be stored from the hex stringN )r2   r3   r    r   r   r7   r   r   r   get_prep_value\   s   
zHexIntegerField.get_prep_valuec                 C   s   |du r|S t  rt|}|S )z:Return an unsigned int representation from all db backendsN)r   r   )r+   r   
expressionr   contextr   r   r   from_db_valuef   s
   zHexIntegerField.from_db_valuec                 C   s"   t |tr|S |du r|S t|S )z.Return a str representation of the hexadecimalN)r2   r3   r$   r7   r   r   r   	to_pythonn   s
   
zHexIntegerField.to_pythonc                    s*   dt i}|| ttj| jdi |S )N
form_classr   )r	   updater)   r   IntegerField	formfield)r+   r-   defaultsr.   r   r   rH   v   s   
zHexIntegerField.formfieldc                    s   t |}ttj| |S )N)r    r)   r   BigIntegerFieldrun_validatorsr7   r.   r   r   rK   |   s   zHexIntegerField.run_validators)r8   r9   r:   r;   r   UNSIGNED_64BIT_INT_MIN_VALUEr   UNSIGNED_64BIT_INT_MAX_VALUE
validatorsr=   r@   rC   rD   rH   rK   r<   r   r   r.   r   r
   A   s    	
)rer   djangor   django.core.validatorsr   r   r   	django.dbr   r   django.utils.translationr   r'   rL   rM   __all__compiler&   r   r   r   r   r    r$   r5   r	   rJ   r
   r   r   r   r   <module>   s$    
