ciyidogan commited on
Commit
75195a9
·
verified ·
1 Parent(s): d0344ba

Update project_controller.py

Browse files
Files changed (1) hide show
  1. project_controller.py +32 -115
project_controller.py CHANGED
@@ -14,43 +14,49 @@ def get_utc_now():
14
  def list_projects():
15
  return service_config.projects
16
 
17
- @router.post("/add")
18
- async def add_project(request: Request):
19
- data = await request.json()
20
- project_name = data.get("project_name")
21
- if not project_name:
22
- raise HTTPException(status_code=400, detail="project_name cannot be empty")
23
- if project_name in service_config.projects:
24
- raise HTTPException(status_code=400, detail="Project already exists")
25
-
26
- service_config.projects[project_name] = {
27
- "enabled": False,
28
- "versions": [],
29
- "last_updated": get_utc_now()
 
 
 
30
  }
31
- with open(service_config.config_path, "w", encoding="utf-8") as f:
32
- json.dump(service_config, f, indent=2)
33
- return {"message": f"Project {project_name} added"}
34
 
35
  @router.post("/update")
36
  async def update_project(request: Request):
37
  data = await request.json()
38
  project_name = data.get("project_name")
39
  client_last_updated = data.get("client_last_updated")
 
40
 
41
  project = service_config.projects.get(project_name)
42
  if not project:
43
  raise HTTPException(status_code=404, detail="Project not found")
44
 
45
  if project["last_updated"] != client_last_updated:
46
- raise HTTPException(status_code=409, detail="This record has been updated by another user. Please reload.")
47
 
48
- # örnek update: sadece enable/disable flag güncelliyoruz
49
- project["enabled"] = data.get("enabled", project["enabled"])
 
50
 
 
 
51
  project["last_updated"] = get_utc_now()
 
52
  with open(service_config.config_path, "w", encoding="utf-8") as f:
53
  json.dump(service_config, f, indent=2)
 
54
  return {"message": f"Project {project_name} updated"}
55
 
56
  @router.post("/publish")
@@ -64,112 +70,23 @@ async def publish_project(request: Request):
64
  raise HTTPException(status_code=404, detail="Project not found")
65
 
66
  if project["last_updated"] != client_last_updated:
67
- raise HTTPException(status_code=409, detail="This record has been updated by another user. Please reload.")
68
 
69
- latest_version = project["versions"][-1] if project["versions"] else None
70
- if not latest_version:
71
- raise HTTPException(status_code=400, detail="No version exists to publish")
72
 
73
- llm = latest_version.get("llm", {})
74
  if not llm.get("repo_id"):
75
  raise HTTPException(status_code=400, detail="repo_id is required")
76
  if llm.get("use_fine_tune") and not llm.get("fine_tune_zip"):
77
  raise HTTPException(status_code=400, detail="fine_tune_zip is required when use_fine_tune is true")
78
 
79
- latest_version["published"] = True
80
- latest_version["last_updated"] = get_utc_now()
81
  project["last_updated"] = get_utc_now()
82
 
83
  with open(service_config.config_path, "w", encoding="utf-8") as f:
84
  json.dump(service_config, f, indent=2)
85
 
86
  return {"message": f"Project {project_name} version published"}
