0003-agendas-import-export-settings-exception-source-4796.patch
chrono/agendas/models.py | ||
---|---|---|
1146 | 1146 |
def import_json(cls, data): |
1147 | 1147 |
timeperiods = data.pop('timeperiods', []) |
1148 | 1148 |
exceptions = data.pop('exceptions', []) |
1149 |
sources = data.pop('exception_sources', []) |
|
1149 | 1150 |
data = clean_import_data(cls, data) |
1150 | 1151 |
desk, created = cls.objects.update_or_create(slug=data['slug'], agenda=data['agenda'], defaults=data) |
1151 | 1152 |
for timeperiod in timeperiods: |
... | ... | |
1154 | 1155 |
for exception in exceptions: |
1155 | 1156 |
exception['desk'] = desk |
1156 | 1157 |
TimePeriodException.import_json(exception) |
1158 |
for source in sources: |
|
1159 |
source['desk'] = desk |
|
1160 |
TimePeriodExceptionSource.import_json(source) |
|
1157 | 1161 |
return desk |
1158 | 1162 | |
1159 | 1163 |
def export_json(self): |
1164 |
time_period_exceptions = self.timeperiodexception_set.filter(source__settings_slug__isnull=True) |
|
1165 |
time_period_exception_sources = self.timeperiodexceptionsource_set.filter(settings_slug__isnull=False) |
|
1160 | 1166 |
return { |
1161 | 1167 |
'label': self.label, |
1162 | 1168 |
'slug': self.slug, |
1163 |
'timeperiods': [time_period.export_json() for time_period in self.timeperiod_set.all()], |
|
1164 |
'exceptions': [exception.export_json() for exception in self.timeperiodexception_set.all()], |
|
1169 |
'timeperiods': [time_period.export_json() for time_period in self.timeperiod_set.filter()], |
|
1170 |
'exceptions': [exception.export_json() for exception in time_period_exceptions], |
|
1171 |
'exception_sources': [source.export_json() for source in time_period_exception_sources], |
|
1165 | 1172 |
} |
1166 | 1173 | |
1167 | 1174 |
def duplicate(self, label=None, agenda_target=None): |
... | ... | |
1485 | 1492 |
self.enabled = False |
1486 | 1493 |
self.save() |
1487 | 1494 | |
1495 |
@classmethod |
|
1496 |
def import_json(cls, data): |
|
1497 |
data = clean_import_data(cls, data) |
|
1498 |
source, _ = cls.objects.update_or_create(**data) |
|
1499 |
if source.enabled: |
|
1500 |
source.enable() |
|
1501 |
return source |
|
1502 | ||
1503 |
def export_json(self): |
|
1504 |
'''Export only sources from settings.''' |
|
1505 |
return { |
|
1506 |
'settings_slug': self.settings_slug, |
|
1507 |
'settings_label': self.settings_label, |
|
1508 |
'enabled': self.enabled, |
|
1509 |
} |
|
1510 | ||
1488 | 1511 | |
1489 | 1512 |
class UnavailabilityCalendar(models.Model): |
1490 | 1513 |
label = models.CharField(_('Label'), max_length=150) |
tests/test_import_export.py | ||
---|---|---|
14 | 14 |
import pytest |
15 | 15 |
from django.contrib.auth.models import Group |
16 | 16 |
from django.core.management import call_command, CommandError |
17 |
from django.test import override_settings |
|
17 | 18 |
from django.utils.encoding import force_bytes |
18 | 19 |
from django.utils.timezone import make_aware, now |
19 | 20 | |
... | ... | |
25 | 26 |
Resource, |
26 | 27 |
TimePeriod, |
27 | 28 |
TimePeriodException, |
29 |
TimePeriodExceptionSource, |
|
28 | 30 |
AgendaImportError, |
29 | 31 |
MeetingType, |
30 | 32 |
VirtualMember, |
... | ... | |
531 | 533 | |
532 | 534 |
# again - check OneToOneField |
533 | 535 |
import_site(payload) |
536 | ||
537 | ||
538 |
@override_settings( |
|
539 |
EXCEPTIONS_SOURCES={'holidays': {'class': 'workalendar.europe.France', 'label': 'Holidays'},} |
|
540 |
) |
|
541 |
def test_import_export_time_period_exception_source(): |
|
542 |
agenda = Agenda.objects.create(label='Foo bar', kind='meetings') |
|
543 |
desk = Desk.objects.create(slug='test', agenda=agenda) |
|
544 |
desk.import_timeperiod_exceptions_from_settings(enable=True) |
|
545 | ||
546 |
output = get_output_of_command('export_site') |
|
547 |
payload = json.loads(output) |
|
548 | ||
549 |
agenda.delete() |
|
550 |
assert not TimePeriodExceptionSource.objects.exists() |
|
551 | ||
552 |
import_site(payload) |
|
553 |
desk = Desk.objects.get(slug='test') |
|
554 |
source = desk.timeperiodexceptionsource_set.first() |
|
555 |
assert source.enabled |
|
556 |
assert desk.timeperiodexception_set.exists() |
|
557 | ||
558 |
source.disable() |
|
559 |
output = get_output_of_command('export_site') |
|
560 |
payload = json.loads(output) |
|
561 |
Agenda.objects.all().delete() |
|
562 | ||
563 |
import_site(payload) |
|
564 |
desk = Desk.objects.get(slug='test') |
|
565 |
source = desk.timeperiodexceptionsource_set.first() |
|
566 |
assert not source.enabled |
|
567 |
assert not desk.timeperiodexception_set.exists() |
|
534 |
- |