Skip to content
This repository was archived by the owner on Feb 27, 2021. It is now read-only.

Commit 77521d7

Browse files
committed
only keep papers in the next 3 years (fixes: #378)
1 parent ff198d9 commit 77521d7

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# -*- coding: utf-8 -*-
2+
# Generated by Django 1.10.5 on 2017-01-05 12:01
3+
from __future__ import unicode_literals
4+
import datetime
5+
from papers.utils import year_margin
6+
from django.db import migrations
7+
import haystack
8+
from papers.models import Paper
9+
10+
11+
# have to put this here as we cannot call methods from the model in a migration...
12+
13+
def remove_from_index(paper):
14+
"""
15+
Remove this paper from Haystack's index
16+
(to be called before deleting the paper for real)
17+
"""
18+
using_backends = haystack.connection_router.for_write(instance=paper)
19+
for using in using_backends:
20+
try:
21+
index = haystack.connections[using].get_unified_index(
22+
).get_index(Paper)
23+
index.remove_object(paper, using=using)
24+
except haystack.exceptions.NotHandled:
25+
pass
26+
27+
28+
def database_forwards(apps, schema_editor):
29+
now = datetime.datetime.now()
30+
current_year = now.year
31+
cutoff_date = datetime.datetime(current_year + year_margin, 12, 31)
32+
Paper_fake = apps.get_model("papers", "Paper")
33+
for paper in Paper_fake.objects.filter(pubdate__gt=cutoff_date):
34+
remove_from_index(paper)
35+
paper.delete()
36+
37+
38+
def database_backwards(apps, schema_editor):
39+
pass
40+
41+
42+
class Migration(migrations.Migration):
43+
44+
dependencies = [
45+
('papers', '0048_remove_paper_researchers'),
46+
]
47+
48+
operations = [
49+
migrations.RunPython(
50+
database_forwards,
51+
database_backwards
52+
),
53+
]

papers/utils.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
filter_punctuation_alphanum_regex = re.compile(r'.*\w')
3535

36+
year_margin = 3
3637

3738
def filter_punctuation(lst):
3839
"""
@@ -546,11 +547,11 @@ def valid_publication_date(dt):
546547
>>> valid_publication_date(None)
547548
False
548549
"""
549-
# In 2042, the problem of access to scientific publications
550-
# will be solved and Dissemin will autodestroy as a consequence
551-
# of this hack.
550+
now = datetime.datetime.now()
551+
current_year = now.year
552+
# some papers are published in the future ("to appear")
552553
return ((type(dt) == datetime.date or type(dt) == datetime.datetime)
553-
and dt.year < 2042)
554+
and dt.year < current_year + year_margin)
554555

555556
### ORCiD utilities ###
556557

0 commit comments

Comments
 (0)