Skip to content
Merged
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
30 changes: 26 additions & 4 deletions coldfront/core/publication/forms.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import datetime as dt

from django import forms
from django.core.exceptions import ValidationError
from django.forms import BaseFormSet


class PublicationAddForm(forms.Form):
Expand All @@ -14,21 +18,39 @@ class PublicationSearchForm(forms.Form):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields[
"search_id"
].help_text = "<br/>Enter ID such as DOI or Bibliographic Code to search."
self.fields["search_id"].help_text = "<br/>Enter ID such as DOI or Bibliographic Code to search."


class PublicationResultForm(forms.Form):
title = forms.CharField(max_length=1024, disabled=True)
author = forms.CharField(disabled=True)
year = forms.CharField(max_length=4, disabled=True)
year = forms.IntegerField(disabled=True)
journal = forms.CharField(max_length=1024, disabled=True)
unique_id = forms.CharField(max_length=255, disabled=True)
source_pk = forms.IntegerField(widget=forms.HiddenInput(), disabled=True)
selected = forms.BooleanField(initial=False, required=False)


class PublicationResultFormset(BaseFormSet):
def clean(self):
"""Checks that no two articles have the same title."""
if any(self.errors):
# Don't bother validating the formset unless each form is valid on its own
return
curr_year = dt.datetime.today().year
for form in self.forms:
year = form.cleaned_data.get("year")
if year < curr_year - 1:
raise ValidationError(
f"Publication year entered is: {year}. Please add recent publications (this year and previous year) only!"
)

if year > curr_year:
raise ValidationError(
f"Publication year entered is: {year} which is in the future. Please fix it.",
)


class PublicationDeleteForm(forms.Form):
title = forms.CharField(max_length=255, disabled=True)
year = forms.CharField(max_length=30, disabled=True)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,65 +1,76 @@
{% load crispy_forms_tags %}

{% if formset.non_form_errors %}
<div class="alert alert-danger">
{{ formset.non_form_errors }}
</div>
{% else %}

{% if formset %}
<div class="card border-light">
<div class="card-body">
<form action="{% url 'add-publication' project_pk %}" method="post">
{% csrf_token %}
<div class="table-responsive">
<table class="table table-sm table-hover">
<thead>
<tr>
<th>
<input type="checkbox" class="form-check-input" id="selectAll">
</th>
<th scope="col">#</th>
<th scope="col">Publication</th>
<th scope="col">Unique ID</th>
</tr>
</thead>
<tbody>
{% for form in formset %}
<tr>
<td>{{ form.selected }}</td>
<td>{{ forloop.counter }}</td>
<td>
<strong>Title: </strong>{{ form.title.value }} <br>
<strong>Author: </strong>{{ form.author.value }} <br>
<strong>Year: </strong>{{ form.year.value }} <br>
<strong>Journal: </strong>{{ form.journal.value }}
</td>
<td class="text-nowrap">{{ form.unique_id.value }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{{ formset.management_form }}
<div>
<button type="submit" class="btn btn-primary"><i class="fas fa-user-minus" aria-hidden="true"></i> Add Selected Publications to Project</button>
<a class="btn btn-secondary" href="{% url 'project-detail' project_pk %}" role="button"><i class="fas fa-long-arrow-left" aria-hidden="true"></i> Back to Project</a>
<input id="search_ids" type="hidden" name="search_ids" value="{{search_ids}}">
<input id="pubs" type="hidden" name="pubs" value="{{pubs}}">
</div>
</form>
</div>
</div>
<div class="card border-light">
<div class="card-body">
<form action="{% url 'add-publication' project_pk %}" method="post">
{% csrf_token %}
<div class="table-responsive">
<table class="table table-sm table-hover">
<thead>
<tr>
<th>
<input type="checkbox" class="form-check-input" id="selectAll">
</th>
<th scope="col">#</th>
<th scope="col">Publication</th>
<th scope="col">Unique ID</th>
</tr>
</thead>
<tbody>
{% for form in formset %}
<tr>
<td>{{ form.selected }}</td>
<td>{{ forloop.counter }}</td>
<td>
<strong>Title:</strong>{{ form.title.value }} <br>
<strong>Author:</strong>{{ form.author.value }} <br>
<strong>Year:</strong>{{ form.year.value }} <br>
<strong>Journal:</strong>{{ form.journal.value }}
</td>
<td class="text-nowrap">{{ form.unique_id.value }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{{ formset.management_form }}
<div>
<button type="submit" class="btn btn-primary"><i class="fas fa-user-minus" aria-hidden="true"></i>
Add Selected Publications to Project</button>
<a class="btn btn-secondary" href="{% url 'project-detail' project_pk %}" role="button"><i class="fas fa-long-arrow-left" aria-hidden="true"></i>
Back to Project</a>
<input id="search_ids" type="hidden" name="search_ids" value="{{search_ids}}">
<input id="pubs" type="hidden" name="pubs" value="{{pubs}}">
</div>
</form>
</div>
</div>
{% else %}
<a class="btn btn-secondary mb-3" href="{% url 'project-detail' project.pk %}" role="button"><i class="fas fa-long-arrow-left" aria-hidden="true"></i> Back to Project</a>
<div class="alert alert-info">
No users to remove!
</div>
<a class="btn btn-secondary mb-3" href="{% url 'project-detail' project.pk %}" role="button"><i class="fas fa-long-arrow-left" aria-hidden="true"></i>
Back to Project</a>
<div class="alert alert-info">
No users to remove!
</div>
{% endif %}

<script>
$("#selectAll").click(function() {
$("input[name^='pubform-']").prop('checked', $(this).prop('checked'));
});
{% endif %}

$("input[name^='pubform-']").click(function(ele) {
var id = $(this).attr('id');
if (id != "selectAll") {
$("#selectAll").prop('checked', false);
}
});
<script>
$("#selectAll").click(function() {
$("input[name^='pubform-']").prop('checked', $(this).prop('checked'));
});

$("input[name^='pubform-']").click(function(ele) {
var id = $(this).attr('id');
if (id != "selectAll") {
$("#selectAll").prop('checked', false);
}
});
</script>
Loading
Loading