87
-
88
- @router.post("/add_intent")
89
- async def add_intent(request: Request):
90
- data = await request.json()
91
- project_name = data.get("project_name")
92
- version_number = data.get("version_number")
93
- intent = data.get("intent")
94
- client_last_updated = data.get("client_last_updated")
95
-
96
- project = service_config.projects.get(project_name)
97
- if not project:
98
- raise HTTPException(status_code=404, detail="Project not found")
99
-
100
- if project["last_updated"] != client_last_updated:
101
- raise HTTPException(status_code=409, detail="This record has been updated by another user. Please reload.")
102
-
103
- version = next((v for v in project["versions"] if v["version_number"] == version_number), None)
104
- if not version:
105
- raise HTTPException(status_code=404, detail="Version not found")
106
-
107
- if any(i["name"] == intent["name"] for i in version.get("intents", [])):
108
- raise HTTPException(status_code=400, detail="Intent already exists")
109
-
110
- version.setdefault("intents", []).append(intent)
111
- version["last_updated"] = get_utc_now()
112
- project["last_updated"] = get_utc_now()
113
-
114
- with open(service_config.config_path, "w", encoding="utf-8") as f:
115
- json.dump(service_config, f, indent=2)
116
-
117
- return {"message": f"Intent {intent['name']} added to project {project_name}, version {version_number}"}
118
-
119
- @router.post("/delete_intent")
120
- async def delete_intent(request: Request):
121
- data = await request.json()
122
- project_name = data.get("project_name")
123
- version_number = data.get("version_number")
124
- intent_name = data.get("intent_name")
125
- client_last_updated = data.get("client_last_updated")
126
-
127
- project = service_config.projects.get(project_name)
128
- if not project:
129
- raise HTTPException(status_code=404, detail="Project not found")
130
-
131
- if project["last_updated"] != client_last_updated:
132
- raise HTTPException(status_code=409, detail="This record has been updated by another user. Please reload.")
133
-
134
- version = next((v for v in project["versions"] if v["version_number"] == version_number), None)
135
- if not version:
136
- raise HTTPException(status_code=404, detail="Version not found")
137
-
138
- version["intents"] = [i for i in version.get("intents", []) if i["name"] != intent_name]
139
- version["last_updated"] = get_utc_now()
140
- project["last_updated"] = get_utc_now()
141
-
142
- with open(service_config.config_path, "w", encoding="utf-8") as f:
143
- json.dump(service_config, f, indent=2)
144
-
145
- return {"message": f"Intent {intent_name} deleted from project {project_name}, version {version_number}"}
146
-
147
- @router.post("/add_api")
148
- async def add_api(request: Request):
149
- data = await request.json()
150
- api_name = data.get("api_name")
151
- api_def = data.get("api_def")
152
-
153
- if api_name in service_config.apis:
154
- raise HTTPException(status_code=400, detail="API already exists")
155
-
156
- service_config.apis[api_name] = api_def
157
-
158
- with open(service_config.config_path, "w", encoding="utf-8") as f:
159
- json.dump(service_config, f, indent=2)
160
-
161
- return {"message": f"API {api_name} added"}
162
-
163
- @router.post("/delete_api")
164
- async def delete_api(request: Request):
165
- data = await request.json()
166
- api_name = data.get("api_name")
167
- if api_name not in service_config.apis:
168
- raise HTTPException(status_code=404, detail="API not found")
169
-
170
- del service_config.apis[api_name]
171
-
172
- with open(service_config.config_path, "w", encoding="utf-8") as f:
173
- json.dump(service_config, f, indent=2)
174
-
175
- return {"message": f"API {api_name} deleted"}
 
14
  def list_projects():
15
  return service_config.projects
16
 
17
+ @router.get("/{project_name}/latest")
18
+ def get_latest_project_version(project_name: str):
19
+ project = service_config.projects.get(project_name)
20
+ if not project:
21
+ raise HTTPException(status_code=404, detail="Project not found")
22
+
23
+ if not project["versions"]:
24
+ raise HTTPException(status_code=404, detail="No versions found")
25
+
26
+ latest = max(project["versions"], key=lambda v: v["version_number"])
27
+
28
+ return {
29
+ "version_number": latest["version_number"],
30
+ "published": latest.get("published", False),
31
+ "intents": latest.get("intents", []),
32
+ "last_updated": project["last_updated"]
33
  }
 
 
 
34
 
35
  @router.post("/update")
36
  async def update_project(request: Request):
37
  data = await request.json()
38
  project_name = data.get("project_name")
39
  client_last_updated = data.get("client_last_updated")
40
+ new_data = data.get("new_data")
41
 
42
  project = service_config.projects.get(project_name)
43
  if not project:
44
  raise HTTPException(status_code=404, detail="Project not found")
45
 
46
  if project["last_updated"] != client_last_updated:
47
+ raise HTTPException(status_code=409, detail="Record updated by another user. Please reload.")
48
 
49
+ latest = max(project["versions"], key=lambda v: v["version_number"])
50
+ if latest.get("published"):
51
+ raise HTTPException(status_code=400, detail="Cannot edit a published version.")
52
 
53
+ # Update latest version data (simplified for example)
54
+ latest.update(new_data)
55
  project["last_updated"] = get_utc_now()
56
+
57
  with open(service_config.config_path, "w", encoding="utf-8") as f:
58
  json.dump(service_config, f, indent=2)
59
+
60
  return {"message": f"Project {project_name} updated"}
61
 
62
  @router.post("/publish")
 
70
  raise HTTPException(status_code=404, detail="Project not found")
71
 
72
  if project["last_updated"] != client_last_updated:
73
+ raise HTTPException(status_code=409, detail="Record updated by another user. Please reload.")
74
 
75
+ latest = max(project["versions"], key=lambda v: v["version_number"])
76
+ if latest.get("published"):
77
+ raise HTTPException(status_code=400, detail="Version already published.")
78
 
79
+ llm = latest.get("llm", {})
80
  if not llm.get("repo_id"):
81
  raise HTTPException(status_code=400, detail="repo_id is required")
82
  if llm.get("use_fine_tune") and not llm.get("fine_tune_zip"):
83
  raise HTTPException(status_code=400, detail="fine_tune_zip is required when use_fine_tune is true")
84
 
85
+ latest["published"] = True
86
+ latest["last_updated"] = get_utc_now()
87
  project["last_updated"] = get_utc_now()
88
 
89
  with open(service_config.config_path, "w", encoding="utf-8") as f:
90
  json.dump(service_config, f, indent=2)
91
 
92
  return {"message": f"Project {project_name} version published"}