diff --git a/Seeder/harvests/migrations/0015_auto_20210504_0731.py b/Seeder/harvests/migrations/0015_auto_20210504_0731.py new file mode 100644 index 00000000..6bd956c4 --- /dev/null +++ b/Seeder/harvests/migrations/0015_auto_20210504_0731.py @@ -0,0 +1,23 @@ +# Generated by Django 2.2.20 on 2021-05-04 07:31 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('harvests', '0014_harvest_seeds_not_harvested'), + ] + + operations = [ + migrations.AddField( + model_name='topiccollection', + name='aggregation_with_same_type', + field=models.BooleanField(default=True, verbose_name='Aggregation with same type'), + ), + migrations.AddField( + model_name='topiccollection', + name='collection_alias', + field=models.CharField(blank=True, max_length=64, verbose_name='Collection alias'), + ), + ] diff --git a/Seeder/harvests/models.py b/Seeder/harvests/models.py index 980b499c..26997eeb 100755 --- a/Seeder/harvests/models.py +++ b/Seeder/harvests/models.py @@ -1,8 +1,8 @@ import os from itertools import chain -from datetime import date -from django.contrib import messages +from hashlib import md5 +from django.utils import timezone from django.db import models from django.utils.translation import ugettext_lazy as _ @@ -72,6 +72,10 @@ def repr(self): def __str__(self): return self.repr() + @staticmethod + def hash_seeds(seeds): + return md5("\n".join(seeds).encode("utf-8")).hexdigest() + def pair_custom_seeds(self): """ Tries to pair the urls from ``custom_seeds`` with existing sources @@ -452,6 +456,12 @@ class TopicCollection(HarvestAbstractModel, OrderedModel): date_from = DatePickerField(_('Date from'), null=True) date_to = DatePickerField(_('Date to'), null=True, blank=True) + # Harvest-specific fields + collection_alias = models.CharField( + _("Collection alias"), max_length=64, blank=True) + aggregation_with_same_type = models.BooleanField( + _("Aggregation with same type"), default=True) + def get_www_url(self): return reverse('www:collection_detail', kwargs={"slug": self.slug}) @@ -468,6 +478,25 @@ def update_slug(self): self.slug = unique_slug self.save() + def get_collection_json(self, blacklisted=None): + if blacklisted is None: + blacklisted = self.get_blacklisted() + seeds = sorted(set(self.get_seeds() - blacklisted)) + alias = (self.collection_alias if len(self.collection_alias) > 0 + else "NoAlias") + collection = { + "name": f"Topics{alias}_{timezone.now():%Y-%m-%d}", + "collectionAlias": alias, + "annotation": self.annotation, + "nameCurator": self.title, + "idCollection": self.pk, + "aggregationWithSameType": self.aggregation_with_same_type, + "hash": self.hash_seeds(seeds), + "seedsNo": len(seeds), + "seeds": seeds, + } + return collection + def __str__(self): sign = '✔' if self.active else '✗' return '{0} {1}'.format(sign, self.title)