mirror of
https://github.com/ae-utbm/sith.git
synced 2025-11-10 05:53:06 +00:00
Merge pull request #1237 from ae-utbm/remove-repair-fs
remove `SithFile._check_fs` and `SithFile.repair_fs`
This commit is contained in:
@@ -1,40 +0,0 @@
|
|||||||
#
|
|
||||||
# Copyright 2018
|
|
||||||
# - Skia <skia@libskia.so>
|
|
||||||
#
|
|
||||||
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
|
|
||||||
# http://ae.utbm.fr.
|
|
||||||
#
|
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
|
||||||
# the terms of the GNU General Public License a published by the Free Software
|
|
||||||
# Foundation; either version 3 of the License, or (at your option) any later
|
|
||||||
# version.
|
|
||||||
#
|
|
||||||
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
||||||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
||||||
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
||||||
# details.
|
|
||||||
#
|
|
||||||
# You should have received a copy of the GNU General Public License along with
|
|
||||||
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
|
|
||||||
# Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
||||||
#
|
|
||||||
#
|
|
||||||
|
|
||||||
from django.core.management.base import BaseCommand
|
|
||||||
|
|
||||||
from core.models import SithFile
|
|
||||||
|
|
||||||
|
|
||||||
class Command(BaseCommand):
|
|
||||||
help = "Recursively check the file system with respect to the DB"
|
|
||||||
|
|
||||||
def add_arguments(self, parser):
|
|
||||||
parser.add_argument(
|
|
||||||
"ids", metavar="ID", type=int, nargs="+", help="The file IDs to process"
|
|
||||||
)
|
|
||||||
|
|
||||||
def handle(self, *args, **options):
|
|
||||||
files = SithFile.objects.filter(id__in=options["ids"]).all()
|
|
||||||
for f in files:
|
|
||||||
f._check_fs()
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
#
|
|
||||||
# Copyright 2018
|
|
||||||
# - Skia <skia@libskia.so>
|
|
||||||
#
|
|
||||||
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
|
|
||||||
# http://ae.utbm.fr.
|
|
||||||
#
|
|
||||||
# This program is free software; you can redistribute it and/or modify it under
|
|
||||||
# the terms of the GNU General Public License a published by the Free Software
|
|
||||||
# Foundation; either version 3 of the License, or (at your option) any later
|
|
||||||
# version.
|
|
||||||
#
|
|
||||||
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
||||||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
||||||
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
||||||
# details.
|
|
||||||
#
|
|
||||||
# You should have received a copy of the GNU General Public License along with
|
|
||||||
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
|
|
||||||
# Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
||||||
#
|
|
||||||
#
|
|
||||||
|
|
||||||
|
|
||||||
from django.core.management.base import BaseCommand
|
|
||||||
|
|
||||||
from core.models import SithFile
|
|
||||||
|
|
||||||
|
|
||||||
class Command(BaseCommand):
|
|
||||||
help = "Recursively repair the file system with respect to the DB"
|
|
||||||
|
|
||||||
def add_arguments(self, parser):
|
|
||||||
parser.add_argument(
|
|
||||||
"ids", metavar="ID", type=int, nargs="+", help="The file IDs to process"
|
|
||||||
)
|
|
||||||
|
|
||||||
def handle(self, *args, **options):
|
|
||||||
files = SithFile.objects.filter(id__in=options["ids"]).all()
|
|
||||||
for f in files:
|
|
||||||
f._repair_fs()
|
|
||||||
@@ -23,8 +23,6 @@
|
|||||||
#
|
#
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
|
||||||
import os
|
|
||||||
import string
|
import string
|
||||||
import unicodedata
|
import unicodedata
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
@@ -411,14 +409,6 @@ class User(AbstractUser):
|
|||||||
else:
|
else:
|
||||||
raise ValidationError(_("A user with that username already exists"))
|
raise ValidationError(_("A user with that username already exists"))
|
||||||
|
|
||||||
def get_profile(self):
|
|
||||||
return {
|
|
||||||
"last_name": self.last_name,
|
|
||||||
"first_name": self.first_name,
|
|
||||||
"nick_name": self.nick_name,
|
|
||||||
"date_of_birth": self.date_of_birth,
|
|
||||||
}
|
|
||||||
|
|
||||||
def get_short_name(self):
|
def get_short_name(self):
|
||||||
"""Returns the short name for the user."""
|
"""Returns the short name for the user."""
|
||||||
if self.nick_name:
|
if self.nick_name:
|
||||||
@@ -973,63 +963,6 @@ class SithFile(models.Model):
|
|||||||
self.clean()
|
self.clean()
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
def _repair_fs(self):
|
|
||||||
"""Rebuilds recursively the filesystem as it should be regarding the DB tree."""
|
|
||||||
if self.is_folder:
|
|
||||||
for c in self.children.all():
|
|
||||||
c._repair_fs()
|
|
||||||
return
|
|
||||||
elif not self._check_path_consistence():
|
|
||||||
# First get future parent path and the old file name
|
|
||||||
# Prepend "." so that we match all relative handling of Django's
|
|
||||||
# file storage
|
|
||||||
parent_path = "." + self.parent.get_full_path()
|
|
||||||
parent_full_path = settings.MEDIA_ROOT + parent_path
|
|
||||||
os.makedirs(parent_full_path, exist_ok=True)
|
|
||||||
old_path = self.file.name # Should be relative: "./users/skia/bleh.jpg"
|
|
||||||
new_path = "." + self.get_full_path()
|
|
||||||
try:
|
|
||||||
# Make this atomic, so that a FS problem rolls back the DB change
|
|
||||||
with transaction.atomic():
|
|
||||||
# Set the new filesystem path
|
|
||||||
self.file.name = new_path
|
|
||||||
self.save()
|
|
||||||
# Really move at the FS level
|
|
||||||
if os.path.exists(parent_full_path):
|
|
||||||
os.rename(
|
|
||||||
settings.MEDIA_ROOT + old_path,
|
|
||||||
settings.MEDIA_ROOT + new_path,
|
|
||||||
)
|
|
||||||
# Empty directories may remain, but that's not really a
|
|
||||||
# problem, and that can be solved with a simple shell
|
|
||||||
# command: `find . -type d -empty -delete`
|
|
||||||
except Exception as e:
|
|
||||||
logging.error(e)
|
|
||||||
|
|
||||||
def _check_path_consistence(self):
|
|
||||||
file_path = str(self.file)
|
|
||||||
file_full_path = settings.MEDIA_ROOT + file_path
|
|
||||||
db_path = ".%s" % self.get_full_path()
|
|
||||||
if not os.path.exists(file_full_path):
|
|
||||||
print("%s: WARNING: real file does not exists!" % self.id) # noqa T201
|
|
||||||
print("file path: %s" % file_path, end="") # noqa T201
|
|
||||||
print(" db path: %s" % db_path) # noqa T201
|
|
||||||
return False
|
|
||||||
if file_path != db_path:
|
|
||||||
print("%s: " % self.id, end="") # noqa T201
|
|
||||||
print("file path: %s" % file_path, end="") # noqa T201
|
|
||||||
print(" db path: %s" % db_path) # noqa T201
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
def _check_fs(self):
|
|
||||||
if self.is_folder:
|
|
||||||
for c in self.children.all():
|
|
||||||
c._check_fs()
|
|
||||||
return
|
|
||||||
else:
|
|
||||||
self._check_path_consistence()
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_file(self):
|
def is_file(self):
|
||||||
return not self.is_folder
|
return not self.is_folder
|
||||||
|
|||||||
Reference in New Issue
Block a user