Coretex
project.py
1 # Copyright (C) 2023 Coretex LLC
2 
3 # This file is part of Coretex.ai
4 
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
9 
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Affero General Public License for more details.
14 
15 # You should have received a copy of the GNU Affero General Public License
16 # along with this program. If not, see <https://www.gnu.org/licenses/>.
17 
18 from typing import Optional, Any, List, Dict
19 from typing_extensions import Self
20 
21 from .base import BaseObject
22 from .task import Task
23 from .project_type import ProjectType
24 from .project_visibility import ProjectVisibility
25 from ..entity_visibility_type import EntityVisibilityType
26 from ..utils import isEntityNameValid
27 from ...networking import networkManager, NetworkRequestError
28 
29 
31 
32  """
33  Represents the project entity from Coretex.ai\n
34  Contains properties that describe the project
35  """
36 
37  tasks: List[Task]
38  visibility: ProjectVisibility
39  isEncrypted: bool
40  secretName: Optional[str]
41 
42  @classmethod
44  cls,
45  name: str,
46  projectType: ProjectType,
47  visiblity: ProjectVisibility = ProjectVisibility.private,
48  description: Optional[str] = None
49  ) -> Self:
50  """
51  Creates a new project with the provided name and description
52 
53  Parameters
54  ----------
55  name : str
56  project name
57  projectType : ProjectType
58  type of the created Project
59  visibility : ProjectVisibility
60  visibility of the created Project
61  description : Optional[str]
62  project description
63 
64  Returns
65  -------
66  Self -> The created project object
67 
68  Raises
69  ------
70  NetworkRequestError -> If project creation failed
71 
72  Example
73  -------
74  >>> from coretex import Project, ProjectType
75  \b
76  >>> try:
77  >>> dummyProject = Project.createProject(
78  name = "dummyProject",
79  projectType = ProjectType.other,
80  description = "This is dummy Coretex Project"
81  )
82  except:
83  print("Failed to create project.")
84  """
85 
86  if not isEntityNameValid(name):
87  raise ValueError(">> [Coretex] Project name is invalid. Requirements: alphanumeric characters (\"a-z\", and \"0-9\") and dash (\"-\") with length between 3 to 50")
88 
89  return cls.create(
90  name = name,
91  project_task = projectType,
92  description = description,
93  visiblity = visiblity
94  )
95 
96  @classmethod
97  def decode(cls, encodedObject: Dict[str, Any]) -> Self:
98  obj = super().decode(encodedObject)
99  obj.tasks = Task.fetchAll(queryParameters=[
100  f"parentId={obj.id}"
101  ])
102 
103  return obj
104 
105  def addTask(self, name: str, description: Optional[str]) -> bool:
106  """
107  Adds new task to the project
108 
109  Parameters
110  ----------
111  name : str
112  task name
113  description : Optional[str]
114  task description
115 
116  Returns
117  -------
118  bool -> True if the task was added. False if the task was not added
119  """
120 
121  task = Task.createTask(name, self.id, description)
122  if task is None:
123  return False
124 
125  self.tasks.append(task)
126  return True
127 
128  @classmethod
129  def fetchByName(cls, name: str) -> Self:
130  """
131  Fetches Project based on specified name
132 
133  Parameters
134  ----------
135  name : str
136  The name of the Project to fetch
137 
138  Returns
139  -------
140  Self -> Fetched Project
141 
142  Raises
143  ------
144  RuntimeError -> If the Project with specified name is not found
145  """
146 
147  results = cls.fetchAll(name = f"={name}")
148  if len(results) == 0:
149  raise ValueError(f"Project with name \"{name}\" not found.")
150 
151  return results[0]
152 
153  def updateVisibility(self, visibility: ProjectVisibility) -> None:
154  """
155  Updates visibility of the project
156 
157  Parameters
158  ----------
159  visibility : ProjectVisibility
160  visibility of the project
161 
162  Raises
163  ------
164  NetworkRequestError -> If request for updating the Project visibility failed
165  """
166 
167  parameters = {
168  "entity_id": self.id,
169  "type": EntityVisibilityType.project,
170  "visibility": visibility
171  }
172 
173  response = networkManager.post("entity-visibility", parameters)
174 
175  if response.hasFailed():
176  raise NetworkRequestError(response, "Failed to update visibility of the Project.")
177 
178  self.visibilityvisibility = visibility
bool addTask(self, str name, Optional[str] description)
Definition: project.py:105
None updateVisibility(self, ProjectVisibility visibility)
Definition: project.py:153
Self createProject(cls, str name, ProjectType projectType, ProjectVisibility visiblity=ProjectVisibility.private, Optional[str] description=None)
Definition: project.py:49