Coverage for ckanext/udc/cli/udc.py: 31%

62 statements  

« prev     ^ index     » next       coverage.py v7.7.1, created at 2026-01-19 23:48 +0000

1import click 

2import ckan.model as model 

3import logging 

4from importlib import import_module 

5import os 

6import polib 

7from ckan.common import config 

8from ckan.lib.i18n import build_js_translations, get_ckan_i18n_dir 

9 

10 

11@click.group(short_help=u"UDC commands.") 

12def udc(): 

13 pass 

14 

15@udc.command() 

16def move_to_catalogues(): 

17 """ 

18 Make all packages have type=catalogue. 

19 This is used when we want to rename 'dataset' to 'catalogue'. 

20 """ 

21 datasets = model.Session.query(model.Package).filter(model.Package.type == "dataset") 

22 nothing_to_do = True 

23 

24 for dataset in datasets: 

25 if dataset.type == 'dataset': 

26 click.echo(f'Update Dataset {dataset.id}: dataset.type: "{dataset.type}" -> "catalogue"') 

27 dataset.type = 'catalogue' 

28 nothing_to_do = False 

29 

30 if nothing_to_do: 

31 click.echo("Nothing to do!") 

32 else: 

33 model.repo.commit_and_remove() 

34 click.echo("Done. Please restart the CKAN instance!") 

35 

36@udc.command() 

37def initdb(): 

38 """ 

39 Initialises the database with the required tables. 

40 """ 

41 log = logging.getLogger(__name__) 

42 

43 model.Session.remove() 

44 model.Session.configure(bind=model.meta.engine) 

45 

46 log.info("Initializing tables") 

47 

48 from ..licenses.model import init_tables 

49 init_tables() 

50 

51 libs = [ 

52 "ckanext.udc_import_other_portals.model", 

53 "ckanext.udc_react.model.organization_access_request", 

54 ] 

55 for lib_str in libs: 

56 try: 

57 lib = import_module(lib_str) 

58 lib.init_tables() 

59 except Exception as e: 

60 print(e) 

61 log.warning(f"Cannot init DB in {lib_str} plugin") 

62 

63 log.info("DB tables initialized") 

64 

65 

66@udc.command() 

67@click.option("--locale", default="fr", show_default=True, help="Locale to override.") 

68@click.option( 

69 "--source", 

70 default=None, 

71 help="Path to override ckan.po (defaults to ckanext-udc i18n).", 

72) 

73@click.option( 

74 "--build-js", 

75 is_flag=True, 

76 default=False, 

77 help="Also rebuild JS translations after copying.", 

78) 

79def override_ckan_translations(locale, source, build_js): 

80 """ 

81 Override CKAN core translations using a plugin-managed ckan.po file. 

82 """ 

83 if not source: 

84 base_dir = os.path.dirname(os.path.dirname(__file__)) 

85 source = os.path.join(base_dir, "i18n", locale, "LC_MESSAGES", "ckan.po") 

86 

87 if not os.path.isfile(source): 

88 raise click.ClickException(f"Source translation not found: {source}") 

89 

90 target_dir = get_ckan_i18n_dir() 

91 dest_dir = os.path.join(target_dir, locale, "LC_MESSAGES") 

92 os.makedirs(dest_dir, exist_ok=True) 

93 

94 dest_po = os.path.join(dest_dir, "ckan.po") 

95 dest_mo = os.path.join(dest_dir, "ckan.mo") 

96 

97 po = polib.pofile(source) 

98 po.save(dest_po) 

99 po.save_as_mofile(dest_mo) 

100 

101 if build_js: 

102 build_js_translations() 

103 

104 click.secho( 

105 f"CKAN translations overridden for locale '{locale}' in {dest_dir}", 

106 fg="green", 

107 bold=True, 

108 )