Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

261 Zeilen
9.2 KiB

  1. from flask import Flask, render_template, request, redirect, url_for, jsonify, flash, session
  2. from flask_sqlalchemy import SQLAlchemy
  3. from werkzeug.security import generate_password_hash, check_password_hash
  4. from flask_jwt_extended import JWTManager, create_access_token, jwt_required, get_jwt_identity
  5. from flask_login import LoginManager, UserMixin, login_user, logout_user, login_required
  6. from datetime import datetime, timedelta
  7. from sqlalchemy import MetaData, create_engine
  8. from sqlalchemy.ext.automap import automap_base
  9. from sqlalchemy.orm import sessionmaker
  10. from sqlalchemy.ext.declarative import declarative_base
  11. app = Flask(__name__)
  12. app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+mysqlconnector://root:12345@localhost/assignment'
  13. app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
  14. app.config['JWT_SECRET_KEY'] = 'jwt_secret_key'
  15. app.config['JWT_ACCESS_TOKEN_EXPIRES'] = timedelta(days=1)
  16. app.config['SECRET_KEY'] = 'secret_key'
  17. jwt = JWTManager(app)
  18. login_manager = LoginManager(app)
  19. login_manager.login_view = 'login'
  20. db = SQLAlchemy(app)
  21. class User(db.Model):
  22. user_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
  23. email = db.Column(db.String(255), nullable=False)
  24. given_name = db.Column(db.String(255), nullable=False)
  25. surname = db.Column(db.String(255), nullable=False)
  26. city = db.Column(db.String(255))
  27. phone_number = db.Column(db.String(20))
  28. profile_description = db.Column(db.Text)
  29. password = db.Column(db.String(255), nullable=False)
  30. role = db.Column(db.String(20), nullable = False)
  31. class Caregiver(db.Model):
  32. caregiver_user_id = db.Column(db.Integer, db.ForeignKey('user.user_id'), primary_key=True)
  33. photo = db.Column(db.LargeBinary)
  34. gender = db.Column(db.String(20))
  35. caregiving_type = db.Column(db.String(255))
  36. hourly_rate = db.Column(db.Integer)
  37. user = db.relationship('User', backref='caregiver', uselist=False)
  38. class Member(db.Model):
  39. member_user_id = db.Column(db.Integer, db.ForeignKey('user.user_id'), primary_key=True)
  40. house_rules = db.Column(db.Text)
  41. user = db.relationship('User', backref='member', uselist=False)
  42. class Address(db.Model):
  43. member_user_id = db.Column(db.Integer, db.ForeignKey('member.member_user_id'), primary_key=True)
  44. house_number = db.Column(db.Integer)
  45. street = db.Column(db.String(255))
  46. town = db.Column(db.String(255))
  47. member = db.relationship('Member', backref='address', uselist=False)
  48. class Job(db.Model):
  49. job_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
  50. member_user_id = db.Column(db.Integer, db.ForeignKey('member.member_user_id'))
  51. required_caregiving_type = db.Column(db.String(255))
  52. other_requirements = db.Column(db.Text)
  53. date_posted = db.Column(db.Date)
  54. member = db.relationship('Member', backref='jobs')
  55. class JobApplication(db.Model):
  56. caregiver_user_id = db.Column(db.Integer, db.ForeignKey('caregiver.caregiver_user_id'), primary_key=True)
  57. job_id = db.Column(db.Integer, db.ForeignKey('job.job_id'), primary_key=True)
  58. date_applied = db.Column(db.Date)
  59. class Appointment(db.Model):
  60. appointment_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
  61. caregiver_user_id = db.Column(db.Integer, db.ForeignKey('caregiver.caregiver_user_id'))
  62. member_user_id = db.Column(db.Integer, db.ForeignKey('member.member_user_id'))
  63. appointment_date = db.Column(db.Date)
  64. appointment_time = db.Column(db.Time)
  65. work_hours = db.Column(db.String(50))
  66. status = db.Column(db.String(50))
  67. login_manager = LoginManager(app)
  68. login_manager.login_view = 'login'
  69. @login_manager.user_loader
  70. def load_user(user_id):
  71. return User.query.get(int(user_id))
  72. headers = {'Content-Type': 'application/json'}
  73. @login_manager.user_loader
  74. def load_user(user_id):
  75. return User.query.get(int(user_id))
  76. @app.route('/', methods=['GET'])
  77. def login_page():
  78. return render_template('login.html')
  79. @app.route('/', methods=['POST'])
  80. def login_user():
  81. if request.method == 'POST':
  82. email = request.form['email']
  83. password = request.form['password']
  84. user = User.query.filter_by(email=email).first()
  85. if user and user.password == password:
  86. session['user_id'] = user.user_id
  87. session['role'] = user.role
  88. access_token = create_access_token(identity=user.user_id)
  89. if user.role == 'caregiver':
  90. return redirect(url_for('caregiver_dashboard'))
  91. elif user.role == 'family':
  92. return redirect(url_for('family_dashboard'))
  93. else:
  94. return jsonify({'status': 'error', 'message': 'Invalid email or password'}), 401
  95. @app.route('/caregiver_dashboard')
  96. def caregiver_dashboard():
  97. if 'user_id' in session and session.get('role') == 'caregiver':
  98. user_id = session.get('user_id')
  99. user = User.query.get(user_id)
  100. return render_template('caregiver_dashboard.html', user=user)
  101. else:
  102. return redirect(url_for('login_page'))
  103. @app.route('/family_dashboard')
  104. def family_dashboard():
  105. if 'user_id' in session and session.get('role') == 'family':
  106. user_id = session.get('user_id')
  107. user = User.query.get(user_id)
  108. jobs = Job.query.filter_by(member_user_id=user_id).all()
  109. return render_template('family_dashboard.html', user=user, jobs=jobs)
  110. else:
  111. return redirect(url_for('login_page'))
  112. @app.route('/protected', methods=['GET'])
  113. @login_required
  114. def protected():
  115. current_user = get_jwt_identity()
  116. return jsonify(logged_in_as=current_user), 200
  117. @app.route('/register', methods=['GET'])
  118. def registration_page():
  119. return render_template('register.html')
  120. @app.route('/register', methods=['POST'])
  121. def register():
  122. username = request.form.get('username', None)
  123. password = request.form.get('password', None)
  124. given_name = request.form.get('name', None)
  125. surname = request.form.get('surname', None)
  126. city = request.form.get('city', None)
  127. phone_number = request.form.get('phone_number', None)
  128. profile_description = request.form.get('profile_description', None)
  129. role = request.form.get('role', None)
  130. if not username or not password or not given_name or not surname or not city or not phone_number or not profile_description or not role:
  131. return jsonify({'message': 'All fields are required'}), 400
  132. existing_user = User.query.filter_by(email=username).first()
  133. if existing_user:
  134. return jsonify({'message': 'User already exists'}), 409
  135. new_user = User(email=username, password=password, given_name=given_name, surname=surname, city=city, phone_number=phone_number, profile_description=profile_description, role=role)
  136. db.session.add(new_user)
  137. db.session.commit()
  138. return redirect(url_for('login_page'))
  139. @app.route('/update/<int:user_id>', methods=['GET', 'POST'])
  140. def update_user(user_id):
  141. user = User.query.get(user_id)
  142. if request.method == 'POST':
  143. user.email = request.form['email']
  144. user.given_name = request.form['given_name']
  145. user.surname = request.form['surname']
  146. user.city = request.form['city']
  147. user.phone_number = request.form['phone_number']
  148. user.profile_description = request.form['profile_description']
  149. db.session.commit()
  150. if user.role == 'caregiver':
  151. return redirect(url_for('caregiver_dashboard'))
  152. elif user.role == 'family':
  153. return redirect(url_for('family_dashboard'))
  154. return render_template('update.html', user=user)
  155. @app.route('/create_job', methods=['GET', 'POST'])
  156. def create_job():
  157. if request.method == 'POST':
  158. user_id = session.get('user_id')
  159. member = User.query.get(user_id)
  160. new_job = Job(
  161. member_user_id=user_id,
  162. required_caregiving_type=request.form['required_caregiving_type'],
  163. other_requirements=request.form['other_requirements'],
  164. date_posted=datetime.now().date()
  165. )
  166. db.session.add(new_job)
  167. db.session.commit()
  168. flash("Job created successfully", "success")
  169. return redirect(url_for('family_dashboard'))
  170. return render_template('create_job.html')
  171. @app.route('/update_job/<int:job_id>', methods=['GET', 'POST'])
  172. def update_job(job_id):
  173. job = Job.query.get(job_id)
  174. if not job:
  175. flash("Job not found", "error")
  176. return redirect(url_for('family_dashboard'))
  177. if request.method == 'POST':
  178. job.required_caregiving_type = request.form['required_caregiving_type']
  179. job.other_requirements = request.form['other_requirements']
  180. job.date_posted = datetime.now().date()
  181. db.session.commit()
  182. flash("Job updated successfully", "success")
  183. return redirect(url_for('family_dashboard'))
  184. return render_template('update_job.html', job=job)
  185. @app.route('/delete_job/<int:job_id>', methods=['GET'])
  186. def delete_job(job_id):
  187. job = Job.query.get(job_id)
  188. if not job:
  189. flash("Job not found", "error")
  190. return redirect(url_for('family_dashboard'))
  191. db.session.delete(job)
  192. db.session.commit()
  193. flash("Job deleted successfully", "success")
  194. return redirect(url_for('family_dashboard'))
  195. @app.route('/logout')
  196. def logout_user():
  197. session.pop('user_id', None)
  198. return redirect(url_for('login_page'))
  199. if __name__ == '__main__':
  200. app.run(debug=True)