Skip to content

Commit 968de4e

Browse files
committed
check publication year
modified: coldfront/core/publication/forms.py modified: coldfront/core/publication/templates/publication/publication_add_publication_search_result.html modified: coldfront/core/publication/views.py
1 parent 0d69682 commit 968de4e

3 files changed

Lines changed: 142 additions & 133 deletions

File tree

coldfront/core/publication/forms.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
import datetime as dt
2+
13
from django import forms
4+
from django.core.exceptions import ValidationError
5+
from django.forms import BaseFormSet
26

37

48
class PublicationAddForm(forms.Form):
@@ -14,21 +18,37 @@ class PublicationSearchForm(forms.Form):
1418

1519
def __init__(self, *args, **kwargs):
1620
super().__init__(*args, **kwargs)
17-
self.fields[
18-
"search_id"
19-
].help_text = "<br/>Enter ID such as DOI or Bibliographic Code to search."
21+
self.fields["search_id"].help_text = "<br/>Enter ID such as DOI or Bibliographic Code to search."
2022

2123

2224
class PublicationResultForm(forms.Form):
2325
title = forms.CharField(max_length=1024, disabled=True)
2426
author = forms.CharField(disabled=True)
25-
year = forms.CharField(max_length=4, disabled=True)
27+
year = forms.IntegerField(disabled=True)
2628
journal = forms.CharField(max_length=1024, disabled=True)
2729
unique_id = forms.CharField(max_length=255, disabled=True)
2830
source_pk = forms.IntegerField(widget=forms.HiddenInput(), disabled=True)
2931
selected = forms.BooleanField(initial=False, required=False)
3032

3133

34+
class PublicationResultFormset(BaseFormSet):
35+
def clean(self):
36+
"""Checks that no two articles have the same title."""
37+
if any(self.errors):
38+
# Don't bother validating the formset unless each form is valid on its own
39+
return
40+
curr_year = dt.datetime.today().year
41+
for form in self.forms:
42+
year = form.cleaned_data.get("year")
43+
if year < curr_year - 1:
44+
raise ValidationError(f"Publication year entered is: {year}. Please add recent publications only!")
45+
46+
if year > curr_year:
47+
raise ValidationError(
48+
f"Publication year entered is: {year} which is in the future. Please fix it.",
49+
)
50+
51+
3252
class PublicationDeleteForm(forms.Form):
3353
title = forms.CharField(max_length=255, disabled=True)
3454
year = forms.CharField(max_length=30, disabled=True)
Lines changed: 68 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,76 @@
11
{% load crispy_forms_tags %}
22

3+
{% if formset.non_form_errors %}
4+
<div class="alert alert-danger">
5+
{{ formset.non_form_errors }}
6+
</div>
7+
{% else %}
8+
39
{% if formset %}
4-
<div class="card border-light">
5-
<div class="card-body">
6-
<form action="{% url 'add-publication' project_pk %}" method="post">
7-
{% csrf_token %}
8-
<div class="table-responsive">
9-
<table class="table table-sm table-hover">
10-
<thead>
11-
<tr>
12-
<th>
13-
<input type="checkbox" class="form-check-input" id="selectAll">
14-
</th>
15-
<th scope="col">#</th>
16-
<th scope="col">Publication</th>
17-
<th scope="col">Unique ID</th>
18-
</tr>
19-
</thead>
20-
<tbody>
21-
{% for form in formset %}
22-
<tr>
23-
<td>{{ form.selected }}</td>
24-
<td>{{ forloop.counter }}</td>
25-
<td>
26-
<strong>Title: </strong>{{ form.title.value }} <br>
27-
<strong>Author: </strong>{{ form.author.value }} <br>
28-
<strong>Year: </strong>{{ form.year.value }} <br>
29-
<strong>Journal: </strong>{{ form.journal.value }}
30-
</td>
31-
<td class="text-nowrap">{{ form.unique_id.value }}</td>
32-
</tr>
33-
{% endfor %}
34-
</tbody>
35-
</table>
36-
</div>
37-
{{ formset.management_form }}
38-
<div>
39-
<button type="submit" class="btn btn-primary"><i class="fas fa-user-minus" aria-hidden="true"></i> Add Selected Publications to Project</button>
40-
<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>
41-
<input id="search_ids" type="hidden" name="search_ids" value="{{search_ids}}">
42-
<input id="pubs" type="hidden" name="pubs" value="{{pubs}}">
43-
</div>
44-
</form>
45-
</div>
46-
</div>
10+
<div class="card border-light">
11+
<div class="card-body">
12+
<form action="{% url 'add-publication' project_pk %}" method="post">
13+
{% csrf_token %}
14+
<div class="table-responsive">
15+
<table class="table table-sm table-hover">
16+
<thead>
17+
<tr>
18+
<th>
19+
<input type="checkbox" class="form-check-input" id="selectAll">
20+
</th>
21+
<th scope="col">#</th>
22+
<th scope="col">Publication</th>
23+
<th scope="col">Unique ID</th>
24+
</tr>
25+
</thead>
26+
<tbody>
27+
{% for form in formset %}
28+
<tr>
29+
<td>{{ form.selected }}</td>
30+
<td>{{ forloop.counter }}</td>
31+
<td>
32+
<strong>Title:</strong>{{ form.title.value }} <br>
33+
<strong>Author:</strong>{{ form.author.value }} <br>
34+
<strong>Year:</strong>{{ form.year.value }} <br>
35+
<strong>Journal:</strong>{{ form.journal.value }}
36+
</td>
37+
<td class="text-nowrap">{{ form.unique_id.value }}</td>
38+
</tr>
39+
{% endfor %}
40+
</tbody>
41+
</table>
42+
</div>
43+
{{ formset.management_form }}
44+
<div>
45+
<button type="submit" class="btn btn-primary"><i class="fas fa-user-minus" aria-hidden="true"></i>
46+
Add Selected Publications to Project</button>
47+
<a class="btn btn-secondary" href="{% url 'project-detail' project_pk %}" role="button"><i class="fas fa-long-arrow-left" aria-hidden="true"></i>
48+
Back to Project</a>
49+
<input id="search_ids" type="hidden" name="search_ids" value="{{search_ids}}">
50+
<input id="pubs" type="hidden" name="pubs" value="{{pubs}}">
51+
</div>
52+
</form>
53+
</div>
54+
</div>
4755
{% else %}
48-
<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>
49-
<div class="alert alert-info">
50-
No users to remove!
51-
</div>
56+
<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>
57+
Back to Project</a>
58+
<div class="alert alert-info">
59+
No users to remove!
60+
</div>
5261
{% endif %}
5362

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

59-
$("input[name^='pubform-']").click(function(ele) {
60-
var id = $(this).attr('id');
61-
if (id != "selectAll") {
62-
$("#selectAll").prop('checked', false);
63-
}
64-
});
65+
<script>
66+
$("#selectAll").click(function() {
67+
$("input[name^='pubform-']").prop('checked', $(this).prop('checked'));
68+
});
69+
70+
$("input[name^='pubform-']").click(function(ele) {
71+
var id = $(this).attr('id');
72+
if (id != "selectAll") {
73+
$("#selectAll").prop('checked', false);
74+
}
75+
});
6576
</script>

0 commit comments

Comments
 (0)