예제 #1
0
def get_default_server_setting_values():
    svalues = {
        sv.key_id: sv
        for sv in SettingValue.objects.filter(key_id__in=(
            MAPI_SERVER_URL,
            MAPI_DOMAIN,
            MAPI_SERVER_SSL,
        ))
    }

    result = {}

    try:
        result['url'] = svalues[MAPI_SERVER_URL]
        result['domain'] = svalues[MAPI_DOMAIN]
        result['ssl'] = svalues[MAPI_SERVER_SSL]
    except KeyError as e:
        logger.warning(
            'activesync.utils.get_default_server_setting_values: unfoundable SettingValue(key=%s) '
            '- Populate has not been run ?! (if you are running unit tests you can '
            'ignore this message' % e)  # NB useful for creme_config tests
        result['url'] = SettingValue(key_id=MAPI_SERVER_URL, value='')
        result['domain'] = SettingValue(key_id=MAPI_DOMAIN, value='')
        result['ssl'] = SettingValue(key_id=MAPI_SERVER_SSL, value=False)

    return result
예제 #2
0
    def test_get_4_keys01(self):
        sk1 = SettingKey(
            id='activities-test_get_4_keys01_1',
            description='Display logo?',
            app_label='activities',
            type=SettingKey.BOOL,
        )
        sk2 = SettingKey(
            id='activities-test_get_4_keys02_2',
            description='Logo size',
            app_label='activities',
            type=SettingKey.INT,
        )
        self._register_key(sk1)
        self._register_key(sk2)

        stored_sv1 = SettingValue(key=sk1)
        stored_sv1.value = True
        stored_sv1.save()
        stored_sv2 = SettingValue(key=sk2)
        stored_sv2.value = 100
        stored_sv2.save()

        pk1 = stored_sv1.pk
        pk2 = stored_sv2.pk

        with self.assertNumQueries(1):
            svalues = SettingValue.objects.get_4_keys(
                {'key': sk1.id},  # <= Key ID
                {'key': sk2},  # <= Key instance
            )

        self.assertIsInstance(svalues, dict)
        self.assertEqual(2, len(svalues))

        sv1 = svalues.get(sk1.id)
        self.assertIsInstance(sv1, SettingValue)
        self.assertEqual(pk1, sv1.pk)

        sv2 = svalues.get(sk2.id)
        self.assertEqual(pk2, sv2.pk)

        # Cache
        with self.assertNumQueries(0):
            svalues = SettingValue.objects.get_4_keys({'key': sk1.id})
        self.assertEqual(pk1, svalues[sk1.id].pk)

        # Cache (shared with get_4_key() )
        with self.assertNumQueries(0):
            sv2 = SettingValue.objects.get_4_key(sk2.id)
        self.assertEqual(pk2, sv2.pk)
예제 #3
0
    def test_type_bool(self):
        self.login()

        sk = SettingKey(
            id='activities-test_model_bool',
            description='Display logo?',
            app_label='activities',
            type=SettingKey.BOOL,
        )
        self._register_key(sk)

        sv = SettingValue(key=sk)
        sv.value = True
        sv.save()

        sv = self.refresh(sv)
        self.assertIs(sv.value, True)
        self.assertEqual(
            '<input type="checkbox" checked disabled/>{}'.format(_('Yes')),
            sv.as_html)

        sv.value = False
        sv.save()

        sv = self.refresh(sv)
        self.assertIs(sv.value, False)
        self.assertEqual('<input type="checkbox" disabled/>{}'.format(_('No')),
                         sv.as_html)
예제 #4
0
    def test_type_int(self):
        sk = SettingKey(
            id='persons-test_model_int',
            description='Page size',
            app_label='persons',
            type=SettingKey.INT,
        )
        self.assertFalse(sk.hidden)
        self.assertFalse(sk.blank)

        self._register_key(sk)

        size = 156
        sv = SettingValue(key=sk)
        sv.value = size
        sv.save()

        sv = self.refresh(sv)
        self.assertEqual(size, sv.value)
        self.assertEqual(size, sv.as_html)

        # ---
        size += 1
        sv.value = str(size)
        self.assertEqual(size, sv.value)
예제 #5
0
    def test_edit_email(self):
        self.login()

        sk = SettingKey(
            id='persons-test_edit_email',
            description='Campaign Sender',
            app_label='persons',
            type=SettingKey.EMAIL,
        )
        setting_key_registry.register(sk)

        email = '*****@*****.**'
        sv = SettingValue(key=sk)
        sv.value = email
        sv.save()

        url = self._build_edit_url(sv)

        response = self.assertPOST200(url, data={'value': 42})
        self.assertFormError(response, 'form', 'value',
                             _('Enter a valid email address.'))

        email = '*****@*****.**'
        self.assertNoFormError(self.client.post(url, data={'value': email}))
        self.assertEqual(email, self.refresh(sv).value)
