Skip to content

views

main.views ¤

Views for the main app.

Classes¤

CapacitiesListView ¤

Bases: LoginRequiredMixin, SingleTableMixin, FilterView

View to display the list of capacities.

CapacityPlanningView ¤

Bases: LoginRequiredMixin, TemplateView

View that renders the Capacity Planning page.

Functions¤
get_context_data(**kwargs) ¤

Add HTML components and Bokeh version to the context.

Source code in main/views.py
137
138
139
140
141
142
143
144
145
def get_context_data(self, **kwargs: Any) -> dict[str, Any]:  # type: ignore
    """Add HTML components and Bokeh version to the context."""
    context = super().get_context_data(**kwargs)
    plot = plots.create_capacity_planning_plot(
        datetime.now(), datetime.now() + timedelta(365)
    )
    context.update(plots.html_components_from_plot(plot))
    context["bokeh_version"] = bokeh.__version__
    return context

CostRecoveryView ¤

Bases: LoginRequiredMixin, FormView

View that renders the Cost Recovery page.

Functions¤
form_valid(form) ¤

Generate csv using the dates provided in the form.

Source code in main/views.py
154
155
156
157
158
159
def form_valid(self, form: Form) -> HttpResponse:
    """Generate csv using the dates provided in the form."""
    month = form.cleaned_data["month"]
    year = form.cleaned_data["year"]
    response = report.create_charges_report_for_download(month, year)
    return response
get_context_data(**kwargs) ¤

Add HTML components and Bokeh version to the context.

Source code in main/views.py
161
162
163
164
165
166
167
168
def get_context_data(self, **kwargs: Any) -> dict[str, Any]:  # type: ignore
    """Add HTML components and Bokeh version to the context."""
    context = super().get_context_data(**kwargs)
    timeseries_plot, bar_plot = plots.create_cost_recovery_plots()
    context.update(plots.html_components_from_plot(timeseries_plot, "timeseries"))
    context.update(plots.html_components_from_plot(bar_plot, "bar"))
    context["bokeh_version"] = bokeh.__version__
    return context

CustomBaseDetailView ¤

Bases: LoginRequiredMixin, UpdateView

Detail view based on a read-only form view.

While there is a generic Detail View, it is not rendered nicely easily as the bootstrap theme needs to be applied on a field by field basis. So we use a form view instead, which can easily be styled, and make the form read only.

Functions¤
get_form(form_class=None) ¤

Customize form to make it read-only.

Parameters:

Name Type Description Default
form_class Any | None

The form class to use, if any.

None
Return

A form associated to the model.

Source code in main/views.py
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def get_form(self, form_class: Any | None = None) -> ModelForm:  # type: ignore
    """Customize form to make it read-only.

    Args:
        form_class: The form class to use, if any.

    Return:
        A form associated to the model.
    """
    form = super().get_form(form_class)

    for field in form.fields.keys():
        form.fields[field].widget.attrs["disabled"] = True
        form.fields[field].widget.attrs["readonly"] = True

    return form

FundingDetailView ¤

Bases: CustomBaseDetailView

View to view details of project funding.

Functions¤
get_context_data(**kwargs) ¤

Add funding name to the context, so it is easy to retrieve.

Source code in main/views.py
125
126
127
128
129
def get_context_data(self, **kwargs: Any) -> dict[str, Any]:  # type: ignore
    """Add funding name to the context, so it is easy to retrieve."""
    context = super().get_context_data(**kwargs)
    context["funding_name"] = str(self.get_object())
    return context

FundingListView ¤

Bases: LoginRequiredMixin, SingleTableMixin, ListView

View to display the funding list for all projects.

ProjectDetailView ¤

Bases: CustomBaseDetailView

View to view details of a project.

Functions¤
get_context_data(**kwargs) ¤

Add project name and funding table to the context.

A custom query is used with the funding table, so only the funding for the current project is displayed.

Source code in main/views.py
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
def get_context_data(self, **kwargs: Any) -> dict[str, Any]:  # type: ignore
    """Add project name and funding table to the context.

    A custom query is used with the funding table, so only the funding for the
    current project is displayed.
    """
    context = super().get_context_data(**kwargs)
    context["project_name"] = self.get_object().name
    # get funding info for the current project
    funding_source = self.get_object().funding_source.all()
    funding_table = tables.FundingTable(funding_source)
    # enables the table to be sorted by column headings
    RequestConfig(self.request).configure(funding_table)
    context["funding_table"] = funding_table
    return context

ProjectsListView ¤

Bases: LoginRequiredMixin, SingleTableMixin, FilterView

View to display the list of projects.

RegistrationView ¤

Bases: CreateView

View to register new users.

TODO: This is a placeholder for development. When SSO is implemented, this won't be needed since available users will be retrieved automatically.

Functions¤

index(request) ¤

View that renders the index page.

Source code in main/views.py
25
26
27
def index(request: HttpRequest) -> HttpResponse:
    """View that renders the index page."""
    return render(request=request, template_name="main/index.html")