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
19 
20 import click
21 
22 from ..modules import ui, project_utils
23 from ..modules.user import initializeUserSession
24 from ..modules.utils import onBeforeCommandExecute
25 from ...entities import Project, ProjectVisibility
26 from ...networking import RequestFailedError
27 from ...configuration import UserConfiguration
28 
29 
30 @click.command()
31 @click.option("--name", "-n", type = str, help = "Project name")
32 @click.option("--type", "-t", "projectType", type = int, help = "Project type")
33 @click.option("--description", "-d", type = str, help = "Project description")
34 def create(name: Optional[str], projectType: Optional[int], description: Optional[str]) -> None:
35  userConfig = UserConfiguration.load()
36  project = project_utils.createProject(userConfig.frontendUrl, name, projectType, description)
37 
38  selectNewProject = ui.clickPrompt("Do you want to select the new project as default? (Y/n)", type = bool, default = True)
39  if selectNewProject:
40  userConfig.selectProject(project.id)
41  ui.successEcho(f"Project \"{project.name}\" successfully selected.")
42 
43 
44 @click.command()
45 @click.option("--project", "-p", type = str, help = "Project name")
46 @click.option("--name", "-n", type = str, help = "New Project name")
47 @click.option("--description", "-d", type = str, help = "New Project description")
48 def edit(project: Optional[str], name: Optional[str], description: Optional[str]) -> None:
49  userConfiguration = UserConfiguration.load()
50  defaultProjectId = userConfiguration.projectId
51  if defaultProjectId is None and project is None:
52  ui.errorEcho(f"To use edit command you need to specifiy project name using \"--project\" or \"-p\" flag, or you can select default project using \"coretex project select\" command.")
53  return
54 
55  if project is None and defaultProjectId is not None:
56  selectedProject = Project.fetchById(defaultProjectId)
57  else:
58  selectedProject = Project.fetchOne(name = project)
59 
60  if name is None:
61  name = ui.clickPrompt("Please enter new name for your project", type = str, default = selectedProject.name)
62 
63  if description is None:
64  description = ui.clickPrompt("Please enter new description for your project", type = str, default = selectedProject.description, show_default = False)
65 
66  try:
67  selectedProject.update(name = selectedProject.name, description = description)
68  selectedProject.updateVisibility(ProjectVisibility.private)
69  ui.successEcho(f"Project id \"{selectedProject.id}\" successfully edited.")
70 
71  except RequestFailedError:
72  raise click.ClickException(f"Failed to edit project \"{selectedProject.name}\".")
73 
74 
75 @click.command()
76 @click.argument("name", type = str)
77 def select(name: str) -> None:
78  project: Optional[Project] = None
79  userConfig = UserConfiguration.load()
80 
81  ui.progressEcho("Validating project...")
82 
83  try:
84  project = Project.fetchOne(name = name)
85  ui.successEcho(f"Project \"{name}\" selected successfully!")
86  userConfig.selectProject(project.id)
87  except ValueError:
88  ui.errorEcho(f"Project \"{name}\" not found.")
89  project = project_utils.promptProjectCreate(
90  "Do you want to create a project with that name?",
91  name,
92  userConfig.frontendUrl
93  )
94  if project is None:
95  return
96 
97  userConfig.selectProject(project.id)
98 
99 
100 @click.group()
101 @onBeforeCommandExecute(initializeUserSession)
102 def project() -> None:
103  pass
104 
105 
106 project.add_command(create, "create")
107 project.add_command(edit, "edit")
108 project.add_command(select, "select")