filestorage

This commit is contained in:
Egor Matveev
2021-11-11 11:47:06 +03:00
parent a8111c45e9
commit edb58e23a3
22 changed files with 237 additions and 93 deletions

View File

@@ -1,13 +1,12 @@
from os import mkdir, walk
from os.path import join, exists
from shutil import rmtree
from subprocess import call
from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist
from django.db import models
from django.utils import timezone
from Main.models.solution_file import SolutionFile
from Main.models.task import Task
from Sprint.settings import CONSTS, SOLUTIONS_ROOT, SOLUTIONS_ROOT_EXTERNAL
from SprintLib.language import languages
@@ -32,36 +31,30 @@ class Solution(models.Model):
@property
def files(self):
data = []
for path, _, files in walk(self.directory):
if path.startswith(self.testing_directory):
for file in SolutionFile.objects.filter(solution=self):
try:
text = file.text
except:
continue
for file in files:
try:
entity = {
'filename': file,
'text': open(join(path, file), 'r').read()
}
end = file.split('.')[-1]
language = None
for l in languages:
if l.file_type == end:
language = l
break
if language is None:
highlight = 'nohighlight'
else:
highlight = 'language-' + language.highlight
entity['highlight'] = highlight
data.append(entity)
except:
continue
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'
else:
highlight = 'language-' + language.highlight
entity['highlight'] = highlight
data.append(entity)
data.sort(key=lambda x: x['filename'])
return data
def create_dirs(self):
mkdir(self.directory)
mkdir(self.testing_directory)
@property
def directory(self):
return join(SOLUTIONS_ROOT, str(self.id))