예제 #6
0
    def test_edit_blank02(self):
        self.login()

        sk = SettingKey(
            id='persons-test_edit_blank02',
            description='API key',
            app_label='persons',
            type=SettingKey.INT,
            blank=True,
        )
        setting_key_registry.register(sk)

        sv = SettingValue(key=sk)
        sv.value = 12345
        sv.save()

        self.assertNoFormError(self.client.post(self._build_edit_url(sv)))

        sv = self.refresh(sv)
        self.assertEqual('', sv.value_str)
        self.assertIsNone(sv.value)

        # ---
        self.assertNoFormError(
            self.client.post(self._build_edit_url(sv), data={'value': ''}))

        sv = self.refresh(sv)
        self.assertEqual('', sv.value_str)
        self.assertIsNone(sv.value)
예제 #7
0
    def test_not_blank(self):
        sk = SettingKey('creme_core-test_model_not_blank',
                        description=u'API key',
                        app_label='creme_core',
                        type=SettingKey.STRING,
                        blank=False,
                       )
        self._register_key(sk)

        sv = SettingValue(key=sk)

        with self.assertRaises(ValueError):
            # # SettingValue.objects.create(key=sk, value='')
            # SettingValue.objects.create(key=sk, value=None)
            sv.value = None
            # sv.value = '' # TODO

        value = '111'
        # sv = SettingValue.objects.create(key=sk, value=value)

        with self.assertNoException():
            sv.value = value

        with self.assertRaises(ValueError):
            sv.value = None

        self.assertEqual(value, sv.value)
예제 #8
0
    def test_edit_string(self):
        self.login()

        sk = SettingKey(
            id='persons-test_edit_string',
            description='Page title',
            app_label='persons',
            type=SettingKey.STRING,
            hidden=False,
        )
        setting_key_registry.register(sk)

        title = 'May the source be with you'
        sv = SettingValue(key=sk)
        sv.value = title
        sv.save()

        url = self._build_edit_url(sv)
        response = self.assertGET200(url)
        self.assertTemplateUsed(
            response, 'creme_core/generics/blockform/edit-popup.html')

        context = response.context
        self.assertEqual(
            _('Edit «{key}»').format(key=sk.description), context.get('title'))
        self.assertEqual(_('Save the modifications'),
                         context.get('submit_label'))

        # ---
        title = title.upper()
        self.assertNoFormError(self.client.post(url, data={'value': title}))
        self.assertEqual(title, self.refresh(sv).value)
예제 #9
0
    def test_get_4_key01(self):
        "Key ID."
        sk = SettingKey(
            id='activities-test_get_4_key01',
            description='Display logo?',
            app_label='activities',
            type=SettingKey.BOOL,
        )
        self._register_key(sk)

        sv = SettingValue(key=sk)
        sv.value = True
        sv.save()

        pk = sv.pk

        with self.assertNumQueries(1):
            sv = SettingValue.objects.get_4_key(sk.id)

        self.assertIsInstance(sv, SettingValue)
        self.assertEqual(pk, sv.pk)

        # Cache
        with self.assertNumQueries(0):
            sv = SettingValue.objects.get_4_key(sk.id)

        self.assertEqual(pk, sv.pk)
예제 #10
0
    def test_bad_value(self):
        sk = SettingKey(id='persons-test_bad_value', description=u'Page size',
                        app_label='persons', type=SettingKey.INT,
                       )
        self._register_key(sk)

        sv = SettingValue(key=sk)

        with self.assertRaises(ValueError):
            # SettingValue.objects.create(key=sk, value='abc')
            sv.value = 'abc'
예제 #11
0
    def test_type_string(self):
        sk = SettingKey('creme_core-test_model_string',
                        description=u"Page title",
                        app_label='creme_core',
                        type=SettingKey.STRING, hidden=False,
                       )
        self._register_key(sk)

        title = 'May the source be with you'
        # sv = SettingValue.objects.create(key=sk, user=None, value=title)
        sv = SettingValue(key=sk)
        sv.value = title
        sv.save()

        self.assertEqual(title, self.refresh(sv).value)
예제 #12
0
    def test_edit_app_perm01(self):
        self.login(is_superuser=False, admin_4_apps=['creme_core'])

        sk = SettingKey(
            id='creme_core-test_edit_app_perm01',
            description='Page title',
            app_label='creme_core',
            type=SettingKey.STRING,
            hidden=False,
        )
        setting_key_registry.register(sk)

        sv = SettingValue(key=sk)
        sv.value = 'May the source be with you'
        sv.save()
        self.assertGET200(self._build_edit_url(sv))
