Ticket #17750: 0001-Fixed-17750-User.get_profile-now-accepts-cached-argu.patch

File 0001-Fixed-17750-User.get_profile-now-accepts-cached-argu.patch, 4.8 KB (added by Renato Alves, 13 years ago)

Final patch with changes to code, documentation and tests.

  • django/contrib/auth/models.py

    From 4c6cee291b0dc058c08ad38015e90f0d99fdc5b1 Mon Sep 17 00:00:00 2001
    From: Renato Alves <rjalves@igc.gulbenkian.pt>
    Date: Fri, 24 Feb 2012 07:06:05 +0000
    Subject: [PATCH] Fixed #17750 - User.get_profile() now accepts "cached"
     argument. Allows bypassing the cache and hitting the
     database again.
    
    ---
     django/contrib/auth/models.py                  |    4 +-
     docs/topics/auth.txt                           |   10 +++++-
     tests/regressiontests/user_profile/models.py   |    6 +++
     tests/regressiontests/user_profile/tests.py    |   40 ++++++++++++++++++++++++
     4 files changed, 57 insertions(+), 3 deletions(-)
     create mode 100644 tests/regressiontests/user_profile/__init__.py
     create mode 100644 tests/regressiontests/user_profile/models.py
     create mode 100644 tests/regressiontests/user_profile/tests.py
    
    diff --git a/django/contrib/auth/models.py b/django/contrib/auth/models.py
    index eb39868..3a745b0 100644
    a b class User(models.Model):  
    371371        """
    372372        send_mail(subject, message, from_email, [self.email])
    373373
    374     def get_profile(self):
     374    def get_profile(self, cached=True):
    375375        """
    376376        Returns site-specific profile for this user. Raises
    377377        SiteProfileNotAvailable if this site does not allow profiles.
    378378        """
    379         if not hasattr(self, '_profile_cache'):
     379        if not cached or not hasattr(self, '_profile_cache'):
    380380            from django.conf import settings
    381381            if not getattr(settings, 'AUTH_PROFILE_MODULE', False):
    382382                raise SiteProfileNotAvailable(
  • docs/topics/auth.txt

    diff --git a/docs/topics/auth.txt b/docs/topics/auth.txt
    index 3b6ad1b..c33d931 100644
    a b Methods  
    258258        :attr:`~django.contrib.auth.models.User.from_email` is ``None``, Django
    259259        uses the :setting:`DEFAULT_FROM_EMAIL`.
    260260
    261     .. method:: models.User.get_profile()
     261    .. method:: models.User.get_profile(cached=True)
     262
     263        .. versionchanged:: 1.5
     264        The ``cached`` argument was added.
    262265
    263266        Returns a site-specific profile for this user. Raises
    264267        :exc:`django.contrib.auth.models.SiteProfileNotAvailable` if the
    Methods  
    267270        have a profile. For information on how to define a site-specific user
    268271        profile, see the section on `storing additional user information`_ below.
    269272
     273        By default it caches the result to prevent hitting the database
     274        multiple times, however if the data is changed after calling it the
     275        first time, subsequent calls will return the original unchanged object.
     276        To force hitting the database again simply set ``cached`` to ``False``.
     277
    270278.. _storing additional user information: #storing-additional-information-about-users
    271279
    272280Manager functions
  • new file tests/regressiontests/user_profile/models.py

    diff --git a/tests/regressiontests/user_profile/__init__.py b/tests/regressiontests/user_profile/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/user_profile/models.py b/tests/regressiontests/user_profile/models.py
    new file mode 100644
    index 0000000..f05c6e3
    - +  
     1from django.db import models
     2from django.contrib.auth.models import User
     3
     4class UserProfile(models.Model):
     5    user = models.OneToOneField(User, related_name="user_profile")
     6    dance = models.CharField(max_length=100)
  • new file tests/regressiontests/user_profile/tests.py

    diff --git a/tests/regressiontests/user_profile/tests.py b/tests/regressiontests/user_profile/tests.py
    new file mode 100644
    index 0000000..208b818
    - +  
     1from django.conf import settings
     2from django.test import TestCase
     3from django.test.utils import override_settings
     4from django.contrib.auth.models import User
     5from models import UserProfile
     6
     7
     8class ProfileTestCase(TestCase):
     9    def tearDown(self):
     10        try:
     11            user = User.objects.get(username="janet")
     12        except User.DoesNotExist:
     13            pass
     14        else:
     15            user.delete()
     16
     17
     18    @override_settings(AUTH_PROFILE_MODULE="user_profile.UserProfile")
     19    def test_get_profile_cache(self):
     20        janet = User.objects.create_user('janet', 'janet@example.com')
     21
     22        profile = UserProfile(user=janet, dance='Tango')
     23        profile.save()
     24
     25        self.assertEqual(janet.get_profile().dance, 'Tango')
     26
     27        profile.dance='Salsa'
     28        profile.save()
     29
     30        # Testing with kwarg
     31        self.assertEqual(janet.get_profile().dance, 'Tango')
     32        self.assertEqual(janet.get_profile(cached=False).dance, 'Salsa')
     33
     34        profile.dance='Polka'
     35        profile.save()
     36
     37        # Testing with arg
     38        self.assertEqual(janet.get_profile().dance, 'Salsa')
     39        self.assertEqual(janet.get_profile(False).dance, 'Polka')
     40        self.assertEqual(janet.get_profile().dance, 'Polka')
Back to Top