def test_clean_overwrite_url(self):
        user = AnonymousUser()
        user.is_superuser = True
        user.pk = 1
        request = type('Request', (object,), {'user': user})
        with SettingsOverride():
            data = {
                'title': 'TestPage',
                'slug': 'test-page',
                'language': 'en',
                'overwrite_url': '/overwrite/url/',
                'site': Site.objects.get_current().pk,
                'template': get_cms_setting('TEMPLATES')[0][0],
                'published': True
            }

            form = PageForm(data)
            self.assertTrue(form.is_valid(), form.errors.as_text())
            # WTF? WHY DOES form.save() not handle this stuff???
            instance = form.save()
            instance.permission_user_cache = user
            instance.permission_advanced_settings_cache = True
            Title.objects.set_or_create(request, instance, form, 'en')
            form = PageForm(data, instance=instance)
            self.assertTrue(form.is_valid(), form.errors.as_text())
Exemplo n.º 2
0
    def get_context_data(self, **kwargs):
        context = super(GroupObjectCountMixin, self).get_context_data(**kwargs)

        object_counts = {}
        for app in app_registry:
            app_name = app_registry.get_name(app)
            if self.group.is_app_deactivated(app):
                continue
            if app in self.app_object_count_mappings:
                model = resolve_class(self.app_object_count_mappings[app])
                # only for counting the objects, we use a fake superuser, so we get the actual
                # counts of the contents, and not the visible ones for current user
                fake_admin = AnonymousUser()
                fake_admin.is_superuser = True
                object_counts[app_name] = model.get_current(
                    self.group, fake_admin).count()
        context.update({
            'object_counts': object_counts,
        })
        return context
Exemplo n.º 3
0
def get_user(request):
    """
    Return the user model instance associated with the given request session.
    If no user is retrieved, return an instance of `AnonymousUser`.
    """

    user = AnonymousUser()

    token = request.META.get('HTTP_AUTHORIZATION')
    if not token:
        return (user, 'token不存在', 400, ResCode.Token_Missing)

    start = time.time()
    try:
        result = requests.get(settings.SSO_VERIFY,
                              headers={'Authorization': token},
                              verify=False)
        status_code = result.status_code
        result = result.json()
    except Exception as ex:
        logger.error('SSO登录授权验证失败:' + str(ex))
        return (user, '服务器异常,登录授权验证失败', 500, ResCode.Token_Missing)
    end = time.time()
    logger.debug('sso verify time:{} ms'.format((end - start) * 1000))

    rescode = result.get('rescode')
    if rescode == res_code['success']:
        user_data = result.get('data')
        user = User()
        user.id = user_data.get('id')
        user.username = user_data.get('username')
        user.mobile = user_data.get('mobile')
        user.email = user_data.get('email')
        user.user_type = user_data.get('user_type')
        user.is_superuser = user_data.get('is_superuser')
        user.is_staff = user_data.get('is_staff')

    return (user, result.get('msg'), status_code, rescode)