changeset 789:9e803323a0d0

Removing AUTH_PROFILE_MODULE and get_profile().
author Brian Neal <bgneal@gmail.com>
date Fri, 23 May 2014 15:10:11 -0500 (2014-05-23)
parents 84aa49497718
children 6a06080e7ca8
files antispam/tests/test_utils.py antispam/utils.py bio/badges.py bio/models.py bio/templatetags/bio_tags.py bio/tests/test_views.py bio/views.py donations/tests.py forums/templatetags/forum_tags.py forums/tests/test_views.py forums/tools.py forums/views/main.py forums/views/spam.py gcalendar/views.py legacy/management/commands/import_old_users.py membermap/views.py sg101/settings/base.py sg101/templates/bio/members.html
diffstat 18 files changed, 45 insertions(+), 57 deletions(-) [+]
line wrap: on
line diff
--- a/antispam/tests/test_utils.py	Sun May 18 17:43:37 2014 -0500
+++ b/antispam/tests/test_utils.py	Fri May 23 15:10:11 2014 -0500
@@ -56,7 +56,7 @@
         user = User.objects.create_user('spammer_guy', '', 'password')
         user.save()
 
-        profile = user.get_profile()
+        profile = user.profile
         profile.location = 'Spamville'
         profile.country = 'US'
         profile.birthday = datetime.date.today()
@@ -93,7 +93,7 @@
 
         deactivate_spammer(user)
 
-        profile = user.get_profile()
+        profile = user.profile
         self.assertFalse(profile.location)
         self.assertFalse(profile.country)
         self.assertIsNone(profile.birthday)
--- a/antispam/utils.py	Sun May 18 17:43:37 2014 -0500
+++ b/antispam/utils.py	Fri May 23 15:10:11 2014 -0500
@@ -36,7 +36,7 @@
     user profile.
     """
     user = request.user
-    if user.get_profile().is_stranger() and contains_spam(content):
+    if user.profile.is_stranger() and contains_spam(content):
 
         ip = request.META.get('REMOTE_ADDR', "unknown")
 
@@ -60,7 +60,7 @@
     """This function marks the user as suspended."""
     user.is_active = False
     user.save()
-    profile = user.get_profile()
+    profile = user.profile
     profile.status = STA_SUSPENDED
     profile.status_date = datetime.datetime.now()
     profile.save(content_update=False)
@@ -77,7 +77,7 @@
     user.is_active = False
     user.save()
 
-    profile = user.get_profile()
+    profile = user.profile
     profile.status = STA_SPAMMER
     profile.status_date = datetime.datetime.now()
     profile.reset()
--- a/bio/badges.py	Sun May 18 17:43:37 2014 -0500
+++ b/bio/badges.py	Fri May 23 15:10:11 2014 -0500
@@ -21,7 +21,7 @@
         logging.error("Can't award badge with numeric_id = %d", badge_id)
         return
 
-    profile = user.get_profile()
+    profile = user.profile
 
     # Does the user already have badges of this type?
     try:
--- a/bio/models.py	Sun May 18 17:43:37 2014 -0500
+++ b/bio/models.py	Fri May 23 15:10:11 2014 -0500
@@ -78,7 +78,7 @@
 class UserProfile(models.Model):
     """model to represent additional information about users"""
 
-    user = models.ForeignKey(User, unique=True)
+    user = models.OneToOneField(User, related_name='profile')
     location = models.CharField(max_length=128, blank=True)
     country = models.CharField(max_length=2, blank=True, default='',
             choices=bio.flags.FLAG_CHOICES,
--- a/bio/templatetags/bio_tags.py	Sun May 18 17:43:37 2014 -0500
+++ b/bio/templatetags/bio_tags.py	Fri May 23 15:10:11 2014 -0500
@@ -16,7 +16,7 @@
 
 def get_img_url(profile=None):
     """
-    This function returns a URL for a user profile avatar. 
+    This function returns a URL for a user profile avatar.
     If the profile is None or the profile doesn't contain a valid
     avatar, the URL for the default avatar is returned.
 
