| 88 | |
| 89 | |
| 90 | class InteractiveMigrationQuestioner(MigrationQuestioner): |
| 91 | def __init__( |
| 92 | self, defaults=None, specified_apps=None, dry_run=None, prompt_output=None |
| 93 | ): |
| 94 | super().__init__( |
| 95 | defaults=defaults, specified_apps=specified_apps, dry_run=dry_run |
| 96 | ) |
| 97 | self.prompt_output = prompt_output or OutputWrapper(sys.stdout) |
| 98 | |
| 99 | def _boolean_input(self, question, default=None): |
| 100 | self.prompt_output.write(f"{question} ", ending="") |
| 101 | result = input() |
| 102 | if not result and default is not None: |
| 103 | return default |
| 104 | while not result or result[0].lower() not in "yn": |
| 105 | self.prompt_output.write("Please answer yes or no: ", ending="") |
| 106 | result = input() |
| 107 | return result[0].lower() == "y" |
| 108 | |
| 109 | def _choice_input(self, question, choices): |
| 110 | self.prompt_output.write(f"{question}") |
| 111 | for i, choice in enumerate(choices): |
| 112 | self.prompt_output.write(" %s) %s" % (i + 1, choice)) |
| 113 | self.prompt_output.write("Select an option: ", ending="") |
| 114 | while True: |
| 115 | try: |
| 116 | result = input() |
| 117 | value = int(result) |
| 118 | except ValueError: |
| 119 | pass |
| 120 | except KeyboardInterrupt: |
| 121 | self.prompt_output.write("\nCancelled.") |
| 122 | sys.exit(1) |
| 123 | else: |
| 124 | if 0 < value <= len(choices): |
| 125 | return value |
| 126 | self.prompt_output.write("Please select a valid option: ", ending="") |
| 127 | |
| 128 | def _ask_default(self, default=""): |
| 129 | """ |
| 130 | Prompt for a default value. |
| 131 | |
| 132 | The ``default`` argument allows providing a custom default value (as a |
| 133 | string) which will be shown to the user and used as the return value |
| 134 | if the user doesn't provide any other input. |
| 135 | """ |
| 136 | self.prompt_output.write("Please enter the default value as valid Python.") |
| 137 | if default: |
| 138 | self.prompt_output.write( |
| 139 | f"Accept the default '{default}' by pressing 'Enter' or " |
| 140 | f"provide another value." |
| 141 | ) |
| 142 | self.prompt_output.write( |
| 143 | "The datetime and django.utils.timezone modules are available, so " |
| 144 | "it is possible to provide e.g. timezone.now as a value." |
| 145 | ) |
| 146 | self.prompt_output.write("Type 'exit' to exit this prompt") |
| 147 | while True: |
no outgoing calls