o
    54g                     @   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dgZd ZdZe d	e jZd
dg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_lazyHexadecimalFieldHexIntegerFieldl    z^(0x)?([0-9a-f])+$
postgresqlsqlitec                   C   s
   t jtv S N)r   vendorsigned_integer_vendors r   r   _/var/www/html/notificationserver/venv/lib/python3.10/site-packages/push_notifications/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#   r   r"   c                 C   s   t | dS )NL)hexrstripr   r   r   r   _unsigned_integer_to_hex_string'   s   r&   c                       s,   e Zd ZdZ fddZ fddZ  ZS )r	   z6
	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,   /   s   zHexadecimalField.__init__c                    s2   |rt |tstjdv rt|}ttj| |S )N)mysqlr   )	
isinstancestrr   r   r&   r+   r   	CharFieldprepare_valuer-   r   r0   r   r   r6   5   s   
zHexadecimalField.prepare_value)__name__
__module____qualname____doc__r,   r6   __classcell__r   r   r0   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                    s*   d|j krdS d|j krdS t j|dS )Nr2   zbigint unsignedr   zUNSIGNED BIG INT)r   )r   r+   db_type)r-   r   r0   r   r   r=   O   s
   

zHexIntegerField.db_typec                 C   s8   |du s|dkr
dS t |trt|}t rt|}|S )z; Return the integer value to be stored from the hex string N )r3   r4   r"   r   r   r7   r   r   r   get_prep_valueW   s   
zHexIntegerField.get_prep_valuec                 G   s   |du r|S t  rt|}|S )z< Return an unsigned int representation from all db backends N)r   r   )r-   r   r.   r   r   r   from_db_valuea   s
   zHexIntegerField.from_db_valuec                 C   s"   t |tr|S |du r|S t|S )z0 Return a str representation of the hexadecimal N)r3   r4   r&   r7   r   r   r   	to_pythoni   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/   defaultsr0   r   r   rE   q   s   
zHexIntegerField.formfieldc                    s   t |}ttj| |S r   )r"   r+   r   BigIntegerFieldrun_validatorsr7   r0   r   r   rH   w   s   zHexIntegerField.run_validators)r8   r9   r:   r;   r   UNSIGNED_64BIT_INT_MIN_VALUEr   UNSIGNED_64BIT_INT_MAX_VALUE
validatorsr=   r?   r@   rA   rE   rH   r<   r   r   r0   r   r
   =   s    
)rer   djangor   django.core.validatorsr   r   r   	django.dbr   r   django.utils.translationr   r)   __all__rI   rJ   compileIr(   r   r   r   r   r"   r&   r5   r	   rG   r
   r   r   r   r   <module>   s(    