Beispiel #1
0
    def setUp(self):
        self.content1 = CmsContent(name='name1', content='content1', site_id=1)
        self.content1.save()

        self.admin_user = User.objects.create_user('admin', '*****@*****.**',
                                                   'temporary')
        self.admin_user.is_staff = True
        self.admin_user.save()

        self.user_wo_perm = User.objects.create_user('user_wo_perm',
                                                     '*****@*****.**',
                                                     'temporary')
        self.user_wo_perm.save()

        content_type = ContentType.objects.get_for_model(CmsContent)

        self.user_w_add_perm = User.objects.create_user(
            'user_w_add_perm', '*****@*****.**', 'temporary')
        self.user_w_add_perm.save()
        add_perm = Permission.objects.create(
            codename='content_edit_add_cmscontent',
            name='add',
            content_type=content_type)
        self.user_w_add_perm.user_permissions.add(add_perm)

        self.user_w_change_perm = User.objects.create_user(
            'user_w_change_perm', '*****@*****.**', 'temporary')
        self.user_w_change_perm.save()
        change_perm = Permission.objects.create(
            codename='content_edit_change_cmscontent',
            name='change',
            content_type=content_type)
        self.user_w_change_perm.user_permissions.add(change_perm)
Beispiel #2
0
    def setUp(self):  
        self.content1 = CmsContent(name='name1', content='content1',site_id=1)
        self.content1.save()

        self.admin_user = User.objects.create_user('admin', '*****@*****.**', 'temporary')
        self.admin_user.is_staff = True
        self.admin_user.save()

        self.user_wo_perm = User.objects.create_user('user_wo_perm', '*****@*****.**', 'temporary')
        self.user_wo_perm.save()

        content_type = ContentType.objects.get_for_model(CmsContent)

        self.user_w_add_perm = User.objects.create_user('user_w_add_perm', '*****@*****.**', 'temporary')
        self.user_w_add_perm.save()
        add_perm = Permission.objects.create(codename='content_edit_add_cmscontent',
                                       name='add',
                                       content_type=content_type)
        self.user_w_add_perm.user_permissions.add(add_perm)

        self.user_w_change_perm = User.objects.create_user('user_w_change_perm', '*****@*****.**', 'temporary')
        self.user_w_change_perm.save()
        change_perm = Permission.objects.create(codename='content_edit_change_cmscontent',
                                       name='change',
                                       content_type=content_type)
        self.user_w_change_perm.user_permissions.add(change_perm)
Beispiel #3
0
    def render(self, context):
        user = context.get('user', None)
        request = context.get('request',None)
        if not user:
            if request:
                user = getattr(request, 'user', None)
            if not user:
                user = AnonymousUser()
        current_site=None

        object_filter = {'name': self.content_name}

        # Get current Site object (if used)
        if site:
            if request:
                current_site = get_current_site(request)
            if current_site:
                object_filter['site'] = current_site
            else:
                object_filter['site_id'] = 1

        # Get current content object
        if AUTOCREATE or (not CHECK_PERMS and user.is_staff) or (CHECK_PERMS and user.has_perm('content_edit_add_cmscontent')):
            content = CmsContent.objects.get_or_create(**object_filter)[0]
        else:
            try:
                content = CmsContent.objects.get(**object_filter)
            except CmsContent.DoesNotExist:
                content= CmsContent(content='')

        # Check user Perms
        change_perm = False
        if user.is_authenticated():
            if (CHECK_PERMS and user.has_perm('content_edit_change_cmscontent')) or (not CHECK_PERMS and user.is_staff):
                change_perm = True

        # Generate content
        if change_perm:
            html_content = u'<div id="content_{0}" onblur="save_cms_content(this, \'{0}\')" contenteditable="true">{1}</div>'.format(
                content.name, content.content)
        else:
            html_content = content.content
        return html_content
    def render(self, context):
        user = context.get('user', None)
        request = context.get('request',None)
        if not user:
            if request:
                user = getattr(request, 'user', None)
            if not user:
                user = AnonymousUser()
        current_site=None

        object_filter = {'name': self.content_name}

        # Get current Site object (if used)
        if site:
            if request:
                current_site = get_current_site(request)
            if current_site:
                object_filter['site'] = current_site
            else:
                object_filter['site_id'] = 1

        # Get current content object
        if AUTOCREATE or (not CHECK_PERMS and user.is_staff) or (CHECK_PERMS and user.has_perm('content_edit_add_cmscontent')):
            content = CmsContent.objects.get_or_create(**object_filter)[0]
        else:
            try:
                content = CmsContent.objects.get(**object_filter)
            except CmsContent.DoesNotExist:
                content= CmsContent(content='')

        # Check user Perms
        change_perm = False
        if user.is_authenticated():
            if (CHECK_PERMS and user.has_perm('content_edit_change_cmscontent')) or (not CHECK_PERMS and user.is_staff):
                change_perm = True

        # Generate content
        if change_perm:
            html_content = u'<span id="content_{0}">{1}</span><a href="#" data-tagname="{0}" class="glyphicon glyphicon-pencil btn btn-info btn-sm" data-toggle="modal" data-target="#content-edit-modal"></a>'.format(content.name, content.content)
            
        else:
            html_content = content.content
        return html_content
