<s> from simplejson import JSONDecodeError <EOL> from rest_framework import viewsets , decorators , exceptions , status <EOL> from rest_framework . response import Response <EOL> from nodeconductor . core import filters as core_filters <EOL> from nodeconductor . structure import filters as structure_filters <EOL> from nodeconductor . template import models , serializers , filters <EOL> class TemplateGroupViewSet ( viewsets . ReadOnlyModelViewSet ) : <EOL> queryset = models . TemplateGroup . objects . filter ( is_active = True ) . prefetch_related ( '<STR_LIT>' ) <EOL> serializer_class = serializers . TemplateGroupSerializer <EOL> lookup_field = '<STR_LIT>' <EOL> filter_class = filters . TemplateGroupFilter <EOL> filter_backends = ( core_filters . DjangoMappingFilterBackend , structure_filters . TagsFilter ) <EOL> tags_filter_db_field = '<STR_LIT>' <EOL> tags_filter_request_field = '<STR_LIT>' <EOL> def list ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return super ( TemplateGroupViewSet , self ) . list ( request , * args , ** kwargs ) <EOL> @ decorators . detail_route ( methods = [ '<STR_LIT>' ] ) <EOL> def provision ( self , request , uuid = None ) : <EOL> """<STR_LIT>""" <EOL> group = self . get_object ( ) <EOL> templates_additional_options = self . _get_templates_additional_options ( request ) <EOL> try : <EOL> response = group . schedule_head_template_provision ( request , templates_additional_options ) <EOL> except models . TemplateActionException as e : <EOL> return Response ( { '<STR_LIT>' : str ( e ) } , status = status . HTTP_400_BAD_REQUEST ) <EOL> if not response . ok : <EOL> try : <EOL> return Response ( response . json ( ) , status = response . status_code ) <EOL> except JSONDecodeError : <EOL> return Response ( <EOL> { '<STR_LIT>' : '<STR_LIT>' % response . content } , <EOL> status = response . status_code ) <EOL> result = group . schedule_tail_templates_provision ( request , templates_additional_options , response ) <EOL> serialized_result = serializers . TemplateGroupResultSerializer ( result , context = { '<STR_LIT>' : request } ) . data <EOL> return Response ( serialized_result , status = status . HTTP_200_OK ) <EOL> def _get_templates_additional_options ( self , request ) : <EOL> """<STR_LIT>""" <EOL> group = self . get_object ( ) <EOL> inputed_additional_options = request . data or [ ] <EOL> if not isinstance ( inputed_additional_options , list ) : <EOL> raise exceptions . ParseError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> templates = group . templates . order_by ( '<STR_LIT>' ) <EOL> if len ( inputed_additional_options ) > templates . count ( ) : <EOL> raise exceptions . ParseError ( <EOL> '<STR_LIT>' % templates . count ( ) ) <EOL> for options in inputed_additional_options : <EOL> if not isinstance ( options , dict ) : <EOL> raise exceptions . ParseError ( <EOL> '<STR_LIT>' % options ) <EOL> templates_additional_options = dict ( zip ( templates , inputed_additional_options ) ) <EOL> return templates_additional_options <EOL> class TemplateGroupResultViewSet ( viewsets . ReadOnlyModelViewSet ) : <EOL> queryset = models . TemplateGroupResult . objects . all ( ) <EOL> serializer_class = serializers . TemplateGroupResultSerializer <EOL> lookup_field = '<STR_LIT>' <EOL> def list ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return super ( TemplateGroupResultViewSet , self ) . list ( request , * args , ** kwargs ) </s>
