-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoordinator.py
More file actions
113 lines (98 loc) · 3.92 KB
/
coordinator.py
File metadata and controls
113 lines (98 loc) · 3.92 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
# coordinator.py
import os
from flask import Blueprint, request, render_template, session, jsonify, current_app
from datetime import datetime
from utils.security import role_required
from utils.file_utils import validate_pdf, save_upload
from pymongo import MongoClient
from pdf_generator import create_pdf
# Import your form‐to‐text builders
from progress_report import builders
bp = Blueprint('coordinator', __name__, url_prefix='/coord')
db = MongoClient(os.getenv('MONGO_URI'))["reportdata"]
@bp.route('/upload_progress_report', methods=['GET','POST'])
@role_required('coordinator', 'doctor')
def upload_progress():
if request.method == 'POST':
pid = request.form.get('patientId','').strip()
rpt_type = request.form.get('reportType','').strip()
attached = request.files.get('reportFile')
# 1) Validate patient ID
if not pid:
return jsonify({'error': 'Patient ID is required'}), 400
patient = db.patients.find_one({'patient_id': pid})
if not patient:
return jsonify({'error':'Patient not found'}), 404
# 2) Build the note text
now_dt = datetime.utcnow()
header = (
f"Progress Note Type: {rpt_type}\n"
f"Patient ID: {pid}\n"
f"Date/Time: {now_dt.strftime('%Y-%m-%d %H:%M UTC')}\n\n"
)
builder = builders.get(rpt_type)
if not builder:
return jsonify({'error':'Unsupported progress note type'}), 400
body = builder(request.form)
full_text = header + body
# 3) If user attached a PDF, validate & save
rel_attach = None
if attached and attached.filename:
if not validate_pdf(attached):
return jsonify({'error':'Attached file must be a valid PDF'}), 400
rel_attach = save_upload(attached, pid, 'progress_docs')
# 4) Generate our own PDF copy of the full note
# into static/uploads/<pid>/progress_docs/
now_ts = int(now_dt.timestamp())
filename = f"{pid}_{rpt_type}_{now_ts}.pdf"
save_dir = os.path.join(current_app.root_path, 'static', 'uploads', pid, 'progress_docs')
os.makedirs(save_dir, exist_ok=True)
full_path = os.path.join(save_dir, filename)
create_pdf(full_text, full_path)
# relative path to store in DB
rel_pdf = os.path.join('static', 'uploads', pid, 'progress_docs', filename)
# 5) Build the history entry
hist_entry = {
'type': rpt_type,
'timestamp': now_dt,
'text': full_text,
'pdf': rel_pdf
}
if rel_attach:
hist_entry['attachment'] = rel_attach
# 6) Update patient document:
# - push into history array
# - update a quick‐access `latest` subdoc
pd = patient.get('progress_docs')
if isinstance(pd, list):
# old style → simple array push
db.patients.update_one(
{'patient_id': pid},
{
'$push': {'progress_docs': hist_entry}
}
)
else:
# nested style → push into history
db.patients.update_one(
{'patient_id': pid},
{
'$push': {'progress_docs.history': hist_entry},
'$set': {'progress_docs.latest': hist_entry}
}
)
# 7) Log in reports_log
log_entry = {
'patient_id': pid,
'report_type': 'progress',
'progress_type': rpt_type,
'uploaded_by': session['user_id'],
'file_path': rel_pdf,
'timestamp': now_dt
}
if rel_attach:
log_entry['attachment'] = rel_attach
db.reports_log.insert_one(log_entry)
return jsonify({'message': 'Progress note saved successfully'}), 200
# GET
return render_template('upload_progress_report.html')