Skip to content

Commit

Permalink
add TopicCollection.[collection_alias, aggregation_with_same_type], i…
Browse files Browse the repository at this point in the history
…mplement get_collection_json
  • Loading branch information
Fasand committed Aug 9, 2021
1 parent 0f1a715 commit 00ffdf6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
23 changes: 23 additions & 0 deletions Seeder/harvests/migrations/0015_auto_20210504_0731.py
Original file line number Diff line number Diff line change
@@ -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'),
),
]
33 changes: 31 additions & 2 deletions Seeder/harvests/models.py
Original file line number Diff line number Diff line change
@@ -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 _
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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})

Expand All @@ -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)
Expand Down

0 comments on commit 00ffdf6

Please sign in to comment.