def profile(request, user_id=None, edit=False): """ View method to display (with this method) or edit (using the update_profile method) the user's profile data. """ if request.method == 'POST': return update_profile(request, user_id) if not request.user.is_authenticated(): return HttpResponseRedirect(urlresolvers.reverse('login')) if user_id is None: return HttpResponseRedirect( urlresolvers.reverse('view_profile', kwargs={'user_id': request.user.id})) else: profile_user = get_object_or_404(User, id=user_id) context = {'profile_user': profile_user, 'settings': settings} if edit is True: if profile_user == request.user: form = ProfileForm( auto_id=True, initial={ 'email': profile_user.email, 'first_name': profile_user.first_name, 'last_name': profile_user.last_name, 'country': profile_user.indexer.country.id, 'languages': [lang.id for lang in profile_user.indexer.languages.all()], 'interests': profile_user.indexer.interests, 'from_where': profile_user.indexer.from_where, 'opt_in_email': profile_user.indexer.opt_in_email, 'issue_detail': profile_user.indexer.issue_detail, 'notify_on_approve': profile_user.indexer.notify_on_approve, 'collapse_compare_view': profile_user.indexer.collapse_compare_view, 'show_wiki_links': profile_user.indexer.show_wiki_links, }) context['form'] = form else: return render_error(request, "You may not edit other users' profiles") if profile_user == request.user: context['ranking'] = ranking(profile_user.indexer) return render_to_response('gcd/accounts/profile.html', context, context_instance=RequestContext(request))
def update_profile(request, user_id=None): """ Helper method to perform the update if the main profile view detects a POST request. """ if request.user.id != int(user_id): return render_error(request, 'You may only edit your own profile.') errors = [] form = ProfileForm(request.POST) if not form.is_valid(): return render_to_response('gcd/accounts/profile.html', {'form': form}, context_instance=RequestContext(request)) set_password = False old = form.cleaned_data['old_password'] new = form.cleaned_data['new_password'] confirm = form.cleaned_data['confirm_new_password'] if (new or confirm) and not old: errors.append( u'You must supply your old password in order to change it.') elif old and (new or confirm): if not request.user.check_password(old): errors.append(u'Old password incorrect, please try again.') elif new != confirm: errors.append( u'New password and confirm new password do not match.') else: set_password = True if errors: return render_to_response('gcd/accounts/profile.html', { 'form': form, 'error_list': errors }, context_instance=RequestContext(request)) request.user.first_name = form.cleaned_data['first_name'] request.user.last_name = form.cleaned_data['last_name'] request.user.email = form.cleaned_data['email'] if set_password is True: request.user.set_password(new) request.user.save() indexer = request.user.indexer indexer.notify_on_approve = form.cleaned_data['notify_on_approve'] indexer.collapse_compare_view = form.cleaned_data['collapse_compare_view'] indexer.show_wiki_links = form.cleaned_data['show_wiki_links'] indexer.country = form.cleaned_data['country'] indexer.languages = form.cleaned_data['languages'] indexer.interests = form.cleaned_data['interests'] indexer.from_where = form.cleaned_data['from_where'] indexer.opt_in_email = form.cleaned_data['opt_in_email'] indexer.issue_detail = form.cleaned_data['issue_detail'] indexer.save() return HttpResponseRedirect( urlresolvers.reverse('view_profile', kwargs={'user_id': request.user.id}))
def profile(request, user_id=None, edit=False): if request.method == 'POST': return update_profile(request, user_id) if user_id is None: if request.user.is_authenticated(): return HttpResponseRedirect( urlresolvers.reverse('view_profile', kwargs={'user_id': request.user.id})) else: return HttpResponseRedirect(urlresolvers.reverse('login')) profile_user = request.user else: profile_user = get_object_or_404(User, id=user_id) context = {'profile_user': profile_user} if edit is True: if profile_user == request.user: form = ProfileForm( auto_id=True, initial={ 'email': profile_user.email, 'first_name': profile_user.first_name, 'last_name': profile_user.last_name, 'country': profile_user.indexer.country.id, 'languages': [lang.id for lang in profile_user.indexer.languages.all()], 'interests': profile_user.indexer.interests, }) context['form'] = form else: return render_error(request, "You may not edit other users' profiles") return render_to_response('gcd/accounts/profile.html', context, context_instance=RequestContext(request))