Skip to content

Commit

Permalink
wop
Browse files Browse the repository at this point in the history
  • Loading branch information
bameda committed Sep 6, 2023
1 parent fa08954 commit a226a7a
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 13 deletions.
1 change: 1 addition & 0 deletions python/apps/taiga/src/taiga/base/db/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# Copyright (c) 2023-present Kaleidos INC


from django.db.models.deletion import RestrictedError # noqa
from django.db.utils import IntegrityError, ProgrammingError # noqa


Expand Down
15 changes: 12 additions & 3 deletions python/apps/taiga/src/taiga/commons/storage/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import TypedDict
from uuid import UUID

from taiga.base.db.exceptions import RestrictedError
from taiga.base.db.models import QuerySet
from taiga.base.utils.datetime import aware_utcnow
from taiga.base.utils.files import File
Expand Down Expand Up @@ -68,9 +69,17 @@ async def list_storaged_objects(filters: StoragedObjectFilters = {}) -> list[Sto

async def delete_storaged_object(
storaged_object: StoragedObject,
) -> None:
await storaged_object.adelete()
storaged_object.file.delete(save=False)
) -> bool:
try:
await storaged_object.adelete()
storaged_object.file.delete(save=False)
return True
except RestrictedError:
# This happens when you try to delete a StoragedObject that is being used by someone
# (using ForeignKey with on_delete=PROTECT).

# TODO: log this
return False


def mark_storaged_object_as_deleted(
Expand Down
7 changes: 4 additions & 3 deletions python/apps/taiga/src/taiga/commons/storage/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@

async def clean_deleted_storaged_objects(before: datetime) -> int:
storaged_objects = await storage_repositories.list_storaged_objects(filters={"deleted_before": before})

deleted = 0
for storaged_object in storaged_objects:
await storage_repositories.delete_storaged_object(storaged_object=storaged_object)
if storage_repositories.delete_storaged_object(storaged_object=storaged_object):
deleted += 1

return len(storaged_objects)
return deleted
12 changes: 8 additions & 4 deletions python/apps/taiga/tests/conf_django.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@

import os

from taiga.base.django.settings import * # noqa, pylint: disable=unused-wildcard-import
from taiga.base.django.settings import * # noqa
from taiga.base.django.settings import INSTALLED_APPS

DEBUG = True

MEDIA_ROOT = "/tmp/taiga/media"
STATIC_ROOT = "/tmp/taiga/static"


INSTALLED_APPS += [ # noqa: F405
"tests.samples.occ",
]
INSTALLED_APPS = [
# NOTE: We should add sample apps first because pytest-django uses an strange strategy to load models to the db and
# it causes integrity errors with foreign keys and many to many fields
"tests.samples.test_occ",
"tests.samples.test_storage",
] + INSTALLED_APPS


# This is only for GitHubActions
Expand Down
5 changes: 3 additions & 2 deletions python/apps/taiga/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,18 @@ def django_db_setup(django_db_setup, django_db_blocker):

@pytest_asyncio.fixture(scope="session", autouse=True)
async def connect_events_manage_on_startup():
from taiga.events import connect_events_manager
from taiga.events import connect_events_manager, disconnect_events_manager

await connect_events_manager()
yield
await disconnect_events_manager()


#
# Manage slow tests
#
def pytest_addoption(parser):
parser.addoption("--slow_only", action="store_true", default=False, help="run slow tests only")

parser.addoption("--fast_only", action="store_true", default=False, help="exclude slow tests")


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import pytest
from taiga.base.occ import repositories
from tests.samples.occ.models import SampleOCCItem
from tests.samples.test_occ.models import SampleOCCItem

pytestmark = pytest.mark.django_db(transaction=True)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from asgiref.sync import sync_to_async
from taiga.base.utils.datetime import aware_utcnow
from taiga.commons.storage import repositories
from tests.samples.test_storage.models import SampleAttachment
from tests.utils import factories as f

pytestmark = pytest.mark.django_db(transaction=True)
Expand Down Expand Up @@ -87,6 +88,27 @@ async def test_delete_storaged_object():
assert not storage.exists(file_path)


async def test_delete_storaged_object_that_has_been_used():
storaged_object = await f.create_storaged_object()
file_path = storaged_object.file.path
storage = storaged_object.file.storage

await SampleAttachment.objects.acreate(storaged_object=storaged_object)

assert len(await repositories.list_storaged_objects()) == 1
assert storage.exists(file_path)

assert not await repositories.delete_storaged_object(storaged_object=storaged_object)

assert len(await repositories.list_storaged_objects()) == 1
assert storage.exists(file_path)


##########################################################
# mark_storaged_object_as_deleted
##########################################################


async def test_mark_storaged_object_as_deleted():
storaged_object = await f.create_storaged_object()

Expand Down
6 changes: 6 additions & 0 deletions python/apps/taiga/tests/samples/test_storage/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (c) 2023-present Kaleidos INC
18 changes: 18 additions & 0 deletions python/apps/taiga/tests/samples/test_storage/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (c) 2023-present Kaleidos INC

from taiga.base.db import models


class SampleAttachment(models.Model):
storaged_object = models.ForeignKey(
"storage.StoragedObject",
null=False,
blank=False,
on_delete=models.RESTRICT,
related_name="+",
)

0 comments on commit a226a7a

Please sign in to comment.