
    [h1$              	           d Z ddlZddlZddlmZ ddlmZ ddlmZm	Z	 ddl
mZmZmZ ddlmZ dd	lmZmZmZ dd
lmZ ddlmZmZ dZdZdZddeeef   dededdfdZ G d de      Z  G d de      Z!y)u  
***************************************************************************
    QGIS Server Plugin Filters: Add a new request to print a specific atlas
    feature
    ---------------------
    Date                 : December 2019
    Copyright            : (C) 2019 by René-Luc D'Hont - 3Liz
    Email                : rldhont at 3liz dot com
***************************************************************************
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
***************************************************************************
    N)Path)Dict)QgsExpression
QgsProject)QgsServerRequestQgsServerResponse
QgsService)pluginMetadata   )AtlasPrintExceptionparse_output_formatprint_layout)Logger)get_lizmap_groupsget_lizmap_user_loginzCopyright 2021, 3LizzGPL version 3zinfo@3liz.orgdataresponsecodereturnc                     |j                  |       |j                  dd       |j                  t        j                  |              y)z! Write data as json response
    Content-Typezapplication/jsonN)setStatusCode	setHeaderwritejsondumps)r   r   r   s      2/var/www/qgis-server/plugins/atlasprint/service.pywrite_json_responser   &   s8     4 ~'9:NN4::d#$    c                   @     e Zd Zdedededdf fdZdeddfdZ xZS )	AtlasPrintErrorr   msg
request_idr   Nc                     t         |   |       || _        || _        || _        t               j                  d| d| d|        y )NzAtlas print request error , X-Request-ID z: )super__init__r"   r   r#   r   critical)selfr   r"   r#   	__class__s       r   r'   zAtlasPrintError.__init__0   sL    	$6tfOJ<WYZ]Y^_`r   r   c                     d| j                   | j                  d}|j                          t        ||| j                         y)z Format error response
        fail)statusr#   messageN)r#   r"   clearr   r   )r)   r   bodys      r   format_responsezAtlasPrintError.format_response7   s8     //xx

 	D(DII6r   )	__name__
__module____qualname__intstrr'   r   r1   __classcell__r*   s   @r   r!   r!   .   s=    aS as a a a	7(9 	7d 	7r   r!   c                        e Zd Zddeddf fdZdefdZdefdZdej                  defdZ
d	ed
ededdfdZedeeef   d
ededdfd       Zdeeef   d
edededededdfdZ xZS )AtlasPrintServicedebugr   Nc                 D    t         |           |}t               | _        y )N)r&   r'   r   logger)r)   r;   _r*   s      r   r'   zAtlasPrintService.__init__E   s    hr   c                      y)z Service name
        ATLAS r)   s    r   namezAtlasPrintService.nameL        r   c                      y)z Service version
        z1.0.0rA   rB   s    r   versionzAtlasPrintService.versionQ   rD   r   methodc                 F    |t         j                  t         j                  fv S )z& Check supported HTTP methods
        )r   	GetMethod
PostMethod)r)   rG   s     r   allowMethodzAtlasPrintService.allowMethodW   s+     &&(8(C(CE E 	Er   requestr   projectc                    |j                         }|j                  dd      }|j                         }	 |j                  dd      j                         }|dk(  r| j	                  |||       n|dk(  rt        ||      }t        ||      }	|j                         }
|
j                  dd      |k7  r?| j                  j                  d	       ||
d<   t        |	      |
d
<   |j                  |
       | j                  |||||	|       nt        dd| d| d|      | j                  j                  d       |j                         }
|
j'                  dd       |
j'                  d
d       |j                  |
       y# t        $ r}|j                  |       Y d}~d}~wt        $ rT | j                  j!                  dt#        j$                          d|        t        dd|      }|j                  |       Y w xY w# | j                  j                  d       |j                         }
|
j'                  dd       |
j'                  d
d       |j                  |
       w xY w)z# Execute a 'ATLAS' request
        zX-Request-IdNDREQUEST getcapabilitiesgetprintlizmap_userNz3Adding user and group variables in the QGIS projectlizmap_user_groups  zTInvalid REQUEST parameter: must be one of 'GetCapabilities', 'GetPrint', Request-ID z	, found ''Unhandled exception:
r%     #Internal 'AtlasPrint' service errorz7Removing user and group variables from the QGIS project)headersget
parameterslowerget_capabilitiesr   r   customVariablesr=   infolistsetCustomVariables	get_printr!   r1   	Exceptionr(   	traceback
format_excpop)r)   rL   r   rM   r[   r#   paramsrequest_paramrT   lizmap_group
custom_varerrs               r   executeRequestz AtlasPrintService.executeRequest]   s:   
 //#[[6
