Source code for debusine.db.models.workspaces

# Copyright 2019, 2021-2024 The Debusine Developers
# See the AUTHORS file at the top-level directory of this distribution
#
# This file is part of Debusine. It is subject to the license terms
# in the LICENSE file found in the top-level directory of this
# distribution. No part of Debusine, including this file, may be copied,
# modified, propagated, or distributed except according to the terms
# contained in the LICENSE file.

"""Data models for db workspaces."""

from datetime import timedelta
from typing import Any

from django.db import models

from debusine.db.models.files import File, FileStore


class WorkspaceManager(models.Manager["Workspace"]):
    """Manager for Workspace model."""

    @classmethod
    def create_with_name(cls, name: str, **kwargs: Any) -> "Workspace":
        """Return a new Workspace with name and the default FileStore."""
        kwargs.setdefault("default_file_store", FileStore.default())
        return Workspace.objects.create(name=name, **kwargs)


DEFAULT_WORKSPACE_NAME = "System"


[docs] def default_workspace() -> "Workspace": """Return the default Workspace.""" return Workspace.objects.get(name=DEFAULT_WORKSPACE_NAME)
[docs] class Workspace(models.Model): """Workspace model.""" objects = WorkspaceManager() name = models.CharField(max_length=255, unique=True) default_file_store = models.ForeignKey( FileStore, on_delete=models.PROTECT, related_name="default_workspaces" ) other_file_stores = models.ManyToManyField( FileStore, related_name="other_workspaces" ) public = models.BooleanField(default=False) default_expiration_delay = models.DurationField( default=timedelta(0), help_text="minimal time that a new artifact is kept in the" " workspace before being expired", )
[docs] def is_file_in_workspace(self, fileobj: File) -> bool: """Return True if fileobj is in any store available for Workspace.""" file_stores = [self.default_file_store, *self.other_file_stores.all()] for file_store in file_stores: if file_store.fileinstore_set.filter(file=fileobj).exists(): return True return False
def __str__(self) -> str: """Return basic information of Workspace.""" return f"Id: {self.id} Name: {self.name}"