comparison bio/views.py @ 789:9e803323a0d0

Removing AUTH_PROFILE_MODULE and get_profile().
author Brian Neal <bgneal@gmail.com>
date Fri, 23 May 2014 15:10:11 -0500
parents cd3343abca9d
children 650ab160cbb9
comparison
equal deleted inserted replaced
788:84aa49497718 789:9e803323a0d0
35 @login_required 35 @login_required
36 def member_list(request, type='user'): 36 def member_list(request, type='user'):
37 """ 37 """
38 This view displays the member list. Only active members are displayed. 38 This view displays the member list. Only active members are displayed.
39 """ 39 """
40 qs = User.objects.filter(is_active=True) 40 qs = User.objects.filter(is_active=True).select_related('profile')
41 if type == 'user': 41 if type == 'user':
42 qs = qs.order_by('username') 42 qs = qs.order_by('username')
43 else: 43 else:
44 qs = qs.order_by('date_joined') 44 qs = qs.order_by('date_joined')
45 num_members = qs.count() 45 num_members = qs.count()
49 try: 49 try:
50 the_page = paginator.page(page) 50 the_page = paginator.page(page)
51 except InvalidPage: 51 except InvalidPage:
52 raise Http404 52 raise Http404
53 53
54 # Attach user profiles to each user to avoid using get_user_profile() in
55 # the template.
56 users = set(user.id for user in the_page.object_list)
57
58 profiles = UserProfile.objects.filter(user__id__in=users).select_related()
59 user_profiles = dict((profile.user.id, profile) for profile in profiles)
60
61 for user in the_page.object_list:
62 user.user_profile = user_profiles[user.id]
63
64 return render(request, 'bio/members.html', { 54 return render(request, 'bio/members.html', {
65 'page': the_page, 55 'page': the_page,
66 'type': type, 56 'type': type,
67 'num_members': num_members, 57 'num_members': num_members,
68 }) 58 })
69 59
70 ####################################################################### 60 #######################################################################
71 61
72 @login_required 62 @login_required
73 def my_profile(request): 63 def my_profile(request):
74 profile = request.user.get_profile() 64 profile = request.user.profile
75 badge_collection = BadgeOwnership.objects.filter( 65 badge_collection = BadgeOwnership.objects.filter(
76 profile=profile).select_related("badge") 66 profile=profile).select_related("badge")
77 67
78 return render(request, 'bio/view_profile.html', { 68 return render(request, 'bio/view_profile.html', {
79 'subject': request.user, 69 'subject': request.user,
86 ####################################################################### 76 #######################################################################
87 77
88 @login_required 78 @login_required
89 def view_profile(request, username): 79 def view_profile(request, username):
90 80
91 user = get_object_or_404(User, username=username) 81 user = get_object_or_404(User.objects.select_related('profile'),
82 username=username)
92 if user == request.user: 83 if user == request.user:
93 return redirect('bio-me') 84 return redirect('bio-me')
94 85
95 profile = user.get_profile() 86 profile = user.profile
96 hide_email = profile.hide_email 87 hide_email = profile.hide_email
97 88
98 badge_collection = BadgeOwnership.objects.filter( 89 badge_collection = BadgeOwnership.objects.filter(
99 profile=profile).select_related("badge") 90 profile=profile).select_related("badge")
100 91
111 @login_required 102 @login_required
112 def edit_profile(request): 103 def edit_profile(request):
113 if request.method == 'POST': 104 if request.method == 'POST':
114 if request.POST.get('submit_button', 'Cancel') == 'Cancel': 105 if request.POST.get('submit_button', 'Cancel') == 'Cancel':
115 return redirect('bio-me') 106 return redirect('bio-me')
116 profile = request.user.get_profile() 107 profile = request.user.profile
117 user_form = EditUserForm(request.POST, instance=request.user) 108 user_form = EditUserForm(request.POST, instance=request.user)
118 profile_form = EditUserProfileForm(request.POST, instance=profile) 109 profile_form = EditUserProfileForm(request.POST, instance=profile)
119 if user_form.is_valid() and profile_form.is_valid(): 110 if user_form.is_valid() and profile_form.is_valid():
120 user_form.save() 111 user_form.save()
121 profile = profile_form.save(commit=False) 112 profile = profile_form.save(commit=False)
122 profile.user = request.user 113 profile.user = request.user
123 profile.save() 114 profile.save()
124 return redirect('bio-me') 115 return redirect('bio-me')
125 else: 116 else:
126 profile = request.user.get_profile() 117 profile = request.user.profile
127 user_form = EditUserForm(instance=request.user) 118 user_form = EditUserForm(instance=request.user)
128 profile_form = EditUserProfileForm(instance=profile) 119 profile_form = EditUserProfileForm(instance=profile)
129 120
130 return render(request, 'bio/edit_profile.html', { 121 return render(request, 'bio/edit_profile.html', {
131 'user_form': user_form, 122 'user_form': user_form,
138 def change_avatar(request): 129 def change_avatar(request):
139 if request.method == 'POST': 130 if request.method == 'POST':
140 form = UploadAvatarForm(request.POST, request.FILES) 131 form = UploadAvatarForm(request.POST, request.FILES)
141 if form.is_valid(): 132 if form.is_valid():
142 # Update the profile with the new avatar 133 # Update the profile with the new avatar
143 profile = request.user.get_profile() 134 profile = request.user.profile
144 135
145 # First delete any old avatar file 136 # First delete any old avatar file
146 if profile.avatar.name != '': 137 if profile.avatar.name != '':
147 profile.avatar.delete(save=False) 138 profile.avatar.delete(save=False)
148 139
237 request.user.website_profiles.get(id=delete_id).delete() 228 request.user.website_profiles.get(id=delete_id).delete()
238 else: 229 else:
239 update_occurred = False 230 update_occurred = False
240 231
241 if update_occurred: 232 if update_occurred:
242 notify_profile_content_update(request.user.get_profile()) 233 notify_profile_content_update(request.user.profile)
243 234
244 return redirect(request.path) 235 return redirect(request.path)
245 236
246 # WTF? 237 # WTF?
247 else: 238 else: