datasource: set slug of chrono datasources (#54889)
This commit is contained in:
parent
859425b37d
commit
f48199fa92
|
@ -648,8 +648,16 @@ def test_data_sources_agenda_refresh(mock_collect, pub, chrono_url):
|
|||
NamedDataSource.wipe()
|
||||
|
||||
mock_collect.return_value = [
|
||||
{'text': 'Events A', 'url': 'http://chrono.example.net/api/agenda/events-A/datetimes/'},
|
||||
{'text': 'Events B', 'url': 'http://chrono.example.net/api/agenda/events-B/datetimes/'},
|
||||
{
|
||||
'slug': 'events-a',
|
||||
'text': 'Events A',
|
||||
'url': 'http://chrono.example.net/api/agenda/events-A/datetimes/',
|
||||
},
|
||||
{
|
||||
'slug': 'events-b',
|
||||
'text': 'Events B',
|
||||
'url': 'http://chrono.example.net/api/agenda/events-B/datetimes/',
|
||||
},
|
||||
]
|
||||
|
||||
app = login(get_app(pub))
|
||||
|
|
|
@ -124,8 +124,16 @@ def test_collect_agenda_data(urlopen, pub, chrono_url):
|
|||
urlopen.side_effect = lambda *args: io.StringIO(json.dumps({"data": AGENDA_EVENTS_DATA}))
|
||||
urlopen.reset_mock()
|
||||
assert collect_agenda_data(pub) == [
|
||||
{'text': 'Events A', 'url': 'http://chrono.example.net/api/agenda/events-A/datetimes/'},
|
||||
{'text': 'Events B', 'url': 'http://chrono.example.net/api/agenda/events-B/datetimes/'},
|
||||
{
|
||||
'slug': 'agenda-events-events-A',
|
||||
'text': 'Events A',
|
||||
'url': 'http://chrono.example.net/api/agenda/events-A/datetimes/',
|
||||
},
|
||||
{
|
||||
'slug': 'agenda-events-events-B',
|
||||
'text': 'Events B',
|
||||
'url': 'http://chrono.example.net/api/agenda/events-B/datetimes/',
|
||||
},
|
||||
]
|
||||
assert urlopen.call_args_list == [mock.call('http://chrono.example.net/api/agenda/')]
|
||||
|
||||
|
@ -138,22 +146,27 @@ def test_collect_agenda_data(urlopen, pub, chrono_url):
|
|||
urlopen.reset_mock()
|
||||
assert collect_agenda_data(pub) == [
|
||||
{
|
||||
'slug': 'agenda-meetings-meetings-A-meetingtypes',
|
||||
'text': 'Meetings A - Meeting types',
|
||||
'url': 'http://chrono.example.net/api/agenda/meetings-A/meetings/',
|
||||
},
|
||||
{
|
||||
'slug': 'agenda-meetings-meetings-A-mt-mt-1',
|
||||
'text': 'Meetings A - Slots of type MT 1',
|
||||
'url': 'http://chrono.example.net/api/agenda/meetings-A/meetings/mt-1/datetimes/',
|
||||
},
|
||||
{
|
||||
'slug': 'agenda-meetings-meetings-A-mt-mt-2',
|
||||
'text': 'Meetings A - Slots of type MT 2',
|
||||
'url': 'http://chrono.example.net/api/agenda/meetings-A/meetings/mt-2/datetimes/',
|
||||
},
|
||||
{
|
||||
'slug': 'agenda-virtual-virtual-B-meetingtypes',
|
||||
'text': 'Virtual B - Meeting types',
|
||||
'url': 'http://chrono.example.net/api/agenda/virtual-B/meetings/',
|
||||
},
|
||||
{
|
||||
'slug': 'agenda-virtual-virtual-B-mt-mt-3',
|
||||
'text': 'Virtual B - Slots of type MT 3',
|
||||
'url': 'http://chrono.example.net/api/agenda/virtual-B/meetings/mt-3/datetimes/',
|
||||
},
|
||||
|
@ -223,8 +236,16 @@ def test_build_agenda_datasources(mock_collect, pub, chrono_url):
|
|||
|
||||
# 2 agenda datasources found
|
||||
mock_collect.return_value = [
|
||||
{'text': 'Events A', 'url': 'http://chrono.example.net/api/agenda/events-A/datetimes/'},
|
||||
{'text': 'Events B', 'url': 'http://chrono.example.net/api/agenda/events-B/datetimes/'},
|
||||
{
|
||||
'slug': 'slug-A',
|
||||
'text': 'Events A',
|
||||
'url': 'http://chrono.example.net/api/agenda/events-A/datetimes/',
|
||||
},
|
||||
{
|
||||
'slug': 'slug-B',
|
||||
'text': 'Events B',
|
||||
'url': 'http://chrono.example.net/api/agenda/events-B/datetimes/',
|
||||
},
|
||||
]
|
||||
|
||||
# agenda datasources does not exist, create them
|
||||
|
@ -233,6 +254,7 @@ def test_build_agenda_datasources(mock_collect, pub, chrono_url):
|
|||
datasource1 = NamedDataSource.get(2 + 1)
|
||||
datasource2 = NamedDataSource.get(2 + 2)
|
||||
assert datasource1.name == 'Events A'
|
||||
assert datasource1.slug == 'chrono_ds_sluga'
|
||||
assert datasource1.external == 'agenda'
|
||||
assert datasource1.external_status is None
|
||||
assert datasource1.data_source == {
|
||||
|
@ -240,6 +262,7 @@ def test_build_agenda_datasources(mock_collect, pub, chrono_url):
|
|||
'value': 'http://chrono.example.net/api/agenda/events-A/datetimes/',
|
||||
}
|
||||
assert datasource2.name == 'Events B'
|
||||
assert datasource2.slug == 'chrono_ds_slugb'
|
||||
assert datasource2.external == 'agenda'
|
||||
assert datasource2.external_status is None
|
||||
assert datasource2.data_source == {
|
||||
|
@ -249,15 +272,19 @@ def test_build_agenda_datasources(mock_collect, pub, chrono_url):
|
|||
|
||||
# again, datasources already exist, but name is wrong => change it
|
||||
datasource1.name = 'wrong'
|
||||
datasource1.slug = 'wrong'
|
||||
datasource1.store()
|
||||
datasource2.name = 'wrong again'
|
||||
datasource2.slug = 'wrong_again'
|
||||
datasource2.store()
|
||||
build_agenda_datasources(pub)
|
||||
assert NamedDataSource.count() == 2 + 2
|
||||
datasource1 = NamedDataSource.get(2 + 1)
|
||||
datasource2 = NamedDataSource.get(2 + 2)
|
||||
assert datasource1.name == 'Events A'
|
||||
assert datasource1.slug == 'wrong'
|
||||
assert datasource2.name == 'Events B'
|
||||
assert datasource2.slug == 'wrong_again'
|
||||
|
||||
# all datasources does not exist, one is unknown
|
||||
datasource1.data_source['value'] = 'http://chrono.example.net/api/agenda/events-FOOBAR/datetimes/'
|
||||
|
@ -300,6 +327,7 @@ def test_build_agenda_datasources(mock_collect, pub, chrono_url):
|
|||
datasource3 = NamedDataSource.get(2 + 3)
|
||||
datasource4 = NamedDataSource.get(2 + 4)
|
||||
assert datasource2.name == 'Events B'
|
||||
assert datasource2.slug == 'wrong_again'
|
||||
assert datasource2.external == 'agenda'
|
||||
assert datasource2.external_status is None
|
||||
assert datasource2.data_source == {
|
||||
|
@ -307,6 +335,7 @@ def test_build_agenda_datasources(mock_collect, pub, chrono_url):
|
|||
'value': 'http://chrono.example.net/api/agenda/events-B/datetimes/',
|
||||
}
|
||||
assert datasource3.name == 'Events A'
|
||||
assert datasource3.slug == 'chrono_ds_sluga'
|
||||
assert datasource3.external == 'agenda'
|
||||
assert datasource3.external_status == 'not-found'
|
||||
assert datasource3.data_source == {
|
||||
|
@ -314,6 +343,7 @@ def test_build_agenda_datasources(mock_collect, pub, chrono_url):
|
|||
'value': 'http://chrono.example.net/api/agenda/events-FOOBAR/datetimes/',
|
||||
}
|
||||
assert datasource4.name == 'Events A'
|
||||
assert datasource4.slug == 'chrono_ds_sluga_1'
|
||||
assert datasource4.external == 'agenda'
|
||||
assert datasource4.external_status is None
|
||||
assert datasource4.data_source == {
|
||||
|
|
|
@ -547,8 +547,8 @@ class NamedDataSource(XmlStorableObject):
|
|||
if get_publisher().snapshot_class:
|
||||
get_publisher().snapshot_class.snap(instance=self, comment=comment)
|
||||
|
||||
def get_new_slug(self):
|
||||
new_slug = misc.simplify(self.name, space='_')
|
||||
def get_new_slug(self, base=None):
|
||||
new_slug = misc.simplify(base or self.name, space='_')
|
||||
base_new_slug = new_slug
|
||||
suffix_no = 0
|
||||
while True:
|
||||
|
@ -870,10 +870,20 @@ def collect_agenda_data(publisher):
|
|||
agenda_data = []
|
||||
for agenda in result.get('data') or []:
|
||||
if agenda['kind'] == 'events':
|
||||
agenda_data.append({'text': agenda['text'], 'url': agenda['api']['datetimes_url']})
|
||||
agenda_data.append(
|
||||
{
|
||||
'slug': 'agenda-%s-%s' % (agenda['kind'], agenda['id']),
|
||||
'text': agenda['text'],
|
||||
'url': agenda['api']['datetimes_url'],
|
||||
}
|
||||
)
|
||||
elif agenda['kind'] in ['meetings', 'virtual']:
|
||||
agenda_data.append(
|
||||
{'text': _('%s - Meeting types') % agenda['text'], 'url': agenda['api']['meetings_url']}
|
||||
{
|
||||
'slug': 'agenda-%s-%s-meetingtypes' % (agenda['kind'], agenda['id']),
|
||||
'text': _('%s - Meeting types') % agenda['text'],
|
||||
'url': agenda['api']['meetings_url'],
|
||||
}
|
||||
)
|
||||
# get also meeting types
|
||||
mt_url = chrono_url(publisher, 'api/agenda/%s/meetings/' % agenda['id'])
|
||||
|
@ -883,6 +893,7 @@ def collect_agenda_data(publisher):
|
|||
for meetingtype in mt_results.get('data') or []:
|
||||
agenda_data.append(
|
||||
{
|
||||
'slug': 'agenda-%s-%s-mt-%s' % (agenda['kind'], agenda['id'], meetingtype['id']),
|
||||
'text': _('%s - Slots of type %s') % (agenda['text'], meetingtype['text']),
|
||||
'url': meetingtype['api']['datetimes_url'],
|
||||
}
|
||||
|
@ -912,6 +923,7 @@ def build_agenda_datasources(publisher):
|
|||
datasource = existing_datasources.get(url)
|
||||
if datasource is None:
|
||||
datasource = NamedDataSource()
|
||||
datasource.slug = datasource.get_new_slug('chrono_ds_%s' % agenda['slug'])
|
||||
datasource.external = 'agenda'
|
||||
datasource.data_source = {'type': 'json', 'value': url}
|
||||
datasource.external_status = None # reset
|
||||
|
|
Loading…
Reference in New Issue