90 lines
3.0 KiB
Python
90 lines
3.0 KiB
Python
# lingo - payment and billing system
|
|
# Copyright (C) 2022 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/>.
|
|
|
|
import collections
|
|
|
|
from django.db import transaction
|
|
|
|
from lingo.agendas.models import Agenda, CheckTypeGroup
|
|
from lingo.pricing.models import AgendaPricing, CriteriaCategory, Pricing
|
|
|
|
|
|
def export_site(
|
|
agendas=True,
|
|
check_type_groups=True,
|
|
pricing_categories=True,
|
|
pricing_models=True,
|
|
pricings=True,
|
|
):
|
|
'''Dump site objects to JSON-dumpable dictionnary'''
|
|
data = collections.OrderedDict()
|
|
if pricings:
|
|
data['pricings'] = [x.export_json() for x in AgendaPricing.objects.all()]
|
|
if pricing_models:
|
|
data['pricing_models'] = [x.export_json() for x in Pricing.objects.all()]
|
|
if pricing_categories:
|
|
data['pricing_categories'] = [x.export_json() for x in CriteriaCategory.objects.all()]
|
|
if check_type_groups:
|
|
data['check_type_groups'] = [x.export_json() for x in CheckTypeGroup.objects.all()]
|
|
if agendas:
|
|
data['agendas'] = [x.export_json() for x in Agenda.objects.all()]
|
|
return data
|
|
|
|
|
|
def import_site(data, if_empty=False, clean=False, overwrite=False):
|
|
if if_empty and (
|
|
AgendaPricing.objects.exists()
|
|
or CheckTypeGroup.objects.exists()
|
|
or CriteriaCategory.objects.exists()
|
|
or Pricing.objects.exists()
|
|
):
|
|
return
|
|
|
|
if clean:
|
|
AgendaPricing.objects.all().delete()
|
|
CriteriaCategory.objects.all().delete()
|
|
Pricing.objects.all().delete()
|
|
CheckTypeGroup.objects.all().delete()
|
|
|
|
results = {
|
|
key: collections.defaultdict(list)
|
|
for key in [
|
|
'agendas',
|
|
'check_type_groups',
|
|
'pricing_categories',
|
|
'pricing_models',
|
|
'pricings',
|
|
]
|
|
}
|
|
|
|
with transaction.atomic():
|
|
for cls, key in (
|
|
(CriteriaCategory, 'pricing_categories'),
|
|
(Pricing, 'pricing_models'),
|
|
(CheckTypeGroup, 'check_type_groups'),
|
|
(Agenda, 'agendas'),
|
|
(AgendaPricing, 'pricings'),
|
|
):
|
|
objs = data.get(key, [])
|
|
for obj in objs:
|
|
created, obj = cls.import_json(obj, overwrite=overwrite)
|
|
results[key]['all'].append(obj)
|
|
if created:
|
|
results[key]['created'].append(obj)
|
|
else:
|
|
results[key]['updated'].append(obj)
|
|
return results
|