##%&	3"JJy"5;;=M 11%%fh@*, 4FGD0A$446
>>-6+EKK$$%Z[0;J}-7;L7IJ34..z:vx+|U_`%..8\=/QRT	  KKVW 002JNN=$/NN/6&&z2  	*)) 	*KK  #9):N:N:P9QQ`ak`l!mn!#'LjYC)	* KKVW 002JNN=$/NN/6&&z2s8   C%E: :	G9FG< AG96G< 8G99G< <A"Iri   c                 L    | |f}d}d|t        |d      dd}t        ||       y)z7 Get atlas capabilities based on metadata file
        
atlasprintsuccessrF   )rC   rF   )r-   metadataN)r
   r   )ri   r   rM   r>   plugin_namer0   s         r   r_   z"AtlasPrintService.get_capabilities   s=     GO"#)+yA
 	D(+r   rT   lizmap_user_groupr#   c                    |j                  d      }|j                  dd      }|j                  d      }	|j                  d      }
t        |j                  d|j                  d                  }	 |st        d      |r7t        |      }|j	                         rt        d	|j                                |	r|
rt        d
      |	r	 t        |	      }	|
r(	 |
j                  d      D 	cg c]  }	t        |	       }
}	|j                         D ci c]  \  }}|j                         dvs|| }}}|r||d<   dj                  |      |d<   t        d||d   |	|
||d|}t'        |      }|j)                         st        dd|j*                   d|      |j-                  d|j.                         |j1                  d       	 |j3                  |j5                                |j7                          y# t        $ r t        d      w xY wc c}	w # t        $ r t        d      w xY wc c}}w # t        $ r}t        dd| |      d}~wt        $ r> | j                  j!                  dt#        j$                                 t        dd|      w xY w# t        $ r+ | j                  j!                  d|j*                   d        w xY w)z Get print document
        TEMPLATE
EXP_FILTERNSCALESCALESFORMATformatzTEMPLATE is requiredzExpression is invalid: z*SCALE and SCALES can not be used together.zInvalid number in SCALE.,zInvalid number in SCALES.)rv   rw   rx   ry   rz   MAPrP   SERVICEDPI
EXCEPTIONSLAYERLIZMAP_OVERRIDE_FILTERTRANSPARENTVERSIONLIZMAP_USERLIZMAP_USER_GROUPSrT   rU   )rM   layout_nameoutput_formatscalescalesfeature_filterr#   rV   z6ATLAS - Error from the user while generating the PDF: rX   rY   rZ   i  zATLAS z
 not foundr      zError occurred while reading z filerA   )r\   r   r   r   hasParserErrorparserErrorStringr5   
ValueErrorsplititemsupperjoinr   r!   re   r=   r(   rf   rg   r   existsrC   r   valuer   r   
read_bytesunlink)r)   ri   r   rM   rT   rt   r#   templater   r   r   r   
expressionkvadditional_paramsoutput_pathepaths                      r   rd   zAtlasPrintService.get_print   s    ::j)L$7

7#H%+FJJxHAU,VW5	Z)*@AA*>:
,,.-1*2N2N2P1QRT T )*VWWJJE K6<ll36GHUc%jHFH
 "(!A1779 E 41! ! 3>!-0:=((CT:U!"67& 	":.+-%	 $	K" K {{}!#0B0B/C:'NPZ[[ 	>=+>+>?s#	NN4??,-KKMa " J-.HIIJ
 I! K-.IJJK!. # 	_!MaSQS]_ _ 	ZKK  #9):N:N:P9Q!RS!#'LjYY	Z  	KK  #@ASAS@TTY!Z[	sz   1AH4 	G9 H4 H *H<H >H4 H.,H.14H4 	/J 9HH4 H H++	H4 4	J=IA
J4K)F)r2   r3   r4   boolr'   r6   rC   rF   r   MethodrK   r   r   rn   staticmethodr   r_   tuplerd   r7   r8   s   @r   r:   r:   C   s   d t c 
 E"2"9"9 Ed E03+037H0303$(03d c3h ;L Wa fj  UcNU (U  	U
 U  %U U 
Ur   r:   )r   )"__doc__r   rf   pathlibr   typingr   	qgis.corer   r   qgis.serverr   r   r	   
qgis.utilsr
   corer   r   r   r=   r   toolsr   r   __copyright____license__	__email__r6   r5   r   re   r!   r:   rA   r   r   <module>r      s   $     / G G % H H  ;&	%d38n %8I %QT %_c %7i 7*q
 qr   