def edit_profile(request, username): '''Display and process profile edit form. On GET, displays the form; on POST, processes the form and saves if valid. Currently redirects to profile view on success. When requested via AJAX, returns HTML that can be displayed in a dashboard tab; otherwise, returns the dashboard page with edit content loaded. ''' user, userprofile = _get_profile_user(username) context = {'author': user} if request.method == 'GET': form = ProfileForm(instance=userprofile) interest_data = [{ 'interest': i } for i in sorted(userprofile.research_interests.all())] interest_formset = InterestFormSet(initial=interest_data, prefix='interests') elif request.method == 'POST': form = ProfileForm(request.POST, request.FILES, instance=userprofile) interest_formset = InterestFormSet(request.POST, prefix='interests') if form.is_valid() and interest_formset.is_valid(): # save and redirect to profile form.save(commit=False) new_interests = [ f.cleaned_data.get('interest') for f in interest_formset.forms if f.cleaned_data.get('interest', '') and not f.cleaned_data.get('DELETE', False) ] userprofile.research_interests.set(*new_interests) # if a new photo file was posted, resize it if 'photo' in request.FILES: form.instance.resize_photo() userprofile.save() # TODO: might want a different behavior when POSTed via ajax return HttpResponseSeeOtherRedirect( reverse('accounts:dashboard-profile', kwargs={'username': username})) else: context['invalid_form'] = True context['form'] = form context['interest_formset'] = interest_formset # template for the tab-only portion template_name = 'accounts/snippets/edit-profile-tab.html' # for a non-ajax request, load the tab template in the dashboard if not request.is_ajax(): context.update({'tab_template': template_name, 'tab': 'profile'}) template_name = 'accounts/dashboard.html' return render(request, template_name, context)
def edit_profile(request, username): '''Display and process profile edit form. On GET, displays the form; on POST, processes the form and saves if valid. Currently redirects to profile view on success. When requested via AJAX, returns HTML that can be displayed in a dashboard tab; otherwise, returns the dashboard page with edit content loaded. ''' user, userprofile = _get_profile_user(username) context = {'author': user} if request.method == 'GET': form = ProfileForm(instance=userprofile) interest_data = [{'interest': i} for i in sorted(userprofile.research_interests.all())] interest_formset = InterestFormSet(initial=interest_data, prefix='interests') elif request.method == 'POST': form = ProfileForm(request.POST, request.FILES, instance=userprofile) interest_formset = InterestFormSet(request.POST, prefix='interests') if form.is_valid() and interest_formset.is_valid(): # save and redirect to profile form.save(commit=False) new_interests = [f.cleaned_data.get('interest') for f in interest_formset.forms if f.cleaned_data.get('interest', '') and not f.cleaned_data.get('DELETE', False)] userprofile.research_interests.set(*new_interests) # if a new photo file was posted, resize it if 'photo' in request.FILES: form.instance.resize_photo() userprofile.save() # TODO: might want a different behavior when POSTed via ajax return HttpResponseSeeOtherRedirect(reverse('accounts:dashboard-profile', kwargs={'username': username})) else: context['invalid_form'] = True context['form'] = form context['interest_formset'] = interest_formset # template for the tab-only portion template_name = 'accounts/snippets/edit-profile-tab.html' # for a non-ajax request, load the tab template in the dashboard if not request.is_ajax(): context.update({'tab_template': template_name, 'tab': 'profile'}) template_name = 'accounts/dashboard.html' return render(request, template_name, context)
def public_profile(request, username): '''Display public profile information and publications for the requested author. When requested via AJAX, returns HTML that can be displayed inside a faculty dashboard tab. ''' user, userprofile = _get_profile_user(username) form, interest_formset = None, None context = {} if request.method == 'POST': form = ProfileForm(request.POST, request.FILES, instance=userprofile) interest_formset = InterestFormSet(request.POST, prefix='interests') if form.is_valid() and interest_formset.is_valid(): # save and redirect to profile form.save(commit=False) new_interests = [f.cleaned_data.get('interest') for f in interest_formset.forms if f.cleaned_data.get('interest', '') and not f.cleaned_data.get('DELETE', False)] userprofile.research_interests.set(*new_interests) # if a new photo file was posted, resize it if 'photo' in request.FILES: form.instance.resize_photo() userprofile.save() messages.success(request, 'Your profile was updated.') # TODO: might want a different behavior when POSTed via ajax return HttpResponseSeeOtherRedirect(reverse('accounts:dashboard-profile', kwargs={'username': username})) else: context['invalid_form'] = True if (request.user.has_perm("accounts.change_userprofile") or request.user == user) and not request.method == 'POST': form = ProfileForm(instance=userprofile) form.inlineformsets interest_data = [{'interest': i} for i in sorted(userprofile.research_interests.all())] interest_formset = InterestFormSet(initial=interest_data, prefix='interests') context.update({ 'author': user, 'form': form, 'interest_formset': interest_formset, }) if request.is_ajax(): # display a briefer version of the profile, for inclusion in faculty dash template_name = 'accounts/snippets/profile-tab.html' # for non-ajax requests, display full profile with documents else: # get articles where the user is the author articles_query = userprofile.recent_articles_query() paginated_articles, show_pages = paginate(request, articles_query) url_params = request.GET.copy() url_params.pop('page', None) context.update({ 'results': paginated_articles, 'show_pages': show_pages, 'url_params': url_params.urlencode(), }) template_name = 'accounts/profile.html' return render(request, template_name, context)
def public_profile(request, username): '''Display public profile information and publications for the requested author. When requested via AJAX, returns HTML that can be displayed inside a faculty dashboard tab. ''' user, userprofile = _get_profile_user(username) form, interest_formset = None, None context = {} if request.method == 'POST': form = ProfileForm(request.POST, request.FILES, instance=userprofile) interest_formset = InterestFormSet(request.POST, prefix='interests') if form.is_valid() and interest_formset.is_valid(): # save and redirect to profile form.save(commit=False) new_interests = [ f.cleaned_data.get('interest') for f in interest_formset.forms if f.cleaned_data.get('interest', '') and not f.cleaned_data.get('DELETE', False) ] userprofile.research_interests.set(*new_interests) # if a new photo file was posted, resize it if 'photo' in request.FILES: form.instance.resize_photo() userprofile.save() messages.success(request, 'Your profile was updated.') # TODO: might want a different behavior when POSTed via ajax return HttpResponseSeeOtherRedirect( reverse('accounts:dashboard-profile', kwargs={'username': username})) else: context['invalid_form'] = True if (request.user.has_perm("accounts.change_userprofile") or request.user == user) and not request.method == 'POST': form = ProfileForm(instance=userprofile) form.inlineformsets interest_data = [{ 'interest': i } for i in sorted(userprofile.research_interests.all())] interest_formset = InterestFormSet(initial=interest_data, prefix='interests') context.update({ 'author': user, 'form': form, 'interest_formset': interest_formset, }) if request.is_ajax(): # display a briefer version of the profile, for inclusion in faculty dash template_name = 'accounts/snippets/profile-tab.html' # for non-ajax requests, display full profile with documents else: # get articles where the user is the author articles_query = userprofile.recent_articles_query() paginated_articles, show_pages = paginate(request, articles_query) url_params = request.GET.copy() url_params.pop('page', None) context.update({ 'results': paginated_articles, 'show_pages': show_pages, 'url_params': url_params.urlencode(), }) template_name = 'accounts/profile.html' return render(request, template_name, context)