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):
|
| 371 | 371 | """ |
| 372 | 372 | send_mail(subject, message, from_email, [self.email]) |
| 373 | 373 | |
| 374 | | def get_profile(self): |
| | 374 | def get_profile(self, cached=True): |
| 375 | 375 | """ |
| 376 | 376 | Returns site-specific profile for this user. Raises |
| 377 | 377 | SiteProfileNotAvailable if this site does not allow profiles. |
| 378 | 378 | """ |
| 379 | | if not hasattr(self, '_profile_cache'): |
| | 379 | if not cached or not hasattr(self, '_profile_cache'): |
| 380 | 380 | from django.conf import settings |
| 381 | 381 | if not getattr(settings, 'AUTH_PROFILE_MODULE', False): |
| 382 | 382 | raise SiteProfileNotAvailable( |
diff --git a/docs/topics/auth.txt b/docs/topics/auth.txt
index 3b6ad1b..c33d931 100644
|
a
|
b
|
Methods
|
| 258 | 258 | :attr:`~django.contrib.auth.models.User.from_email` is ``None``, Django |
| 259 | 259 | uses the :setting:`DEFAULT_FROM_EMAIL`. |
| 260 | 260 | |
| 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. |
| 262 | 265 | |
| 263 | 266 | Returns a site-specific profile for this user. Raises |
| 264 | 267 | :exc:`django.contrib.auth.models.SiteProfileNotAvailable` if the |
| … |
… |
Methods
|
| 267 | 270 | have a profile. For information on how to define a site-specific user |
| 268 | 271 | profile, see the section on `storing additional user information`_ below. |
| 269 | 272 | |
| | 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 | |
| 270 | 278 | .. _storing additional user information: #storing-additional-information-about-users |
| 271 | 279 | |
| 272 | 280 | Manager functions |
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
|
-
|
+
|
|
| | 1 | from django.db import models |
| | 2 | from django.contrib.auth.models import User |
| | 3 | |
| | 4 | class UserProfile(models.Model): |
| | 5 | user = models.OneToOneField(User, related_name="user_profile") |
| | 6 | dance = models.CharField(max_length=100) |
diff --git a/tests/regressiontests/user_profile/tests.py b/tests/regressiontests/user_profile/tests.py
new file mode 100644
index 0000000..208b818
|
-
|
+
|
|
| | 1 | from django.conf import settings |
| | 2 | from django.test import TestCase |
| | 3 | from django.test.utils import override_settings |
| | 4 | from django.contrib.auth.models import User |
| | 5 | from models import UserProfile |
| | 6 | |
| | 7 | |
| | 8 | class 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') |