Ticket #5968: 5968-4.patch
File 5968-4.patch, 6.0 KB (added by , 13 years ago) |
---|
-
docs/ref/contrib/databrowse.txt
33 33 2. Register a number of models with the Databrowse site:: 34 34 35 35 from django.contrib import databrowse 36 from myapp.models import SomeModel, SomeOtherModel 36 from myapp.models import SomeModel, SomeOtherModel, YetAnotherModel 37 37 38 38 databrowse.site.register(SomeModel) 39 databrowse.site.register(SomeOtherModel )39 databrowse.site.register(SomeOtherModel, YetAnotherModel) 40 40 41 41 Note that you should register the model *classes*, not instances. 42 42 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 43 48 It doesn't matter where you put this, as long as it gets executed at some 44 49 point. A good place for it is in your :doc:`URLconf file 45 50 </topics/http/urls>` (``urls.py``). -
tests/runtests.py
4 4 import subprocess 5 5 import sys 6 6 import tempfile 7 import warnings 7 8 8 9 from django import contrib 9 10 11 # databrowse is deprecated, but we still want to run its tests 12 warnings.filterwarnings('ignore', "The Databrowse contrib app is deprecated", 13 PendingDeprecationWarning, 'django.contrib.databrowse') 10 14 11 15 CONTRIB_DIR_NAME = 'django.contrib' 12 16 MODEL_TESTS_DIR_NAME = 'modeltests' … … 34 38 'django.contrib.comments', 35 39 'django.contrib.admin', 36 40 'django.contrib.admindocs', 41 'django.contrib.databrowse', 37 42 'django.contrib.staticfiles', 38 43 'django.contrib.humanize', 39 44 'regressiontests.staticfiles_tests', -
django/contrib/databrowse/tests.py
1 from django.contrib import databrowse 2 from django.db import models 3 from django.test import TestCase 4 5 6 class SomeModel(models.Model): 7 some_field = models.CharField(max_length=50) 8 9 def __unicode__(self): 10 return self.some_field 11 12 13 class 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 20 class 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 27 class 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
73 73 self.registry = {} # model_class -> databrowse_class 74 74 self.root_url = None 75 75 76 def register(self, model_or_iterable, databrowse_class=None, **options):76 def register(self, *model_list, **options): 77 77 """ 78 78 Registers the given model(s) with the given databrowse site. 79 79 … … 84 84 85 85 If a model is already registered, this will raise AlreadyRegistered. 86 86 """ 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: 91 89 if model in self.registry: 92 90 raise AlreadyRegistered('The model %s is already registered' % model.__name__) 93 91 self.registry[model] = databrowse_class 94 92 95 def unregister(self, model_or_iterable):93 def unregister(self, *model_list): 96 94 """ 97 95 Unregisters the given model(s). 98 96 99 97 If a model isn't already registered, this will raise NotRegistered. 100 98 """ 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: 104 100 if model not in self.registry: 105 101 raise NotRegistered('The model %s is not registered' % model.__name__) 106 102 del self.registry[model]