combo/combo/data/utils.py

77 lines
2.7 KiB
Python

# combo - content management system
# Copyright (C) 2017 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from django.contrib.auth.models import Group
from django.db import transaction
from django.utils.translation import ugettext_lazy as _
from combo.apps.assets.models import Asset
from combo.apps.maps.models import MapLayer
from .models import Page
class MissingGroups(Exception):
def __init__(self, names):
self.names = names
def __unicode__(self):
return _('Missing groups: %s') % ', '.join(self.names)
def export_site():
'''Dump site objects to JSON-dumpable dictionnary'''
return {'pages': Page.export_all_for_json(),
'map-layers': MapLayer.export_all_for_json(),
'assets': Asset.export_all_for_json(),}
def import_site(data, if_empty=False, clean=False):
if isinstance(data, list):
# old export form with a list of pages, convert it to new dictionary
# format.
data = {'pages': data}
if if_empty and (Page.objects.count() or MapLayer.objects.count()):
return
# check groups used in access control are all available.
groups = set()
for page in data.get('pages') or []:
for group in page['fields']['groups']:
groups.add(group if isinstance(group, basestring) else group[0])
for cell in page['cells']:
for group in cell['fields']['groups']:
groups.add(group if isinstance(group, basestring) else group[0])
existing_groups = set([x.name for x in Group.objects.filter(name__in=groups)])
missing_groups = groups - existing_groups
if missing_groups:
raise MissingGroups(names=sorted([x for x in missing_groups]))
if clean:
MapLayer.objects.all().delete()
Asset.objects.all().delete()
Page.objects.all().delete()
with transaction.atomic():
MapLayer.load_serialized_objects(data.get('map-layers') or [])
with transaction.atomic():
Asset.load_serialized_objects(data.get('assets') or [])
with transaction.atomic():
Page.load_serialized_pages(data.get('pages') or [])