Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
only keep papers in the next 3 years (fixes: #378)
  • Loading branch information
a3nm committed Jul 8, 2017
1 parent ff198d9 commit 77521d73238c519e252e2062c41a533275dfa75d
Showing with 58 additions and 4 deletions.
  1. +53 −0 papers/migrations/0049_filter_future_dates.py
  2. +5 −4 papers/utils.py
@@ -0,0 +1,53 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2017-01-05 12:01
from __future__ import unicode_literals
import datetime
from papers.utils import year_margin
from django.db import migrations
import haystack
from papers.models import Paper


# have to put this here as we cannot call methods from the model in a migration...

def remove_from_index(paper):
"""
Remove this paper from Haystack's index
(to be called before deleting the paper for real)
"""
using_backends = haystack.connection_router.for_write(instance=paper)
for using in using_backends:
try:
index = haystack.connections[using].get_unified_index(
).get_index(Paper)
index.remove_object(paper, using=using)
except haystack.exceptions.NotHandled:
pass


def database_forwards(apps, schema_editor):
now = datetime.datetime.now()
current_year = now.year
cutoff_date = datetime.datetime(current_year + year_margin, 12, 31)
Paper_fake = apps.get_model("papers", "Paper")
for paper in Paper_fake.objects.filter(pubdate__gt=cutoff_date):
remove_from_index(paper)
paper.delete()


def database_backwards(apps, schema_editor):
pass


class Migration(migrations.Migration):

dependencies = [
('papers', '0048_remove_paper_researchers'),
]

operations = [
migrations.RunPython(
database_forwards,
database_backwards
),
]
@@ -33,6 +33,7 @@

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

year_margin = 3

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

### ORCiD utilities ###

0 comments on commit 77521d7

Please sign in to comment.