def dashboard_documents(request, username): '''Display dashboard tab with documents for a logged-in faculty user looking at their own profile or a site admin looking at any faculty profile. When requested via AJAX, returns HTML for the dashboard tab only; otherwise, returns the dashboard with tab content loaded. ''' user, userprofile = _get_profile_user(username) # 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 = { 'author': user, 'results': paginated_articles, 'show_pages': show_pages, 'url_params': url_params.urlencode(), 'unpublished_articles': userprofile.unpublished_articles() } # template for the tab-only portion template_name = 'accounts/snippets/documents-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': 'documents'}) template_name = 'accounts/dashboard.html' return render(request, template_name, context)
def tagged_items(request, tag): tag = get_object_or_404(Tag, slug=tag) articles = articles_by_tag(request.user, tag) results, show_pages = paginate(request, articles) context = { 'tag': tag, 'articles': results, 'show_pages': show_pages, } return render(request, 'accounts/tagged_items.html', context)
def queue(request): '''Display the queue of harvested records. ''' # - Restrict to only harvested records (which can be ingested or ignored) records = HarvestRecord.objects.filter(status='harvested').order_by('harvested').all() results, show_pages = paginate(request, records) template_name = 'harvest/queue.html' # for ajax requests, only display the inner content if request.is_ajax(): template_name = 'harvest/snippets/queue.html' return render(request, template_name, {'results': results, 'show_pages': show_pages})
def dashboard_summary(request, username): '''Display dashboard summary information for a logged-in faculty user looking at their own profile (or a site admin looking at any faculty profile). When requested via AJAX, returns HTML for the dashboard tab only; otherwise, returns the dashboard with tab content loaded. ''' user, userprofile = _get_profile_user(username) # get articles where the user is the author articles_query = userprofile.recent_articles_query() paginated_articles, show_pages = paginate(request, articles_query) # collect all stats for articles user_stats = defaultdict(int) user_stats['total_items'] = paginated_articles.paginator.count # get individual stat records and add them up for article in paginated_articles.object_list: stats = ArticleStatistics.objects.filter(pid=article['pid']) # FIXME: use django aggregations here? # (should aggregate stats be a function userprofile?) for stat in stats: user_stats['views'] += stat.num_views user_stats['downloads'] += stat.num_downloads announcements = Announcement.get_displayable() context = { 'author': user, 'user_stats': user_stats, 'unpublished_articles': userprofile.unpublished_articles(), 'announcements': announcements } # template for the tab-only portion template_name = 'accounts/snippets/dashboard-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': 'dashboard'}) 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)