Example #1
0
class TemplateSerializer(_WithVariablesSerializer):
    data = DataSerializer(required=True, write_only=True)
    options = DataSerializer(write_only=True)
    options_list = serializers.ListField(read_only=True)
    kind = serializers.ChoiceField(choices=[(k, k)
                                            for k in models.Template.kinds],
                                   required=False,
                                   default=models.Template.kinds[0],
                                   label='Type')

    class Meta:
        model = models.Template
        fields = (
            'id',
            'name',
            'kind',
            'data',
            'options',
            'options_list',
        )

    def get_vars(self, representation):
        try:
            return representation['data']['vars']
        except KeyError:  # nocv
            return None

    def set_opts_vars(self, rep, hidden_vars: List[str]):
        if not rep.get('vars', None):
            return rep
        var = rep['vars']
        for mask_key in hidden_vars:
            if mask_key in var.keys():
                var[mask_key] = "[~~ENCRYPTED~~]"
        return rep

    def repr_options(self, instance: models.Template, data: Dict,
                     hidden_vars: List):
        hv = hidden_vars
        hv = instance.HIDDEN_VARS if hv is None else hv
        for name, rep in data.get('options', {}).items():
            data['options'][name] = self.set_opts_vars(rep, hv)

    def to_representation(self,
                          instance,
                          hidden_vars: List[str] = None) -> OrderedDict:
        data = OrderedDict()
        if instance.kind in ["Task", "Module"]:
            hidden_vars = models.PeriodicTask.HIDDEN_VARS
            data = super(TemplateSerializer,
                         self).to_representation(instance,
                                                 hidden_vars=hidden_vars)
            self.repr_options(instance, data, hidden_vars)
        return data
Example #2
0
 def list(self, request):
     start_time = time.time()
     headers = request._request.META
     # Don't send wsgi.* headers
     headers = {
         k: v
         for k, v in headers.items() if not k.startswith('wsgi.')
     }
     serializer = DataSerializer(data=json.dumps(
         dict(headers=headers,
              query=request.query_params,
              user_id=request.user.id)))
     serializer.is_valid(raise_exception=True)
     request._request.is_bulk = False
     return responses.HTTP_200_OK(
         serializer.data,
         timings=dict(req=(
             i for i in ['test case', time.time() - start_time]),
                      miss=None))
Example #3
0
class OneModuleSerializer(ModuleSerializer):
    data = DataSerializer()

    class Meta:
        model = models.Module
        fields = (
            'id',
            'path',
            'name',
            'data',
        )
Example #4
0
class OneTemplateSerializer(TemplateSerializer):
    data = DataSerializer(required=True)
    options = DataSerializer(required=False)
    options_list = serializers.ListField(read_only=True)
    notes = vst_fields.TextareaField(required=False, allow_blank=True)

    class Meta:
        model = models.Template
        fields = (
            'id',
            'name',
            'notes',
            'kind',
            'data',
            'options',
            'options_list',
        )

    def execute(self, request):
        return self.instance.execute(request.user, request.data.get('option', None))