-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoutput.py
More file actions
1209 lines (1069 loc) · 52 KB
/
output.py
File metadata and controls
1209 lines (1069 loc) · 52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
CodeCraft PMS Backend Project
파일명 : output.py
생성자 : 김창환
생성일 : 2024/10/20
업데이트 : 2025/04/26
설명 : 산출물의 생성, 수정, 조회, 삭제, 업로드를 위한 API 엔드포인트 정의
"""
from fastapi import APIRouter, HTTPException, File, UploadFile, Form
from fastapi.responses import FileResponse
from starlette.background import BackgroundTask
from pydantic import BaseModel
from dotenv import load_dotenv
from datetime import datetime
from urllib.parse import quote
from logger import logger
from typing import List
import sys, os, random, requests, json, logging, shutil, subprocess
sys.path.append(os.path.abspath('/data/Database Project')) # Database Project와 연동하기 위해 사용
import output_DB
import push
router = APIRouter()
class SummaryDocumentPayload(BaseModel):
"""프로젝트 개요서 간단본 모델"""
pname: str
pteam: str
psummary: str
pstart: str
pend: str
prange: str
poutcomes: str
add_date: str
pid: int = None
doc_s_no: int = None # 추가: 수정 작업에서 사용되며, 추가 작업에서는 선택적(None)
class OverviewDocumentPayload(BaseModel):
"""프로젝트 개요서 상세본 모델"""
pname: str
pteam: str
poverview: str
poutcomes: str
pgoals: str
pstart: str
pend: str
prange: str
pstack: str
add_date: str
pid: int = None
doc_s_no: int = None
class DocumentDeletePayload(BaseModel):
"""산출물 삭제 모델"""
doc_s_no: int
class DocumentFetchPayload(BaseModel):
"""산출물 조회 모델"""
pid: int
class MeetingMinutesPayload(BaseModel):
"""회의록 모델"""
main_agenda: str # 안건
date_time: str # 일시
location: str # 장소
participants: str # 참석인원
responsible_person: str # 책임자명
meeting_content: str # 회의 내용
meeting_outcome: str # 회의 결과
pid: int = None
doc_m_no: int = None
class ReqSpecPayload(BaseModel):
"""요구사항 명세서 모델"""
feature_name: str
description: str
priority: int
non_functional_requirement_name: str
non_functional_description: str
non_functional_priority: int
system_item: str
system_description: str
add_date: str
pid: int = None
doc_r_no: int = None
class TestCasePayload(BaseModel):
"""테스트 케이스 모델"""
doc_t_group1: str
doc_t_name: str
doc_t_start: str
doc_t_end: str
doc_t_pass: int
doc_t_group1no: int
class MultipleTestCasesPayload(BaseModel):
"""여러 개의 테스트 케이스 추가 모델"""
pid: int
testcases: List[TestCasePayload]
class ReportPayload(BaseModel):
"""보고서 모델"""
rname: str
rwriter: str
rdate: str
pname: str
pmember: str
pprof: str
presearch: str
pdesign: str
parch: str
presult: str
pconc: str
pid: int = None
doc_rep_no: int = None
class OtherDocumentPayload(BaseModel):
"""기타 산출물 모델"""
file_unique_id: str = None
pid: int = None
class FilePathEditPayload(BaseModel):
"""파일 경로 수정 모델"""
file_unique_id: str
new_file_path: str
class FileNameEditPayload(BaseModel):
"""파일 이름 수정 모델"""
file_unique_id: str
new_file_name: str
class DownloadPayload(BaseModel):
"""파일 다운로드 모델"""
file_type: int
class OtherDocDownloadPayload(BaseModel):
"""
기타 산출물 다운로드 요청 모델
"""
file_no: int
TEMP_DOWNLOAD_DIR = "/data/tmp"
STORAGE_API_KEY = os.getenv('ST_KEY')
STORAGE_SERVER_URL = "http://192.168.50.84:10080/api/output"
@router.post("/output/sum_doc_add")
async def add_summary_document(payload: SummaryDocumentPayload):
"""프로젝트 개요서 간단본 추가 API"""
try:
logger.info(f"Adding summary document for project {payload.pid}")
document_id = output_DB.add_summary_document(
pname=payload.pname,
pteam=payload.pteam,
psummary=payload.psummary,
pstart=payload.pstart,
pend=payload.pend,
prange=payload.prange,
poutcomes=payload.poutcomes,
add_date=payload.add_date,
pid=payload.pid
)
logger.info(f"Summary document added: ID = {document_id}")
return {"RESULT_CODE": 200, "RESULT_MSG": "Summary document added successfully", "PAYLOADS": {"doc_s_no": document_id}}
except Exception as e:
logger.error(f"Error adding summary document for project {payload.pid}: {e}", exc_info=True)
raise HTTPException(status_code=500, detail=f"Error adding summary document: {e}")
@router.post("/output/sum_doc_edit")
async def edit_summary_document(payload: SummaryDocumentPayload):
"""프로젝트 개요서 간단본 수정 API"""
try:
logger.info(f"Editing summary document ID: {payload.doc_s_no}")
result = output_DB.edit_summary_document(
pname=payload.pname,
pteam=payload.pteam,
psummary=payload.psummary,
pstart=payload.pstart,
pend=payload.pend,
prange=payload.prange,
poutcomes=payload.poutcomes,
add_date=payload.add_date,
doc_s_no=payload.doc_s_no
)
if result:
logger.info(f"Summary document updated successfully: ID = {payload.doc_s_no}")
return {"RESULT_CODE": 200, "RESULT_MSG": "Summary document updated successfully"}
else:
raise HTTPException(status_code=500, detail="Failed to update summary document")
except Exception as e:
logger.error(f"Error editing summary document ID {payload.doc_s_no}: {e}", exc_info=True)
raise HTTPException(status_code=500, detail=f"Error editing summary document: {e}")
@router.post("/output/sum_doc_delete")
async def delete_summary_document(payload: DocumentDeletePayload):
"""프로젝트 개요서 간단본 삭제 API"""
try:
logger.info(f"Deleting summary document ID: {payload.doc_s_no}")
result = output_DB.delete_summary_document(payload.doc_s_no)
if result:
logger.info(f"Summary document deleted: ID = {payload.doc_s_no}")
return {"RESULT_CODE": 200, "RESULT_MSG": "Summary document deleted successfully"}
else:
raise HTTPException(status_code=500, detail="Failed to delete summary document")
except Exception as e:
logger.error(f"Error deleting summary document ID {payload.doc_s_no}: {e}", exc_info=True)
raise HTTPException(status_code=500, detail=f"Error deleting summary document: {e}")
@router.post("/output/sum_doc_fetch")
async def fetch_all_summary_documents(payload: DocumentFetchPayload):
"""프로젝트 개요서 간단본 조회 API"""
try:
logger.info(f"Fetching all summary documents for project {payload.pid}")
documents = output_DB.fetch_all_summary_documents(payload.pid)
logger.info(f"Retrieved {len(documents)} summary documents for project {payload.pid}")
return {"RESULT_CODE": 200, "RESULT_MSG": "Summary documents fetched successfully", "PAYLOADS": documents}
except Exception as e:
logger.error(f"Error fetching summary documents for project {payload.pid}: {e}", exc_info=True)
raise HTTPException(status_code=500, detail=f"Error fetching summary documents: {e}")
@router.post("/output/ovr_doc_add")
async def add_overview_document(payload: OverviewDocumentPayload):
"""프로젝트 개요서 상세본 추가 API"""
try:
logger.info(f"Adding overview document for project {payload.pid}")
document_id = output_DB.add_overview_document(
pname=payload.pname,
pteam=payload.pteam,
poverview=payload.poverview,
poutcomes=payload.poutcomes,
pgoals=payload.pgoals,
pstart=payload.pstart,
pend=payload.pend,
prange=payload.prange,
pstack=payload.pstack,
add_date=payload.add_date,
pid=payload.pid
)
logger.info(f"Overview document added: ID = {document_id}")
return {"RESULT_CODE": 200, "RESULT_MSG": "Overview document added successfully", "PAYLOADS": {"doc_s_no": document_id}}
except Exception as e:
logger.error(f"Error adding overview document for project {payload.pid}: {e}", exc_info=True)
raise HTTPException(status_code=500, detail=f"Error adding overview document: {e}")
@router.post("/output/ovr_doc_edit")
async def edit_overview_document(payload: OverviewDocumentPayload):
"""프로젝트 개요서 상세본 수정 API"""
try:
logger.info(f"Editing overview document ID: {payload.doc_s_no}")
result = output_DB.edit_overview_document(
pname=payload.pname,
pteam=payload.pteam,
poverview=payload.poverview,
poutcomes=payload.poutcomes,
pgoals=payload.pgoals,
pstart=payload.pstart,
pend=payload.pend,
prange=payload.prange,
pstack=payload.pstack,
add_date=payload.add_date,
doc_s_no=payload.doc_s_no
)
if result:
logger.info(f"Overview document updated successfully: ID = {payload.doc_s_no}")
return {"RESULT_CODE": 200, "RESULT_MSG": "Overview document updated successfully"}
else:
raise HTTPException(status_code=500, detail="Failed to update overview document")
except Exception as e:
logger.error(f"Error editing overview document ID {payload.doc_s_no}: {e}", exc_info=True)
raise HTTPException(status_code=500, detail=f"Error editing overview document: {e}")
@router.post("/output/ovr_doc_fetch")
async def fetch_all_overview_documents(payload: DocumentFetchPayload):
"""프로젝트 개요서 상세본 조회 API"""
try:
logger.info(f"Fetching all overview documents for project {payload.pid}")
documents = output_DB.fetch_all_overview_documents(payload.pid)
logger.info(f"Retrieved {len(documents)} overview documents for project {payload.pid}")
return {"RESULT_CODE": 200, "RESULT_MSG": "Overview documents fetched successfully", "PAYLOADS": documents}
except Exception as e:
logger.error(f"Error fetching overview documents for project {payload.pid}: {e}", exc_info=True)
raise HTTPException(status_code=500, detail=f"Error fetching overview documents: {e}")
@router.post("/output/sum_doc_fetch_one")
async def fetch_one_summary_document(payload: DocumentDeletePayload):
"""특정 프로젝트 개요서 간단본 조회 API"""
try:
logger.info(f"Fetching summary document ID: {payload.doc_s_no}")
document = output_DB.fetch_one_summary_document(payload.doc_s_no)
if document:
logger.info(f"Summary document fetched successfully: ID = {payload.doc_s_no}")
return {"RESULT_CODE": 200, "RESULT_MSG": "Summary document fetched successfully", "PAYLOADS": document}
else:
raise HTTPException(status_code=404, detail="Summary document not found")
except Exception as e:
logger.error(f"Error fetching summary document ID {payload.doc_s_no}: {e}", exc_info=True)
raise HTTPException(status_code=500, detail=f"Error fetching summary document: {e}")
@router.post("/output/mm_add")
async def add_mm_document(payload: MeetingMinutesPayload):
"""회의록 추가 API"""
try:
logger.info(f"Adding meeting minutes document for project {payload.pid}")
result = output_DB.add_meeting_minutes(
main_agenda=payload.main_agenda,
date_time=payload.date_time,
location=payload.location,
participants=payload.participants,
responsible_person=payload.responsible_person,
meeting_content=payload.meeting_content,
meeting_outcome=payload.meeting_outcome,
pid=payload.pid
)
logger.info(f"Meeting minutes document added successfully: ID = {result}")
return {"RESULT_CODE": 200, "RESULT_MSG": "MM document added successfully", "PAYLOADS": {"doc_m_no": result}}
except Exception as e:
logger.error(f"Error adding meeting minutes document: {e}", exc_info=True)
raise HTTPException(status_code=500, detail=f"Error add mm document: {e}")
@router.post("/output/mm_edit")
async def edit_mm_document(payload: MeetingMinutesPayload):
"""회의록 수정 API"""
try:
logger.info(f"Editing meeting minutes document ID: {payload.doc_m_no}")
result = output_DB.edit_meeting_minutes(
main_agenda=payload.main_agenda,
date_time=payload.date_time,
location=payload.location,
participants=payload.participants,
responsible_person=payload.responsible_person,
meeting_content=payload.meeting_content,
meeting_outcome=payload.meeting_outcome,
doc_m_no=payload.doc_m_no
)
if result:
logger.info(f"Meeting minutes document updated successfully: ID = {payload.doc_m_no}")
return {"RESULT_CODE": 200, "RESULT_MSG": "MM document edit successfully"}
else:
logger.warning(f"Meeting minutes document edit failed: ID = {payload.doc_m_no}")
return {"RESULT_CODE": 500, "RESULT_MSG": "MM document edit failed"}
except Exception as e:
logger.error(f"Error editing meeting minutes document ID {payload.doc_m_no}: {e}", exc_info=True)
raise HTTPException(status_code=500, detail=f"Error edit mm document: {e}")
@router.post("/output/mm_delete")
async def delete_mm_document(payload: DocumentDeletePayload):
"""
회의록 삭제 API
"""
try:
result = output_DB.delete_meeting_minutes(payload.doc_s_no)
if result:
logger.debug(f"MM document deleted: doc_s_no={payload.doc_s_no}")
return {"RESULT_CODE": 200, "RESULT_MSG": "MM document deleted successfully"}
else:
raise HTTPException(status_code=500, detail="Failed to delete MM document")
except Exception as e:
logger.error(f"Error deleting MM document [doc_s_no={payload.doc_s_no}]: {e}")
raise HTTPException(status_code=500, detail=f"Error deleting MM document: {e}")
@router.post("/output/mm_fetch_one")
async def fetch_one_meeting_minutes(payload: DocumentDeletePayload):
"""
특정 회의록 조회 API
"""
try:
meeting = output_DB.fetch_one_meeting_minutes(payload.doc_s_no)
if meeting:
logger.debug(f"Fetched one MM document: doc_s_no={payload.doc_s_no}")
return {"RESULT_CODE": 200, "RESULT_MSG": "Meeting minutes fetched successfully", "PAYLOADS": meeting}
else:
raise HTTPException(status_code=404, detail="Meeting minutes not found")
except Exception as e:
logger.error(f"Error fetching one MM document [doc_s_no={payload.doc_s_no}]: {e}")
raise HTTPException(status_code=500, detail=f"Error fetching meeting minutes: {e}")
@router.post("/output/mm_fetch")
async def fetch_all_meeting_minutes(payload: DocumentFetchPayload):
"""
회의록 조회 API
"""
try:
documents = output_DB.fetch_all_meeting_minutes(payload.pid)
logger.debug(f"Fetched all MM documents: pid={payload.pid}, count={len(documents)}")
return {"RESULT_CODE": 200, "RESULT_MSG": "MM documents fetched successfully", "PAYLOADS": documents}
except Exception as e:
logger.error(f"Error fetching MM documents [pid={payload.pid}]: {e}")
raise HTTPException(status_code=500, detail=f"Error fetching MM documents: {e}")
@router.post("/output/reqspec_add")
async def add_reqspec(payload: ReqSpecPayload):
"""
요구사항 명세서 추가 API
"""
try:
result = output_DB.add_reqspec(
feature_name=payload.feature_name,
description=payload.description,
priority=payload.priority,
non_functional_requirement_name=payload.non_functional_requirement_name,
non_functional_description=payload.non_functional_description,
non_functional_priority=payload.non_functional_priority,
system_item=payload.system_item,
system_description=payload.system_description,
add_date=payload.add_date,
pid=payload.pid
)
logger.debug(f"ReqSpec document added: pid={payload.pid}, doc_r_no={result}")
return {"RESULT_CODE": 200, "RESULT_MSG": "ReqSpec document added successfully", "PAYLOADS": {"doc_r_no": result}}
except Exception as e:
logger.error(f"Error adding ReqSpec document [pid={payload.pid}]: {e}")
raise HTTPException(status_code=500, detail=f"Error add ReqSpec document: {e}")
@router.post("/output/reqspec_edit")
async def edit_reqspec(payload: ReqSpecPayload):
"""
요구사항 명세서 수정 API
"""
try:
result = output_DB.edit_reqspec(
feature_name=payload.feature_name,
description=payload.description,
priority=payload.priority,
non_functional_requirement_name=payload.non_functional_requirement_name,
non_functional_description=payload.non_functional_description,
non_functional_priority=payload.non_functional_priority,
system_item=payload.system_item,
system_description=payload.system_description,
doc_r_date=payload.add_date,
doc_r_no=payload.doc_r_no
)
if result:
logger.debug(f"ReqSpec document updated: doc_r_no={payload.doc_r_no}")
return {"RESULT_CODE": 200, "RESULT_MSG": "Requirement specification updated successfully"}
else:
raise HTTPException(status_code=500, detail="Failed to update requirement specification")
except Exception as e:
logger.error(f"Error editing ReqSpec document [doc_r_no={payload.doc_r_no}]: {e}")
raise HTTPException(status_code=500, detail=f"Error editing requirement specification: {e}")
@router.post("/output/reqspec_fetch_all")
async def fetch_all_reqspec(payload: DocumentFetchPayload):
"""
요구사항 명세서 조회 API
"""
try:
documents = output_DB.fetch_all_reqspec(payload.pid)
logger.debug(f"Fetched {len(documents)} requirement specifications for pid={payload.pid}")
return {"RESULT_CODE": 200, "RESULT_MSG": "Requirement specifications fetched successfully", "PAYLOADS": documents}
except Exception as e:
logger.error(f"Error [fetch_all_reqspec] for pid={payload.pid}: {e}")
raise HTTPException(status_code=500, detail=f"Error fetching requirement specifications: {e}")
@router.post("/output/reqspec_delete")
async def delete_reqspec(payload: DocumentDeletePayload):
"""
요구사항 명세서 삭제 API
"""
try:
result = output_DB.delete_reqspec(payload.doc_s_no)
if result:
logger.debug(f"Requirement specification deleted: doc_s_no={payload.doc_s_no}")
return {"RESULT_CODE": 200, "RESULT_MSG": "Requirement specification deleted successfully"}
else:
raise HTTPException(status_code=500, detail="Failed to delete requirement specification")
except Exception as e:
logger.error(f"Error [delete_reqspec] for doc_s_no={payload.doc_s_no}: {e}")
raise HTTPException(status_code=500, detail=f"Error deleting requirement specification: {e}")
def init_testcase(data, pid):
try:
result = output_DB.add_multiple_testcase(data, pid)
if isinstance(result, Exception):
logger.error(f"Failed to add initial test case data for project {pid}: {str(result)}", exc_info=True)
raise Exception(f"Failed to add init Testcase data. Error: {str(result)}")
logger.info(f"Project {pid} has been successfully initialized with test cases")
return {"RESULT_CODE": 200, "RESULT_MSG": "Testcase init successful"}
except Exception as e:
logger.error(f"Error occurred while initializing test cases for project {pid}: {str(e)}", exc_info=True)
raise HTTPException(status_code=500, detail=f"Error during Testcase batch update: {str(e)}")
@router.post("/output/testcase_update")
async def update_tastcase(payload: MultipleTestCasesPayload):
"""WBS 스타일의 TC 업데이트 API"""
try:
logger.info(f"Received request to update test cases for project {payload.pid}")
delete_result = output_DB.delete_all_testcase(payload.pid)
logger.info(f"Existing test cases deleted for project {payload.pid}")
testcase_data = [
[
tc.doc_t_group1,
tc.doc_t_name,
tc.doc_t_start,
tc.doc_t_end,
tc.doc_t_pass,
tc.doc_t_group1no
]
for tc in payload.testcases
]
add_result = output_DB.add_multiple_testcase(testcase_data, payload.pid)
if add_result != True:
logger.error(f"Failed to add new test cases for project {payload.pid}: {add_result}", exc_info=True)
raise HTTPException(status_code=500, detail=f"Failed to add new test cases. Error: {add_result}")
logger.info(f"Successfully updated test cases for project {payload.pid}")
return {"RESULT_CODE": 200, "RESULT_MSG": "Test cases updated successfully"}
except Exception as e:
logger.error(f"Error occurred while updating test cases for project {payload.pid}: {str(e)}", exc_info=True)
raise HTTPException(status_code=500, detail=f"Error during test case update: {str(e)}")
@router.post("/output/testcase_add") # TC 컨셉 변경으로 인한 미사용 (25.03.24)
async def add_multiple_testcase(payload: MultipleTestCasesPayload):
""" 테스트 케이스 추가 API """
try:
testcase_data = [
[
tc.doc_t_group1,
tc.doc_t_name,
tc.doc_t_start,
tc.doc_t_end,
tc.doc_t_pass,
tc.doc_t_group1no
]
for tc in payload.testcases
]
result = output_DB.add_multiple_testcase(testcase_data, payload.pid)
if result is True:
logger.debug(f"Added {len(testcase_data)} test cases for pid={payload.pid}")
return {"RESULT_CODE": 200, "RESULT_MSG": "Test cases added successfully"}
else:
raise HTTPException(status_code=500, detail=f"Error adding test cases: {result}")
except Exception as e:
logger.error(f"Error [add_multiple_testcase] for pid={payload.pid}: {e}")
raise HTTPException(status_code=500, detail=f"Error adding test cases: {e}")
@router.post("/output/testcase_load")
async def fetch_all_testcase(payload: DocumentFetchPayload):
"""
테스트 케이스 조회 API
"""
try:
testcases = output_DB.fetch_all_testcase(payload.pid)
logger.debug(f"Fetched {len(testcases)} test cases for pid={payload.pid}")
return {"RESULT_CODE": 200, "RESULT_MSG": "Test cases fetched successfully", "PAYLOADS": testcases}
except Exception as e:
logger.error(f"Error [fetch_all_testcase] for pid={payload.pid}: {e}")
raise HTTPException(status_code=500, detail=f"Error fetching test cases: {e}")
@router.post("/output/testcase_delete")
async def delete_all_testcase(payload: DocumentFetchPayload):
"""
테스트 케이스 삭제 API
"""
try:
result = output_DB.delete_all_testcase(payload.pid)
if result is True:
logger.debug(f"Deleted all test cases for pid={payload.pid}")
return {"RESULT_CODE": 200, "RESULT_MSG": "All test cases deleted successfully"}
else:
raise HTTPException(status_code=500, detail=f"Error deleting test cases: {result}")
except Exception as e:
logger.error(f"Error [delete_all_testcase] for pid={payload.pid}: {e}")
raise HTTPException(status_code=500, detail=f"Error deleting test cases: {e}")
@router.post("/output/report_add")
async def add_report(payload: ReportPayload):
"""보고서 추가 API"""
try:
result = output_DB.add_report(
doc_rep_name=payload.rname,
doc_rep_writer=payload.rwriter,
doc_rep_date=payload.rdate,
doc_rep_pname=payload.pname,
doc_rep_member=payload.pmember,
doc_rep_professor=payload.pprof,
doc_rep_research=payload.presearch,
doc_rep_design=payload.pdesign,
doc_rep_arch=payload.parch,
doc_rep_result=payload.presult,
doc_rep_conclusion=payload.pconc,
pid=payload.pid
)
logger.debug(f"Added report document for pid={payload.pid}, doc_rep_no={result}")
return {"RESULT_CODE": 200, "RESULT_MSG": "Report document added successfully", "PAYLOADS": {"doc_rep_no": result}}
except Exception as e:
logger.error(f"Error [add_report] for pid={payload.pid}: {e}")
raise HTTPException(status_code=500, detail=f"Error adding report document: {e}")
@router.post("/output/report_edit")
async def edit_report(payload: ReportPayload):
"""
보고서 수정 API
"""
try:
result = output_DB.edit_report(
doc_rep_name=payload.rname,
doc_rep_writer=payload.rwriter,
doc_rep_date=payload.rdate,
doc_rep_pname=payload.pname,
doc_rep_member=payload.pmember,
doc_rep_professor=payload.pprof,
doc_rep_research=payload.presearch,
doc_rep_design=payload.pdesign,
doc_rep_arch=payload.parch,
doc_rep_result=payload.presult,
doc_rep_conclusion=payload.pconc,
doc_rep_no=payload.doc_rep_no
)
if result:
logger.debug(f"Updated report document: doc_rep_no={payload.doc_rep_no}")
return {"RESULT_CODE": 200, "RESULT_MSG": "Report updated successfully"}
else:
raise HTTPException(status_code=500, detail="Failed to update report")
except Exception as e:
logger.error(f"Error [edit_report] for doc_rep_no={payload.doc_rep_no}: {e}")
raise HTTPException(status_code=500, detail=f"Error editing report: {e}")
@router.post("/output/report_fetch_all")
async def fetch_all_report(payload: DocumentFetchPayload):
"""
보고서 조회 API
"""
try:
report = output_DB.fetch_all_report(payload.pid)
logger.debug(f"Fetched {len(report)} reports for pid={payload.pid}")
return {"RESULT_CODE": 200, "RESULT_MSG": "Report fetched successfully", "PAYLOADS": report}
except Exception as e:
logger.error(f"Error [fetch_all_report] for pid={payload.pid}: {e}")
raise HTTPException(status_code=500, detail=f"Error fetching report: {e}")
@router.post("/output/report_delete")
async def delete_report(payload: DocumentDeletePayload):
"""
보고서 삭제 API
"""
try:
result = output_DB.delete_report(payload.doc_s_no)
if result:
logger.debug(f"Deleted report document: doc_s_no={payload.doc_s_no}")
return {"RESULT_CODE": 200, "RESULT_MSG": "Report deleted successfully"}
else:
raise HTTPException(status_code=500, detail="Failed to delete report")
except Exception as e:
logger.error(f"Error [delete_report] for doc_s_no={payload.doc_s_no}: {e}")
raise HTTPException(status_code=500, detail=f"Error deleting report: {e}")
def gen_file_uid():
"""파일 고유 ID 생성"""
while True:
tmp_uid = random.randint(1, 2_147_483_647) # 본래 9,999,999이었으나, int형 최대 범위 때문에 줄임
if not output_DB.is_uid_exists(tmp_uid):
return tmp_uid
@router.post("/output/otherdoc_add")
async def add_other_documents(
files: List[UploadFile] = File(...),
pid: int = Form(...),
univ_id: int = Form(...)
):
"""
기타 산출물 추가 API
"""
logger.info("------------------------------------------------------------")
logger.info("Starting process to add other documents")
try:
load_dotenv()
logger.info("Environment variables loaded successfully")
headers = {"Authorization": os.getenv('ST_KEY')}
attachments = []
total_files = len(files)
logger.info(f"Number of files to process: {total_files}")
for idx, file in enumerate(files, start=1):
logger.info(f"[{idx}/{total_files}] Processing file: {file.filename}")
file_unique_id = gen_file_uid()
logger.info(f"Generated file unique id: {file_unique_id} for file: {file.filename}")
data = {
"fuid": file_unique_id,
"pid": pid,
"userid": univ_id
}
file_content = await file.read()
logger.info(f"Read {len(file_content)} bytes from file: {file.filename}")
files_payload = {"file": (file.filename, file_content, file.content_type)}
# 최대 3회 재시도
max_attempts = 3
for attempt in range(1, max_attempts + 1):
try:
logger.info(f"Uploading file {file.filename} to storage server (Attempt {attempt})")
response = requests.post(
"http://192.168.50.84:10080/api/output/otherdoc_add",
files=files_payload,
data=data,
headers=headers,
timeout=60
)
if response.status_code == 200:
logger.info(f"File upload succeeded on attempt {attempt} for {file.filename}")
break
else:
error_msg = (f"File upload failed for {file.filename} on attempt {attempt} "
f"with status code {response.status_code}: {response.text}")
logger.error(error_msg)
if attempt == max_attempts:
raise HTTPException(
status_code=response.status_code,
detail=error_msg
)
except requests.exceptions.RequestException as req_exc:
logger.error(
f"RequestException on attempt {attempt} for file {file.filename}: {str(req_exc)}",
exc_info=True
)
if attempt == max_attempts:
raise HTTPException(
status_code=500,
detail=f"Request failed for {file.filename} after {attempt} attempts: {str(req_exc)}"
)
response_data = response.json()
logger.info(f"Received response for file {file.filename}: {response_data}")
file_path = response_data.get("FILE_PATH")
uploaded_date = response_data.get("uploaded_date")
if uploaded_date:
try:
uploaded_date = datetime.strptime(uploaded_date, "%y%m%d-%H%M%S").strftime("%Y-%m-%d %H:%M:%S")
logger.info(f"Parsed uploaded_date for file {file.filename}: {uploaded_date}")
except Exception as parse_error:
error_msg = f"Error parsing uploaded_date for {file.filename}: {str(parse_error)}"
logger.error(error_msg, exc_info=True)
raise HTTPException(status_code=500, detail=error_msg)
else:
error_msg = f"uploaded_date is missing in the response for {file.filename}"
logger.error(error_msg)
raise HTTPException(status_code=500, detail=error_msg)
logger.info(f"Saving metadata to database for file: {file.filename}")
db_result = output_DB.add_other_document(
file_unique_id=file_unique_id,
file_name=file.filename,
file_path=file_path,
file_date=uploaded_date,
univ_id=univ_id,
pid=pid
)
if not db_result:
logger.error(f"Database failed to save metadata for {file.filename}. Removing file from storage.")
try:
os.remove(file_path)
logger.info(f"Removed file from storage: {file_path}")
except Exception as remove_exc:
logger.error(f"Failed to remove file {file_path}: {str(remove_exc)}", exc_info=True)
raise HTTPException(
status_code=500,
detail="File uploaded but failed to save metadata to the database."
)
logger.info(f"File processed successfully: {file.filename}")
attachments.append({
"file_unique_id": file_unique_id,
"file_name": file.filename,
"file_path": file_path,
"file_date": uploaded_date
})
logger.info("All files processed successfully")
return {
"RESULT_CODE": 200,
"RESULT_MSG": "Files uploaded and metadata saved successfully.",
"PAYLOADS": attachments
}
except HTTPException as http_exc:
logger.error(f"HTTPException occurred: {http_exc.detail}")
raise http_exc
except Exception as e:
logger.error("Unexpected error occurred during file upload and metadata saving", exc_info=True)
raise HTTPException(status_code=500, detail=f"Unexpected error: {str(e)}")
finally:
logger.info("------------------------------------------------------------")
@router.post("/output/otherdoc_edit_path")
async def edit_otherdoc_path(file_unique_id: int = Form(...), new_file_path: str = Form(...)):
"""
기타 산출물 경로 수정 API
"""
logger.info(f"Starting file path update for file_unique_id: {file_unique_id}")
try:
result = output_DB.edit_file_path(
file_unique_id=file_unique_id,
new_file_path=new_file_path
)
if result:
logger.info(f"File path updated successfully for file_unique_id: {file_unique_id}")
return {"RESULT_CODE": 200, "RESULT_MSG": "File path updated successfully"}
else:
logger.error(f"Failed to update file path for file_unique_id: {file_unique_id}")
raise HTTPException(status_code=500, detail="Failed to update file path")
except Exception as e:
logger.error(f"Error [edit_file_path] for file_unique_id {file_unique_id}: {e}", exc_info=True)
raise HTTPException(status_code=500, detail=f"Error editing file path: {e}")
@router.post("/output/otherdoc_edit_name")
async def edit_otherdoc_name(file_unique_id: int = Form(...), new_file_name: str = Form(...)):
"""
기타 산출물 이름 수정 API
"""
logger.info(f"Starting file name update for file_unique_id: {file_unique_id}")
try:
result = output_DB.edit_file_name(
file_unique_id=file_unique_id,
new_file_name=new_file_name
)
if result:
logger.info(f"File name updated successfully for file_unique_id: {file_unique_id}")
return {"RESULT_CODE": 200, "RESULT_MSG": "File name updated successfully"}
else:
logger.error(f"Failed to update file name for file_unique_id: {file_unique_id}")
raise HTTPException(status_code=500, detail="Failed to update file name")
except Exception as e:
logger.error(f"Error [edit_file_name] for file_unique_id {file_unique_id}: {e}", exc_info=True)
raise HTTPException(status_code=500, detail=f"Error editing file name: {e}")
@router.post("/output/otherdoc_fetch_all")
async def fetch_all_otherdoc(pid: int = Form(...)):
"""
기타 산출물 조회 API
"""
logger.info(f"Fetching all other documents for pid: {pid}")
try:
documents = output_DB.fetch_all_other_documents(pid)
logger.info(f"Fetched {len(documents)} other documents for pid: {pid}")
return {"RESULT_CODE": 200, "RESULT_MSG": "Other Documents fetched successfully", "PAYLOADS": documents}
except Exception as e:
logger.error(f"Error [fetch_all_other_documents] for pid {pid}: {e}", exc_info=True)
raise HTTPException(status_code=500, detail=f"Error fetching documents: {e}")
@router.post("/output/otherdoc_fetch_path")
async def fetch_all_otherdoc(payload: OtherDocDownloadPayload):
"""
기타 산출물 경로 조회 API
"""
logger.info(f"Fetching file path for file_no: {payload.file_no}")
try:
documents = output_DB.fetch_file_path(payload.file_no)
logger.info(f"Fetched file path for file_no: {payload.file_no}")
return {"RESULT_CODE": 200, "RESULT_MSG": "Other Documents fetched successfully", "PAYLOADS": documents}
except Exception as e:
logger.error(f"Error [fetch_file_path] for file_no {payload.file_no}: {e}", exc_info=True)
raise HTTPException(status_code=500, detail=f"Error fetching file path: {e}")
@router.post("/output/otherdoc_delete")
async def delete_other_document(payload: OtherDocDownloadPayload):
"""
기타 산출물 삭제 API
"""
logger.info(f"Starting deletion of other document with file_no: {payload.file_no}")
try:
result = output_DB.delete_other_document(payload.file_no)
if result:
logger.info(f"Other document deleted successfully for file_no: {payload.file_no}")
return {"RESULT_CODE": 200, "RESULT_MSG": "Other document deleted successfully"}
else:
logger.error(f"Failed to delete other document for file_no: {payload.file_no}")
raise HTTPException(status_code=500, detail="Failed to delete other document")
except Exception as e:
logger.error(f"Error [delete_other_document] for file_no {payload.file_no}: {e}", exc_info=True)
raise HTTPException(status_code=500, detail=f"Error deleting other document: {e}")
@router.post("/output/otherdoc_download")
async def api_otherdoc_download(payload: OtherDocDownloadPayload):
"""
API 서버가 Storage 서버에서 파일을 복사해 프론트로 전송
다운로드 흐름
1. Next.JS에서 file_no를 인자로 API Server에 요청
2. API Server에서는 해당 file_no 인자를 이용해서 다운로드하고자 하는 파일의 이름(file_name)과 경로(file_path)를 db에서 확인
3. 확인한 경로를 Storage Server에 인자로 전달
4. Storage Server에서 해당 경로에 있는 파일을 form 데이터로 다시 API Server에 전달
5. API Server에서 form 데이터를 받으면 그것을 /data/tmp에 저장하되, 파일 이름은 db에서 확인한 파일 이름을 사용
6. 저장한 파일을 Next.JS에 post로 전달하며, 해당 파일의 원본 이름은 헤더에 저장
"""
temp_file_path = None # 파일 삭제를 위해 finally에서 접근하기 위한 변수
try:
# 1. DB에서 파일 정보 조회
logger.info(f"Fetching file info from DB for file_no: {payload.file_no}")
file_info = output_DB.fetch_one_other_documents(payload.file_no)
if not file_info:
logger.error(f"File not found in DB for file_no: {payload.file_no}")
raise HTTPException(status_code=404, detail="File not found in database")
file_path = file_info['file_path']
file_name = file_info['file_name']
logger.info(f"File info retrieved: {file_name} at {file_path}")
# 2. Storage 서버에서 파일 요청
logger.info(f"Requesting file from Storage Server: {file_path}")
try:
response = requests.post(
"http://192.168.50.84:10080/api/output/otherdoc_download",
data={"file_path": file_path},
# headers={"Authorization": STORAGE_API_KEY},
stream=True,
timeout=60
)
except requests.exceptions.RequestException as e:
logger.error(f"Failed to request file from storage server: {str(e)}", exc_info=True)
raise HTTPException(status_code=500, detail=f"Request to storage server failed: {str(e)}")
if response.status_code != 200:
logger.error(f"Storage server response error: {response.status_code} - {response.text}")
raise HTTPException(status_code=500, detail=f"Failed to download file: {response.text}")
# 3. 파일을 /data/tmp에 저장
logger.info(f"Saving file to temporary directory: {TEMP_DOWNLOAD_DIR}")
temp_file_path = os.path.join(TEMP_DOWNLOAD_DIR, file_name)
try:
with open(temp_file_path, "wb") as f:
shutil.copyfileobj(response.raw, f)
logger.info(f"File saved to {temp_file_path}")
except Exception as e:
logger.error(f"Failed to save file to {temp_file_path}: {str(e)}", exc_info=True)
raise HTTPException(status_code=500, detail=f"File save error: {str(e)}")
# 4. Next.js에 파일 데이터 전송
logger.info(f"Pushing file to Next.js: {file_name}")
push.push_to_nextjs(temp_file_path, file_name)
except Exception as e:
logger.error(f"Unexpected error during file transfer: {str(e)}", exc_info=True)
raise HTTPException(status_code=500, detail=f"Unexpected error: {str(e)}")
finally:
# 5. 전송 후 임시 파일 삭제
if temp_file_path and os.path.exists(temp_file_path):
try:
os.remove(temp_file_path)
logger.info(f"Temporary file deleted: {temp_file_path}")
except Exception as e:
logger.error(f"Failed to delete temporary file {temp_file_path}: {str(e)}", exc_info=True)
@router.post("/output/load_type")
async def api_otherdoc_type(payload: OtherDocumentPayload):
logger.info(f"Loading document type for file_unique_id: {payload.file_unique_id}")
try:
result = output_DB.fetch_document_type(payload.file_unique_id)
logger.info(f"Successfully fetched document type for file_unique_id: {payload.file_unique_id} - Result: {result}")
return {"RESULT_CODE": 200, "RESULT_MSG": result}
except HTTPException as e:
logger.error(f"Error fetching document type for file_unique_id: {payload.file_unique_id} - {e.detail}", exc_info=True)
raise e