Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Research Output Report added #576

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions coldfront/core/research_output/forms.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
from django import forms
from django.forms import ModelForm


from coldfront.core.research_output.models import ResearchOutput


class ResearchOutputForm(ModelForm):
class Meta:
model = ResearchOutput
exclude = ['project', ]

class ResearchOutputReportForm(forms.Form):
pk = forms.IntegerField(required=False, disabled=True)
title = forms.CharField(required=False, disabled=True)
description = forms.CharField(required=False, disabled=True)
created_by = forms.CharField(required=False, disabled=True)
project = forms.CharField(required=False, disabled=True)
created = forms.DateField(required=False, disabled=True)
selected = forms.BooleanField(initial=False, required=False)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['pk'].widget = forms.HiddenInput()
2 changes: 2 additions & 0 deletions coldfront/core/research_output/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class ResearchOutput(TimeStampedModel):
"""

# core fields
class Meta:
permissions = (("can_view_research_output_report","Can view research output report"),)
project = models.ForeignKey(Project, on_delete=models.CASCADE)
title = models.CharField(max_length=128, blank=True)
description = models.TextField(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{% extends "common/base.html" %}
{% load crispy_forms_tags %}
{% load static %}
{% load humanize %}


{% block title %}
Project Output Report
{% endblock %}


{% block content %}
<!-- Start Research Report -->
<div class="card">
<div class="card-header">
<h4 class="d-inline"><i class="fas fa-trophy" aria-hidden="true"></i> Research Outputs</h4>
<button type="submit" form="download_form" class="btn btn-success float-right"><i class="fas fa-download" aria-hidden="true"></i> Export to CSV</button>
</div>
<div class="card-body">
{% if formset %}
<form id="download_form" action="{% url 'research-output-report' %}" method="post">
{% csrf_token %}
<div class="table-responsive">
<table id="research-reports-table" class="table table-hover table-sm">
<thead>
<tr>
<th scope="col"><input type="checkbox" class="check" id="selectAll"></th>
<th scope="col">Title</th>
<th scope="col">Description</th>
<th scope="col">Created By</th>
<th scope="col">Project</th>
<th scope="col">Created</th>
</tr>
</thead>
<tbody>
{% for form in formset %}
<tr>
<td>{{ form.selected }}</td>
<td class="text-nowrap" style="min-width: 200px">{{ form.title.value }}</td>
<td class="text-nowrap" style="min-width: 200px">{{ form.description.value }}</td>
<td class="text-nowrap">{{ form.created_by.value }}</td>
<td class="text-nowrap">{{ form.project.value }}</td>
<td class="text-nowrap">{{ form.created.value|date:"M. d, Y" }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{{ formset.management_form }}
</form>
{% else %}
<div class="alert alert-info" role="alert"><i class="fas fa-info-circle" aria-hidden="true"></i> There are no reports to display.</div>
{% endif %}
</div>
</div>
<!-- End Research Output Report -->
<!--Table Script-->
<script>
$("#navbar-main > ul > li.active").removeClass("active")
$("#navbar-admin").addClass("active")
$("#navbar-director").addClass("active")
$("#navbar-research-output-report").addClass("active")
$(document).ready(function() {
$('#research-reports-table').DataTable({
"iDisplayLength": 50,
"bSortClasses": false,
"order": [[ 5, "desc" ]],
"columnDefs": [{ 'orderable': false, 'targets': 0 }],
"aaSorting": [[1, 'asc']]
});
});

$("#selectAll").click(function () {
$("input[name^='researchoutputform-']").prop('checked', $(this).prop('checked'));
});

$("input[name^='researchoutputform-']").click(function (ele) {
var id = $(this).attr('id');
if (id != "selectAll") {
$("#selectAll").prop('checked', false);
}
});
</script>
{% endblock %}
<!--End Table Script-->
2 changes: 2 additions & 0 deletions coldfront/core/research_output/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
urlpatterns = [
path('add-research-output/<int:project_pk>/', research_output_views.ResearchOutputCreateView.as_view(), name='add-research-output'),
path('project/<int:project_pk>/delete-research-outputs', research_output_views.ResearchOutputDeleteResearchOutputsView.as_view(), name='research-output-delete-research-outputs'),
path('research-output-report/', research_output_views.ResearchOutputReportView.as_view(), name='research-output-report'),

]
98 changes: 98 additions & 0 deletions coldfront/core/research_output/views.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import csv
from django.contrib import messages
from django.contrib.messages.views import SuccessMessageMixin
from django.core.exceptions import PermissionDenied
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404
from django.http import HttpResponseRedirect,StreamingHttpResponse
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from django.views.generic import CreateView, ListView
from django.forms import formset_factory

from coldfront.core.utils.common import Echo
from coldfront.core.project.models import Project
from coldfront.core.research_output.forms import ResearchOutputForm
from coldfront.core.research_output.forms import ResearchOutputForm, ResearchOutputReportForm
from coldfront.core.research_output.models import ResearchOutput
from coldfront.core.utils.mixins.views import (
UserActiveManagerOrHigherMixin,
Expand All @@ -16,7 +22,99 @@
SnakeCaseTemplateNameMixin,
)

class ResearchOutputReportView(ListView):
template_name = 'research_output/research_output_report.html'

_research_output_fields_for_end = ['created_by', 'project', 'created', 'modified']

def get_r_outputs(self):
research_outputs = ResearchOutput.objects.all()

research_outputs=[
{
"pk":research_output.pk,
"title":research_output.title,
"description":research_output.description,
"project":research_output.project.title,
"created_by":research_output.created_by,
"created":research_output.created
}
for research_output in research_outputs
]
return research_outputs

def get(self,request):

context = {}
research_outputs=self.get_r_outputs()
formset = formset_factory(ResearchOutputReportForm,max_num=len(research_outputs))
formset = formset(initial=research_outputs, prefix='researchoutputform')
context["formset"] = formset



return render(request, self.template_name, context)



def post(self, request, *args, **kwargs):
research_outputs = self.get_r_outputs()

formset = formset_factory(ResearchOutputReportForm,max_num=len(research_outputs))
formset = formset(request.POST, initial=research_outputs, prefix='researchoutputform')

header = [
"Title",
"Description",
"Created By",
"Project",
"Created"
]
rows = []
research_outputs_selected = 0

if formset.is_valid():
for form in formset:
form_data = form.cleaned_data
if(form_data):
if form_data['selected']:
research_output_1 = get_object_or_404(ResearchOutput, pk=form_data['pk'])

row = [
research_output_1.title,
research_output_1.description,
research_output_1.created_by,
research_output_1.project,
research_output_1.created,

]
rows.append(row)
research_outputs_selected += 1

if research_outputs_selected == 0:
research_outputs_2 = ResearchOutput.objects.all()
for research_output in research_outputs_2:
row = [
research_output.title,
research_output.description,
research_output.created_by,
research_output.project,
research_output.created,
]
rows.append(row)

rows.insert(0, header)
pseudo_buffer = Echo()
writer = csv.writer(pseudo_buffer)
response = StreamingHttpResponse((writer.writerow(row) for row in rows),
content_type="text/csv")
response['Content-Disposition'] = 'attachment; filename="researchoutput_report.csv"'
return response
else:
for error in formset.errors:
messages.error(request, error)
return HttpResponseRedirect(reverse('research-output-report'))

class ResearchOutputCreateView(
UserActiveManagerOrHigherMixin,
ChangesOnlyOnActiveProjectMixin,
Expand Down
1 change: 1 addition & 0 deletions coldfront/templates/common/navbar_admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
<a id="navbar-allocation-change-requests" class="dropdown-item" href="{% url 'allocation-change-list' %}">
Allocation Change Requests</a>
<a id="navbar-grant-report" class="dropdown-item" href="{% url 'grant-report' %}">Grant Report</a>
<a id="navbar-report" class="dropdown-item" href="{% url 'research-output-report' %}">Research Output Report</a>
</div>
</li>
1 change: 1 addition & 0 deletions coldfront/templates/common/navbar_director.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
<a class="dropdown-item" href="{% url 'resource-list' %}">All Resources</a>
<a id="navbar-project-reviews" class="dropdown-item" href="{% url 'project-review-list' %}">Project Reviews</a>
<a id="navbar-grant-report" class="dropdown-item" href="{% url 'grant-report' %}">Grant Report</a>
<a id="navbar-report" class="dropdown-item" href="{% url 'research-output-report' %}">Research Output Report</a>
</div>
</li>
4 changes: 4 additions & 0 deletions coldfront/templates/common/navbar_nonadmin_staff.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
{% endif %}
{% if perms.grant.can_view_all_grants %}
<a id="navbar-grant-report" class="dropdown-item" href="{% url 'grant-report' %}">Grant Report</a>
{% endif %}
{% if perms.research_output.can_view_research_output_report %}
<a id="navbar-report" class="dropdown-item" href="{% url 'research-output-report' %}">Research Output Report</a>
{% endif %}

</div>
</li>