Skip to content
This repository has been archived by the owner on Jan 24, 2022. It is now read-only.

Commit

Permalink
get expected date of next pkg-update
Browse files Browse the repository at this point in the history
  • Loading branch information
stefina committed Jan 12, 2018
1 parent 7a7da27 commit 5b1376f
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 1 deletion.
10 changes: 10 additions & 0 deletions ckanext/switzerland/dcat-ap-switzerland_scheming.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@
},
"preset": "ogdch_date"
},
{
"field_name": "expected_update",
"label": {
"en": "Date of expected update",
"de": "Vorraussichtlich nächste Aktualisierung",
"fr": "Date of expected update",
"it": "Date of expected update"
},
"preset": "ogdch_date"
},
{
"field_name": "owner_org",
"label": {
Expand Down
44 changes: 44 additions & 0 deletions ckanext/switzerland/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from urlparse import urlparse
from ckan.lib.helpers import localised_number
import ckan.lib.i18n as i18n
from datetime import datetime, timedelta

import unicodedata

Expand Down Expand Up @@ -367,3 +368,46 @@ def uri_to_iri(uri):
return iri
except:
raise ValueError("Provided URI can't be converted to IRI")


def get_timedelta_by_frequency(accrual_periodicity):

time_deltas = {
'http://purl.org/cld/freq/completelyIrregular': None, # noqa
'http://purl.org/cld/freq/continuous': None, # noqa
'http://purl.org/cld/freq/daily': timedelta(days=1), # noqa
'http://purl.org/cld/freq/threeTimesAWeek': timedelta(days=2), # noqa
'http://purl.org/cld/freq/semiweekly': timedelta(days=4), # noqa
'http://purl.org/cld/freq/weekly': timedelta(days=7), # noqa
'http://purl.org/cld/freq/threeTimesAMonth': timedelta(days=10), # noqa
'http://purl.org/cld/freq/biweekly': timedelta(days=14), # noqa
'http://purl.org/cld/freq/semimonthly': timedelta(days=15), # noqa
'http://purl.org/cld/freq/monthly': timedelta(days=30), # noqa
'http://purl.org/cld/freq/bimonthly': timedelta(days=60), # noqa
'http://purl.org/cld/freq/quarterly': timedelta(days=91), # noqa
'http://purl.org/cld/freq/threeTimesAYear': timedelta(days=122), # noqa
'http://purl.org/cld/freq/semiannual': timedelta(days=183), # noqa
'http://purl.org/cld/freq/annual': timedelta(days=365), # noqa
'http://purl.org/cld/freq/biennial': timedelta(days=730), # noqa
'http://purl.org/cld/freq/triennial': timedelta(days=1095), # noqa
}
try:
return time_deltas[accrual_periodicity]
except KeyError:
return None


def get_expected_update(modified, accrual_periodicity):
time_pattern = '%Y-%m-%dT%H:%M:%S'

try:
updated_datetime = datetime.strptime(modified, time_pattern)
time_delta = get_timedelta_by_frequency(accrual_periodicity)

if time_delta:
next_expected_datetime = updated_datetime + time_delta
log.error(next_expected_datetime.strftime(time_pattern))
return next_expected_datetime.strftime(time_pattern)
except ValueError:
pass
return None
13 changes: 12 additions & 1 deletion ckanext/switzerland/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
get_political_level, get_dataset_by_identifier, get_readable_file_size,
simplify_terms_of_use, parse_json, get_piwik_config,
ogdch_localised_number, ogdch_render_tree, ogdch_group_tree,
map_to_valid_format
map_to_valid_format, get_expected_update
)

import ckan.plugins as plugins
Expand Down Expand Up @@ -444,6 +444,17 @@ def before_map(self, map):
return map

# IPackageController
def before_view(self, pkg_dict):
if not self.is_supported_package_type(pkg_dict):
return pkg_dict

if pkg_dict.get('modified') and pkg_dict.get('accrual_periodicity'):
pkg_dict['expected_update'] = get_expected_update(
pkg_dict.get('modified'),
pkg_dict.get('accrual_periodicity')
) or None

return super(OgdchPackagePlugin, self).before_view(pkg_dict)

# TODO: before_view isn't called in API requests -> after_show is
# BUT (!) after_show is also called when packages get indexed
Expand Down
41 changes: 41 additions & 0 deletions ckanext/switzerland/tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import ckanext.switzerland.helpers as helpers
import sys
from copy import deepcopy
from datetime import datetime, timedelta

if sys.version_info < (2, 7):
import unittest2 as unittest
Expand Down Expand Up @@ -38,6 +39,26 @@

organization_title = u'{"fr": "Swisstopo FR", "de": "Swisstopo DE", "en": "Swisstopo EN", "it": "Swisstopo IT"}' # noqa

frequencies = {
'http://purl.org/cld/freq/completelyIrregular': None, # noqa
'http://purl.org/cld/freq/continuous': None, # noqa
'http://purl.org/cld/freq/daily': timedelta(days=1), # noqa
'http://purl.org/cld/freq/threeTimesAWeek': timedelta(days=3), # noqa
'http://purl.org/cld/freq/semiweekly': timedelta(days=4), # noqa
'http://purl.org/cld/freq/weekly': timedelta(days=7), # noqa
'http://purl.org/cld/freq/threeTimesAMonth': timedelta(days=10), # noqa
'http://purl.org/cld/freq/biweekly': timedelta(days=14), # noqa
'http://purl.org/cld/freq/semimonthly': timedelta(days=15), # noqa
'http://purl.org/cld/freq/monthly': timedelta(days=30), # noqa
'http://purl.org/cld/freq/bimonthly': timedelta(days=60), # noqa
'http://purl.org/cld/freq/quarterly': timedelta(days=91), # noqa
'http://purl.org/cld/freq/threeTimesAYear': timedelta(days=122), # noqa
'http://purl.org/cld/freq/semiannual': timedelta(days=183), # noqa
'http://purl.org/cld/freq/annual': timedelta(days=365), # noqa
'http://purl.org/cld/freq/biennial': timedelta(days=730), # noqa
'http://purl.org/cld/freq/triennial': timedelta(days=1095), # noqa
}

class TestHelpers(unittest.TestCase):
def test_simplify_terms_of_use_open(self):
term_id = 'NonCommercialAllowed-CommercialAllowed-ReferenceRequired'
Expand Down Expand Up @@ -179,3 +200,23 @@ def find_position_of_org(self, org_list, title):
org['title'] == title),
-1)
return index

def test_get_expected_update(self):
modified_set = '2016-09-20T00:00:00'
modified_empty = ''
modified_invalid = '9. September 2015'

period_irregular = 'http://purl.org/cld/freq/completelyIrregular'
period_continuous = 'http://purl.org/cld/freq/continuous'
period_empty = ''
period_invalid = 'something_that_does_not_match'
period_annual = 'http://purl.org/cld/freq/annual'

self.assertEquals('2017-09-20T00:00:00', helpers.get_expected_update(modified_set, period_annual)) #noqa
self.assertEquals(None, helpers.get_expected_update(modified_empty, period_annual)) #noqa
self.assertEquals(None, helpers.get_expected_update(modified_invalid, period_annual)) #noqa

self.assertEquals(None, helpers.get_expected_update(modified_set, period_irregular)) #noqa
self.assertEquals(None, helpers.get_expected_update(modified_set, period_continuous)) #noqa
self.assertEquals(None, helpers.get_expected_update(modified_set, period_empty)) #noqa
self.assertEquals(None, helpers.get_expected_update(modified_set, period_invalid)) #noqa

0 comments on commit 5b1376f

Please sign in to comment.