a
    .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 )N	databasesinsardatabasecustom_groups)r   r   beginqueryCustomGroupModelorder_bynameallappendoutputr   )selfr   r   
db_sessionr   custom_group r   J/var/www/staging/api/app_home/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   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 foundr1   r   Tr$   'z	' updated   r%   )r&   r'   r   r(   r   r	   r   r   r   r2   r"   r3   r   r!   r   r)   r*   r+   r   )r   r   r5   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 )Nr6   r7   r1    z and z pointsr$   zCustom Group 'r8   z deletedr9   zLCould not delete Custom Point Group - does it have any active Custom Points?r%   )r   r	   r   r   CustomPointModelr2   r5   r   r   r"   r3   r   deletelenr+   r   r   )r   r   r5   r   custom_pointsr   Zcustom_points_messagecustom_pointr   r   r    r=   F   s     
.zCustomGroup.deleteN)r-   r.   r/   r   r0   r!   r:   r=   r   r   r   r    r   '   s   	r   )flaskr   r   r   r   r   main.models._mapped_tablesr   r   r   r<   r	   flask_restfulr
   setupr   r   r   r&   Zsqlalchemy.excr   r   r   r   r   r    <module>   s   