def test_register_anonym_only():
    class UserAnonym(register_models.AnonymBase):
        email = fields.string('test_email_{seq}@preply.com',
                              seq_callback=datetime.datetime.now)
        username = fields.string('test_username_{seq}@preply.com',
                                 seq_callback=datetime.datetime.now)
        first_name = fields.string('first name {seq}')
        last_name = fields.string('last name {seq}')
        password = fields.password('password')
        is_staff = fields.function(lambda: False)

        class Meta:
            exclude_fields = [
                'is_active', 'is_superuser', 'last_login', 'date_joined'
            ]

    class GroupAnonym(register_models.AnonymBase):
        name = fields.string("group_name_{seq}")

        class Meta:
            pass

    register_models.register_anonym([
        (User, UserAnonym),
        (Group, GroupAnonym),
    ])

    Anonymizer(only='django.contrib.auth.models.Group')

    with pytest.raises(LookupError):
        Anonymizer(only='django.contrib.auth.models.WrongGroup')
def test_register_skip_only():
    register_models.register_skip([User, Permission, Group])

    Anonymizer(only='django.contrib.auth.models.Group')

    with pytest.raises(LookupError):
        Anonymizer(only='django.contrib.auth.models.WrongGroup')
def test_register_clean_only():
    register_models.register_clean([
        (User, register_models.AnonymBase),
        (Permission, register_models.AnonymBase(truncate=True)),
        (Group, register_models.AnonymBase())
    ])
    Anonymizer(only='django.contrib.auth.models.Group')

    with pytest.raises(LookupError):
        Anonymizer(only='django.contrib.auth.models.WrongGroup')
Example #4
0
def register_anonym(models):
    for model, cls_anonym in models:

        if model._meta.db_table not in connection.introspection.table_names():
            raise ProgrammingError(f'Model {Anonymizer.key(model)} '
                                   f'registered, but table does not exist')

        cls_anonym.init_meta(model)

        anonym_fields = set(cls_anonym.get_fields_names())
        model_fields = set(field.name for field in model._meta.get_fields()
                           if isinstance(field, Field))

        if hasattr(cls_anonym.Meta, 'exclude_fields'):
            exclude_fields = set(cls_anonym.Meta.exclude_fields)
        else:
            exclude_fields = model_fields - anonym_fields

        exclude_fields.update(cls_anonym.get_relation_fields(model))

        if exclude_fields & anonym_fields:
            raise LookupError(
                f'Fields {list(exclude_fields & anonym_fields)} of model '
                f'{Anonymizer.key(model)} are present in both anonymization '
                f'and excluded lists')

        specified_fields = exclude_fields | anonym_fields

        if specified_fields < model_fields:
            raise LookupError(
                f'Fields {list(model_fields - specified_fields)} were not '
                f'registered in {Anonymizer.key(cls_anonym)} class for '
                f'{Anonymizer.key(model)} model')
        if specified_fields > model_fields:
            raise LookupError(
                f'Fields {list(specified_fields - model_fields)} present in '
                f'{Anonymizer.key(cls_anonym)} class, but does not exist in '
                f'{Anonymizer.key(model)} model'
                '')
        if specified_fields != model_fields:
            raise LookupError(
                f'Fields in {Anonymizer.key(cls_anonym)} are not '
                f'the same as in {Anonymizer.key(model)} Check spelling')

        if Anonymizer.key(model) in Anonymizer.anonym_models.keys():
            raise ValueError(f'Model {Anonymizer.key(model)} '
                             f'is already declared in register_anonym')
        Anonymizer.anonym_models[Anonymizer.key(model)] = cls_anonym
Example #5
0
def register_clean(models):
    for model, cls_anonym in models:
        if not (cls_anonym == AnonymBase
                or isinstance(cls_anonym, AnonymBase)):
            raise TypeError(
                f'Class used for cleaning model {Anonymizer.key(model)} does '
                f'not belong to the allowed {Anonymizer.key(AnonymBase)}')

        if model._meta.db_table not in connection.introspection.table_names():
            raise ProgrammingError(f'Model {Anonymizer.key(model)} '
                                   f'registered, but table does not exist')

        queryset = model.objects.all()
        queryset.truncate = cls_anonym.truncate
        if Anonymizer.key(model) in Anonymizer.clean_models.keys():
            raise ValueError(f'Model {Anonymizer.key(model)} '
                             f'is already declared in register_clean')
        Anonymizer.clean_models[Anonymizer.key(model)] = queryset
Example #6
0
 def handle(self, *args, **options):
     self.action = options['action']
     self.check_only = options['check_only']
     self.only = options['only']
     self.soft_mode = options['soft_mode']
     start = time.time()
     anonymizer = Anonymizer(soft_mode=self.soft_mode, only=self.only)
     self.stdout.write('Check pass successfully')
     self.stdout.write(f'{"=-" * 25}=')
     if self.check_only is True:
         return
     if self.action is None or self.action == 'anonymize':
         if ((self.only is None) or
             (self.only is not None
              and self.only in Anonymizer.anonym_models)) and len(
                  anonymizer.anonym_models) > 0:
             self.stdout.write('Anonymizing started')
             anonymizer.anonymize(only=self.only)
             self.stdout.write('Anonymizing finished')
     if self.action is None or self.action == 'clean':
         if ((self.only is None) or
             (self.only is not None and self.only in Anonymizer.clean_models
              )) and len(anonymizer.clean_models) > 0:
             self.stdout.write('Cleaning started')
             anonymizer.clean(only=self.only)
             self.stdout.write('Cleaning finished')
     end = time.time() - start
     self.stdout.write(f'{"=-" * 25}=')
     self.stdout.write(f'Total time (sec.): {end:.2f}')
Example #7
0
 def handle(self, *args, **options):
     start = time.time()
     anonymizer = Anonymizer(soft_mode=options['soft_mode'])
     if options['action'] is None or options['action'] == 'anonymize':
         anonymizer.anonymize()
     if options['action'] is None or options['action'] == 'clean':
         anonymizer.clean()
     end = time.time()
     print('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=')
     print('Total time (sec.): {}'.format(end - start))
Example #8
0
 def handle(self, *args, **options):
     print 'Anonymizer found next DB settings:'
     for db in settings.DATABASES:
         print '{} -- NAME: {}, HOST: {}'.format(
             db, settings.DATABASES[db]['NAME'],
             settings.DATABASES[db]['HOST'] or 'localhsot')
     start = time.time()
     anonymizer = Anonymizer(soft_mode=options['soft_mode'])
     if options['action'] is None or options['action'] == 'anonymize':
         anonymizer.anonymize()
     if options['action'] is None or options['action'] == 'clean':
         anonymizer.clean()
     end = time.time()
     print '=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-='
     print 'Total time (sec.): {}'.format(end - start)
Example #9
0
def register_skip(models):
    for model in models:
        if Anonymizer.key(model) in Anonymizer.skip_models:
            raise ValueError(f'Model {Anonymizer.key(model)} '
                             f'is already declared in register_skip')
        Anonymizer.skip_models.append(Anonymizer.key(model))