|
| 1 | +# coding: utf-8 |
| 2 | +from __future__ import (unicode_literals, print_function, |
| 3 | + absolute_import, division) |
| 4 | + |
| 5 | +import io |
| 6 | +import itertools |
| 7 | +from collections import OrderedDict |
| 8 | + |
| 9 | +from .xls_to_ss_structure import xls_to_dicts |
| 10 | +from formpack.constants import ( |
| 11 | + KOBO_LOCKING_RESTRICTIONS, |
| 12 | + KOBO_LOCK_COLUMN, |
| 13 | + KOBO_LOCK_KEY, |
| 14 | + KOBO_LOCK_SHEET, |
| 15 | +) |
| 16 | +from formpack.utils.exceptions import FormPackLibraryLockingError |
| 17 | + |
| 18 | +def get_kobo_locking_profiles(xls_file_object: io.BytesIO) -> list: |
| 19 | + """ |
| 20 | + Return the locking profiles, if there are any, in a dictionary structure |
| 21 | + from an XLSForm matrix. For example, the following matrix structure: |
| 22 | +
|
| 23 | + # kobo--locking-profiles |
| 24 | + | restriction | profile_1 | profile_2 | |
| 25 | + |-------------------|-----------|-----------| |
| 26 | + | choice_add | locked | | |
| 27 | + | choice_delete | | locked | |
| 28 | + | choice_label_edit | locked | | |
| 29 | + | choice_order_edit | locked | locked | |
| 30 | +
|
| 31 | + Will be transformed into the following JSON structure: |
| 32 | + [ |
| 33 | + { |
| 34 | + "name": "profile_1", |
| 35 | + "restrictions": [ |
| 36 | + "choice_add", |
| 37 | + "choice_label_edit", |
| 38 | + "choice_order_edit" |
| 39 | + ], |
| 40 | + }, |
| 41 | + { |
| 42 | + "name": "profile_2", |
| 43 | + "restrictions": [ |
| 44 | + "choice_delete", |
| 45 | + "choice_order_edit" |
| 46 | + ], |
| 47 | + } |
| 48 | + ] |
| 49 | + """ |
| 50 | + survey_dict = xls_to_dicts(xls_file_object) |
| 51 | + |
| 52 | + if KOBO_LOCK_SHEET not in survey_dict: |
| 53 | + return |
| 54 | + |
| 55 | + locks = survey_dict[KOBO_LOCK_SHEET] |
| 56 | + |
| 57 | + # Get a unique list of profile names |
| 58 | + profiles = set() |
| 59 | + for lock in locks: |
| 60 | + profiles.update(lock.keys()) |
| 61 | + |
| 62 | + # So some basic validation of locking profiles |
| 63 | + profiles = _validate_locking_profiles(profiles) |
| 64 | + |
| 65 | + # Set up an indexed dictionary for convenience -- return only its values |
| 66 | + locking_profiles = { |
| 67 | + name: dict(name=name, restrictions=[]) for name in profiles |
| 68 | + } |
| 69 | + |
| 70 | + for lock in locks: |
| 71 | + restriction = lock.get('restriction') |
| 72 | + # ensure that valid lock values are being used |
| 73 | + if restriction not in KOBO_LOCKING_RESTRICTIONS: |
| 74 | + raise FormPackLibraryLockingError( |
| 75 | + f'{restriction} is not a valid restriction.' |
| 76 | + ) |
| 77 | + for name in profiles: |
| 78 | + if lock.get(name, '').lower() == KOBO_LOCK_KEY: |
| 79 | + locking_profiles[name]['restrictions'].append(restriction) |
| 80 | + |
| 81 | + return list(locking_profiles.values()) |
| 82 | + |
| 83 | +def revert_kobo_lock_structure(content: dict) -> None: |
| 84 | + """ |
| 85 | + Revert the structure of the locks to one that is ready to be exported into |
| 86 | + an XLSForm again -- the reverse of `get_kobo_locking_profiles` |
| 87 | +
|
| 88 | + It is essentially a preprocessor used within KPI before converting all the |
| 89 | + sheets and content to OrderedDicts and exporting to XLS. |
| 90 | +
|
| 91 | + For example, this JSON structure: |
| 92 | + [ |
| 93 | + { |
| 94 | + "name": "profile_1", |
| 95 | + "restrictions": [ |
| 96 | + "choice_add", |
| 97 | + "choice_label_edit", |
| 98 | + "choice_order_edit" |
| 99 | + ], |
| 100 | + }, |
| 101 | + { |
| 102 | + "name": "profile_2", |
| 103 | + "restrictions": [ |
| 104 | + "choice_delete", |
| 105 | + "choice_order_edit" |
| 106 | + ], |
| 107 | + } |
| 108 | + ] |
| 109 | +
|
| 110 | + Will be transformed into: |
| 111 | + [ |
| 112 | + { |
| 113 | + 'restriction': 'choice_add', |
| 114 | + 'profile_1': 'locked', |
| 115 | + }, |
| 116 | + { |
| 117 | + 'restriction': 'choice_label_edit', |
| 118 | + 'profile_1': 'locked', |
| 119 | + }, |
| 120 | + { |
| 121 | + 'restriction': 'choice_order_edit', |
| 122 | + 'profile_1': 'locked', |
| 123 | + 'profile_2': 'locked', |
| 124 | + }, |
| 125 | + { |
| 126 | + 'restriction': 'choice_delete', |
| 127 | + 'profile_2': 'locked', |
| 128 | + }, |
| 129 | + ] |
| 130 | + """ |
| 131 | + if KOBO_LOCK_SHEET not in content: |
| 132 | + return |
| 133 | + locking_profiles = [] |
| 134 | + for res in KOBO_LOCKING_RESTRICTIONS: |
| 135 | + profile = {'restriction': res} |
| 136 | + for item in content[KOBO_LOCK_SHEET]: |
| 137 | + name = item['name'] |
| 138 | + restrictions = item['restrictions'] |
| 139 | + if res in restrictions: |
| 140 | + profile[name] = KOBO_LOCK_KEY |
| 141 | + locking_profiles.append(profile) |
| 142 | + content[KOBO_LOCK_SHEET] = locking_profiles |
| 143 | + |
| 144 | +def strip_kobo_locking_profile(content: OrderedDict) -> None: |
| 145 | + """ |
| 146 | + Strip all `kobo--locking-profile` values from a survey. Used when creating |
| 147 | + blocks or adding questions to the library from a locked survey or template. |
| 148 | + The locks should only be applied on survey and template types. |
| 149 | + """ |
| 150 | + survey = content.get('survey') |
| 151 | + for item in survey: |
| 152 | + if KOBO_LOCK_COLUMN in item: |
| 153 | + item.pop(KOBO_LOCK_COLUMN) |
| 154 | + |
| 155 | +def _validate_locking_profiles(profiles): |
| 156 | + """ |
| 157 | + Some simple validation of the locking profiles to provide helpful error |
| 158 | + messages to the user |
| 159 | + """ |
| 160 | + if 'restriction' not in profiles: |
| 161 | + raise FormPackLibraryLockingError( |
| 162 | + 'The column name `restriction` must be present.' |
| 163 | + ) |
| 164 | + |
| 165 | + # Remove the `restriction` column header from the list to only have the |
| 166 | + # user-defined profile names |
| 167 | + profiles.remove('restriction') |
| 168 | + |
| 169 | + if not profiles: |
| 170 | + raise FormPackLibraryLockingError( |
| 171 | + 'At least one locking profile must be defined.' |
| 172 | + ) |
| 173 | + |
| 174 | + if KOBO_LOCK_KEY in profiles: |
| 175 | + raise FormPackLibraryLockingError( |
| 176 | + f'Locking profile name of "{KOBO_LOCK_KEY}" cannot be used.' |
| 177 | + ) |
| 178 | + |
| 179 | + return profiles |
0 commit comments