264 lines
12 KiB
Python
264 lines
12 KiB
Python
|
import os
|
||
|
from datetime import date
|
||
|
|
||
|
from sqlalchemy import distinct, desc
|
||
|
|
||
|
from flask_app.decorators import permission_required
|
||
|
from . import user
|
||
|
from .forms import EditProfileForm
|
||
|
from flask import render_template, flash, redirect, url_for, request, session, current_app, jsonify
|
||
|
from flask_login import current_user
|
||
|
from .. import db
|
||
|
from ..models import User, Permission, Appointment, Workday, Report, Call_number, Comment, Post, Private_message, Like, \
|
||
|
Collect
|
||
|
import shutil
|
||
|
root_path=user.root_path
|
||
|
root_dir=os.path.dirname(root_path)
|
||
|
UPLOAD_FOLDER = os.path.join(root_dir,"static/images")
|
||
|
|
||
|
@user.route('/user_edit/<username>', methods=['GET', 'POST'])
|
||
|
@permission_required(Permission.COMMENT)
|
||
|
def user_edit(username):
|
||
|
session['next'] = request.url # 将当前URL保存到session中
|
||
|
active_page = "user"
|
||
|
active_page1="user_edit"
|
||
|
user = User.query.filter_by(username=username).first_or_404()
|
||
|
user_initial = user.username[0]
|
||
|
form = EditProfileForm(obj=user)
|
||
|
if form.validate_on_submit():
|
||
|
form.populate_obj(user)
|
||
|
db.session.commit()
|
||
|
return redirect(url_for('main.user', username=current_user.username))
|
||
|
return render_template('user/user_edit.html', form=form,user=user,user_initial=user_initial,active_page=active_page,active_page1=active_page1)
|
||
|
|
||
|
|
||
|
|
||
|
@user.route('/upload_avatar/<username>', methods=['POST'])
|
||
|
def upload_avatar(username):
|
||
|
print(username)
|
||
|
file = request.files['file']
|
||
|
avatar_path = os.path.join(UPLOAD_FOLDER,'avatar',username + '.jpg')
|
||
|
print(avatar_path)
|
||
|
file.save(avatar_path)
|
||
|
# 将图片路径保存到 users 表中的 avatar_path 字段
|
||
|
user = User.query.filter_by(username=username).first()
|
||
|
if user:
|
||
|
user.has_avatar=True
|
||
|
db.session.commit()
|
||
|
return redirect(url_for('main.user', username=username))
|
||
|
|
||
|
@user.route('/my_appointment/<username>',methods=['GET','POST'])
|
||
|
@permission_required(Permission.COMMENT)
|
||
|
def my_appointment(username):
|
||
|
session['next'] = request.url # 将当前URL保存到session中
|
||
|
active_page = "user"
|
||
|
active_page1 = "my_date"
|
||
|
today = date.today()
|
||
|
today_str = today.strftime('%Y-%m-%d')
|
||
|
user = User.query.filter_by(username=username).first_or_404()
|
||
|
user_initial = user.username[0]
|
||
|
appointments = Appointment.query.filter_by(patient_id=current_user.id).order_by(desc(Appointment.date)).all()
|
||
|
return render_template('user/my_appointment.html',appointments=appointments,
|
||
|
user=user,user_initial=user_initial,active_page=active_page,active_page1=active_page1,User=User,today_str=today_str)
|
||
|
|
||
|
@user.route('/my_post/<username>',methods=['GET','POST'])
|
||
|
@permission_required(Permission.COMMENT)
|
||
|
def my_post(username):
|
||
|
posts=Post.query.filter_by(author_id=current_user.id).all()
|
||
|
user = User.query.filter_by(username=username).first_or_404()
|
||
|
return render_template('user/my_post.html',posts=posts,user=user,Like=Like,Collect=Collect,
|
||
|
user_initial=user.username[0],active_page="user",active_page1="my_post")
|
||
|
|
||
|
@user.route('/cancel_myappointment',methods=['GET','POST'])
|
||
|
def cancel_myappointment():
|
||
|
patient_id = request.form.get('patient_id')
|
||
|
doc_id = request.form.get('doc_id')
|
||
|
date = request.form.get('date')
|
||
|
time=request.form.get('time')
|
||
|
print(patient_id,doc_id,time)
|
||
|
appointment = Appointment.query.filter_by(patient_id=patient_id,doc_id=doc_id,date=date,time=time).first()
|
||
|
workday = Workday.query.filter_by(doc_id=doc_id, date=date).first()
|
||
|
if(time=="1"):
|
||
|
workday.temp_morning+=1
|
||
|
else:
|
||
|
workday.temp_afternoon+=1
|
||
|
db.session.delete(appointment)
|
||
|
db.session.add(workday)
|
||
|
db.session.commit()
|
||
|
return redirect(url_for('user.my_appointment',username=current_user.username))
|
||
|
|
||
|
@user.route('/my_report/<uid>',methods=['GET','POST'])
|
||
|
@permission_required(Permission.COMMENT)
|
||
|
def my_report(uid):
|
||
|
session['next'] = request.url # 将当前URL保存到session中
|
||
|
user = User.query.filter_by(id=uid).first()
|
||
|
appointments=Appointment.query.filter_by(patient_id=uid).order_by(Appointment.date.desc()).all()
|
||
|
reports=[]
|
||
|
for appointment in appointments:
|
||
|
report=Report.query.filter_by(appointment_id=appointment.id).first()
|
||
|
if report:
|
||
|
reports.append(report)
|
||
|
return render_template('user/my_report.html',reports=reports,active_page1='my_report',user=user,User=User,Appointment=Appointment,user_initial = user.username[0])
|
||
|
|
||
|
|
||
|
@user.route('/my_patient/<docid>')
|
||
|
@permission_required(Permission.DETECT)
|
||
|
def my_patient(docid):
|
||
|
session['next'] = request.url # 将当前URL保存到session中
|
||
|
active_page = "user"
|
||
|
active_page1 = "my_patient"
|
||
|
user = User.query.filter_by(id=docid).first_or_404()
|
||
|
page = request.args.get('page', 1, type=int)
|
||
|
per_page = current_app.config['FLASKY_POSTS_PER_PAGE']
|
||
|
pagination = Appointment.query.filter_by(doc_id=docid).order_by(Appointment.date.desc(),Appointment.time.desc(),Appointment.num.asc()).paginate(
|
||
|
page, per_page, # 每页最多显示记录数
|
||
|
error_out=False)
|
||
|
appintments = pagination.items
|
||
|
today = date.today()
|
||
|
today_str = today.strftime('%Y-%m-%d')
|
||
|
return render_template('user/my_patient.html',appointments=appintments,user=user,pagination=pagination,User=User,
|
||
|
active_page=active_page,active_page1=active_page1,user_initial = user.username[0],Report=Report,today_str=today_str)
|
||
|
|
||
|
@user.route('/mypatient_info/<appointment_id>')
|
||
|
@permission_required(Permission.DETECT)
|
||
|
def mypatient_info(appointment_id):
|
||
|
session['next'] = request.url # 将当前URL保存到session中
|
||
|
appointment=Appointment.query.filter_by(id=appointment_id).first()
|
||
|
user=User.query.filter_by(id=appointment.doc_id).first()
|
||
|
report = Report.query.filter_by(id=4).first()
|
||
|
active_page1 = 'my_patient'
|
||
|
return render_template('user/mypatient_info.html',appointment=appointment,User=User,user=user,active_page1=active_page1,user_initial = user.username[0],report=report)
|
||
|
|
||
|
@user.route('/check_report',methods=['POST'])
|
||
|
@permission_required(Permission.DETECT)
|
||
|
def check_report():
|
||
|
report_id = request.form.get('report')
|
||
|
print(report_id)
|
||
|
report = Report.query.get(report_id)
|
||
|
reports=[report]
|
||
|
user = User.query.filter_by(id=current_user.id).first()
|
||
|
return render_template('user/my_report.html',reports=reports,active_page1='my_patient',user=user,User=User,Appointment=Appointment,user_initial = user.username[0])
|
||
|
|
||
|
@user.route('/write_report',methods=['POST'])
|
||
|
@permission_required(Permission.DETECT)
|
||
|
def write_report():
|
||
|
session['next'] = request.url # 将当前URL保存到session中
|
||
|
temp_dir=os.path.join(UPLOAD_FOLDER,'upload_img',str(current_user.id))
|
||
|
files=os.listdir(temp_dir)
|
||
|
if not files:
|
||
|
flash("请先上传图片!")
|
||
|
return redirect(url_for('user.my_patient',docid=current_user.id))
|
||
|
else:
|
||
|
file = files[0]
|
||
|
appointment_id = request.form.get('appointment')
|
||
|
appointment=Appointment.query.filter_by(id=appointment_id).first()
|
||
|
user = User.query.filter_by(id=appointment.doc_id).first()
|
||
|
active_page1 = 'my_patient'
|
||
|
return render_template('user/write_report.html',appointment=appointment,user=user,
|
||
|
User=User,active_page1=active_page1,user_initial = user.username[0],file=file)
|
||
|
|
||
|
@user.route('/send_report',methods=['POST'])
|
||
|
def send_report():
|
||
|
session['next'] = request.url # 将当前URL保存到session中
|
||
|
diagnosis_result = request.form.get('diagnosis_result')
|
||
|
diagnosis_date = request.form.get('diagnosis_date')
|
||
|
diagnosis_advice = request.form.get('diagnosis_advice')
|
||
|
diagnosis_sign = request.form.get('diagnosis_sign')
|
||
|
appointment_id = request.form.get('appointment')
|
||
|
temp_dir = os.path.join(UPLOAD_FOLDER, 'upload_img', str(current_user.id))
|
||
|
files = os.listdir(temp_dir)
|
||
|
file=files[0]
|
||
|
cur_path=os.path.join(temp_dir,file)
|
||
|
des_path = os.path.join(UPLOAD_FOLDER, 'diagnosis',str(appointment_id)+".jpg")
|
||
|
shutil.copy(cur_path, des_path)
|
||
|
os.remove(cur_path)
|
||
|
report=Report(appointment_id=appointment_id,diagnosis_advice=diagnosis_advice,
|
||
|
diagnosis_result=diagnosis_result,diagnosis_date=diagnosis_date,diagnosis_sign=diagnosis_sign)
|
||
|
db.session.add(report)
|
||
|
db.session.commit()
|
||
|
flash("发送成功!")
|
||
|
return redirect(url_for('user.my_patient',docid=current_user.id))
|
||
|
|
||
|
|
||
|
@user.route('/call_number',methods=['GET','POST'])
|
||
|
def call_number():
|
||
|
appointment_id = request.form.get('appointment')
|
||
|
call=Call_number()
|
||
|
call.appointment_id=appointment_id
|
||
|
db.session.add(call)
|
||
|
db.session.commit()
|
||
|
flash("叫号成功!")
|
||
|
return redirect(url_for('user.my_patient', docid=current_user.id))
|
||
|
|
||
|
|
||
|
@user.route('/query_appointment', methods=['POST'])
|
||
|
def query_appointment():
|
||
|
if not current_user.is_authenticated:
|
||
|
return {'hasAppointment': False}
|
||
|
today = date.today()
|
||
|
today_str = today.strftime('%Y-%m-%d')
|
||
|
has_appointment = Appointment.query.filter_by(date=today_str,patient_id=current_user.id).first() is not None
|
||
|
return {'hasAppointment': has_appointment}
|
||
|
|
||
|
|
||
|
|
||
|
@user.route('/query_call', methods=['GET', 'POST'])
|
||
|
def query_call():
|
||
|
calls = Call_number.query.filter_by(notified=0).all()
|
||
|
# 为了避免向同一病人发送多个通知,创建一个字典来保存病人和他们对应的通知
|
||
|
for call in calls:
|
||
|
appointment = Appointment.query.get(call.appointment_id)
|
||
|
patient_id = appointment.patient_id
|
||
|
if(patient_id==current_user.id):
|
||
|
print(call.appointment_id)
|
||
|
flash("收到一条叫号提醒")
|
||
|
call.notified=1
|
||
|
db.session.add(call)
|
||
|
db.session.commit()
|
||
|
return "ok"
|
||
|
|
||
|
@user.route("/appointments/<int:id>", methods=["POST"])
|
||
|
def update_appointment(id):
|
||
|
appointment = Appointment.query.get(id)
|
||
|
if appointment:
|
||
|
notified = request.form.get("notified")
|
||
|
if notified is not None:
|
||
|
appointment.notified = notified
|
||
|
db.session.commit()
|
||
|
return "Appointment updated successfully."
|
||
|
return "Appointment not found.", 404
|
||
|
|
||
|
|
||
|
@user.route('/my_message/<uid>',methods=['GET','POST'])
|
||
|
@permission_required(Permission.COMMENT)
|
||
|
def my_message(uid):
|
||
|
session['next'] = request.url # 将当前URL保存到session中
|
||
|
posts=Post.query.filter_by(author_id=current_user.id).all()
|
||
|
comments=[]
|
||
|
for post in posts:
|
||
|
comment=Comment.query.filter_by(post_id=post.id).first()
|
||
|
comments.append(comment)
|
||
|
user = User.query.filter_by(id=uid).first()
|
||
|
senders = db.session.query(distinct(Private_message.sender_id)).filter_by(recipient_id=current_user.id).all()
|
||
|
messagess = []
|
||
|
for sender_id in senders:
|
||
|
messages = Private_message.query.filter(
|
||
|
(Private_message.recipient_id == current_user.id) & (Private_message.sender_id == sender_id[0]) | (
|
||
|
Private_message.recipient_id == sender_id[0]) & (
|
||
|
Private_message.sender_id == current_user.id)).all()
|
||
|
messagess.append(messages)
|
||
|
return render_template('user/my_message.html',user=user,user_initial=user.username[0],
|
||
|
active_page1='my_msg',comment_post_paris=zip(comments,posts),User=User,messagess=messagess,senders=senders)
|
||
|
|
||
|
@user.route('/send_message',methods=['GET','POST'])
|
||
|
@permission_required(Permission.COMMENT)
|
||
|
def send_message():
|
||
|
session['next'] = request.url # 将当前URL保存到session中
|
||
|
send_id = request.form.get('send_id')
|
||
|
recipient_id = request.form.get('recipient_id')
|
||
|
body=request.form.get('message_body')
|
||
|
message=Private_message(recipient_id=int(recipient_id),sender_id=int(send_id),body=body)
|
||
|
db.session.add(message)
|
||
|
db.session.commit()
|
||
|
return redirect(url_for('user.my_message',uid=current_user.id))
|