def test_integrity_on_delete(self):

        for model in get_our_models():
            obj, related_nullable_objects = self.create_full_object(model)

            obj_count = model.objects.count()

            for relation_name, rel_obj in related_nullable_objects.items():

                try:
                    # check if the test should be skipped
                    if relation_name in obj.exclude_on_on_delete_test:
                        continue
                except AttributeError:
                    pass

                rel_obj.delete()

                error_msg = (
                    '<{}> object, was deleted after deleting a nullable '
                    'related <{}> object, the relation was "{}"').format(
                        model.__name__, rel_obj.__class__.__name__,
                        relation_name)

                self.assertEqual(obj_count, model.objects.count(), error_msg)
    def setUp(self):
        super(UrlsTest, self).setUp()

        # we are going to send parameters, so one thing we'll do is to send
        # tie id 1
        self.user.delete()
        self.user.id = 1

        # give the user all the permissions, so we test every page
        self.user.is_superuser = True

        self.user.save()
        self.login()

        self.default_params = {}

        for model in get_our_models():
            model_name = underscore(model.__name__)
            method_name = 'create_{}'.format(model_name)
            param_name = '{}_id'.format(model_name)

            obj = getattr(self, method_name)()

            self.assertIsNotNone(obj, '{} returns None'.format(method_name))

            self.default_params[param_name] = obj.id
    def setUp(self):
        super(UrlsTest, self).setUp()

        # we are going to send parameters, so one thing we'll do is to send
        # tie id 1
        self.user.delete()
        self.user.id = 1

        # give the user all the permissions, so we test every page
        self.user.is_superuser = True

        self.user.save()
        self.login()

        # store default values for urls. E.g. user_id
        self.default_params = {}

        # store default objects to get foreign key parameters
        self.default_objects = {}

        for model in get_our_models():
            model_name = underscore(model.__name__)
            method_name = 'create_{}'.format(model_name)

            # store the created object
            obj = getattr(self, method_name)(**self.get_obj_kwargs(model))
            self.default_objects[model_name] = obj

            self.assertIsNotNone(obj, '{} returns None'.format(method_name))

            # store the object id with the expected name a url should use
            # when using object ids:
            param_name = '{}_id'.format(model_name)
            self.default_params[param_name] = obj.id
示例#4
0
    def test_integrity_on_delete(self):

        for model in get_our_models():
            # ignore gobcl_cms
            if (model._meta.app_label == 'gobcl_cms'
                    or model.__name__.endswith('Translation')
                    or model._meta.app_label == 'bootstrap4_grid'):
                continue

            obj, related_nullable_objects = self.create_full_object(model)

            obj_count = model.objects.count()

            for relation_name, rel_obj in related_nullable_objects.items():

                try:
                    # check if the test should be skipped
                    if relation_name in obj.exclude_on_on_delete_test:
                        continue
                except AttributeError:
                    pass

                if model.__name__.endswith('Translation'):
                    continue

                rel_obj.delete()

                error_msg = (
                    '<{}> object, was deleted after deleting a nullable '
                    'related <{}> object, the relation was "{}"').format(
                        model.__name__, rel_obj.__class__.__name__,
                        relation_name)

                self.assertEqual(obj_count, model.objects.count(), error_msg)
示例#5
0
def audit_delete_log(sender, instance, **kwargs):
    """
    Post delete signal that creates a log when an object from a models from
    our apps is deleted.
    """
    # only listening models created in our apps
    if sender not in get_our_models():
        return
    user = get_user()
    instance.save_deletion(user)
示例#6
0
def audit_log(sender, instance, created, raw, update_fields, **kwargs):
    """
    Post save signal that creates a log when an object from a models from
    our apps is created or updated.
    """
    # only listening models created in our apps
    if sender not in get_our_models():
        return

    sensitive_fields = settings.LOG_SENSITIVE_FIELDS
    ignored_fields = settings.LOG_IGNORE_FIELDS
    user = get_user()

    if created:
        message = {
            'Created':
            instance.to_dict(
                exclude=ignored_fields + sensitive_fields,
                include_m2m=False,
            )
        }
        instance.save_addition(user, message)
    elif not raw:
        change_message = []
        changed_field_labels = {}
        original_dict = instance.original_dict
        actual_dict = instance.to_dict(
            exclude=ignored_fields,
            include_m2m=False,
        )
        change = False
        for key in original_dict.keys():
            if original_dict[key] != actual_dict[key]:
                change = True
                if key == sensitive_fields:
                    changed_field_labels[key] = {'change': 'field updated'}
                else:
                    changed_field_labels[key] = {
                        'from': original_dict[key],
                        'to': actual_dict[key],
                    }
        if change:
            change_message = {'changed': {'fields': changed_field_labels}}
            instance.save_edition(user, change_message)
示例#7
0
    def setUp(self):
        super(UrlsTest, self).setUp()

        # it is mandatory for this project to always have an
        # active government_structure and presidency.
        get_current_government_structure()
        create_presidency()
        create_sociocultural_department()

        # we are going to send parameters, so one thing we'll do is to send
        # tie id 1
        self.user.delete()
        self.user.id = 1

        # give the user all the permissions, so we test every page
        self.user.is_superuser = True

        self.user.save()
        self.login()

        self.default_params = {}

        for model in get_our_models():
            model_name = underscore(model.__name__)
            method_name = 'create_{}'.format(model_name)
            param_name = '{}_id'.format(model_name)

            if model_name.endswith('_translation'):
                continue

            if model._meta.app_label == 'bootstrap4_grid':
                continue

            if model._meta.app_label == 'gobcl_cms':
                continue

            obj = getattr(self, method_name)()

            self.assertIsNotNone(obj, '{} returns None'.format(method_name))

            self.default_params[param_name] = obj.id