From f47a680e2a94b6818658ef51de8593f649d46c01 Mon Sep 17 00:00:00 2001 From: Shreyas Date: Sun, 8 Oct 2023 00:38:59 -0400 Subject: [PATCH 1/2] routput 2 --- coldfront/core/research_output/forms.py | 15 +++ coldfront/core/research_output/models.py | 2 + .../research_output_report.html | 85 ++++++++++++++++ coldfront/core/research_output/urls.py | 2 + coldfront/core/research_output/views.py | 98 +++++++++++++++++++ 5 files changed, 202 insertions(+) create mode 100644 coldfront/core/research_output/templates/research_output/research_output_report.html diff --git a/coldfront/core/research_output/forms.py b/coldfront/core/research_output/forms.py index ca7d16665..ab37039ed 100644 --- a/coldfront/core/research_output/forms.py +++ b/coldfront/core/research_output/forms.py @@ -1,5 +1,7 @@ +from django import forms from django.forms import ModelForm + from coldfront.core.research_output.models import ResearchOutput @@ -7,3 +9,16 @@ 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() \ No newline at end of file diff --git a/coldfront/core/research_output/models.py b/coldfront/core/research_output/models.py index 92977c598..ace84a213 100644 --- a/coldfront/core/research_output/models.py +++ b/coldfront/core/research_output/models.py @@ -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( diff --git a/coldfront/core/research_output/templates/research_output/research_output_report.html b/coldfront/core/research_output/templates/research_output/research_output_report.html new file mode 100644 index 000000000..08f317b48 --- /dev/null +++ b/coldfront/core/research_output/templates/research_output/research_output_report.html @@ -0,0 +1,85 @@ +{% extends "common/base.html" %} +{% load crispy_forms_tags %} +{% load static %} +{% load humanize %} + + +{% block title %} +Project Output Report +{% endblock %} + + +{% block content %} + +
+
+

Research Outputs

+ +
+
+ {% if formset %} +
+ {% csrf_token %} +
+ + + + + + + + + + + + + {% for form in formset %} + + + + + + + + + {% endfor %} + +
TitleDescriptionCreated ByProjectCreated
{{ form.selected }}{{ form.title.value }}{{ form.description.value }}{{ form.created_by.value }}{{ form.project.value }}{{ form.created.value|date:"M. d, Y" }}
+
+ {{ formset.management_form }} +
+ {% else %} + + {% endif %} +
+
+ + + +{% endblock %} + \ No newline at end of file diff --git a/coldfront/core/research_output/urls.py b/coldfront/core/research_output/urls.py index db78e0870..9a0da3b75 100644 --- a/coldfront/core/research_output/urls.py +++ b/coldfront/core/research_output/urls.py @@ -5,4 +5,6 @@ urlpatterns = [ path('add-research-output//', research_output_views.ResearchOutputCreateView.as_view(), name='add-research-output'), path('project//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'), + ] diff --git a/coldfront/core/research_output/views.py b/coldfront/core/research_output/views.py index dd421afe7..3eaf9bd2f 100644 --- a/coldfront/core/research_output/views.py +++ b/coldfront/core/research_output/views.py @@ -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, @@ -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, From 26894c1afa4eba08487d2dcbe4837d72d306ac4d Mon Sep 17 00:00:00 2001 From: Shreyas Date: Sun, 8 Oct 2023 00:43:18 -0400 Subject: [PATCH 2/2] rouptut remake pr commit 2 adds missing director support --- coldfront/templates/common/navbar_admin.html | 1 + coldfront/templates/common/navbar_director.html | 1 + coldfront/templates/common/navbar_nonadmin_staff.html | 4 ++++ 3 files changed, 6 insertions(+) diff --git a/coldfront/templates/common/navbar_admin.html b/coldfront/templates/common/navbar_admin.html index c59857cf9..9cb5b5642 100644 --- a/coldfront/templates/common/navbar_admin.html +++ b/coldfront/templates/common/navbar_admin.html @@ -12,5 +12,6 @@ Allocation Change Requests Grant Report + Research Output Report diff --git a/coldfront/templates/common/navbar_director.html b/coldfront/templates/common/navbar_director.html index d4ac2217e..9525c1603 100644 --- a/coldfront/templates/common/navbar_director.html +++ b/coldfront/templates/common/navbar_director.html @@ -6,5 +6,6 @@ All Resources Project Reviews Grant Report + Research Output Report diff --git a/coldfront/templates/common/navbar_nonadmin_staff.html b/coldfront/templates/common/navbar_nonadmin_staff.html index ea11e8d50..fb08a5775 100644 --- a/coldfront/templates/common/navbar_nonadmin_staff.html +++ b/coldfront/templates/common/navbar_nonadmin_staff.html @@ -23,6 +23,10 @@ {% endif %} {% if perms.grant.can_view_all_grants %} Grant Report + {% endif %} + {% if perms.research_output.can_view_research_output_report %} + Research Output Report {% endif %} +