예제 #13
0
    def test_edit_hidden02(self):
        "Hidden => not editable (value=False)"
        self.login()

        sk = SettingKey(
            id='persons-test_edit_hidden02',
            description='Display logo ?',
            app_label='persons',
            type=SettingKey.BOOL,
            hidden=True,
        )
        setting_key_registry.register(sk)

        sv = SettingValue(key=sk)
        sv.value = False
        sv.save()
        self.assertGET409(self._build_edit_url(sv))
예제 #14
0
    def test_type_email(self):
        self.login()

        sk = SettingKey(id='persons-test_model_email', description='Campaign Sender',
                        app_label='emails', type=SettingKey.EMAIL,
                       )
        self._register_key(sk)

        email = u'*****@*****.**'
        # sv = SettingValue.objects.create(key=sk, user=self.user, value=email)
        sv = SettingValue(key=sk)
        sv.value = email
        sv.save()

        sv = self.refresh(sv)
        self.assertEqual(email, sv.value)
        self.assertEqual(email, sv.as_html)
예제 #15
0
    def test_type_hour(self):
        self.login()

        sk = SettingKey(id='persons-test_model_hour', description='Reminder hour',
                        app_label='persons', type=SettingKey.HOUR,
                       )
        self._register_key(sk)

        hour = 9
        # sv = SettingValue.objects.create(key=sk, user=self.user, value=hour)
        sv = SettingValue(key=sk)
        sv.value = hour
        sv.save()

        sv = self.refresh(sv)
        self.assertEqual(hour, sv.value)
        self.assertEqual(_('{hour}h').format(hour=hour), sv.as_html)
예제 #16
0
    def test_edit_bool(self):
        self.login()

        sk = SettingKey(
            id='persons-test_edit_bool',
            description='Display logo ?',
            app_label='persons',
            type=SettingKey.BOOL,
        )
        setting_key_registry.register(sk)

        sv = SettingValue(key=sk)
        sv.value = True
        sv.save()

        # False -> empty POST
        self.assertNoFormError(
            self.client.post(self._build_edit_url(sv), data={}))
        self.assertFalse(self.refresh(sv).value)
예제 #17
0
    def test_edit_hour(self):
        self.login()

        sk = SettingKey(
            id='persons-test_edit_hour',
            description='Reminder hour',
            app_label='persons',
            type=SettingKey.HOUR,
        )
        setting_key_registry.register(sk)

        hour = 11
        sv = SettingValue(key=sk)
        sv.value = hour
        sv.save()

        url = self._build_edit_url(sv)
        hour += 1
        self.assertNoFormError(self.client.post(url, data={'value': hour}))
        self.assertEqual(hour, self.refresh(sv).value)

        response = self.assertPOST200(url, data={'value': 24})
        self.assertFormError(
            response,
            'form',
            'value',
            _('Ensure this value is less than or equal to %(limit_value)s.') %
            {
                'limit_value': 23,
            },
        )

        response = self.assertPOST200(url, data={'value': -1})
        self.assertFormError(
            response,
            'form',
            'value',
            _('Ensure this value is greater than or equal to %(limit_value)s.')
            % {
                'limit_value': 0,
            },
        )
예제 #18
0
    def test_edit_int(self):
        self.login()

        sk = SettingKey(
            id='persons-test_edit_int',
            description='Page size',
            app_label='persons',
            type=SettingKey.INT,
        )
        setting_key_registry.register(sk)

        size = 156
        sv = SettingValue(key=sk)
        sv.value = size
        sv.save()

        size += 15
        self.assertNoFormError(
            self.client.post(self._build_edit_url(sv), data={'value': size}))
        self.assertEqual(size, self.refresh(sv).value)
예제 #19
0
    def test_blank(self):
        sk = SettingKey('creme_core-test_model_blank',
                        description=u'API key',
                        app_label='creme_core',
                        type=SettingKey.STRING,
                        blank=True,
                       )
        self._register_key(sk)

        # sv = SettingValue.objects.create(key=sk, value='')
        sv = SettingValue(key=sk)
        sv.value = ''
        sv.save()

        sv = self.refresh(sv)
        self.assertEqual('', sv.value_str)
        self.assertIsNone(sv.value)
        self.assertEqual('', sv.as_html)

        sv.value = None
        self.assertIsNone(sv.value)
예제 #20
0
    def test_edit_blank01(self):
        self.login()

        sk = SettingKey(
            id='persons-test_edit_blank01',
            description='API key',
            app_label='persons',
            type=SettingKey.STRING,
            blank=True,
        )
        setting_key_registry.register(sk)

        # sv = SettingValue.objects.create(key=sk, value='123-456-abc')
        sv = SettingValue(key=sk)
        sv.value = '123-456-abc'
        sv.save()

        self.assertNoFormError(
            self.client.post(self._build_edit_url(sv), data={'value': ''}))

        sv = self.refresh(sv)
        self.assertEqual('', sv.value_str)
        self.assertIsNone(sv.value)