Exemplo n.º 1
0
 def serialize_reverse_fields(self, obj):
     if not self.reverse_fields:
         return
     for fieldname in self.reverse_fields:
         if self.inline and fieldname in self.inline:
             tmpserializer = Serializer()
             fields, exclude, maps, inline = self._get_serialize_options_for_subfield(fieldname)
             if isinstance(getattr(obj, fieldname), Model):
                 self._current[fieldname] = tmpserializer.serialize(
                     getattr(obj, fieldname),
                     inline=inline, 
                     fields=fields, 
                     exclude=exclude,
                     map_fields=maps,
                 )
             else:
                 self._current[fieldname] = [tmpserializer.serialize(
                     related,
                     inline=inline,
                     fields=fields,
                     exclude=exclude,
                     map_fields=maps,
                 ) for related in getattr(obj, fieldname).iterator()]
         else:
             #rev = lambda field: reverse('object-%s-%s' % (self.api_name, field._meta), kwargs={'id': field._get_pk_val()})
             rev = lambda field: get_url_for_object(self.api_name, field)
             if isinstance(getattr(obj, fieldname), Model):
                 if self.related_as_ids:
                     self._current[fieldname] = getattr(obj, fieldname)._get_pk_val()
                 else:
                     #try:
                     #    self._current[fieldname] = rev(getattr(obj, fieldname))
                     #except NoReverseMatch:
                     #    self._current[fieldname] = getattr(obj, fieldname)._get_pk_val()
                     self._current[fieldname] = rev(getattr(obj, fieldname))
             else:
                 if self.related_as_ids:
                     self._current[fieldname] = [related._get_pk_val() for related in getattr(obj, fieldname).iterator()]
                 else:
                     #try:
                     #    self._current[fieldname] = [rev(related) for related in getattr(obj, fieldname).iterator()]
                     #except NoReverseMatch:
                     #    self._current[fieldname] = [related._get_pk_val() for related in getattr(obj, fieldname).iterator()]
                     self._current[fieldname] = [rev(related) for related in getattr(obj, fieldname).iterator()]
Exemplo n.º 2
0
 def handle_fk_field(self, obj, field):
     if self.inline and field.name in self.inline:
         tmpserializer = self.__class__()
         fields, exclude, maps, inline = self._get_serialize_options_for_subfield(field.name)
         tmpserializer.serialize(
             getattr(obj, field.name),
             inline=inline,
             fields=fields,
             exclude=exclude,
             map_fields=maps,
             finalize=False
         )
         self._current[field.name] = tmpserializer.objects
     else:
         super(Serializer, self).handle_fk_field(obj, field)
         if not self.related_as_ids:
             related = getattr(obj, field.name)
             if related:
                 #val = reverse('object-%s-%s' % (self.api_name, related._meta), kwargs={'id': self._current[field.name]})
                 val = get_url_for_object(self.api_name, related, self._current[field.name])
                 self._current[field.name] = val
Exemplo n.º 3
0
    def _rest_to_http_response(self, request, restresponse):
        format = request.rest_info.format
        if not format:
            if settings.DEBUG and self.display_errors:
                raise ConfigurationError('No format for the response specified')
            else:
                return HttpResponseServerError()

        response = HttpResponse(content_type=get_mime_for_format(format))

        if restresponse.form and restresponse.form.errors:
            data = {'error': restresponse.form.errors}
            response.status_code = 400
            render_only = True

        if restresponse.content:
            data = restresponse.content
            render_only = not (self._meta.model or False)
            if self._meta.model:
                if isinstance(data, QuerySet):
                    if data.model != self._meta.model:
                        if settings.DEBUG and self.display_errors:
                            raise ConfigurationError('The returned model %s is not compatible with the resources\' model %s' % (data.model, self._meta.model))
                        else:
                            return HttpResponseServerError()
                elif isinstance(data, list):
                    if not all(map(lambda x: isinstance(x, self._meta.model), data)):
                        if settings.DEBUG and self.display_errors:
                            raise ConfigurationError('At least one model in the returned list is not compatible with the resources\' model %s' % (self._meta.model,))
                        else:
                            return HttpResponseServerError()
                elif not isinstance(data, self._meta.model):
                    if settings.DEBUG and self.display_errors:
                        raise ConfigurationError('The returned object\'s model %s is not compatible with the resource model %s' % (type(data), self._meta.model))
                    else:
                        return HttpResponseServerError()

                # Add a location header
                if request.rest_info.request_method == 'POST' or (request.rest_info.request_method == 'PUT' and request.rest_info.request_type == 'list'):
                    try:
                        response['Location'] = get_url_for_object(self._meta.api_name, data[0])
                    except TypeError:
                        response['Location'] = get_url_for_object(self._meta.api_name, data)
                    if not self._meta.render_object_after_creation:
                        response.content = ''
                        return response

        try:
            s = serializers.serialize('rest%s' % (format), 
                data, 
                related_as_ids=self._meta.related_as_ids, 
                api_name=self._meta.api_name, 
                fields=self._optionlist_for_type(request.rest_info.request_method, self._meta.fields),
                exclude=self._optionlist_for_type(request.rest_info.request_method, self._meta.exclude),
                reverse_fields=self._meta.reverse_fields, 
                inline=self._meta.inline, 
                map_fields=self._meta.map_fields, 
                extra=self._meta.extra_fields,
                render_only=render_only
            )
        except serializers.base.SerializerDoesNotExist:
            if settings.DEBUG and self.display_errors:
                raise UnsupportedFormat('Format %s is not supported. Check if you included the serializers in the settings file.' % (format,))
            else:
                return HttpResponseUnsupportedMediaType()
        except serializers.base.SerializationError:
            if settings.DEBUG and self.display_errors:
                raise
            else:
                return HttpResponseServerError()
        response.content = s
        return response