Beispiel #5
0
class TemplateTagDefaultSettingsTests(TestCase):
    def setUp(self):  
        self.content1 = CmsContent(name='name1', content='content1',site_id=1)
        self.content1.save()

        self.admin_user = User.objects.create_user('admin', '*****@*****.**', 'temporary')
        self.admin_user.is_staff = True
        self.admin_user.save()

        self.user_wo_perm = User.objects.create_user('user_wo_perm', '*****@*****.**', 'temporary')
        self.user_wo_perm.save()

        content_type = ContentType.objects.get_for_model(CmsContent)

        self.user_w_add_perm = User.objects.create_user('user_w_add_perm', '*****@*****.**', 'temporary')
        self.user_w_add_perm.save()
        add_perm = Permission.objects.create(codename='content_edit_add_cmscontent',
                                       name='add',
                                       content_type=content_type)
        self.user_w_add_perm.user_permissions.add(add_perm)

        self.user_w_change_perm = User.objects.create_user('user_w_change_perm', '*****@*****.**', 'temporary')
        self.user_w_change_perm.save()
        change_perm = Permission.objects.create(codename='content_edit_change_cmscontent',
                                       name='change',
                                       content_type=content_type)
        self.user_w_change_perm.user_permissions.add(change_perm)

    def test_existing_content_without_user(self):
        out = Template(
            "{% load content_edit_tags %}"
            "{% cms_content 'name1' %}"
        ).render(Context())
        self.assertEqual(1, CmsContent.objects.count())
        self.assertEqual(out,'content1')

    def test_existing_content_for_user_without_perms(self):
        out = Template(
            "{% load content_edit_tags %}"
            "{% cms_content 'name1' %}"
        ).render(Context({'user':self.user_wo_perm}))
        self.assertEqual(1, CmsContent.objects.count())
        self.assertEqual(out,'content1')    

    def test_existing_content_for_user_with_add_perm(self):
        out = Template(
            "{% load content_edit_tags %}"
            "{% cms_content 'name1' %}"
        ).render(Context({'user':self.user_w_add_perm}))
        self.assertEqual(1, CmsContent.objects.count())
        self.assertEqual(out,'content1')

    def test_existing_content_for_user_with_change_perm(self):
        out = Template(
            "{% load content_edit_tags %}"
            "{% cms_content 'name1' %}"
        ).render(Context({'user':self.user_w_change_perm}))
        self.assertEqual(1, CmsContent.objects.count())
        self.assertEqual(out,'content1')

    def test_existing_content_for_admin_user(self):
        out = Template(
            "{% load content_edit_tags %}"
            "{% cms_content 'name1' %}"
        ).render(Context({'user':self.admin_user}))
        self.assertEqual(1, CmsContent.objects.count())
        self.assertEqual(out,'<div id="content_name1" onblur="save_cms_content(this, \'name1\')" contenteditable="true">content1</div>')

    def test_non_existing_content_without_user(self):
        out = Template(
            "{% load content_edit_tags %}"
            "{% cms_content 'name2' %}"
        ).render(Context())
        self.assertEqual(2, CmsContent.objects.count())
        self.assertEqual(out,'')
        CmsContent.objects.exclude(name='name1').delete()

    def test_non_existing_content_for_user_without_perms(self):
        out = Template(
            "{% load content_edit_tags %}"
            "{% cms_content 'name2' %}"
        ).render(Context({'user':self.user_wo_perm}))
        self.assertEqual(2, CmsContent.objects.count())
        self.assertEqual(out,'')   
        CmsContent.objects.exclude(name='name1').delete()

    def test_non_existing_content_for_user_with_add_perm(self):
        out = Template(
            "{% load content_edit_tags %}"
            "{% cms_content 'name2' %}"
        ).render(Context({'user':self.user_w_add_perm}))
        self.assertEqual(2, CmsContent.objects.count())
        self.assertEqual(out,'')
        CmsContent.objects.exclude(name='name1').delete()

    def test_non_existing_content_for_user_with_change_perm(self):
        out = Template(
            "{% load content_edit_tags %}"
            "{% cms_content 'name2' %}"
        ).render(Context({'user':self.user_w_change_perm}))
        self.assertEqual(2, CmsContent.objects.count())
        self.assertEqual(out,'')
        CmsContent.objects.exclude(name='name1').delete()

    def test_non_existing_content_for_admin_user(self):
        out = Template(
            "{% load content_edit_tags %}"
            "{% cms_content 'name2' %}"
        ).render(Context({'user':self.admin_user}))
        self.assertEqual(2, CmsContent.objects.count())
        self.assertEqual(out,'<div id="content_name2" onblur="save_cms_content(this, \'name2\')" contenteditable="true"></div>')
        CmsContent.objects.exclude(name='name1').delete()
