def test_filetracker_to_django_field(self): data = 'eloziom' path = 'my/path' abspath = '/' + path storage = default_storage try: self.assertEqual(storage.save(path, ContentFile(data)), path) model = TestFileModel() # File field is ignoring preferred name, as we can't copy file # in filetracker to another location with self.assertRaises(NotImplementedError): model.file_field.save( 'xx', filetracker_to_django_file(abspath, storage)) model.file_field = filetracker_to_django_file(abspath, storage) model.save() self.assertEqual(model.file_field.name, path) pk = model.pk # Here the model is removed from Django's cache, so the query # below actually hits the database. del model model = TestFileModel.objects.get(pk=pk) self.assertEqual(model.file_field.name, path) self.assertEqual(django_to_filetracker_path(model.file_field), abspath) self.assertEqual(model.file_field.read(), data) finally: default_storage.delete(path)
def test_filetracker_to_django_field(self): data = 'eloziom' path = 'my/path' abspath = '/' + path storage = default_storage try: self.assertEqual(storage.save(path, ContentFile(data)), path) model = TestFileModel() # File field is ignoring preferred name, as we can't copy file # in filetracker to another location with self.assertRaises(NotImplementedError): model.file_field.save('xx', filetracker_to_django_file(abspath, storage)) model.file_field = filetracker_to_django_file(abspath, storage) model.save() self.assertEqual(model.file_field.name, path) pk = model.pk # Here the model is removed from Django's cache, so the query # below actually hits the database. del model model = TestFileModel.objects.get(pk=pk) self.assertEqual(model.file_field.name, path) self.assertEqual(django_to_filetracker_path(model.file_field), abspath) self.assertEqual(model.file_field.read(), data) finally: default_storage.delete(path)
def test_django_to_filetracker_path(self): storage = FiletrackerStorage(prefix='/foo', client=DummyClient()) field = FileField(storage=storage) value = FieldFile(None, field, 'bar') self.assertEqual(django_to_filetracker_path(value), '/foo/bar') with self.assertRaises(ValueError): django_to_filetracker_path(ContentFile('whatever', name='gizmo')) self.assertEqual('/foo/bar', django_to_filetracker_path( filetracker_to_django_file('/foo/bar', storage=storage)))
def fill_outfile_in_existing_test_reports(env, **kwargs): """Fill output files into existing test reports that are not directly related to present submission. Also change status of UserOutGenStatus object to finished. Used ``environ`` keys: * ``extra_args`` dictionary with ``submission_report`` object * ``test_results`` """ if 'submission_report_id' not in env['extra_args']: logger.info('No submission_report given to fill tests outputs') return env submission_report_id = env['extra_args']['submission_report_id'] submission_report = SubmissionReport.objects.get(id=submission_report_id) test_reports = TestReport.objects.filter( submission_report=submission_report) test_results = env.get('test_results', {}) for test_name, result in six.iteritems(test_results): try: testreport = test_reports.get(test_name=test_name) except (TestReport.DoesNotExist, TestReport.MultipleObjectsReturned): logger.warning('Test report for test: %s can not be determined', test_name) continue if testreport.output_file: logger.warning( 'Output for test report %s exists. Deleting old one.', testreport.id) get_client().delete_file(testreport.output_file) testreport.output_file = filetracker_to_django_file(result['out_file']) testreport.save() try: download_controller = UserOutGenStatus.objects.get( testreport=testreport) except UserOutGenStatus.DoesNotExist: download_controller = UserOutGenStatus(testreport=testreport) download_controller.status = 'OK' download_controller.save() return env
def fill_outfile_in_existing_test_reports(env, **kwargs): """Fill output files into existing test reports that are not directly related to present submission. Also change status of UserOutGenStatus object to finished. Used ``environ`` keys: * ``extra_args`` dictionary with ``submission_report`` object * ``test_results`` """ if 'submission_report_id' not in env['extra_args']: logger.info('No submission_report given to fill tests outputs') return env submission_report_id = env['extra_args']['submission_report_id'] submission_report = SubmissionReport.objects.get(id=submission_report_id) test_reports = TestReport.objects.filter( submission_report=submission_report) test_results = env.get('test_results', {}) for test_name, result in test_results.iteritems(): try: testreport = test_reports.get(test_name=test_name) except (TestReport.DoesNotExist, TestReport.MultipleObjectsReturned): logger.warn('Test report for test: %s can not be determined', test_name) continue if testreport.output_file: logger.warn('Output for test report %s exists. Deleting old one.', testreport.id) get_client().delete_file(testreport.output_file) testreport.output_file = filetracker_to_django_file(result['out_file']) testreport.save() try: download_controller = UserOutGenStatus.objects.get( testreport=testreport) except UserOutGenStatus.DoesNotExist: download_controller = UserOutGenStatus(testreport=testreport) download_controller.status = 'OK' download_controller.save() return env
def make_report(env, **kwargs): """Builds entities for testrun reports in a database. Used ``environ`` keys: * ``tests`` * ``test_results`` * ``status`` * ``score`` * ``compilation_result`` * ``compilation_message`` Produced ``environ`` keys: * ``report_id``: id of the produced :class:`~oioioi.contests.models.SubmissionReport` """ _submission, submission_report = _make_base_report(env, 'TESTRUN') if env['compilation_result'] != 'OK': return env test = env['tests']['test'] test_result = env['test_results']['test'] comment = test_result.get('result_string', '') if comment.lower() == 'ok': # Annoying comment = '' testrun_report = TestRunReport(submission_report=submission_report) testrun_report.status = env['status'] testrun_report.comment = \ slice_str(comment, TestRunReport._meta .get_field('comment').max_length) testrun_report.time_used = test_result['time_used'] testrun_report.test_time_limit = test.get('exec_time_limit') testrun_report.output_file = filetracker_to_django_file( test_result['out_file']) testrun_report.save() return env
def make_report(env, **kwargs): """Builds entities for testrun reports in a database. Used ``environ`` keys: * ``tests`` * ``test_results`` * ``status`` * ``score`` * ``compilation_result`` * ``compilation_message`` Produced ``environ`` keys: * ``report_id``: id of the produced :class:`~oioioi.contests.models.SubmissionReport` """ submission, submission_report = _make_base_report(env, 'TESTRUN') if env['compilation_result'] != 'OK': return env test = env['tests']['test'] test_result = env['test_results']['test'] comment = test_result.get('result_string', '') if comment.lower() == 'ok': # Annoying comment = '' testrun_report = TestRunReport(submission_report=submission_report) testrun_report.status = env['status'] testrun_report.comment = \ slice_str(comment, TestRunReport._meta .get_field('comment').max_length) testrun_report.time_used = test_result['time_used'] testrun_report.test_time_limit = test.get('exec_time_limit') testrun_report.output_file = filetracker_to_django_file( test_result['out_file']) testrun_report.save() return env
def _save_to_field(self, field, file): basename = os.path.basename(filetracker_to_django_file(file).name) filename = os.path.join(self.rootdir, basename) get_client().get_file(file, filename) field.save(os.path.basename(filename), File(open(filename, 'rb'))) get_client().delete_file(file)
def make_report(env, kind='NORMAL', save_scores=True, **kwargs): """Builds entities for tests results in a database. Used ``environ`` keys: * ``tests`` * ``test_results`` * ``group_results`` * ``status`` * ``score`` * ``compilation_result`` * ``compilation_message`` * ``submission_id`` Produced ``environ`` keys: * ``report_id``: id of the produced :class:`~oioioi.contests.models.SubmissionReport` """ submission, submission_report = _make_base_report(env, kind) if env['compilation_result'] != 'OK': return env tests = env['tests'] test_results = env.get('test_results', {}) for test_name, result in six.iteritems(test_results): test = tests[test_name] if 'report_id' in result: continue test_report = TestReport(submission_report=submission_report) test_report.test_id = test.get('id') test_report.test_name = test_name test_report.test_group = test['group'] test_report.test_time_limit = test.get('exec_time_limit') test_report.max_score = result['max_score'] test_report.score = result['score'] if save_scores else None test_report.status = result['status'] test_report.time_used = result['time_used'] comment = result.get('result_string', '') if comment.lower() in ['ok', 'time limit exceeded']: # Annoying comment = '' test_report.comment = Truncator(comment).chars( TestReport._meta.get_field('comment').max_length) if env.get('save_outputs', False): test_report.output_file = filetracker_to_django_file( result['out_file']) test_report.save() result['report_id'] = test_report.id group_results = env.get('group_results', {}) for group_name, group_result in six.iteritems(group_results): if 'report_id' in group_result: continue group_report = GroupReport(submission_report=submission_report) group_report.group = group_name group_report.score = group_result['score'] if save_scores else None group_report.max_score = \ group_result['max_score'] if save_scores else None group_report.status = group_result['status'] group_report.save() group_result['result_id'] = group_report.id if kind == 'INITIAL': if submission.user is not None and not env.get('is_rejudge', False): logger.info( "Submission %(submission_id)d by user %(username)s" " for problem %(short_name)s got initial result.", { 'submission_id': submission.pk, 'username': submission.user.username, 'short_name': submission.problem_instance.short_name }, extra={ 'notification': 'initial_results', 'user': submission.user, 'submission': submission }) return env
def make_report(env, kind='NORMAL', save_scores=True, **kwargs): """Builds entities for tests results in a database. Used ``environ`` keys: * ``tests`` * ``test_results`` * ``group_results`` * ``status`` * ``score`` * ``compilation_result`` * ``compilation_message`` * ``submission_id`` Produced ``environ`` keys: * ``report_id``: id of the produced :class:`~oioioi.contests.models.SubmissionReport` """ submission, submission_report = _make_base_report(env, kind) if env['compilation_result'] != 'OK': return env tests = env['tests'] test_results = env.get('test_results', {}) for test_name, result in test_results.iteritems(): test = tests[test_name] if 'report_id' in result: continue test_report = TestReport(submission_report=submission_report) test_report.test_id = test.get('id') test_report.test_name = test_name test_report.test_group = test['group'] test_report.test_time_limit = test.get('exec_time_limit') test_report.test_max_score = test['max_score'] test_report.score = result['score'] if save_scores else None test_report.status = result['status'] test_report.time_used = result['time_used'] comment = result.get('result_string', '') if comment.lower() in ['ok', 'time limit exceeded']: # Annoying comment = '' test_report.comment = Truncator(comment).chars(TestReport. _meta.get_field('comment').max_length) if env.get('save_outputs', False): test_report.output_file = filetracker_to_django_file( result['out_file']) test_report.save() result['report_id'] = test_report.id group_results = env.get('group_results', {}) for group_name, group_result in group_results.iteritems(): if 'report_id' in group_result: continue group_report = GroupReport(submission_report=submission_report) group_report.group = group_name group_report.score = group_result['score'] if save_scores else None group_report.max_score = \ group_result['max_score'] if save_scores else None group_report.status = group_result['status'] group_report.save() group_result['result_id'] = group_report.id if kind == 'INITIAL': if submission.user is not None and not env.get('is_rejudge', False): logger.info("Submission %(submission_id)d by user %(username)s" " for problem %(short_name)s got initial result.", {'submission_id': submission.pk, 'username': submission.user.username, 'short_name': submission.problem_instance .short_name}, extra={'notification': 'initial_results', 'user': submission.user, 'submission': submission}) return env
def make_report(env, kind="NORMAL", save_scores=True, **kwargs): """Builds entities for tests results in a database. Used ``environ`` keys: * ``tests`` * ``test_results`` * ``group_results`` * ``status`` * ``score`` * ``compilation_result`` * ``compilation_message`` * ``submission_id`` Produced ``environ`` keys: * ``report_id``: id of the produced :class:`~oioioi.contests.models.SubmissionReport` """ submission, submission_report = _make_base_report(env, kind) if env["compilation_result"] != "OK": return env tests = env["tests"] test_results = env.get("test_results", {}) for test_name, result in test_results.iteritems(): test = tests[test_name] if "report_id" in result: continue test_report = TestReport(submission_report=submission_report) test_report.test_id = test.get("id") test_report.test_name = test_name test_report.test_group = test["group"] test_report.test_time_limit = test.get("exec_time_limit") test_report.test_max_score = test["max_score"] test_report.score = result["score"] if save_scores else None test_report.status = result["status"] test_report.time_used = result["time_used"] comment = result.get("result_string", "") if comment.lower() in ["ok", "time limit exceeded"]: # Annoying comment = "" test_report.comment = Truncator(comment).chars(TestReport._meta.get_field("comment").max_length) if env.get("save_outputs", False): test_report.output_file = filetracker_to_django_file(result["out_file"]) test_report.save() result["report_id"] = test_report.id group_results = env.get("group_results", {}) for group_name, group_result in group_results.iteritems(): if "report_id" in group_result: continue group_report = GroupReport(submission_report=submission_report) group_report.group = group_name group_report.score = group_result["score"] if save_scores else None group_report.max_score = group_result["max_score"] if save_scores else None group_report.status = group_result["status"] group_report.save() group_result["result_id"] = group_report.id if kind == "INITIAL": if submission.user is not None and not env.get("is_rejudge", False): logger.info( "Submission %(submission_id)d by user %(username)s" " for problem %(short_name)s got initial result.", { "submission_id": submission.pk, "username": submission.user.username, "short_name": submission.problem_instance.short_name, }, extra={"notification": "initial_results", "user": submission.user, "submission": submission}, ) return env