Ticket #5968: 5968-4.patch

File 5968-4.patch, 6.0 KB (added by Aymeric Augustin, 13 years ago)
  • docs/ref/contrib/databrowse.txt

     
    33332. Register a number of models with the Databrowse site::
    3434
    3535       from django.contrib import databrowse
    36        from myapp.models import SomeModel, SomeOtherModel
     36       from myapp.models import SomeModel, SomeOtherModel, YetAnotherModel
    3737
    3838       databrowse.site.register(SomeModel)
    39        databrowse.site.register(SomeOtherModel)
     39       databrowse.site.register(SomeOtherModel, YetAnotherModel)
    4040
    4141   Note that you should register the model *classes*, not instances.
    4242
     43   .. versionchanged:: 1.4
     44
     45   Since Django 1.4, it is possible to register several models in the same
     46   call to :func:`~databrowse.site.register`.
     47
    4348   It doesn't matter where you put this, as long as it gets executed at some
    4449   point. A good place for it is in your :doc:`URLconf file
    4550   </topics/http/urls>` (``urls.py``).
  • tests/runtests.py

     
    44import subprocess
    55import sys
    66import tempfile
     7import warnings
    78
    89from django import contrib
    910
     11# databrowse is deprecated, but we still want to run its tests
     12warnings.filterwarnings('ignore', "The Databrowse contrib app is deprecated",
     13                        PendingDeprecationWarning, 'django.contrib.databrowse')
    1014
    1115CONTRIB_DIR_NAME = 'django.contrib'
    1216MODEL_TESTS_DIR_NAME = 'modeltests'
     
    3438    'django.contrib.comments',
    3539    'django.contrib.admin',
    3640    'django.contrib.admindocs',
     41    'django.contrib.databrowse',
    3742    'django.contrib.staticfiles',
    3843    'django.contrib.humanize',
    3944    'regressiontests.staticfiles_tests',
  • django/contrib/databrowse/tests.py

     
     1from django.contrib import databrowse
     2from django.db import models
     3from django.test import TestCase
     4
     5
     6class SomeModel(models.Model):
     7    some_field = models.CharField(max_length=50)
     8
     9    def __unicode__(self):
     10        return self.some_field
     11
     12
     13class SomeOtherModel(models.Model):
     14    some_other_field = models.CharField(max_length=50)
     15
     16    def __unicode__(self):
     17        return self.some_other_field
     18
     19
     20class YetAnotherModel(models.Model):
     21    yet_another_field = models.CharField(max_length=50)
     22
     23    def __unicode__(self):
     24        return self.yet_another_field
     25
     26
     27class DatabrowseTests(TestCase):
     28
     29    def test_databrowse_register_unregister(self):
     30        databrowse.site.register(SomeModel)
     31        self.assertTrue(SomeModel in databrowse.site.registry)
     32        databrowse.site.register(SomeOtherModel, YetAnotherModel)
     33        self.assertTrue(SomeOtherModel in databrowse.site.registry)
     34        self.assertTrue(YetAnotherModel in databrowse.site.registry)
     35
     36        self.assertRaisesMessage(
     37            databrowse.sites.AlreadyRegistered,
     38            'The model SomeModel is already registered',
     39            databrowse.site.register, SomeModel, SomeOtherModel
     40        )
     41
     42        databrowse.site.unregister(SomeOtherModel)
     43        self.assertFalse(SomeOtherModel in databrowse.site.registry)
     44        databrowse.site.unregister(SomeModel, YetAnotherModel)
     45        self.assertFalse(SomeModel in databrowse.site.registry)
     46        self.assertFalse(YetAnotherModel in databrowse.site.registry)
     47
     48        self.assertRaisesMessage(
     49            databrowse.sites.NotRegistered,
     50            'The model SomeModel is not registered',
     51            databrowse.site.unregister, SomeModel, SomeOtherModel
     52        )
     53
     54        self.assertRaisesMessage(
     55            databrowse.sites.AlreadyRegistered,
     56            'The model SomeModel is already registered',
     57            databrowse.site.register, SomeModel, SomeModel
     58        )
  • django/contrib/databrowse/models.py

     
     1# Empty models.py to allow for specifying databrowse as a test label.
  • django/contrib/databrowse/sites.py

     
    7373        self.registry = {} # model_class -> databrowse_class
    7474        self.root_url = None
    7575
    76     def register(self, model_or_iterable, databrowse_class=None, **options):
     76    def register(self, *model_list, **options):
    7777        """
    7878        Registers the given model(s) with the given databrowse site.
    7979
     
    8484
    8585        If a model is already registered, this will raise AlreadyRegistered.
    8686        """
    87         databrowse_class = databrowse_class or DefaultModelDatabrowse
    88         if issubclass(model_or_iterable, models.Model):
    89             model_or_iterable = [model_or_iterable]
    90         for model in model_or_iterable:
     87        databrowse_class = options.pop('databrowse_class', DefaultModelDatabrowse)
     88        for model in model_list:
    9189            if model in self.registry:
    9290                raise AlreadyRegistered('The model %s is already registered' % model.__name__)
    9391            self.registry[model] = databrowse_class
    9492
    95     def unregister(self, model_or_iterable):
     93    def unregister(self, *model_list):
    9694        """
    9795        Unregisters the given model(s).
    9896
    9997        If a model isn't already registered, this will raise NotRegistered.
    10098        """
    101         if issubclass(model_or_iterable, models.Model):
    102             model_or_iterable = [model_or_iterable]
    103         for model in model_or_iterable:
     99        for model in model_list:
    104100            if model not in self.registry:
    105101                raise NotRegistered('The model %s is not registered' % model.__name__)
    106102            del self.registry[model]
Back to Top