Beispiel #6
0
class TemplateTagDefaultSettingsTests(TestCase):
    def setUp(self):
        self.content1 = CmsContent(name='name1', content='content1', site_id=1)
        self.content1.save()

        self.admin_user = User.objects.create_user('admin', '*****@*****.**',
                                                   'temporary')
        self.admin_user.is_staff = True
        self.admin_user.save()

        self.user_wo_perm = User.objects.create_user('user_wo_perm',
                                                     '*****@*****.**',
                                                     'temporary')
        self.user_wo_perm.save()

        content_type = ContentType.objects.get_for_model(CmsContent)

        self.user_w_add_perm = User.objects.create_user(
            'user_w_add_perm', '*****@*****.**', 'temporary')
        self.user_w_add_perm.save()
        add_perm = Permission.objects.create(
            codename='content_edit_add_cmscontent',
            name='add',
            content_type=content_type)
        self.user_w_add_perm.user_permissions.add(add_perm)

        self.user_w_change_perm = User.objects.create_user(
            'user_w_change_perm', '*****@*****.**', 'temporary')
        self.user_w_change_perm.save()
        change_perm = Permission.objects.create(
            codename='content_edit_change_cmscontent',
            name='change',
            content_type=content_type)
        self.user_w_change_perm.user_permissions.add(change_perm)

    def test_existing_content_without_user(self):
        out = Template("{% load content_edit_tags %}"
                       "{% cms_content 'name1' %}").render(Context())
        self.assertEqual(1, CmsContent.objects.count())
        self.assertEqual(out, 'content1')

    def test_existing_content_for_user_without_perms(self):
        out = Template("{% load content_edit_tags %}"
                       "{% cms_content 'name1' %}").render(
                           Context({'user': self.user_wo_perm}))
        self.assertEqual(1, CmsContent.objects.count())
        self.assertEqual(out, 'content1')

    def test_existing_content_for_user_with_add_perm(self):
        out = Template("{% load content_edit_tags %}"
                       "{% cms_content 'name1' %}").render(
                           Context({'user': self.user_w_add_perm}))
        self.assertEqual(1, CmsContent.objects.count())
        self.assertEqual(out, 'content1')

    def test_existing_content_for_user_with_change_perm(self):
        out = Template("{% load content_edit_tags %}"
                       "{% cms_content 'name1' %}").render(
                           Context({'user': self.user_w_change_perm}))
        self.assertEqual(1, CmsContent.objects.count())
        self.assertEqual(out, 'content1')

    def test_existing_content_for_admin_user(self):
        out = Template("{% load content_edit_tags %}"
                       "{% cms_content 'name1' %}").render(
                           Context({'user': self.admin_user}))
        self.assertEqual(1, CmsContent.objects.count())
        self.assertEqual(
            out,
            '<div id="content_name1" onblur="save_cms_content(this, \'name1\')" contenteditable="true">content1</div>'
        )

    def test_non_existing_content_without_user(self):
        out = Template("{% load content_edit_tags %}"
                       "{% cms_content 'name2' %}").render(Context())
        self.assertEqual(2, CmsContent.objects.count())
        self.assertEqual(out, '')
        CmsContent.objects.exclude(name='name1').delete()

    def test_non_existing_content_for_user_without_perms(self):
        out = Template("{% load content_edit_tags %}"
                       "{% cms_content 'name2' %}").render(
                           Context({'user': self.user_wo_perm}))
        self.assertEqual(2, CmsContent.objects.count())
        self.assertEqual(out, '')
        CmsContent.objects.exclude(name='name1').delete()

    def test_non_existing_content_for_user_with_add_perm(self):
        out = Template("{% load content_edit_tags %}"
                       "{% cms_content 'name2' %}").render(
                           Context({'user': self.user_w_add_perm}))
        self.assertEqual(2, CmsContent.objects.count())
        self.assertEqual(out, '')
        CmsContent.objects.exclude(name='name1').delete()

    def test_non_existing_content_for_user_with_change_perm(self):
        out = Template("{% load content_edit_tags %}"
                       "{% cms_content 'name2' %}").render(
                           Context({'user': self.user_w_change_perm}))
        self.assertEqual(2, CmsContent.objects.count())
        self.assertEqual(out, '')
        CmsContent.objects.exclude(name='name1').delete()

    def test_non_existing_content_for_admin_user(self):
        out = Template("{% load content_edit_tags %}"
                       "{% cms_content 'name2' %}").render(
                           Context({'user': self.admin_user}))
        self.assertEqual(2, CmsContent.objects.count())
        self.assertEqual(
            out,
            '<div id="content_name2" onblur="save_cms_content(this, \'name2\')" contenteditable="true"></div>'
        )
        CmsContent.objects.exclude(name='name1').delete()