Ticket #16502: aggregated_patch_with_tests.diff

File aggregated_patch_with_tests.diff, 7.3 KB (added by Silver_Ghost, 13 years ago)

merging of patches and adding regression tests

  • django/template/loader.py

    diff --git a/django/template/loader.py b/django/template/loader.py
    a b  
    179179
    180180def select_template(template_name_list):
    181181    "Given a list of template names, returns the first that can be loaded."
     182    if not template_name_list:
     183        raise ValueError("Couldn't choose template for load since there are"
     184                         " no candidates")
    182185    not_found = []
    183186    for template_name in template_name_list:
    184187        try:
  • django/views/generic/detail.py

    diff --git a/django/views/generic/detail.py b/django/views/generic/detail.py
    a b  
    5858        `get_object` is overridden.
    5959        """
    6060        if self.queryset is None:
    61             if self.model:
    62                 return self.model._default_manager.all()
     61            model = self.get_model()
     62            if model:
     63                return model._default_manager.all()
    6364            else:
    6465                raise ImproperlyConfigured(u"%(cls)s is missing a queryset. Define "
    6566                                           u"%(cls)s.model, %(cls)s.queryset, or override "
     
    9293            context[context_object_name] = self.object
    9394        return context
    9495
     96    def get_model(self):
     97        return self.model
     98
    9599
    96100class BaseDetailView(SingleObjectMixin, View):
    97101    def get(self, request, **kwargs):
     
    132136                self.object._meta.object_name.lower(),
    133137                self.template_name_suffix
    134138            ))
    135         elif hasattr(self, 'model') and hasattr(self.model, '_meta'):
    136             names.append("%s/%s%s.html" % (
    137                 self.model._meta.app_label,
    138                 self.model._meta.object_name.lower(),
    139                 self.template_name_suffix
    140             ))
     139        elif hasattr(self, 'get_model'):
     140            model = self.get_model()
     141            if hasattr(model, '_meta'):
     142                names.append("%s/%s%s.html" % (
     143                    model._meta.app_label,
     144                    model._meta.object_name.lower(),
     145                    self.template_name_suffix
     146                ))
    141147        return names
    142148
    143149
  • django/views/generic/edit.py

    diff --git a/django/views/generic/edit.py b/django/views/generic/edit.py
    a b  
    7575        if self.form_class:
    7676            return self.form_class
    7777        else:
    78             if self.model is not None:
    79                 # If a model has been explicitly provided, use it
    80                 model = self.model
    81             elif hasattr(self, 'object') and self.object is not None:
    82                 # If this view is operating on a single object, use
    83                 # the class of that object
    84                 model = self.object.__class__
    85             else:
    86                 # Try to get a queryset and extract the model class
    87                 # from that
    88                 model = self.get_queryset().model
     78            model = self.get_model()
    8979            return model_forms.modelform_factory(model)
    9080
    9181    def get_form_kwargs(self):
     
    120110            if context_object_name:
    121111                context[context_object_name] = self.object
    122112        return context
     113       
     114    def get_model(self):
     115        if self.model:
     116            return self.model
     117        elif self.form_class and issubclass(self.form_class, model_forms.ModelForm):
     118            return self.form_class._meta.model
     119        elif hasattr(self, 'object') and self.object is not None:
     120            return self.object.__class__
     121        else:
     122            return self.get_queryset().model
    123123
    124124
    125125class ProcessFormView(View):
  • tests/regressiontests/generic_views/edit.py

    diff --git a/tests/regressiontests/generic_views/edit.py b/tests/regressiontests/generic_views/edit.py
    a b  
    7575        self.assertRedirects(res, reverse('author_detail', kwargs={'pk': obj.pk}))
    7676        self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
    7777
     78    def test_create_with_only_form_class(self):
     79        res = self.client.get('/edit/artists/create/formclass/')
     80        self.assertEqual(res.status_code, 200)
     81        self.assertTrue(isinstance(res.context['form'], views.ArtistForm))
     82        self.assertFalse('object' in res.context)
     83        self.assertFalse('artist' in res.context)
     84        self.assertTemplateUsed(res, 'generic_views/artist_form.html')
     85       
     86        res = self.client.post('/edit/artists/create/formclass/',
     87                               {'name': 'Rene Magritte'})
     88        self.assertEqual(res.status_code, 302)
     89        artist = Artist.objects.get(name='Rene Magritte')
     90        self.assertRedirects(res, 'http://testserver/detail/artist/%d/' % artist.pk)
     91        self.assertQuerysetEqual(Artist.objects.all(), ['<Artist: Rene Magritte>'])
     92
    7893    def test_create_without_redirect(self):
    7994        try:
    8095            res = self.client.post('/edit/authors/create/naive/',
  • tests/regressiontests/generic_views/forms.py

    diff --git a/tests/regressiontests/generic_views/forms.py b/tests/regressiontests/generic_views/forms.py
    a b  
    11from django import forms
    22
    3 from regressiontests.generic_views.models import Author
     3from regressiontests.generic_views.models import Author, Artist
    44
    55
    66class AuthorForm(forms.ModelForm):
     
    99
    1010    class Meta:
    1111        model = Author
     12
     13
     14class ArtistForm(forms.ModelForm):
     15    name = forms.CharField()
     16
     17    class Meta:
     18        model = Artist
  • tests/regressiontests/generic_views/urls.py

    diff --git a/tests/regressiontests/generic_views/urls.py b/tests/regressiontests/generic_views/urls.py
    a b  
    5454    # Create/UpdateView
    5555    (r'^edit/artists/create/$',
    5656        views.ArtistCreate.as_view()),
     57    (r'^edit/artists/create/formclass/$',
     58        views.ArtistOnlyFormClassCreate.as_view()),
    5759    (r'^edit/artists/(?P<pk>\d+)/update/$',
    5860        views.ArtistUpdate.as_view()),
    5961
  • tests/regressiontests/generic_views/views.py

    diff --git a/tests/regressiontests/generic_views/views.py b/tests/regressiontests/generic_views/views.py
    a b  
    55from django.views import generic
    66
    77from regressiontests.generic_views.models import Artist, Author, Book, Page
    8 from regressiontests.generic_views.forms import AuthorForm
     8from regressiontests.generic_views.forms import AuthorForm, ArtistForm
    99
    1010
    1111class CustomTemplateView(generic.TemplateView):
     
    7777class ArtistCreate(generic.CreateView):
    7878    model = Artist
    7979
     80class ArtistOnlyFormClassCreate(generic.CreateView):
     81    form_class = ArtistForm
     82
    8083
    8184class NaiveAuthorCreate(generic.CreateView):
    8285    queryset = Author.objects.all()
  • tests/regressiontests/templates/loaders.py

    diff --git a/tests/regressiontests/templates/loaders.py b/tests/regressiontests/templates/loaders.py
    a b  
    143143        self.assertEqual(output, 'obj:after')
    144144        self.assertEqual(context['obj'], 'before')
    145145
     146    def test_empty_list(self):
     147        self.assertRaises(ValueError, loader.render_to_string, [])
     148
     149    def test_select_templates_from_empty_list(self):
     150        self.assertRaises(ValueError, loader.select_template, [])
     151
    146152if __name__ == "__main__":
    147153    unittest.main()
Back to Top