def handle(self, *args, **options): helpers.debug_with_pdb(**options) os.environ['DJANGO_COLORS'] = 'nocolor' commands_buffer = StringIO() for app in apps.get_app_configs(): call_command('sqlsequencereset', app.label, stdout=commands_buffer) commands = commands_buffer.getvalue() message = textwrap.dedent("""\ You have requested to reset sequences for all Django apps. The following commands will be run: {} Are you sure you want to do this? Type 'yes' to continue, or 'no' to cancel:\ """).format(textwrap.indent(commands, '\t')) if options['interactive'] and input(message) != "yes": raise CommandError("Resetting sequences cancelled.") cursor = connection.cursor() cursor.execute(commands)
def handle(self, *args, **options): helpers.debug_with_pdb(**options) # Parse file contents data = yaml.load(options['infile']) if not data: raise CommandError("Could not load announcement file") # Get window window_basename = data['window'] try: data['window'] = Window.objects.get(codename=window_basename) except Window.DoesNotExist as err: self.stderr.write( "No window with codename {!r} found".format(window_basename)) raise err # Extract problems if 'problems' in data: problems = data['problems'] del data['problems'] else: problems = () # Check whether ID is already used id = data.get('id', None) if id and Announcement.objects.filter(id=id).exists(): raise CommandError( "An existing announcement with the same ID {!r} exists".format(id)) announcement = Announcement(**data) try: with transaction.atomic(): try: announcement.save() except ValidationError as err: self.stderr.write("Validation failed") raise err # Associate with problems announcement.problems.clear() for problem in problems: announcement.problems.add(CtfProblem.objects.get(id=problem)) announcement.validate_windows() except: self.stderr.write("Exception encountered; rolled back transaction") raise # Mark this announcement as unread by competitors # (This is outside the transaction manager as it is unlikely to fail # but might take some time.) announcement.competitors.add(*Competitor.objects.all()) self.stdout.write('Successfully created announcement: {}'.format(announcement))
def handle(self, **options): helpers.debug_with_pdb(**options) try: with transaction.atomic(): management.call_command('flush', *helpers.filter_dict({ '--no-input': not options['interactive'], })) management.call_command('makemigrations') management.call_command('migrate') for fixture in PRE_PROBLEMS_FIXTURES: self.load_fixture(fixture) # (loadprobs will call collectstatic.) management.call_command('loadprobs', *helpers.filter_dict({ '--no-input': not options['interactive'], '--debug': options['debug'], '--clear': options['clear'] })) self.stdout.write('') if options['skiplater']: return for fixture in POST_PROBLEMS_FIXTURES: self.load_fixture(fixture) announcements_dir = join(BASE_DIR, 'announcements') for basename in glob.glob(os.path.join(announcements_dir, '*.yaml')): print(basename) path = join(announcements_dir, basename) if isfile(path): management.call_command('announce', path) except helpers.ForeseenCommandError as err: raise err except Exception as err: self.stderr.write("Unforeseen exception encountered; rolling back") raise CommandError(err) else: self.stdout.write("Successfully (re)loaded all fixtures and problems")
def handle(self, **options): helpers.debug_with_pdb(**options) self.stdout.write("Beginning transaction\n") try: with transaction.atomic(): management.call_command('flush', *helpers.filter_dict({ '--no-input': not options['interactive'], })) management.call_command('migrate') for fixture in PRE_PROBLEMS_FIXTURES: self.load_fixture(fixture) # (loadprobs will call collectstatic.) management.call_command('loadprobs', *helpers.filter_dict({ '--no-input': not options['interactive'], '--debug': options['debug'], '--clear': options['clear'] })) self.stdout.write('') if not options['skiplater']: for fixture in POST_PROBLEMS_FIXTURES: self.load_fixture(fixture) announcements_dir = join(BASE_DIR, 'announcements') for basename in glob.glob(os.path.join(announcements_dir, '*.yaml')): print(basename) path = join(announcements_dir, basename) if isfile(path): management.call_command('announce', path) except Exception as err: self.stderr.write("Unforeseen exception encountered; rolled back transaction") raise CommandError(err) else: self.stdout.write("Successfully (re)loaded all fixtures and problems")
def handle(self, **options): write = self.stdout.write self.processed_problems = [] # Initialize error handling self.errored = False self.debug = options[helpers.DEBUG_OPTION_NAME] helpers.debug_with_pdb(**options) # Delete any existing files after confirmation if isdir(PROBLEMS_STATIC_DIR): message = textwrap.dedent("""\ You have requested to load problems into the database and collect static files to the intermediate location as specified in your settings: {} This will DELETE ALL FILES in this location! Are you sure you want to do this? Type 'yes' to continue, or 'no' to cancel:\ """.format(PROBLEMS_STATIC_DIR)) if options['interactive'] and input(message) != "yes": raise CommandError("Loading problems cancelled.") write("Deleting all files in the intermediate location\n\n") shutil.rmtree(PROBLEMS_STATIC_DIR) os.makedirs(PROBLEMS_STATIC_DIR, exist_ok=True) # Load problems for window_basename, window_path in self.walk(PROBLEMS_DIR): for prob_basename, prob_path in self.walk(window_path): self.process_problem_folder(window_basename=window_basename, prob_basename=prob_basename, prob_path=prob_path) # Stop if errors were encountered if self.errored: write("") raise CommandError( "Exception(s) were encountered; database was not modified") # Collect all static files to final location write("") write("Collecting static files to final location") management.call_command( 'collectstatic', *helpers.filter_dict({ '--no-input': not options['interactive'], # '--clear': options['clear'], })) self.stdout.write("Beginning transaction to actually save problems\n") try: # Actually load problems with transaction.atomic(): print(self.processed_problems) for problem in self.processed_problems: print("Saving {} to window {}".format( problem, problem.window)) problem.save() # Delete unprocessed problems self.delete_unprocessed(options) except Exception as err: self.stderr.write( "Unforeseen exception encountered while saving problems; rolled back transaction" ) raise CommandError(err)
def handle(self, **options): write = self.stdout.write self.processed_problems = [] # Initialize error handling self.errored = False self.debug = options[helpers.DEBUG_OPTION_NAME] helpers.debug_with_pdb(**options) # Delete any existing files after confirmation if isdir(PROBLEMS_STATIC_DIR): message = textwrap.dedent("""\ You have requested to load problems into the database and collect static files to the intermediate location as specified in your settings: {} This will DELETE ALL FILES in this location! Are you sure you want to do this? Type 'yes' to continue, or 'no' to cancel:\ """.format(PROBLEMS_STATIC_DIR)) if options['interactive'] and input(message) != "yes": raise CommandError("Loading problems cancelled.") write("Deleting all files in the intermediate location\n\n") shutil.rmtree(PROBLEMS_STATIC_DIR) os.makedirs(PROBLEMS_STATIC_DIR, exist_ok=True) # Load problems for window_basename, window_path in self.walk(PROBLEMS_DIR): for prob_basename, prob_path in self.walk(window_path): self.process_problem_folder( window_basename=window_basename, prob_basename=prob_basename, prob_path=prob_path ) # Stop if errors were encountered if self.errored: write("") raise CommandError("Exception(s) were encountered; database was not modified") # Collect all static files to final location write("") write("Collecting static files to final location") management.call_command('collectstatic', *helpers.filter_dict({ '--no-input': not options['interactive'], # '--clear': options['clear'], })) self.stdout.write("Beginning transaction to actually save problems\n") try: # Actually load problems with transaction.atomic(): print(self.processed_problems) for problem in self.processed_problems: print("Saving {} to window {}".format(problem, problem.window)) problem.save() # Delete unprocessed problems self.delete_unprocessed(options) except Exception as err: self.stderr.write("Unforeseen exception encountered while saving problems; rolled back transaction") raise CommandError(err)