From d2d427fd6eac238a3c3200e43b3f201fab284b4b Mon Sep 17 00:00:00 2001 From: Egor Matveev Date: Thu, 2 Dec 2021 18:24:51 +0300 Subject: [PATCH] codestyle --- Main/Tester.py | 275 ---------------------------- Main/management/commands/bot.py | 37 +++- Main/management/commands/receive.py | 18 +- Main/management/commands/storage.py | 2 +- Main/models/extrafile.py | 10 +- Main/models/set.py | 2 +- Main/models/solution.py | 23 +-- Main/models/solution_file.py | 2 +- Main/models/task.py | 8 +- Main/models/token.py | 2 +- Main/views/AccountView.py | 12 +- Main/views/ImageView.py | 5 +- Main/views/SendCodeView.py | 15 +- Main/views/SetSettingsView.py | 4 +- Main/views/SolutionView.py | 2 +- Main/views/SolutionsTableView.py | 15 +- Main/views/TaskRuntimeView.py | 8 +- Main/views/TaskSettingsView.py | 50 ++--- Main/views/TaskView.py | 4 +- SprintLib/language.py | 6 +- SprintLib/queue.py | 6 +- SprintLib/testers/BaseTester.py | 14 +- SprintLib/testers/JavaTester.py | 12 +- SprintLib/testers/KotlinTester.py | 12 +- SprintLib/utils.py | 2 +- 25 files changed, 168 insertions(+), 378 deletions(-) delete mode 100644 Main/Tester.py diff --git a/Main/Tester.py b/Main/Tester.py deleted file mode 100644 index f638e12..0000000 --- a/Main/Tester.py +++ /dev/null @@ -1,275 +0,0 @@ -from os import listdir, mkdir -from os.path import basename, isdir -from shutil import rmtree, copyfile -from threading import Thread -from xml.dom.minidom import parse - -from Main.models import * -from .main import solution_path - - -def start_new(host): - in_queue = list(Solution.objects.filter(result="IN QUEUE")) - if in_queue: - sol = in_queue[0] - for s in in_queue: - dif = ( - s.task.block.priority * 10 - + s.task.priority - - sol.task.block.priority * 10 - - sol.task.priority - ) - if dif > 0: - sol = s - elif dif == 0 and s.id < sol.id: - sol = s - Tester(sol, host).test() - - -def is_project(path): - return any([x.endswith(".csproj") for x in listdir(path)]) - - -def get_node_value(element): - return element[0].firstChild.nodeValue - - -def nunit_path(working_dir): - return "..{}".format(sep) * len( - working_dir.split(sep) - ) + "nunit_console{}nunit3-console.exe".format(sep) - - -class Tester: - def __init__(self, solution, host): - self.solution = solution - self.host = host - self.working_dir = "" - self.files = [] - - # функция компиляции - def build(self, path): - # решение для UNIX - # shell('msbuild ' + path + ' /p:Configuration=Debug') - - # решение для Windows - cmd = "dotnet build {} -o {}\\bin\\Debug".format(path, path) - with self.solution.log_fs as fs: - shell(cmd, fs) - - def build_and_copy(self, path, working_dir): - if exists(join(path, "bin", "Debug")): - rmtree(join(path, "bin", "Debug")) - self.build(path) - name = basename(path) - if not exists(join(path, "bin", "Debug")) or not any( - x.endswith(".exe") for x in listdir(join(path, "bin", "Debug")) - ): - return False - self.files.append(basename(path)) - for file in listdir(join(path, "bin", "Debug")): - if exists(join(path, "bin", "Debug", file)): - new_file = join(working_dir, basename(file)) - try: - copyfile(join(path, "bin", "Debug", file), new_file) - except: - pass - else: - return False - return True - - def push(self): - solution = self.solution - if solution.result == "SOLUTION ERROR": - return - solution.result = "IN QUEUE" - solution.save() - from Main.models import System - - if len(Solution.objects.filter(result="TESTING")) < int( - System.objects.get(key="queue_size").value - ): - self.test() - - def delete_everything(self): - ssp = solution_path(self.solution.path()) - sln_path = join(ssp, ".idea") - if exists(sln_path): - rmtree(sln_path) - sln_path = join(ssp, ".vs") - if exists(sln_path): - rmtree(sln_path) - sln_path = ssp - for p in listdir(sln_path): - if isdir(join(sln_path, p)): - if exists(join(sln_path, p, "bin")): - rmtree(join(sln_path, p, "bin")) - if exists(join(sln_path, p, "obj")): - rmtree(join(sln_path, p, "obj")) - if exists(self.working_dir): - rmtree(self.working_dir) - if exists(join(self.solution.path(), "solution.zip")): - remove(join(self.solution.path(), "solution.zip")) - if exists(join(self.solution.path(), "__MACOSX")): - rmtree(join(self.solution.path(), "__MACOSX")) - if exists(join(sln_path, ".DS_Store")): - remove(join(sln_path, ".DS_Store")) - if exists(join(sln_path, "test_folder")): - rmtree(join(sln_path, "test_folder")) - - def nunit_testing(self): - solution = self.solution - with self.solution.log_fs as fs: - fs.write(b"Building image\n") - shell( - "docker build -t solution_{} {}".format(self.solution.id, self.working_dir) - ) - with self.solution.log_fs as fs: - fs.write(b"Image built successfully\n") - - def execute(): - with self.solution.log_fs as fs: - shell( - "docker run --name solution_container_{} solution_{}".format( - self.solution.id, self.solution.id - ), - output=fs, - ) - - solution.write_log("Running container") - t = Thread(target=execute) - t.start() - t.join(self.solution.task.time_limit / 1000) - solution.write_log("Running finished") - with self.solution.log_fs as fs: - shell( - "docker cp solution_container_{}:/app/TestResults.xml {}".format( - self.solution.id, self.working_dir - ), - fs, - ) - with self.solution.log_fs as fs: - shell( - "docker rm --force solution_container_{}".format(self.solution.id), fs - ) - with self.solution.log_fs as fs: - shell("docker image rm solution_{}".format(self.solution.id), fs) - if not exists(join(self.working_dir, "TestResults.xml")): - self.solution.set_result("Time limit") - solution.write_log("Result file not found in container") - return - solution.write_log("Result file found in container") - try: - doc = parse(join(self.working_dir, "TestResults.xml")) - res = ( - get_node_value(doc.getElementsByTagName("Passed")) - + "/" - + get_node_value(doc.getElementsByTagName("Total")) - ) - self.solution.details = "" - for el in doc.getElementsByTagName("Result"): - self.solution.details += ( - "
" - + get_node_value(el.getElementsByTagName("MethodName")) - + "
" - ) - r = get_node_value(el.getElementsByTagName("Successful")) - if r == "true": - self.solution.details += '
Passed
' - else: - self.solution.details += '
Failed
' - mes = get_node_value(el.getElementsByTagName("Message")) - self.solution.details += "
{}
".format(mes) - except: - solution.write_log("Unknown error") - res = "TEST ERROR" - self.solution.set_result(res) - - def test(self): - solution = self.solution - solution.result = "TESTING" - solution.save() - try: - if not exists(self.solution.task.tests_path()): - with self.solution.log_fs as fs: - fs.write(b"No test file found\n") - solution.set_result("TEST ERROR") - solution.save() - self.delete_everything() - start_new(self.host) - return - sln_path = solution_path(join(MEDIA_ROOT, "solutions", str(solution.id))) - if sln_path == "": - solution.set_result("TEST ERROR") - solution.save() - self.delete_everything() - start_new(self.host) - return - working_dir = join(sln_path, "test_folder") - if exists(working_dir): - try: - rmtree(working_dir) - except: - remove(working_dir) - mkdir(working_dir) - with self.solution.log_fs as fs: - fs.write(b"Testing directory created\n") - for project in listdir(sln_path): - solution.write_log("Checking if {} is project".format(project)) - prj = project - project = join(sln_path, project) - if ( - isdir(project) - and is_project(project) - and basename(project) != "TestsProject" - ): - if not self.build_and_copy(project, working_dir): - solution.set_result("Compilation error") - solution.write_log("Failed to compile project {}".format(prj)) - solution.save() - self.delete_everything() - start_new(self.host) - return - dll_path = solution.task.tests_path() - solution.write_log("Copying test file to working directory") - copyfile(dll_path, join(working_dir, str(solution.task.id) + ".cs")) - solution.write_log("Test file copied") - for file in listdir("SprintTest"): - try: - copyfile(join("SprintTest", file), join(working_dir, file)) - except: - pass - self.working_dir = working_dir - build_tests_cmd = "csc -out:{} -t:library /r:{} /r:{} /r:{} ".format( - join(self.working_dir, "tests.dll"), - join(self.working_dir, "SprintTest.dll"), - join(working_dir, "System.Runtime.dll"), - join(working_dir, "System.Reflection.dll"), - ) - for file in self.files: - build_tests_cmd += "/r:{}.dll ".format(join(self.working_dir, file)) - build_tests_cmd += self.solution.task.tests_path() - if exists(join(self.working_dir, "tests.dll")): - remove(join(self.working_dir, "tests.dll")) - solution.write_log("Building tests file started") - with self.solution.log_fs as fs: - shell(build_tests_cmd, fs) - with self.solution.log_fs as fs: - fs.write(b"Building tests file finished\n") - if exists(join(self.working_dir, "tests.dll")): - with self.solution.log_fs as fs: - fs.write(b"Got .dll tests file\n") - for file in ExtraFile.objects.filter(task=self.solution.task): - copyfile(file.path, join(working_dir, file.filename)) - self.nunit_testing() - else: - solution.set_result("TEST ERROR") - solution.write_log("Failed to compile tests") - except: - solution.set_result("TEST ERROR") - raise - with self.solution.log_fs as fs: - fs.write(b"Unknown error\n") - solution.save() - self.delete_everything() - start_new(self.host) diff --git a/Main/management/commands/bot.py b/Main/management/commands/bot.py index 0ea40e8..33ff98c 100644 --- a/Main/management/commands/bot.py +++ b/Main/management/commands/bot.py @@ -10,14 +10,19 @@ bot = telebot.TeleBot("1994460106:AAGrGsCZjF6DVG_T-zycELuVfxnWw8x7UyU") @bot.message_handler(commands=["start"]) def do_action(message: Message): - bot.send_message(message.chat.id, "Привет! Я тут чтобы помогать!\n/register - зарегистрироваться в сервисе\nБольше команд нет:(") + bot.send_message( + message.chat.id, + "Привет! Я тут чтобы помогать!\n/register - зарегистрироваться в сервисе\nБольше команд нет:(", + ) @bot.message_handler(commands=["register"]) def register(message: Message): username = message.from_user.username if username == "" or message.from_user.username is None: - bot.send_message(message.chat.id, "Добавть имя пользователя к своему телеграм аккаунту") + bot.send_message( + message.chat.id, "Добавть имя пользователя к своему телеграм аккаунту" + ) return ui = UserInfo.objects.filter(telegram_chat_id=message.chat.id).first() if ui: @@ -27,21 +32,30 @@ def register(message: Message): ui = UserInfo.objects.create(user=user, telegram_chat_id=message.chat.id) name = message.from_user.first_name surname = message.from_user.last_name - if surname is None or surname == '' or name is None or name == '': - bot.send_message(message.chat.id, 'Приветствую в Sprint! Сейчас я помогу тебе создать аккаунт.\nДля начала отправь мне свою фамилию') + if surname is None or surname == "" or name is None or name == "": + bot.send_message( + message.chat.id, + "Приветствую в Sprint! Сейчас я помогу тебе создать аккаунт.\nДля начала отправь мне свою фамилию", + ) else: ui.surname = surname ui.name = name ui.verified = True ui.save() - bot.send_message(message.chat.id, f"Регистрация завершена! Теперь можешь ты можешь войти в сервис под именем пользователя: {user.username}") + bot.send_message( + message.chat.id, + f"Регистрация завершена! Теперь можешь ты можешь войти в сервис под именем пользователя: {user.username}", + ) @bot.message_handler(content_types=["text"]) def do_action(message: Message): user = User.objects.filter(userinfo__telegram_chat_id=message.chat.id).first() if not user: - bot.send_message(message.chat.id, "Зарегистрируйся в сервисе, чтобы взаимодействовать со мной") + bot.send_message( + message.chat.id, + "Зарегистрируйся в сервисе, чтобы взаимодействовать со мной", + ) return if user.userinfo.surname is None: user.userinfo.surname = message.text @@ -51,15 +65,18 @@ def do_action(message: Message): user.userinfo.name = message.text user.userinfo.verified = True user.userinfo.save() - bot.send_message(message.chat.id, f"Регистрация завершена! Теперь можешь ты можешь войти в сервис под именем пользователя: {user.username}") + bot.send_message( + message.chat.id, + f"Регистрация завершена! Теперь можешь ты можешь войти в сервис под именем пользователя: {user.username}", + ) else: bot.send_message(message.chat.id, "Я пока больше ничего не умею") class Command(BaseCommand): - help = 'starts bot' + help = "starts bot" def handle(self, *args, **options): - print('bot is starting') + print("bot is starting") bot.polling() - print('bot failed') + print("bot failed") diff --git a/Main/management/commands/receive.py b/Main/management/commands/receive.py index cc13e28..62ddc4f 100644 --- a/Main/management/commands/receive.py +++ b/Main/management/commands/receive.py @@ -9,29 +9,31 @@ from SprintLib.testers import * class Command(BaseCommand): - help = 'Tests solution' + help = "Tests solution" def handle(self, *args, **options): print("Enter worker") - connection = pika.BlockingConnection(pika.ConnectionParameters(host=settings.RABBIT_HOST)) + connection = pika.BlockingConnection( + pika.ConnectionParameters(host=settings.RABBIT_HOST) + ) channel = connection.channel() - channel.queue_declare(queue='test') + channel.queue_declare(queue="test") def callback(ch, method, properties, body): try: - id = int(str(body, encoding='utf-8')) + id = int(str(body, encoding="utf-8")) print(f"Received id {id}") while True: try: solution = Solution.objects.get(id=id) break except: - sleep(.5) - eval(solution.language.work_name + 'Tester')(solution).execute() + sleep(0.5) + eval(solution.language.work_name + "Tester")(solution).execute() except Exception as e: print(e) - solution.result = 'TE' + solution.result = "TE" solution.save() - channel.basic_consume(queue='test', on_message_callback=callback, auto_ack=True) + channel.basic_consume(queue="test", on_message_callback=callback, auto_ack=True) channel.start_consuming() diff --git a/Main/management/commands/storage.py b/Main/management/commands/storage.py index 1263566..68f9163 100644 --- a/Main/management/commands/storage.py +++ b/Main/management/commands/storage.py @@ -3,7 +3,7 @@ from FileStorage.root import runserver class Command(BaseCommand): - help = 'starts FileStorage' + help = "starts FileStorage" def handle(self, *args, **options): runserver() diff --git a/Main/models/extrafile.py b/Main/models/extrafile.py index c2895a3..6b11ca7 100644 --- a/Main/models/extrafile.py +++ b/Main/models/extrafile.py @@ -25,9 +25,11 @@ class ExtraFile(FileStorageMixin, models.Model): def delete(self, using=None, keep_parents=False): self.remove_from_fs() - if self.is_test and self.filename.endswith('.a'): + if self.is_test and self.filename.endswith(".a"): try: - ef = ExtraFile.objects.get(task=self.task, filename=self.filename.rstrip('.a'), is_test=True) + ef = ExtraFile.objects.get( + task=self.task, filename=self.filename.rstrip(".a"), is_test=True + ) ef.is_sample = False ef.save() except ObjectDoesNotExist: @@ -36,4 +38,6 @@ class ExtraFile(FileStorageMixin, models.Model): @property def answer(self): - return ExtraFile.objects.get(task=self.task, is_test=True, filename=self.filename + '.a') + return ExtraFile.objects.get( + task=self.task, is_test=True, filename=self.filename + ".a" + ) diff --git a/Main/models/set.py b/Main/models/set.py index 026f195..81dc2e8 100644 --- a/Main/models/set.py +++ b/Main/models/set.py @@ -23,4 +23,4 @@ class Set(models.Model): @property def tasks(self): - return Task.objects.filter(settasks__set=self).order_by('settasks__name') + return Task.objects.filter(settasks__set=self).order_by("settasks__name") diff --git a/Main/models/solution.py b/Main/models/solution.py index 8c1a491..73c4fa4 100644 --- a/Main/models/solution.py +++ b/Main/models/solution.py @@ -36,23 +36,20 @@ class Solution(models.Model): text = file.text except: continue - entity = { - 'filename': file.path, - 'text': text - } - end = file.path.split('.')[-1] + entity = {"filename": file.path, "text": text} + end = file.path.split(".")[-1] language = None for l in languages: if l.file_type == end: language = l break if language is None: - highlight = 'nohighlight' + highlight = "nohighlight" else: - highlight = 'language-' + language.highlight - entity['highlight'] = highlight + highlight = "language-" + language.highlight + entity["highlight"] = highlight data.append(entity) - data.sort(key=lambda x: x['filename']) + data.sort(key=lambda x: x["filename"]) return data @property @@ -67,5 +64,9 @@ class Solution(models.Model): def volume_directory(self): return "/sprint-data/worker/" + str(self.id) - def exec_command(self, command, working_directory='app', timeout=None): - return call(f'docker exec -i solution_{self.id} sh -c "cd {working_directory} && {command}"', shell=True, timeout=timeout) + def exec_command(self, command, working_directory="app", timeout=None): + return call( + f'docker exec -i solution_{self.id} sh -c "cd {working_directory} && {command}"', + shell=True, + timeout=timeout, + ) diff --git a/Main/models/solution_file.py b/Main/models/solution_file.py index 702b5ff..d1961ea 100644 --- a/Main/models/solution_file.py +++ b/Main/models/solution_file.py @@ -6,4 +6,4 @@ from Main.models.mixins import FileStorageMixin class SolutionFile(FileStorageMixin, models.Model): path = models.TextField() fs_id = models.IntegerField() - solution = models.ForeignKey('Solution', on_delete=models.CASCADE) + solution = models.ForeignKey("Solution", on_delete=models.CASCADE) diff --git a/Main/models/task.py b/Main/models/task.py index 42d037a..2f646f0 100644 --- a/Main/models/task.py +++ b/Main/models/task.py @@ -29,12 +29,9 @@ class Task(models.Model): @property def samples(self): data = [] - for test in self.tests.order_by('test_number'): + for test in self.tests.order_by("test_number"): if test.is_sample and test.readable: - data.append({ - 'input': test.text, - 'output': test.answer.text - }) + data.append({"input": test.text, "output": test.answer.text}) count = 1 for entity in data: entity["num"] = count @@ -43,6 +40,7 @@ class Task(models.Model): def delete(self, using=None, keep_parents=False): from Main.models.progress import Progress + for progress in Progress.objects.filter(task=self): progress.user.userinfo.rating -= progress.score progress.user.userinfo.save() diff --git a/Main/models/token.py b/Main/models/token.py index 9747204..6884bb7 100644 --- a/Main/models/token.py +++ b/Main/models/token.py @@ -6,7 +6,7 @@ from django.utils import timezone def create_token(): - symbols = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890' + symbols = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890" return "".join([choice(symbols) for _ in range(30)]) diff --git a/Main/views/AccountView.py b/Main/views/AccountView.py index d4556ed..cdb7bd0 100644 --- a/Main/views/AccountView.py +++ b/Main/views/AccountView.py @@ -29,13 +29,17 @@ class AccountView(BaseView): def post_set_language(self): lang_id = int(self.request.POST["language"]) - self.request.user.userinfo.favourite_language_id = lang_id if lang_id != -1 else None + self.request.user.userinfo.favourite_language_id = ( + lang_id if lang_id != -1 else None + ) self.request.user.userinfo.save() return "/account" def post_notifications(self): for attr in dir(self.request.user.userinfo): - if attr.startswith('notification'): - setattr(self.request.user.userinfo, attr, attr in self.request.POST.keys()) + if attr.startswith("notification"): + setattr( + self.request.user.userinfo, attr, attr in self.request.POST.keys() + ) self.request.user.userinfo.save() - return '/account' + return "/account" diff --git a/Main/views/ImageView.py b/Main/views/ImageView.py index bbf9687..102c020 100644 --- a/Main/views/ImageView.py +++ b/Main/views/ImageView.py @@ -6,5 +6,8 @@ from SprintLib.utils import get_bytes class ImageView(BaseView): endpoint = "image" + def get(self): - return HttpResponse(get_bytes(int(self.request.GET['id'])), content_type="image/jpg") + return HttpResponse( + get_bytes(int(self.request.GET["id"])), content_type="image/jpg" + ) diff --git a/Main/views/SendCodeView.py b/Main/views/SendCodeView.py index 1cfae37..4d7efda 100644 --- a/Main/views/SendCodeView.py +++ b/Main/views/SendCodeView.py @@ -8,22 +8,31 @@ from random import randrange class SendCodeView(BaseView): endpoint = "send_code" + def post_create(self): username = self.request.POST["username"] user = User.objects.filter(username=username).first() if not user: - return {"success": False, "message": "Пользователя с таким именем не существует"} + return { + "success": False, + "message": "Пользователя с таким именем не существует", + } code = randrange(10000, 100000) user.userinfo.code = code user.userinfo.save() - bot.send_message(user.userinfo.telegram_chat_id, "Код для входа в сервис: " + str(code)) + bot.send_message( + user.userinfo.telegram_chat_id, "Код для входа в сервис: " + str(code) + ) return {"success": True, "message": "Код отправлен"} def post_check(self): username = self.request.POST["username"] user = User.objects.filter(username=username).first() if not user: - return {"success": False, "message": "Пользователя с таким именем не существует"} + return { + "success": False, + "message": "Пользователя с таким именем не существует", + } code = int(self.request.POST["code"]) if code == user.userinfo.code: user.userinfo.code = None diff --git a/Main/views/SetSettingsView.py b/Main/views/SetSettingsView.py index 3e374c2..d6be868 100644 --- a/Main/views/SetSettingsView.py +++ b/Main/views/SetSettingsView.py @@ -3,5 +3,5 @@ from SprintLib.BaseView import BaseView class SetSettingsView(BaseView): required_login = True - view_file = 'set_settings.html' - endpoint = "admin/set" \ No newline at end of file + view_file = "set_settings.html" + endpoint = "admin/set" diff --git a/Main/views/SolutionView.py b/Main/views/SolutionView.py index 34de84f..a370461 100644 --- a/Main/views/SolutionView.py +++ b/Main/views/SolutionView.py @@ -2,7 +2,7 @@ from SprintLib.BaseView import BaseView, AccessError class SolutionView(BaseView): - view_file = 'solution.html' + view_file = "solution.html" required_login = True endpoint = "solution" diff --git a/Main/views/SolutionsTableView.py b/Main/views/SolutionsTableView.py index b8a9e81..2eea685 100644 --- a/Main/views/SolutionsTableView.py +++ b/Main/views/SolutionsTableView.py @@ -6,17 +6,20 @@ from SprintLib.BaseView import BaseView class SolutionsTableView(BaseView): - view_file = 'solutions_table.html' + view_file = "solutions_table.html" required_login = True endpoint = "solutions_table" def get(self): - self.context['solutions'] = Solution.objects.filter( + self.context["solutions"] = Solution.objects.filter( user=self.request.user, task=self.entities.task ).order_by("-time_sent") - if 'render' in self.request.GET.keys(): + if "render" in self.request.GET.keys(): return - for sol in self.context['solutions']: - if sol.result == CONSTS['testing_status'] or sol.result == CONSTS['in_queue_status']: + for sol in self.context["solutions"]: + if ( + sol.result == CONSTS["testing_status"] + or sol.result == CONSTS["in_queue_status"] + ): return - return HttpResponse('done') + return HttpResponse("done") diff --git a/Main/views/TaskRuntimeView.py b/Main/views/TaskRuntimeView.py index 0f41bc4..49138c2 100644 --- a/Main/views/TaskRuntimeView.py +++ b/Main/views/TaskRuntimeView.py @@ -5,14 +5,14 @@ from SprintLib.BaseView import BaseView class TaskRuntimeView(BaseView): - view_file = 'task_runtime.html' + view_file = "task_runtime.html" required_login = True endpoint = "task_runtime" def get(self): progress = Progress.objects.get(task=self.entities.task, user=self.request.user) - self.context['progress'] = progress - if 'render' in self.request.GET.keys(): + self.context["progress"] = progress + if "render" in self.request.GET.keys(): return if progress.finished: - return HttpResponse('done') + return HttpResponse("done") diff --git a/Main/views/TaskSettingsView.py b/Main/views/TaskSettingsView.py index 313936a..82c7232 100644 --- a/Main/views/TaskSettingsView.py +++ b/Main/views/TaskSettingsView.py @@ -13,47 +13,49 @@ class TaskSettingsView(BaseView): def pre_handle(self): if self.entities.task not in self.request.user.userinfo.available_tasks: raise AccessError() - if self.request.method == 'POST': - for progress in Progress.objects.filter(task=self.entities.task, finished=False): + if self.request.method == "POST": + for progress in Progress.objects.filter( + task=self.entities.task, finished=False + ): progress.start_time = timezone.now() progress.save() def get(self): - self.context['error_message'] = self.request.GET.get('error_message', '') + self.context["error_message"] = self.request.GET.get("error_message", "") def post(self): for key, value in self.request.POST.items(): setattr(self.entities.task, key, value.strip()) - self.entities.task.public = 'public' in self.request.POST + self.entities.task.public = "public" in self.request.POST self.entities.task.save() return f"/admin/task?task_id={self.entities.task.id}" def _upload(self, is_test): - filename = self.request.FILES['file'].name + filename = self.request.FILES["file"].name ef, created = None, None if is_test: - name = filename.strip('.a') + name = filename.strip(".a") if not name.isnumeric(): - return f'/admin/task?task_id={self.entities.task.id}&error_message=Формат файла не соответствует тесту' - ef, created = ExtraFile.objects.get_or_create(task=self.entities.task, is_test=True, filename=filename) + return f"/admin/task?task_id={self.entities.task.id}&error_message=Формат файла не соответствует тесту" + ef, created = ExtraFile.objects.get_or_create( + task=self.entities.task, is_test=True, filename=filename + ) if not created: ef.is_sample = False ef.save() - return f'/admin/task?task_id={self.entities.task.id}' + return f"/admin/task?task_id={self.entities.task.id}" if ef is None or created is None: ef, created = ExtraFile.objects.get_or_create( - task=self.entities.task, - filename=filename, - is_test=is_test + task=self.entities.task, filename=filename, is_test=is_test ) - ef.write(self.request.FILES['file'].read()) + ef.write(self.request.FILES["file"].read()) try: var = ef.text ef.readable = True except UnicodeDecodeError: ef.readable = False ef.save() - return '/admin/task?task_id=' + str(self.entities.task.id) + return "/admin/task?task_id=" + str(self.entities.task.id) def post_file_upload(self): return self._upload(False) @@ -62,21 +64,23 @@ class TaskSettingsView(BaseView): return self._upload(True) def post_delete_file(self): - ef = ExtraFile.objects.get(id=self.request.POST['id']) + ef = ExtraFile.objects.get(id=self.request.POST["id"]) ef.delete() return HttpResponse("ok") def _create(self, is_test): - name = self.request.POST['newfile_name'] + name = self.request.POST["newfile_name"] - ef, created = ExtraFile.objects.get_or_create(filename=name, task=self.entities.task) + ef, created = ExtraFile.objects.get_or_create( + filename=name, task=self.entities.task + ) if not created: - return f'/admin/task?task_id={self.entities.task.id}&error_message=Файл с таким именем уже существует' + return f"/admin/task?task_id={self.entities.task.id}&error_message=Файл с таким именем уже существует" ef.write(b"") ef.is_test = is_test ef.readable = True ef.save() - return f'/admin/task?task_id={self.entities.task.id}' + return f"/admin/task?task_id={self.entities.task.id}" def post_create_file(self): return self._create(False) @@ -85,9 +89,9 @@ class TaskSettingsView(BaseView): return self._create(True) def post_save_test(self): - ef = ExtraFile.objects.get(id=self.request.POST['test_id']) + ef = ExtraFile.objects.get(id=self.request.POST["test_id"]) ef.remove_from_fs() - ef.write(self.request.POST['text'].encode('utf-8')) - ef.is_sample = 'is_sample' in self.request.POST.keys() + ef.write(self.request.POST["text"].encode("utf-8")) + ef.is_sample = "is_sample" in self.request.POST.keys() ef.save() - return f'/admin/task?task_id={self.entities.task.id}' + return f"/admin/task?task_id={self.entities.task.id}" diff --git a/Main/views/TaskView.py b/Main/views/TaskView.py index 81b5dbb..1d567a3 100644 --- a/Main/views/TaskView.py +++ b/Main/views/TaskView.py @@ -44,7 +44,7 @@ class TaskView(BaseView): return "task?task_id=" + str(self.entities.task.id) filename = self.request.FILES["file"].name if filename.endswith(".zip"): - archive = ZipFile(io.BytesIO(self.request.FILES['file'].read())) + archive = ZipFile(io.BytesIO(self.request.FILES["file"].read())) for file in archive.infolist(): if file.is_dir(): continue @@ -55,7 +55,7 @@ class TaskView(BaseView): fs_id=fs_id, ) else: - fs_id = write_bytes(self.request.FILES['file'].read()) + fs_id = write_bytes(self.request.FILES["file"].read()) SolutionFile.objects.create( path=filename, solution=self.solution, diff --git a/SprintLib/language.py b/SprintLib/language.py index 0db263a..28d3145 100644 --- a/SprintLib/language.py +++ b/SprintLib/language.py @@ -50,7 +50,7 @@ languages = [ file_type="java", logo_url="https://upload.wikimedia.org/wikipedia/ru/thumb/3/39/Java_logo.svg/1200px-Java_logo.svg.png", image="openjdk", - highlight="java" + highlight="java", ), Language( id=4, @@ -59,6 +59,6 @@ languages = [ file_type="cs", logo_url="https://cdn.worldvectorlogo.com/logos/c--4.svg", image="mono", - highlight="csharp" - ) + highlight="csharp", + ), ] diff --git a/SprintLib/queue.py b/SprintLib/queue.py index 8b4bbc5..e30de9a 100644 --- a/SprintLib/queue.py +++ b/SprintLib/queue.py @@ -9,4 +9,8 @@ def send_testing(solution_id): ) as connection: channel = connection.channel() channel.queue_declare(queue="test") - channel.basic_publish(exchange="", routing_key="test", body=bytes(str(solution_id), encoding='utf-8')) + channel.basic_publish( + exchange="", + routing_key="test", + body=bytes(str(solution_id), encoding="utf-8"), + ) diff --git a/SprintLib/testers/BaseTester.py b/SprintLib/testers/BaseTester.py index 5b227a1..56422e5 100644 --- a/SprintLib/testers/BaseTester.py +++ b/SprintLib/testers/BaseTester.py @@ -63,12 +63,16 @@ class BaseTester: mkdir("solutions") mkdir("solutions/" + str(self.solution.id)) for file in SolutionFile.objects.filter(solution=self.solution): - dirs = file.path.split('/') + dirs = file.path.split("/") for i in range(len(dirs) - 1): - name = join(str("solutions/" + self.solution.id), '/'.join(dirs[:i + 1])) + name = join( + str("solutions/" + self.solution.id), "/".join(dirs[: i + 1]) + ) if not exists(name): mkdir(name) - with open(join("solutions/" + str(self.solution.id), file.path), 'wb') as fs: + with open( + join("solutions/" + str(self.solution.id), file.path), "wb" + ) as fs: fs.write(get_bytes(file.fs_id)) self.solution.result = CONSTS["testing_status"] self.solution.save() @@ -77,7 +81,9 @@ class BaseTester: call(docker_command, shell=True) print("Container created") for file in ExtraFile.objects.filter(task=self.solution.task): - with open(join("solutions/" + str(self.solution.id), file.filename), 'wb') as fs: + with open( + join("solutions/" + str(self.solution.id), file.filename), "wb" + ) as fs: fs.write(get_bytes(file.fs_id)) print("Files copied") try: diff --git a/SprintLib/testers/JavaTester.py b/SprintLib/testers/JavaTester.py index d4180f5..75090b8 100644 --- a/SprintLib/testers/JavaTester.py +++ b/SprintLib/testers/JavaTester.py @@ -7,13 +7,17 @@ class JavaTester(BaseTester): _executable = None def before_test(self): - files = [file for file in listdir(self.solution.testing_directory) if file.endswith('.java')] + files = [ + file + for file in listdir(self.solution.testing_directory) + if file.endswith(".java") + ] code = self.solution.exec_command(f"javac {' '.join(files)}") if code != 0: - raise TestException('CE') + raise TestException("CE") for file in listdir(self.solution.testing_directory): - if file.endswith('.class'): - self._executable = file.rstrip('.class') + if file.endswith(".class"): + self._executable = file.rstrip(".class") break if self._executable is None: raise TestException("TE") diff --git a/SprintLib/testers/KotlinTester.py b/SprintLib/testers/KotlinTester.py index 94f6b60..e00d3c6 100644 --- a/SprintLib/testers/KotlinTester.py +++ b/SprintLib/testers/KotlinTester.py @@ -5,10 +5,16 @@ from SprintLib.testers.BaseTester import BaseTester, TestException class KotlinTester(BaseTester): def before_test(self): - files = [file for file in listdir(self.solution.testing_directory) if file.endswith('.kt')] - code = self.solution.exec_command(f'kotlinc {" ".join(files)} -include-runtime -d solution.jar') + files = [ + file + for file in listdir(self.solution.testing_directory) + if file.endswith(".kt") + ] + code = self.solution.exec_command( + f'kotlinc {" ".join(files)} -include-runtime -d solution.jar' + ) if code != 0: - raise TestException('CE') + raise TestException("CE") @property def command(self): diff --git a/SprintLib/utils.py b/SprintLib/utils.py index 751538f..f9e1b17 100644 --- a/SprintLib/utils.py +++ b/SprintLib/utils.py @@ -6,7 +6,7 @@ from Sprint import settings def write_bytes(data): url = settings.FS_HOST + ":" + str(settings.FS_PORT) + "/upload_file" print(url) - return post(url, data=data).json()['id'] + return post(url, data=data).json()["id"] def get_bytes(num):