Dump a set of Model objects to a file as text, ask the user to edit it, and apply any changes to the objects. Return a boolean indicating whether the edit succeeded.
(self, objs, fields)
| 221 | self.save_changes(objs) |
| 222 | |
| 223 | def edit_objects(self, objs, fields): |
| 224 | """Dump a set of Model objects to a file as text, ask the user |
| 225 | to edit it, and apply any changes to the objects. |
| 226 | |
| 227 | Return a boolean indicating whether the edit succeeded. |
| 228 | """ |
| 229 | # Get the content to edit as raw data structures. |
| 230 | old_data = [flatten(o, fields) for o in objs] |
| 231 | |
| 232 | # Set up a temporary file with the initial data for editing. |
| 233 | new = NamedTemporaryFile( |
| 234 | mode="w", suffix=".yaml", delete=False, encoding="utf-8" |
| 235 | ) |
| 236 | old_str = dump(old_data) |
| 237 | new.write(old_str) |
| 238 | new.close() |
| 239 | |
| 240 | # Loop until we have parseable data and the user confirms. |
| 241 | try: |
| 242 | while True: |
| 243 | # Ask the user to edit the data. |
| 244 | edit(new.name, self._log) |
| 245 | |
| 246 | # Read the data back after editing and check whether anything |
| 247 | # changed. |
| 248 | with codecs.open(new.name, encoding="utf-8") as f: |
| 249 | new_str = f.read() |
| 250 | if new_str == old_str: |
| 251 | ui.print_("No changes; aborting.") |
| 252 | return False |
| 253 | |
| 254 | # Parse the updated data. |
| 255 | try: |
| 256 | new_data = load(new_str) |
| 257 | except ParseError as e: |
| 258 | ui.print_(f"Could not read data: {e}") |
| 259 | if ui.input_yn("Edit again to fix? (Y/n)", True): |
| 260 | continue |
| 261 | return False |
| 262 | |
| 263 | # Show the changes. |
| 264 | # If the objects are not on the DB yet, we need a copy of their |
| 265 | # original state for show_model_changes. |
| 266 | objs_old = [obj.copy() if obj.id < 0 else None for obj in objs] |
| 267 | self.apply_data(objs, old_data, new_data) |
| 268 | changed = False |
| 269 | for obj, obj_old in zip(objs, objs_old): |
| 270 | changed |= ui.show_model_changes(obj, obj_old) |
| 271 | if not changed: |
| 272 | ui.print_("No changes to apply.") |
| 273 | return False |
| 274 | |
| 275 | # For cancel/keep-editing, restore objects to their original |
| 276 | # in-memory state so temp edits don't leak into the session |
| 277 | choice = ui.input_options( |
| 278 | ("continue Editing", "apply", "cancel") |
| 279 | ) |
| 280 | if choice == "a": # Apply. |