add support for all kinds of categories (#63943)

This commit is contained in:
Frédéric Péters 2022-04-15 11:01:02 +02:00
parent 1bcdb14e91
commit f94ab37d1f
2 changed files with 36 additions and 16 deletions

4
README
View File

@ -13,7 +13,9 @@ Expected directory layout
* blocks: list of w.c.s. blocks (XML files)
* carddefs: list of w.c.s. cards (XML files)
* category: list of w.c.s. categories (XML files)
* category, carddef_category, workflow_category, block_category, mail_template_category,
data_source_category: list of w.c.s. categories, for their respective objects
(XML files)
* datasources: list of w.c.s. data sources (XML files)
* forms: list of w.c.s. forms (XML files)
* workflows: list of w.c.s. workflows (XML files)

View File

@ -3,7 +3,14 @@ import xml.etree.ElementTree as ET
from wcs.blocks import BlockDef
from wcs.carddef import CardDef
from wcs.categories import Category
from wcs.categories import (
Category,
CardDefCategory,
WorkflowCategory,
BlockCategory,
MailTemplateCategory,
DataSourceCategory,
)
from wcs.data_sources import NamedDataSource
from wcs.formdef import FormDef
from wcs.mail_templates import MailTemplate
@ -41,20 +48,31 @@ class Cmd(Command):
self.import_formdefs()
def import_categories(self):
if not os.path.exists(os.path.join(self.directory, 'category')):
return
for filename in os.listdir(os.path.join(self.directory, 'category')):
category = Category.import_from_xml(open(os.path.join(self.directory, 'category', filename)))
try:
existing_category = Category.get_by_urlname(category.url_name)
except KeyError:
category.store()
except Exception as e:
raise Exception('failed to load existing category %r' % filename) from e
else:
# replace
category.id = existing_category.id
category.store()
for category_klass in (
Category,
CardDefCategory,
WorkflowCategory,
BlockCategory,
MailTemplateCategory,
DataSourceCategory,
):
dirname = category_klass.xml_root_node
if not os.path.exists(os.path.join(self.directory, dirname)):
continue
for filename in os.listdir(os.path.join(self.directory, dirname)):
category = category_klass.import_from_xml(
open(os.path.join(self.directory, dirname, filename))
)
try:
existing_category = category_klass.get_by_urlname(category.url_name)
except KeyError:
category.store()
except Exception as e:
raise Exception('failed to load existing category %r' % filename) from e
else:
# replace
category.id = existing_category.id
category.store()
def import_datasources(self):
if not os.path.exists(os.path.join(self.directory, 'datasources')):