예제 #1
0
    def test_can_save_prefs(self):
        user = UserFactory.create(is_superuser=True)

        key1 = KeyFactory.create(name='some.random.1')
        key2 = KeyFactory.create(name='some.random.2')

        UsersViolationsPrefsFactory.create(user=user, key=key1, is_active=True)
        UsersViolationsPrefsFactory.create(user=user, key=key2, is_active=False)

        loaded_prefs = UsersViolationsPrefs.get_prefs(self.db, user)
        expect(loaded_prefs).to_length(2)
        expect(loaded_prefs).to_be_like({
            'some.random.1': True,
            'some.random.2': False
        })

        yield self.authenticated_fetch(
            '/users/violations-prefs/',
            user_email=user.email,
            method='POST',
            body=dumps([
                {'key': 'some.random.1', 'is_active': False},
                {'key': 'some.random.2', 'is_active': True},
            ])
        )

        loaded_prefs = UsersViolationsPrefs.get_prefs(self.db, user)
        expect(loaded_prefs).to_length(2)
        expect(loaded_prefs).to_be_like({
            'some.random.1': False,
            'some.random.2': True
        })
    def test_can_get_prefs(self):
        user = UserFactory.create()
        key1 = KeyFactory.create()
        key2 = KeyFactory.create()

        UsersViolationsPrefs.insert_pref(self.db, user, key1, True)
        UsersViolationsPrefs.insert_pref(self.db, user, key2, False)

        data = UsersViolationsPrefs.get_prefs(self.db, user)

        expect(data).to_length(2)

        expect(data).to_be_like({key1.name: True, key2.name: False})
    def test_can_get_prefs(self):
        user = UserFactory.create()
        key1 = KeyFactory.create()
        key2 = KeyFactory.create()

        UsersViolationsPrefs.insert_pref(self.db, user, key1, True)
        UsersViolationsPrefs.insert_pref(self.db, user, key2, False)

        data = UsersViolationsPrefs.get_prefs(self.db, user)

        expect(data).to_length(2)

        expect(data).to_be_like(
            {key1.name: True, key2.name: False}
        )
예제 #4
0
    def get(self):
        user = self.get_authenticated_user()

        if not user:
            return

        user_prefs = UsersViolationsPrefs.get_prefs(self.db, user)

        violation_defs = self.application.violation_definitions

        user_prefs_keys = set(user_prefs.keys())
        violation_defs_keys = set(violation_defs.keys())

        insert_items = list(violation_defs_keys - user_prefs_keys)
        if insert_items:
            UsersViolationsPrefs.insert_prefs(self.db, user, insert_items)

        remove_items = list(user_prefs_keys - violation_defs_keys)
        if remove_items:
            UsersViolationsPrefs.delete_prefs(self.db, user, remove_items)

        result = []
        for key_name in violation_defs:
            category = violation_defs[key_name]['category']
            name = violation_defs[key_name]['title']
            description = violation_defs[key_name]['generic_description']
            is_active = user_prefs.get(key_name, True)

            result.append({
                'key': key_name,
                'name': self._(name),
                'description': self._(description),
                'category': self._(category),
                'is_active': is_active
            })

        self.write_json(result)