Ejemplo n.º 1
0
    def create(self, data={}, files={}):
        """
        Handle a POST request by adding a new model instance.

        This view will only do something if BackboneAPIView.add_form_class is specified
        by the subclass. This should be a ModelForm corresponding to the model used by
        base_queryset.

        Backbone.js will send the new object's attributes as json in the request body,
        so use our json decoder on it, rather than looking at request.POST.
        """
        if self.add_form_class == None:
            return False, { 'status': 501 }
        if not self.user_has_perm(self.request, None, 'create'):
            return False, { 'status': 403 }
        form = self.add_form_class(data, files)
        if hasattr(form, 'set_request'):
            form.set_request(self.request)
        if form.is_valid():
            new_object = form.save()
            logger.info("%s:CREATE:SUCCESS:%s: id=%s, data=%s, files=%s"%\
                    (self.base_queryset.model.__name__, self.request.user.username, new_object.pk, logging_dict(data), logging_dict(files)))
            # Serialize the new object to json using our built-in methods.
            # The extra DB read here is not ideal, but it keeps the code DRY:
            wrapper_qs = self.base_queryset.filter(id=new_object.id)
            return True, self.serialize_qs(wrapper_qs, single_object=True)
        else:
            logger.warning("%s:CREATE:ERROR:%s: data=%s, files=%s, errors=%s"%\
                    (self.base_queryset.model.__name__, self.request.user.username, logging_dict(data), logging_dict(files), logging_dict(form.errors)))
            return False, { 'errors': form.errors, 'status': 400 }
Ejemplo n.º 2
0
    def create(self, data={}, files={}):
        """
        Handle a POST request by adding a new model instance.

        This view will only do something if BackboneAPIView.add_form_class is specified
        by the subclass. This should be a ModelForm corresponding to the model used by
        base_queryset.

        Backbone.js will send the new object's attributes as json in the request body,
        so use our json decoder on it, rather than looking at request.POST.
        """
        if self.add_form_class == None:
            return False, {'status': 501}
        if not self.user_has_perm(self.request, None, 'create'):
            return False, {'status': 403}
        form = self.add_form_class(data, files)
        if hasattr(form, 'set_request'):
            form.set_request(self.request)
        if form.is_valid():
            new_object = form.save()
            logger.info("%s:CREATE:SUCCESS:%s: id=%s, data=%s, files=%s"%\
                    (self.base_queryset.model.__name__, self.request.user.username, new_object.pk, logging_dict(data), logging_dict(files)))
            # Serialize the new object to json using our built-in methods.
            # The extra DB read here is not ideal, but it keeps the code DRY:
            wrapper_qs = self.base_queryset.filter(id=new_object.id)
            return True, self.serialize_qs(wrapper_qs, single_object=True)
        else:
            logger.warning("%s:CREATE:ERROR:%s: data=%s, files=%s, errors=%s"%\
                    (self.base_queryset.model.__name__, self.request.user.username, logging_dict(data), logging_dict(files), logging_dict(form.errors)))
            return False, {'errors': form.errors, 'status': 400}
Ejemplo n.º 3
0
    def update(self, id, data={}, files={}):
        """
        Handle a PUT request by editing an existing model.

        This view will only do something if BackboneAPIView.edit_form_class is specified
        by the subclass. This should be a ModelForm corresponding to the model used by
        base_queryset.
        """
        if self.edit_form_class == None:
            return False, { 'status': 501 }
        qs = self.base_queryset.filter(pk=id)
        if not len(qs) == 1:
            return False, { 'status': 404 }
        instance = qs[0]
        if not self.user_has_perm(self.request, instance, 'update'):
            return False, { 'status': 404 }
        form = self.edit_form_class(data, files, instance=instance)
        if hasattr(form, 'set_request'):
            form.set_request(self.request)
        data_diff = get_data_diff(qs, data)
        if form.is_valid():
            logger.info("%s:UPDATE:SUCCESS:%s: id=%s, updated_data=%s, files=%s"%\
                    (self.base_queryset.model.__name__, self.request.user.username, instance.pk, data_diff, logging_dict(files)))
            item = form.save()
            wrapper_qs = self.base_queryset.filter(id=item.id)
            return True, self.serialize_qs(wrapper_qs, single_object=True)
        else:
            logger.info("%s:UPDATE:ERROR:%s: id=%s, updated_data=%s, files=%s"%\
                    (self.base_queryset.model.__name__, self.request.user.username, instance.pk, data_diff, logging_dict(files)))
            return False, { 'errors': form.errors, 'status': 400 }
Ejemplo n.º 4
0
    def update(self, id, data={}, files={}):
        """
        Handle a PUT request by editing an existing model.

        This view will only do something if BackboneAPIView.edit_form_class is specified
        by the subclass. This should be a ModelForm corresponding to the model used by
        base_queryset.
        """
        if self.edit_form_class == None:
            return False, {'status': 501}
        qs = self.base_queryset.filter(pk=id)
        if not len(qs) == 1:
            return False, {'status': 404}
        instance = qs[0]
        if not self.user_has_perm(self.request, instance, 'update'):
            return False, {'status': 404}
        form = self.edit_form_class(data, files, instance=instance)
        if hasattr(form, 'set_request'):
            form.set_request(self.request)
        data_diff = get_data_diff(qs, data)
        if form.is_valid():
            logger.info("%s:UPDATE:SUCCESS:%s: id=%s, updated_data=%s, files=%s"%\
                    (self.base_queryset.model.__name__, self.request.user.username, instance.pk, data_diff, logging_dict(files)))
            item = form.save()
            wrapper_qs = self.base_queryset.filter(id=item.id)
            return True, self.serialize_qs(wrapper_qs, single_object=True)
        else:
            logger.info("%s:UPDATE:ERROR:%s: id=%s, updated_data=%s, files=%s"%\
                    (self.base_queryset.model.__name__, self.request.user.username, instance.pk, data_diff, logging_dict(files)))
            return False, {'errors': form.errors, 'status': 400}