Ticket #11775: patch.diff

File patch.diff, 4.4 KB (added by Jukka Välimaa, 11 years ago)

Proposed patch

  • django/db/models/base.py

     
    231231        if cls.__doc__ is None:
    232232            cls.__doc__ = "%s(%s)" % (cls.__name__, ", ".join([f.attname for f in opts.fields]))
    233233
    234         if hasattr(cls, 'get_absolute_url'):
    235             cls.get_absolute_url = curry(get_absolute_url, opts, cls.get_absolute_url)
     234        app_label_model_name = '%s.%s' % (opts.app_label, opts.module_name)
     235        get_absolute_url_override = settings.ABSOLUTE_URL_OVERRIDES.get(app_label_model_name)
     236        if get_absolute_url_override:
     237            setattr(cls, 'get_absolute_url', get_absolute_url_override)
    236238
    237239        signals.class_prepared.send(sender=cls)
    238240
     
    631633            ordered_obj.objects.filter(**{order_name: rel_val}).values(pk_name)]
    632634
    633635
    634 ##############################################
    635 # HELPER FUNCTIONS (CURRIED MODEL FUNCTIONS) #
    636 ##############################################
    637 
    638 def get_absolute_url(opts, func, self, *args, **kwargs):
    639     return settings.ABSOLUTE_URL_OVERRIDES.get('%s.%s' % (opts.app_label, opts.module_name), func)(self, *args, **kwargs)
    640 
    641 
    642636########
    643637# MISC #
    644638########
  • tests/modeltests/url_overrides/tests.py

     
     1from new import classobj
     2from unittest import TestCase
     3
     4from django.conf import settings
     5from django.db import models
     6
     7class AbsoluteUrlOverrideTests(TestCase):
     8    """
     9    Tests for inserting or overriding model class get_absolute_url methods with
     10    ABSOLUTE_URL_OVERRIDES -setting.
     11    """
     12    def setUp(self):
     13        super(AbsoluteUrlOverrideTests, self).setUp()
     14        self.old_overrides = settings.ABSOLUTE_URL_OVERRIDES
     15        settings.ABSOLUTE_URL_OVERRIDES = {}
     16
     17    def tearDown(self):
     18        super(AbsoluteUrlOverrideTests, self).tearDown()
     19        settings.ABSOLUTE_URL_OVERRIDES = self.old_overrides
     20       
     21    def test_insert_method(self):
     22        klass_one = self._create_model_class('TestModelOne')
     23        self.assertFalse(hasattr(klass_one, 'get_absolute_url'))
     24       
     25        settings.ABSOLUTE_URL_OVERRIDES['url_overrides.testmodeltwo'] = lambda o: '/%s/' % o.name
     26       
     27        klass_two = self._create_model_class('TestModelTwo')
     28        self.assertTrue(hasattr(klass_two, 'get_absolute_url'))
     29        name = 'Rose'
     30        self.assertEquals('/%s/' % name, klass_two(name=name).get_absolute_url())
     31   
     32    def test_override_method(self):
     33        class_method = lambda o: 'class_method'
     34        klass_one = self._create_model_class('TestModelUno', class_method)
     35        name = 'Rose'
     36        self.assertTrue(hasattr(klass_one, 'get_absolute_url'))
     37        self.assertEqual('class_method', klass_one(name=name).get_absolute_url())
     38       
     39        settings.ABSOLUTE_URL_OVERRIDES['url_overrides.testmodeldos'] = lambda o: '/%s/' % o.name
     40       
     41        klass_two = self._create_model_class('TestModelDos', class_method)
     42        self.assertEquals('/%s/' % name, klass_two(name=name).get_absolute_url())
     43       
     44   
     45    def _create_model_class(self, class_name, get_absolute_url_method=None):
     46        members = {'name': models.CharField(max_length=50)}
     47        if get_absolute_url_method:
     48            members['get_absolute_url'] = get_absolute_url_method
     49       
     50        return classobj(class_name, (models.Model,), members)
     51 No newline at end of file
  • docs/ref/settings.txt

     
    1414Default: ``{}`` (Empty dictionary)
    1515
    1616A dictionary mapping ``"app_label.model_name"`` strings to functions that take
    17 a model object and return its URL. This is a way of overriding
     17a model object and return its URL. This is a way of inserting or overriding
    1818``get_absolute_url()`` methods on a per-installation basis. Example::
    1919
    2020    ABSOLUTE_URL_OVERRIDES = {
Back to Top