@@ -30,12 +30,14 @@
 @register.inclusion_tag('bio/avatar_tag.html')
 def avatar(user, profile_link=True, align='bottom'):
     """
-    Returns the HTML for a user's avatar image.
+    Returns a dict of attributes for a user's avatar image.
 
     If the user object has an attribute 'user_profile', this is assumed to be
     the user's profile that has been pre-fetched. Otherwise, the cache is
     consulted to retrieve the avatar info for the user. If there is a cache
-    miss, only then will a get_profile() call be made.
+    miss, only then will we access the profile attribute to retrieve the profile
+    from the database.
+
     """
     # img_info is a tuple that contains info about the avatar:
     # (url, width, height)
@@ -47,11 +49,7 @@
         cache_key = 'avatar_' + user.username
         img_url = cache.get(cache_key)
         if img_url is None:
-            try:
-                profile = user.get_profile()
-            except UserProfile.DoesNotExist:
-                profile = None
-
+            profile = user.profile
             img_url = get_img_url(profile)
             cache.set(cache_key, img_url)
 
--- a/bio/tests/test_views.py	Sun May 18 17:43:37 2014 -0500
+++ b/bio/tests/test_views.py	Fri May 23 15:10:11 2014 -0500
@@ -197,7 +197,7 @@
         self.assertEqual(u.first_name, 'Paul')
         self.assertEqual(u.last_name, 'Johnson')
         self.assertEqual(u.email, 'pjmoto@example.com')
-        profile = u.get_profile()
+        profile = u.profile
         self.assertEqual(profile.occupation, 'Surf Guitar Pioneer')
         self.assertEqual(profile.profile_text, 'Wrote Mr. Moto at age 15.')
 
@@ -250,7 +250,7 @@
 
         flag = UserProfileFlag.objects.first()
         self.assertEqual(flag.user, paul)
-        self.assertEqual(flag.profile, eddie.get_profile())
+        self.assertEqual(flag.profile, eddie.profile)
 
         self.assertEqual(len(mail.outbox), 1)
         self.assertTrue('A Profile Has Been Flagged' in mail.outbox[0].subject)
--- a/bio/views.py	Sun May 18 17:43:37 2014 -0500
+++ b/bio/views.py	Fri May 23 15:10:11 2014 -0500
@@ -37,7 +37,7 @@
     """
     This view displays the member list. Only active members are displayed.
     """
-    qs = User.objects.filter(is_active=True)
+    qs = User.objects.filter(is_active=True).select_related('profile')
     if type == 'user':
         qs = qs.order_by('username')
     else:
@@ -51,16 +51,6 @@
     except InvalidPage:
         raise Http404
 
