Commit 50b16945 authored by nano's avatar nano

little code enhancements

parent 2423dd97
Pipeline #79 passed with stage
in 56 seconds
......@@ -44,12 +44,9 @@ class Exchange(models.Model):
"""
An exchange contains files that can be downloaded by anyone knowing the unique token -
and if set - the required password
Also the upload of files is possible -if allowed
Also the upload of files is possible - if allowed
"""
"""
safety
"""
token = models.CharField('access token', max_length=100, unique=True,
default=gen_random_token, editable=False)
password = models.CharField('optional password', max_length=100, null=True)
......@@ -90,7 +87,6 @@ class Exchange(models.Model):
assert isinstance(user, User)
assert isinstance(permission, Permission)
if PermissionGranted.objects.filter(exchange=self).filter(user=user).count() > 0:
# permission already granted
return True
else:
return False
......@@ -133,7 +129,7 @@ class Exchange(models.Model):
def _create_data_store_root_dir(self):
"""
Creates the root dir in the data store for this exchange
Creates the root dir in the data store of this exchange
:return: None
:raise: FileExistsError
:raises If the root dir already exists
......@@ -159,8 +155,10 @@ class Exchange(models.Model):
def path_from_exchange_data_dir_root(self, path_str: str):
"""
this function should always be called when dealing with paths to prevent directory traversal.
It assures that the path is relative to the root dir of the exchange data dir
It assures that the given path is relative to the root dir of the exchange data dir
: param path: the relative path
:raise: OperationNotPermittedException
:raises If the passed path is either absolute or points to a directory outside the data dir root
:returns the absolute path from the exchange data dir
:return: str
"""
......@@ -190,7 +188,7 @@ class Exchange(models.Model):
assert isinstance(settings.FIEX_DATA_FOLDER_DIR_PERM_DEFAULT, int)
destination_path.mkdir(mode=settings.FIEX_DATA_FOLDER_DIR_PERM_DEFAULT, parents=True, exist_ok=False)
def delete_dir(self, path, not_empty_ok=False):
def rmdir(self, path, not_empty_ok=False):
"""
deletes a directory in the local data store
:param path: the path to the directory to delete
......@@ -207,11 +205,11 @@ class Exchange(models.Model):
for entity in dir_to_delete.iterdir():
entity_path_str = str(entity.relative_to(self._data_store_dir_path()))
if entity.is_dir():
self.delete_dir(entity_path_str, not_empty_ok=True)
self.rmdir(entity_path_str, not_empty_ok=True)
elif entity.is_file():
file = \
FiexFile.objects.select_related().filter(exchange=self).filter(path=entity_path_str)[
0]
0]
file.delete()
else:
raise DirectoryNotEmptyException()
......@@ -245,20 +243,20 @@ class Permission(models.Model):
Permission.WRITE_PERMISSION = Permission(name='WRITE_PERMISSION', description='the permission to write')
Permission.WRITE_PERMISSION.save()
else:
Permission.WRITE_PERMISSION = Permission.objects.get(name='WRITE_PERMISSION')
Permission.WRITE_PERMISSION = Permission.objects.get(name='WRITE_PERMISSION')[0]
if Permission.objects.filter(name='READ_PERMISSION').count() <= 0:
Permission.READ_PERMISSION = Permission(name='READ_PERMISSION', description='the permission to read')
Permission.READ_PERMISSION.save()
else:
Permission.READ_PERMISSION = Permission.objects.get(name='READ_PERMISSION')
Permission.READ_PERMISSION = Permission.objects.get(name='READ_PERMISSION')[0]
if Permission.objects.filter(name='CHANGE_PERMISSION').count() <= 0:
Permission.CHANGE_PERMISSION = Permission(name='CHANGE_PERMISSION',
description='the permission to change the exchange')
Permission.CHANGE_PERMISSION.save()
else:
Permission.CHANGE_PERMISSION = Permission.objects.get(name='CHANGE_PERMISSION')
Permission.CHANGE_PERMISSION = Permission.objects.get(name='CHANGE_PERMISSION')[0]
class PermissionGranted(models.Model):
......@@ -283,7 +281,7 @@ class FiexFile(models.Model):
def write_content_from_mem(self, content: bytes, override: bool) -> None:
"""
creates a new file with the given content and safes it into the specified path.
creates a new file with the given content and safes it into the set path.
The method does not create any not existing dirs
:param content: the content to write
:param override: must be true if an existing file should be overriden
......
......@@ -133,7 +133,7 @@ class ExchangeTestCase(TestCase):
def test_folder_destruction(self):
# delete empty dir
self.test_exchange1.mkdir('test_folder_destruction1')
self.test_exchange1.delete_dir('test_folder_destruction1', False)
self.test_exchange1.rmdir('test_folder_destruction1', False)
# delete not empty dir
self.test_exchange1.mkdir('test_folder_destruction2')
......@@ -144,8 +144,8 @@ class ExchangeTestCase(TestCase):
new_file.write_content_from_mem("test".encode(), False)
with self.assertRaises(DirectoryNotEmptyException):
self.test_exchange1.delete_dir('test_folder_destruction2', False)
self.test_exchange1.delete_dir('test_folder_destruction2', True)
self.test_exchange1.rmdir('test_folder_destruction2', False)
self.test_exchange1.rmdir('test_folder_destruction2', True)
self.assertTrue(FiexFile.objects.select_related().filter(exchange=self.test_exchange1).filter(
path=new_file_path).count() == 0)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment