Python django.contrib.admin.sites.AdminSite() Examples

The following are 30 code examples of django.contrib.admin.sites.AdminSite(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.contrib.admin.sites , or try the search function .
Example #1
Source File: test_admin.py    From django-usersettings2 with BSD 3-Clause "New" or "Revised" License 8 votes vote down vote up
def setUp(self):
        Site.objects.get_or_create(id=settings.SITE_ID, domain='example.com', name='example.com')
        self.obj = get_usersettings_model().objects.create(**self.usersettings_data)
        self.user = get_user_model().objects.create_superuser(
            self.username, self.email, self.password)

        self.assertTrue(self.client.login(
            username=self.username, password=self.password),
            'Failed to login user %s' % self.email)

        factory = RequestFactory()
        request = factory.get('/admin')
        request.user = self.user
        request.session = {}

        self.request = request
        self.settings_admin = SettingsAdmin(get_usersettings_model(), AdminSite())

        # Hack to test this function as it calls 'messages.add'
        # See https://code.djangoproject.com/ticket/17971
        setattr(self.request, 'session', 'session')
        messages = FallbackStorage(self.request)
        setattr(self.request, '_messages', messages) 
Example #2
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_inline_has_add_permission_uses_obj(self):
        class ConcertInline(TabularInline):
            model = Concert

            def has_add_permission(self, request, obj):
                return bool(obj)

        class BandAdmin(ModelAdmin):
            inlines = [ConcertInline]

        ma = BandAdmin(Band, AdminSite())
        request = MockRequest()
        request.user = self.MockAddUser()
        self.assertEqual(ma.get_inline_instances(request), [])
        band = Band(name='The Doors', bio='', sign_date=date(1965, 1, 1))
        inline_instances = ma.get_inline_instances(request, band)
        self.assertEqual(len(inline_instances), 1)
        self.assertIsInstance(inline_instances[0], ConcertInline) 
Example #3
Source File: tests.py    From django-otp with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def setUp(self):
        """
        Create a device at the fourth time step. The current token is 154567.
        """
        try:
            self.admin = self.create_user(
                'admin', 'password', email='admin@example.com',
                is_staff=True
            )
        except IntegrityError:
            self.skipTest("Unable to create the test user.")
        else:
            self.device = self.admin.totpdevice_set.create(
                key='2a2bbba1092ffdd25a328ad1a0a5f5d61d7aacc4', step=30,
                t0=int(time() - (30 * 3)), digits=6, tolerance=0, drift=0
            )
        self.device_admin = TOTPDeviceAdmin(TOTPDevice, AdminSite())
        self.get_request = RequestFactory().get('/')
        self.get_request.user = self.admin 
Example #4
Source File: tests.py    From django-otp with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def setUp(self):
        """
        Create a device at the fourth time step. The current token is 154567.
        """
        try:
            self.admin = self.create_user(
                'admin', 'password', email='admin@example.com',
                is_staff=True
            )
        except IntegrityError:
            self.skipTest("Unable to create test user.")
        else:
            self.device = self.admin.hotpdevice_set.create(
                key='d2e8a68036f68960b1c30532bb6c56da5934d879', digits=6,
                tolerance=1, counter=0
            )
        self.device_admin = HOTPDeviceAdmin(HOTPDevice, AdminSite())
        self.get_request = RequestFactory().get('/')
        self.get_request.user = self.admin 
Example #5
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_inline_has_add_permission_uses_obj(self):
        class ConcertInline(TabularInline):
            model = Concert

            def has_add_permission(self, request, obj):
                return bool(obj)

        class BandAdmin(ModelAdmin):
            inlines = [ConcertInline]

        ma = BandAdmin(Band, AdminSite())
        request = MockRequest()
        request.user = self.MockAddUser()
        self.assertEqual(ma.get_inline_instances(request), [])
        band = Band(name='The Doors', bio='', sign_date=date(1965, 1, 1))
        inline_instances = ma.get_inline_instances(request, band)
        self.assertEqual(len(inline_instances), 1)
        self.assertIsInstance(inline_instances[0], ConcertInline) 
Example #6
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_autocomplete_e040(self):
        class NoSearchFieldsAdmin(ModelAdmin):
            pass

        class AutocompleteAdmin(ModelAdmin):
            autocomplete_fields = ('featuring',)

        site = AdminSite()
        site.register(Band, NoSearchFieldsAdmin)
        self.assertIsInvalid(
            AutocompleteAdmin, Song,
            msg=(
                'NoSearchFieldsAdmin must define "search_fields", because '
                'it\'s referenced by AutocompleteAdmin.autocomplete_fields.'
            ),
            id='admin.E040',
            invalid_obj=AutocompleteAdmin,
            admin_site=site,
        ) 
Example #7
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_inline_has_add_permission_without_obj(self):
        # This test will be removed in Django 3.0 when `obj` becomes a required
        # argument of has_add_permission() (#27991).
        class ConcertInline(TabularInline):
            model = Concert

            def has_add_permission(self, request):
                return super().has_add_permission(request)

        class BandAdmin(ModelAdmin):
            inlines = [ConcertInline]

        ma = BandAdmin(Band, AdminSite())
        request = MockRequest()
        request.user = self.MockAddUserWithInline()
        band = Band(name='The Doors', bio='', sign_date=date(1965, 1, 1))
        inline_instances = ma.get_inline_instances(request, band)
        self.assertEqual(len(inline_instances), 1)
        self.assertIsInstance(inline_instances[0], ConcertInline) 
Example #8
Source File: test_admin.py    From ANALYSE with GNU Affero General Public License v3.0 6 votes vote down vote up
def setUp(self):
        """ Test case setup """
        self.user = User.objects.create_user('test_user', 'test_user+courses@edx.org', 'foo')
        self.table_entry = CourseCreator(user=self.user)
        self.table_entry.save()

        self.admin = User.objects.create_user('Mark', 'admin+courses@edx.org', 'foo')
        self.admin.is_staff = True

        self.request = HttpRequest()
        self.request.user = self.admin

        self.creator_admin = CourseCreatorAdmin(self.table_entry, AdminSite())

        self.studio_request_email = 'mark@marky.mark'
        self.enable_creator_group_patch = {
            "ENABLE_CREATOR_GROUP": True,
            "STUDIO_REQUEST_EMAIL": self.studio_request_email
        } 
Example #9
Source File: tests.py    From instiapp-api with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_admin_actions(self):
        complaint_admin = ComplaintModelAdmin(Complaint, AdminSite())
        request = SimpleNamespace()

        Complaint.objects.create(created_by=self.user.profile, status=STATUS_REPORTED)
        queryset = Complaint.objects.filter(status=STATUS_REPORTED)
        complaint_admin.mark_as_resolved(complaint_admin, request, queryset)
        self.assertEqual(Complaint.objects.get(status=STATUS_RESOLVED).status, STATUS_RESOLVED)

        Complaint.objects.create(created_by=self.user.profile, status=STATUS_REPORTED)
        queryset = Complaint.objects.filter(status=STATUS_REPORTED)
        complaint_admin.mark_as_in_progress(complaint_admin, request, queryset)
        self.assertEqual(Complaint.objects.get(status=STATUS_IN_PROGRESS).status, STATUS_IN_PROGRESS)

        Complaint.objects.create(created_by=self.user.profile, status=STATUS_REPORTED)
        queryset = Complaint.objects.filter(status=STATUS_REPORTED)
        complaint_admin.mark_as_deleted(complaint_admin, request, queryset)
        self.assertEqual(Complaint.objects.get(status=STATUS_DELETED).status, STATUS_DELETED) 
Example #10
Source File: test_productdb_admin.py    From product-database with MIT License 6 votes vote down vote up
def test_has_valid_migration_options(self):
        site = AdminSite()
        product_admin = admin.ProductAdmin(models.Product, site)
        obj = models.Product.objects.create(
            product_id="Product",
            eox_update_time_stamp=None
        )

        result = product_admin.has_migration_options(obj)
        expected = False
        assert result == expected

        models.ProductMigrationOption.objects.create(
            product=obj,
            migration_source=models.ProductMigrationSource.objects.create(name="test")
        )

        result = product_admin.has_migration_options(obj)
        expected = True
        assert result == expected 
Example #11
Source File: test_productdb_admin.py    From product-database with MIT License 6 votes vote down vote up
def test_preferred_replacement_option(self):
        site = AdminSite()
        product_admin = admin.ProductAdmin(models.Product, site)
        obj = models.Product.objects.create(
            product_id="Product",
            eox_update_time_stamp=None
        )

        result = product_admin.preferred_replacement_option(obj)
        expected = ""
        assert result == expected

        models.ProductMigrationOption.objects.create(
            product=obj,
            migration_source=models.ProductMigrationSource.objects.create(name="test"),
            replacement_product_id="MyProductId"
        )

        result = product_admin.preferred_replacement_option(obj)
        expected = "MyProductId"
        assert result == expected 
Example #12
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_autocomplete_e040(self):
        class NoSearchFieldsAdmin(ModelAdmin):
            pass

        class AutocompleteAdmin(ModelAdmin):
            autocomplete_fields = ('featuring',)

        site = AdminSite()
        site.register(Band, NoSearchFieldsAdmin)
        self.assertIsInvalid(
            AutocompleteAdmin, Song,
            msg=(
                'NoSearchFieldsAdmin must define "search_fields", because '
                'it\'s referenced by AutocompleteAdmin.autocomplete_fields.'
            ),
            id='admin.E040',
            invalid_obj=AutocompleteAdmin,
            admin_site=site,
        ) 
Example #13
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_has_change_permission(self):
        """
        has_change_permission returns True for users who can edit objects and
        False for users who can't.
        """
        ma = ModelAdmin(Band, AdminSite())
        request = MockRequest()
        request.user = self.MockViewUser()
        self.assertIs(ma.has_change_permission(request), False)
        request.user = self.MockAddUser()
        self.assertFalse(ma.has_change_permission(request))
        request.user = self.MockChangeUser()
        self.assertTrue(ma.has_change_permission(request))
        request.user = self.MockDeleteUser()
        self.assertFalse(ma.has_change_permission(request)) 
Example #14
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_has_delete_permission(self):
        """
        has_delete_permission returns True for users who can delete objects and
        False for users who can't.
        """
        ma = ModelAdmin(Band, AdminSite())
        request = MockRequest()
        request.user = self.MockViewUser()
        self.assertIs(ma.has_delete_permission(request), False)
        request.user = self.MockAddUser()
        self.assertFalse(ma.has_delete_permission(request))
        request.user = self.MockChangeUser()
        self.assertFalse(ma.has_delete_permission(request))
        request.user = self.MockDeleteUser()
        self.assertTrue(ma.has_delete_permission(request)) 
Example #15
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def assertIsInvalid(self, model_admin, model, msg, id=None, hint=None, invalid_obj=None, admin_site=None):
        if admin_site is None:
            admin_site = AdminSite()
        invalid_obj = invalid_obj or model_admin
        admin_obj = model_admin(model, admin_site)
        self.assertEqual(admin_obj.check(), [Error(msg, hint=hint, obj=invalid_obj, id=id)]) 
Example #16
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def assertIsInvalidRegexp(self, model_admin, model, msg, id=None, hint=None, invalid_obj=None):
        """
        Same as assertIsInvalid but treats the given msg as a regexp.
        """
        invalid_obj = invalid_obj or model_admin
        admin_obj = model_admin(model, AdminSite())
        errors = admin_obj.check()
        self.assertEqual(len(errors), 1)
        error = errors[0]
        self.assertEqual(error.hint, hint)
        self.assertEqual(error.obj, invalid_obj)
        self.assertEqual(error.id, id)
        self.assertRegex(error.msg, msg) 
Example #17
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def assertIsValid(self, model_admin, model, admin_site=None):
        if admin_site is None:
            admin_site = AdminSite()
        admin_obj = model_admin(model, admin_site)
        self.assertEqual(admin_obj.check(), []) 
Example #18
Source File: test_has_add_permission_obj_deprecation.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def setUp(self):
        self.site = AdminSite()
        self.request = MockRequest()
        self.request.POST = {
            'song_set-TOTAL_FORMS': 4,
            'song_set-INITIAL_FORMS': 1,
        }
        self.request.user = self.MockAddUser()
        self.ma = BandAdmin(Band, self.site) 
Example #19
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_has_add_permission(self):
        """
        has_add_permission returns True for users who can add objects and
        False for users who can't.
        """
        ma = ModelAdmin(Band, AdminSite())
        request = MockRequest()
        request.user = self.MockViewUser()
        self.assertFalse(ma.has_add_permission(request))
        request.user = self.MockAddUser()
        self.assertTrue(ma.has_add_permission(request))
        request.user = self.MockChangeUser()
        self.assertFalse(ma.has_add_permission(request))
        request.user = self.MockDeleteUser()
        self.assertFalse(ma.has_add_permission(request)) 
Example #20
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def setUp(self):
        self.site = AdminSite() 
Example #21
Source File: test_has_add_permission_obj_deprecation.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def setUp(self):
        self.band = Band.objects.create(name='The Doors', bio='', sign_date=date(1965, 1, 1))
        self.song = Song.objects.create(name='test', band=self.band)
        self.site = AdminSite()
        self.request = MockRequest()
        self.request.user = self.MockAddUser()
        self.ma = BandAdmin(Band, self.site) 
Example #22
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_autocomplete_is_onetoone(self):
        class UserAdmin(ModelAdmin):
            search_fields = ('name',)

        class Admin(ModelAdmin):
            autocomplete_fields = ('best_friend',)

        site = AdminSite()
        site.register(User, UserAdmin)
        self.assertIsValid(Admin, ValidationTestModel, admin_site=site) 
Example #23
Source File: test_admin.py    From mapstory with GNU General Public License v3.0 5 votes vote down vote up
def setUp(self):
        self.site = AdminSite()

        # GetPage for testing
        self.getPage = GetPage.objects.create(
            name='Test name', title='Test title', subtitle='Test subtitle',)
        self.assertIsInstance(self.getPage, GetPage)

        # Page content for testing
        self.getPageContent = GetPageContent(
            title='Test title', main_link='#', page=self.getPage)
        self.assertIsInstance(self.getPageContent, GetPageContent) 
Example #24
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def assertIsValid(self, model_admin, model, admin_site=None):
        if admin_site is None:
            admin_site = AdminSite()
        admin_obj = model_admin(model, admin_site)
        self.assertEqual(admin_obj.check(), []) 
Example #25
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def assertIsInvalidRegexp(self, model_admin, model, msg, id=None, hint=None, invalid_obj=None):
        """
        Same as assertIsInvalid but treats the given msg as a regexp.
        """
        invalid_obj = invalid_obj or model_admin
        admin_obj = model_admin(model, AdminSite())
        errors = admin_obj.check()
        self.assertEqual(len(errors), 1)
        error = errors[0]
        self.assertEqual(error.hint, hint)
        self.assertEqual(error.obj, invalid_obj)
        self.assertEqual(error.id, id)
        self.assertRegex(error.msg, msg) 
Example #26
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def assertIsInvalid(self, model_admin, model, msg, id=None, hint=None, invalid_obj=None, admin_site=None):
        if admin_site is None:
            admin_site = AdminSite()
        invalid_obj = invalid_obj or model_admin
        admin_obj = model_admin(model, admin_site)
        self.assertEqual(admin_obj.check(), [Error(msg, hint=hint, obj=invalid_obj, id=id)]) 
Example #27
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_has_module_permission(self):
        """
        as_module_permission returns True for users who have any permission
        for the module and False for users who don't.
        """
        ma = ModelAdmin(Band, AdminSite())
        request = MockRequest()
        request.user = self.MockViewUser()
        self.assertIs(ma.has_module_permission(request), True)
        request.user = self.MockAddUser()
        self.assertTrue(ma.has_module_permission(request))
        request.user = self.MockChangeUser()
        self.assertTrue(ma.has_module_permission(request))
        request.user = self.MockDeleteUser()
        self.assertTrue(ma.has_module_permission(request))

        original_app_label = ma.opts.app_label
        ma.opts.app_label = 'anotherapp'
        try:
            request.user = self.MockViewUser()
            self.assertIs(ma.has_module_permission(request), False)
            request.user = self.MockAddUser()
            self.assertFalse(ma.has_module_permission(request))
            request.user = self.MockChangeUser()
            self.assertFalse(ma.has_module_permission(request))
            request.user = self.MockDeleteUser()
            self.assertFalse(ma.has_module_permission(request))
        finally:
            ma.opts.app_label = original_app_label 
Example #28
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_has_change_permission(self):
        """
        has_change_permission returns True for users who can edit objects and
        False for users who can't.
        """
        ma = ModelAdmin(Band, AdminSite())
        request = MockRequest()
        request.user = self.MockViewUser()
        self.assertIs(ma.has_change_permission(request), False)
        request.user = self.MockAddUser()
        self.assertFalse(ma.has_change_permission(request))
        request.user = self.MockChangeUser()
        self.assertTrue(ma.has_change_permission(request))
        request.user = self.MockDeleteUser()
        self.assertFalse(ma.has_change_permission(request)) 
Example #29
Source File: test_admin.py    From django-email-confirm-la with MIT License 5 votes vote down vote up
def setUp(self):
        mommy.make(
            'email_confirm_la.EmailConfirmation',
            _quantity=1,
        )
        self.site = AdminSite() 
Example #30
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_get_formset_kwargs(self):
        media_inline = MediaInline(Media, AdminSite())

        # Create a formset with default arguments
        formset = media_inline.get_formset(request)
        self.assertEqual(formset.max_num, DEFAULT_MAX_NUM)
        self.assertIs(formset.can_order, False)

        # Create a formset with custom keyword arguments
        formset = media_inline.get_formset(request, max_num=100, can_order=True)
        self.assertEqual(formset.max_num, 100)
        self.assertIs(formset.can_order, True)