-    # Attach user profiles to each user to avoid using get_user_profile() in
-    # the template.
-    users = set(user.id for user in the_page.object_list)
-
-    profiles = UserProfile.objects.filter(user__id__in=users).select_related()
-    user_profiles = dict((profile.user.id, profile) for profile in profiles)
-
-    for user in the_page.object_list:
-        user.user_profile = user_profiles[user.id]
-
     return render(request, 'bio/members.html', {
         'page': the_page,
         'type': type,
@@ -71,7 +61,7 @@
 
 @login_required
 def my_profile(request):
-    profile = request.user.get_profile()
+    profile = request.user.profile
     badge_collection = BadgeOwnership.objects.filter(
             profile=profile).select_related("badge")
 
@@ -88,11 +78,12 @@
 @login_required
 def view_profile(request, username):
 
-    user = get_object_or_404(User, username=username)
+    user = get_object_or_404(User.objects.select_related('profile'),
+            username=username)
     if user == request.user:
         return redirect('bio-me')
 
-    profile = user.get_profile()
+    profile = user.profile
     hide_email = profile.hide_email
 
     badge_collection = BadgeOwnership.objects.filter(
@@ -113,7 +104,7 @@
     if request.method == 'POST':
         if request.POST.get('submit_button', 'Cancel') == 'Cancel':
             return redirect('bio-me')
-        profile = request.user.get_profile()
+        profile = request.user.profile
         user_form = EditUserForm(request.POST, instance=request.user)
         profile_form = EditUserProfileForm(request.POST, instance=profile)
         if user_form.is_valid() and profile_form.is_valid():
@@ -123,7 +114,7 @@
             profile.save()
             return redirect('bio-me')
     else:
-        profile = request.user.get_profile()
+        profile = request.user.profile
         user_form = EditUserForm(instance=request.user)
         profile_form = EditUserProfileForm(instance=profile)
 
@@ -140,7 +131,7 @@
         form = UploadAvatarForm(request.POST, request.FILES)
         if form.is_valid():
             # Update the profile with the new avatar
-            profile = request.user.get_profile()
+            profile = request.user.profile
 
             # First delete any old avatar file
             if profile.avatar.name != '':
@@ -239,7 +230,7 @@
                 update_occurred = False
 
             if update_occurred:
-                notify_profile_content_update(request.user.get_profile())
+                notify_profile_content_update(request.user.profile)
 
             return redirect(request.path)
 
--- a/donations/tests.py	Sun May 18 17:43:37 2014 -0500
+++ b/donations/tests.py	Fri May 23 15:10:11 2014 -0500
@@ -66,7 +66,7 @@
                     datetime.datetime(2011, 1, 21, 4, 14, 8))
 
             # user should have got a badge for donating
-            p = user.get_profile()
+            p = user.profile
             badges = list(p.badges.all())
             self.assertEqual(len(badges), 1)
             if len(badges) == 1:
@@ -107,7 +107,7 @@
                     datetime.datetime(2011, 1, 16, 5, 40, 33))
 
             # user should not have got a badge for donating
-            p = user.get_profile()
+            p = user.profile
             self.assertEqual(p.badges.count(), 0)
 
 
--- a/forums/templatetags/forum_tags.py	Sun May 18 17:43:37 2014 -0500
+++ b/forums/templatetags/forum_tags.py	Fri May 23 15:10:11 2014 -0500
@@ -68,7 +68,7 @@
     cache_key = '%s_tz_prefs' % user.username
     tz_prefs = cache.get(cache_key)
     if tz_prefs is None:
-        profile = user.get_profile()
+        profile = user.profile
         tz_prefs = profile.use_24_time, profile.time_zone
         cache.set(cache_key, tz_prefs)
 
--- a/forums/tests/test_views.py	Sun May 18 17:43:37 2014 -0500
+++ b/forums/tests/test_views.py	Fri May 23 15:10:11 2014 -0500
@@ -131,5 +131,8 @@
             self.assertEqual(post.user.pk, self.user.pk)
         self.assertEqual(topic.post_count, 3)
 
-        profile = self.user.get_profile()
+        # our cached copy of the profile is out of date; need to refetch from
+        # the database
+        user = User.objects.get(username=self.username)
+        profile = user.profile
         self.assertEqual(profile.forum_post_count, 3)
--- a/forums/tools.py	Sun May 18 17:43:37 2014 -0500
+++ b/forums/tools.py	Fri May 23 15:10:11 2014 -0500
@@ -118,7 +118,7 @@
     creator has that option set in their profile.
 
     """
-    profile = post.user.get_profile()
+    profile = post.user.profile
     if profile.auto_favorite:
         post.topic.bookmarkers.add(post.user)
 
@@ -129,6 +129,6 @@
     creator has that option set in their profile.
 
     """
-    profile = post.user.get_profile()
+    profile = post.user.profile
     if profile.auto_subscribe:
         post.topic.subscribers.add(post.user)
--- a/forums/views/main.py	Sun May 18 17:43:37 2014 -0500
+++ b/forums/views/main.py	Fri May 23 15:10:11 2014 -0500
@@ -362,7 +362,6 @@
 
         post = form.save(request.user, request.META.get("REMOTE_ADDR", ""))
         post.unread = True
-        post.user.user_profile = request.user.get_profile()
         post.attach_list = post.attachments.all()
         _bump_post_count(request.user)
         _update_last_visit(request.user, form.topic, datetime.datetime.now())
@@ -469,7 +468,7 @@
     else:
         form = PostForm(instance=post, topic_name=topic_name)
 
-    post.user.user_profile = post.user.get_profile()
+    post.user.user_profile = post.user.profile
 
     return render_to_response('forums/edit_post.html', {
         'forum': post.topic.forum,
@@ -523,7 +522,7 @@
     Adjusts the parent topic and forum's last_post as needed.
     """
     # Adjust post creator's post count
-    profile = post.user.get_profile()
+    profile = post.user.profile
     if profile.forum_post_count > 0:
         profile.forum_post_count -= 1
         profile.save(content_update=False)
@@ -996,7 +995,7 @@
     """
     Increments the forum_post_count for the given user.
     """
-    profile = user.get_profile()
+    profile = user.profile
     profile.forum_post_count += 1
     profile.save(content_update=False)
 
--- a/forums/views/spam.py	Sun May 18 17:43:37 2014 -0500
+++ b/forums/views/spam.py	Fri May 23 15:10:11 2014 -0500
@@ -29,7 +29,7 @@
 def promote_stranger(user):
     """This function upgrades the user from stranger status to a regular user.
     """
-    profile = user.get_profile()
+    profile = user.profile
     if user.is_active and profile.status == bio.models.STA_STRANGER:
         profile.status = bio.models.STA_ACTIVE
         profile.status_date = datetime.datetime.now()
@@ -42,7 +42,7 @@
 
     post = get_object_or_404(Post.objects.select_related(), pk=post_id)
     poster = post.user
-    poster_profile = poster.get_profile()
+    poster_profile = poster.profile
 
     can_moderate = perms.can_moderate(post.topic.forum, request.user)
     can_deactivate = (poster_profile.status == bio.models.STA_STRANGER and not
@@ -80,7 +80,7 @@
     deactivated.
     """
     user = get_object_or_404(User, pk=spammer_id)
-    profile = user.get_profile()
+    profile = user.profile
 
     success = not user.is_active and profile.status == bio.models.STA_SPAMMER
 
@@ -98,7 +98,7 @@
     """
     post = get_object_or_404(Post.objects.select_related(), pk=post_id)
     poster = post.user
-    poster_profile = poster.get_profile()
+    poster_profile = poster.profile
 
     can_moderate = perms.can_moderate(post.topic.forum, request.user)
     can_promote = poster_profile.status == bio.models.STA_STRANGER
--- a/gcalendar/views.py	Sun May 18 17:43:37 2014 -0500
+++ b/gcalendar/views.py	Fri May 23 15:10:11 2014 -0500
@@ -20,8 +20,7 @@
 def index(request):
     user = request.user
     if user.is_authenticated():
-        profile = user.get_profile()
-        tz = profile.time_zone
+        tz = user.profile.time_zone
     else:
         tz = 'US/Pacific'
 
--- a/legacy/management/commands/import_old_users.py	Sun May 18 17:43:37 2014 -0500
+++ b/legacy/management/commands/import_old_users.py	Fri May 23 15:10:11 2014 -0500
@@ -139,7 +139,7 @@
 
         u.save()
 
-        p = u.get_profile()
+        p = u.profile
         p.location = row['user_from'].decode('latin-1')
         p.occupation = row['user_occ'].decode('latin-1')
         p.interests = row['user_interests'].decode('latin-1')
--- a/membermap/views.py	Sun May 18 17:43:37 2014 -0500
+++ b/membermap/views.py	Fri May 23 15:10:11 2014 -0500
@@ -132,7 +132,7 @@
     cache.delete(CACHE_KEY)
 
     avatar_url = None
-    profile = entry.user.get_profile()
+    profile = entry.user.profile
     if profile.avatar and profile.avatar.url:
         avatar_url = profile.avatar.url
 
--- a/sg101/settings/base.py	Sun May 18 17:43:37 2014 -0500
+++ b/sg101/settings/base.py	Fri May 23 15:10:11 2014 -0500
@@ -19,8 +19,6 @@
 
 MANAGERS = ADMINS
 
-AUTH_PROFILE_MODULE = 'bio.userprofile'
-
 INTERNAL_IPS = ['127.0.0.1']
 
 # Local time zone for this installation. Choices can be found here:
--- a/sg101/templates/bio/members.html	Sun May 18 17:43:37 2014 -0500
+++ b/sg101/templates/bio/members.html	Fri May 23 15:10:11 2014 -0500
@@ -37,12 +37,12 @@
    <td>{% avatar u %}</td>
    <td>{% profile_link u.username %}</td>
    <td>{{ u.get_full_name }}</td>
-   <td>{{ u.user_profile.location }}</td>
+   <td>{{ u.profile.location }}</td>
    <td>{{ u.date_joined|date:"M. d, Y" }}</td>
    <td>
       {% ifnotequal user u %}<a href="{% url 'messages-compose_to' u.username %}">
          <img src="{{ STATIC_URL }}icons/note.png" alt="PM" title="Send private message" /></a>{% endifnotequal %}
-      {% if not u.user_profile.hide_email %}<a href="mailto:{{ u.email }}">
+      {% if not u.profile.hide_email %}<a href="mailto:{{ u.email }}">
          <img src="{{ STATIC_URL }}icons/email.png" alt="Email" title="Send Email" /></a>{% endif %}
    </td>
 </tr>