예제 #1
0
        def do_create(_cls, request):
            """Use an inner function so that we can generate a proper
            permission name at run time.
            """
            key_name = self.model.model._meta.pk.name
            if not request.POST.has_key(key_name):
                created = True
            else:
                filter_args = {key_name: request.POST[key_name]}
                objects = self.model.model.objects
                created = (objects.filter(**filter_args).count() == 0)

            instance = self.model.model(**dict([(k, v)
                for k, v in request.POST.items()]))
            instance.save()

            # Reset the sequence point in case there was a PK set
            cursor = connection.cursor()
            reset_sequence_command_lines = connection.ops.sequence_reset_sql(
                no_style(), [self.model.model])
            if len(reset_sequence_command_lines) != 0:
                cursor.execute(';'.join(reset_sequence_command_lines))

            instance_data(response, self.model, instance)
            response['pk'] = to_json_data(self.model, instance, key_name,
                self.model.fields[key_name])
            response['created'] = created
예제 #2
0
        def do_create(_cls, request):
            """Use an inner function so that we can generate a proper
            permission name at run time.
            """
            key_name = self.model.model._meta.pk.name
            if not request.POST.has_key(key_name):
                created = True
            else:
                filter_args = {key_name: request.POST[key_name]}
                objects = self.model.model.objects
                created = (objects.filter(**filter_args).count() == 0)

            instance = self.model.model(**dict([(k, v)
                for k, v in request.POST.items()]))
            instance.save()

            # Reset the sequence point in case there was a PK set
            cursor = connection.cursor()
            reset_sequence_command_lines = connection.ops.sequence_reset_sql(
                no_style(), [self.model.model])
            if len(reset_sequence_command_lines) != 0:
                cursor.execute(';'.join(reset_sequence_command_lines))

            instance_data(response, self.model, instance)
            response['pk'] = to_json_data(self.model, instance, 'pk',
                self.model.fields[key_name])
            response['created'] = created
예제 #3
0
        def do_put(_cls, request):
            """Apply the permission check to this inner function.
            """
            key_name = self.model.model._meta.pk.name
            pk_filter = {key_name: pk}
            created = (
                self.model.model.objects.filter(**pk_filter).count() == 0)

            instance = self.model.model(**dict(pk_filter.items() +
                [(k, v) for k, v in request.POST.items()]))
            instance.save()

            if created:
                response['_meta']['status'] = 201
                _reset_sequence_number(self.model.model)

            instance_data(response, self.model, instance)
            response['pk'] = to_json_data(self.model, instance, key_name,
                self.model.fields[key_name])
예제 #4
0
 def get(self, _request, response, _appname, _modelname, pk):
     """Implement the fetching of attribute data for an instance.
     """
     root = reverse('slumber.server.views.get_applications')
     instance = self.model.model.objects.get(pk=pk)
     response['identity'] = root + self.model.path + \
         '%s/%s/' % (self.name, instance.pk)
     response['display'] = unicode(instance)
     response['operations'] = dict(
         [(op.name, root + op.path + '%s/' % instance.pk)
             for op in self.model.operations() if not op.model_operation])
     response['fields'] = {}
     for field, meta in self.model.fields.items():
         response['fields'][field] = dict(
             data=to_json_data(self.model, instance, field, meta),
             kind=meta['kind'], type=meta['type'])
     response['data_arrays'] = {}
     for field in self.model.data_arrays:
         response['data_arrays'][field] = \
             response['identity'] + '%s/' % field
예제 #5
0
        def do_create(_cls, request):
            """Use an inner function so that we can generate a proper
            permission name at run time.
            """
            key_name = self.model.model._meta.pk.name
            if not request.POST.has_key(key_name):
                created = True
            else:
                filter_args = {key_name: request.POST[key_name]}
                objects = self.model.model.objects
                created = (objects.filter(**filter_args).count() == 0)

            instance = self.model.model(**dict([(k, v)
                for k, v in request.POST.items()]))
            instance.save()
            _reset_sequence_number(self.model.model)

            instance_data(response, self.model, instance)
            response['pk'] = to_json_data(self.model, instance, key_name,
                self.model.fields[key_name])
            response['created'] = created
예제 #6
0
def instance_data(into, model, instance):
    """Fill in the dict `into` with information about the instance of the
    specified model.
    """
    root = get_slumber_root()
    into['type'] = root + model.path
    into['identity'] = root + model.path + \
        '%s/%s/' % ('data', instance.pk)
    into['display'] = unicode(instance)
    into['operations'] = dict(
        [(op.name, op(instance))
            for op in model.operations.values() if not op.model_operation])
    into['fields'] = {}
    for field, meta in model.fields.items():
        into['fields'][field] = dict(
            data=to_json_data(model, instance, field, meta),
            kind=meta['kind'], type=meta['type'])
    into['data_arrays'] = {}
    for field in model.data_arrays:
        into['data_arrays'][field] = \
            into['identity'] + '%s/' % field
예제 #7
0
def instance_data(into, model, instance):
    """Fill in the dict `into` with information about the instance of the
    specified model.
    """
    root = get_slumber_root()
    into['type'] = root + model.path
    into['identity'] = root + model.path + \
        '%s/%s/' % ('data', instance.pk)
    into['display'] = unicode(instance)
    into['operations'] = dict(
        [(op.name, root + op.path + '%s/' % instance.pk)
            for op in model.operations() if not op.model_operation])
    into['fields'] = {}
    for field, meta in model.fields.items():
        into['fields'][field] = dict(
            data=to_json_data(model, instance, field, meta),
            kind=meta['kind'], type=meta['type'])
    into['data_arrays'] = {}
    for field in model.data_arrays:
        into['data_arrays'][field] = \
            into['identity'] + '%s/' % field
예제 #8
0
        def do_create(_cls, request):
            """Use an inner function so that we can generate a proper
            permission name at run time.
            """
            key_name = self.model.model._meta.pk.name
            if not request.POST.has_key(key_name):
                created = True
            else:
                filter_args = {key_name: request.POST[key_name]}
                objects = self.model.model.objects
                created = (objects.filter(**filter_args).count() == 0)

            instance = self.model.model(
                **dict([(k, v) for k, v in request.POST.items()]))
            instance.save()
            _reset_sequence_number(self.model.model)

            instance_data(response, self.model, instance)
            response['pk'] = to_json_data(self.model, instance, key_name,
                                          self.model.fields[key_name])
            response['created'] = created