U
    YHg	                     @   s>   d dl mZ d dlmZ G dd deZG dd dejZdS )    )db)	BaseQueryc                   @   s$   e Zd Zdd Zdd Zdd ZdS )SystemConfigQueryc                 C   s0   |  tj| k}d}|r,| r,| }|S )zW
        This method is used to get a config value in the system_config table.
        N)filterSystemConfigurationnameuppercountfirst)selfconfig_nameexisting_recordsoutput r   6/var/www/dostadmin/dostadmin/db_model/configuration.pyget_config_value   s
    z"SystemConfigQuery.get_config_valuec                 C   s0   |  tjt|k}d }|r,| r,| }|S N)r   r   idintr	   r
   )r   	config_idr   r   r   r   r   get_config_by_id   s
    z"SystemConfigQuery.get_config_by_idc                 C   s   |   S r   )r   r   r   r   r   get_all_configs   s    z!SystemConfigQuery.get_all_configsN)__name__
__module____qualname__r   r   r   r   r   r   r   r      s   	r   c                   @   s~   e Zd ZdZeZejdejddZ	eje
dddddZee
dZd	d
 Zdd Zdd Zdd Zdd Zdd ZdS )r   system_configurationr   T)primary_key2   F)uniqueindexnullable   c                 C   sn   | j |d}|r&tj| tj  t| |d}ztj| tj  |W S  tk
rh   Y d S X d S )Nr   )r   value)	
get_configr   sessiondeletecommitr   r   add	Exception)r   r   config_valueZexisting_recordZsystem_config_recordr   r   r   
set_config'   s    
 
zSystemConfiguration.set_configc                 C   sF   |  |}|rBztj| tj  W dS  tk
r@   Y dS X dS )NTF)r   r   r&   r'   r(   r*   )r   r   recordr   r   r   delete_config:   s    

z!SystemConfiguration.delete_configc                 C   s   t jj| dS )Nr#   )r   queryr   r   )r   r   r   r   r   r%   E   s    zSystemConfiguration.get_configc                 C   s   t jjt|dS )N)r   )r   r/   r   r   )r   r   r   r   r   r   J   s    z$SystemConfiguration.get_config_by_idc                    s    fdd j jD S )Nc                    s   i | ]}|j t |j qS r   )r   getattr).0cr   r   r   
<dictcomp>N   s      z/SystemConfiguration.as_dict.<locals>.<dictcomp>)	__table__columnsr   r   r   r   as_dictM   s    zSystemConfiguration.as_dictc                 C   s   d| j  dS )Nz<SystemConfiguration >)r   r   r   r   r   __repr__P   s    zSystemConfiguration.__repr__N)r   r   r   __tablename__r   query_classr   ColumnIntegerr   Stringr   r$   r,   r.   r%   r   r6   r8   r   r   r   r   r      s   r   N)	dostadminr   flask_sqlalchemyr   r   Modelr   r   r   r   r   <module>   s   