o
     -Ãh   ã                   @   s^   d dl mZ d dlmZ d dlmZ d dlmZ ddlm	Z	m
Z
 ddlmZ G dd	„ d	ƒZd
S )é    )Úredirect)Úreverse)Úmessages)ÚHttpResponseRedirecté   )ÚTenantÚ
TenantUser)Úrouterc                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚTenantMiddlewarec                 C   s
   || _ d S )N)Úget_response)Úselfr   © r   ú'/var/www/html/pos/tenants/middleware.pyÚ__init__	   s   
zTenantMiddleware.__init__c              	   C   sŽ  t  d ¡ |j d¡s|j d¡s|j d¡r)z|  |¡W t  d ¡ S t  d ¡ w |j d¡sF|j d¡sF|jdksF|j d¡sF|j d¡rXz|  |¡W t  d ¡ S t  d ¡ w zè|jjr6|j d	¡}|r%z¢t	j
j|d
}|jdkrƒtd|› dƒW W t  d ¡ S |jsœt |d|j› d¡ tdƒW W t  d ¡ S ||_t  |¡ |jjszBtj
j||jdd}d|jjv rç|jj dd¡d }t	j
j|d ¡ rç| ¡ | ¡ krçt |d¡ tdƒW W W t  d ¡ S ||_W n tjy   t |d¡ tdƒ Y W W t  d ¡ S w W n, t	jy$   t |d¡ tdƒ Y W t  d ¡ S w |j d¡s6tdƒW t  d ¡ S |  |¡W t  d ¡ S t  d ¡ w )Nz/admin/z/static/z/media/z	/landing/z/auth/ú/z/system-admin/z/system-admin/suspended/Útenant_code)r   Ú	suspendedzTenant z trial has expired.zauthapp:loginT)ÚtenantÚuserÚ	is_activeÚ_r   r   )Útenant_code__iexactzBAccess violation detected. Please log in with correct credentials.z&You do not have access to this tenant.zInvalid tenant code.)r	   Ú
set_tenantÚpathÚ
startswithr   r   Úis_authenticatedÚsessionÚgetr   ÚobjectsÚstatusr   r   r   ÚerrorÚnamer   r   Úis_superuserr   ÚusernameÚsplitÚfilterÚexistsÚlowerÚtenant_userÚDoesNotExist)r   Úrequestr   r   r(   Úprefixr   r   r   Ú__call__   s|   
$

ÿ

ÿ
þ


2Ñ
-Ö

ý
ïð€
ô
üzTenantMiddleware.__call__N)Ú__name__Ú
__module__Ú__qualname__r   r,   r   r   r   r   r
      s    r
   N)Údjango.shortcutsr   Údjango.urlsr   Údjango.contribr   Údjango.httpr   Úmodelsr   r   Ú r	   r
   r   r   r   r   Ú<module>   s    