a
    Si                     @   s   d dl mZmZmZmZmZ 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mZ d dlZd dlmZ G d	d
 d
eZG dd deZdS )    )Flaskjsonifyrequestmake_responseg)CustomGroup)CustomPoint)db_selector)Resource)
DB_SESSIONadmin_requiredCONFIGN)IntegrityErrorc                   @   s"   e Zd ZegZdd Zdd ZdS )CustomGroupsc              	   C   s   g }t d d D ]d}t|  D}|ttj }|D ]}||j	d|iB  q<W d    q1 sj0    Y  qt
d|iS )NZ	databasesZinsardatabasecustom_groups)r   r   beginqueryCustomGroupModelZorder_bynameallappendoutputr   )selfr   r   
db_sessionr   custom_group r   w/var/www/staging/api/virtual_environments/venv/lib/python3.9/site-packages/main/controllers/custom_groups_controller.pyget   s    6zCustomGroups.getc              	   C   s   t tj}t|dd}|jdu rz`tt| 	 <}|
| |  tt|j|jddW  d    W S 1 sz0    Y  W q ty   tdd|d did	 Y S 0 nttd|jid	S d S )
Nr   )r   T)idr      messagezName 'z' already exists  )jsonloadsr   datar   r   	validatesr   r	   r   addcommitr   r   r   r   r   )r   r   r%   r   r   r   r   r   post   s    

<$zCustomGroups.postN)__name__
__module____qualname__r   method_decoratorsr   r)   r   r   r   r   r      s   
r   c                   @   s*   e Zd ZegZdd Zdd Zdd ZdS )r   c                 C   s   t t|  \}|ttj|k }|sLtt	i dW  d    S t	|j
d|iB W  d    S 1 sr0    Y  d S )N  r   )r   r	   r   r   r   filterr   one_or_noner   r   Zoutput_with_custom_points)r   r   custom_group_idr   r   r   r   r   r   *   s
    zCustomGroup.getc                 C   s   t tj}tt|  J}|t	tj
|k }|sXtddidW  d    S W d    n1 sl0    Y  |d|_|jdu rtt|  :}|| |  tdd|j did	W  d    S 1 s0    Y  nttd|jid
S d S )NerrorCustom Point Group not foundr.   r   Tr!   'z	' updated   r"   )r#   r$   r   r%   r   r	   r   r   r   r/   r   r0   r   r   r   r&   r'   r(   r   )r   r   r1   r%   r   r   r   r   r   put3   s    :

8zCustomGroup.putc              	   C   s.  t t|  }|ttj|k }|ttj	|k
 }|shtddidW  d    S zpd}|r|D ]}|| qv|dt| d7 }|| |  tdd|j d	| d
idW W  d    S  ty   tddid Y W  d    S 0 W d    n1 s 0    Y  d S )Nr2   r3   r.    z and z pointsr!   zCustom Group 'r4   z deletedr5   zLCould not delete Custom Point Group - does it have any active Custom Points?r"   )r   r	   r   r   CustomPointModelr/   r1   r   r   r   r0   r   deletelenr(   r   r   )r   r   r1   r   Zcustom_pointsr   Zcustom_points_messageZcustom_pointr   r   r   r9   F   s     
.zCustomGroup.deleteN)r*   r+   r,   r   r-   r   r6   r9   r   r   r   r   r   '   s   	r   )Zflaskr   r   r   r   r   Zmain.models._mapped_tablesr   r   r   r8   r	   Zflask_restfulr
   setupr   r   r   r#   Zsqlalchemy.excr   r   r   r   r   r   <module>   s   