def get(self, activity_id): '''Get detailed information about an activity.''' activity = SportActivity.query.get_or_404(activity_id) exclude = () if current_user.is_leader_of(activity_id) else ( 'assigned_students', ) out_schema = SportActivitySchema(many=False, exclude=exclude) return out_schema.jsonify(activity)
def get(self, activity_id): if not (current_user.is_admin or current_user.email == request.args['student_email'] or current_user.is_leader_of(activity_id)): abort(403) activity = SportActivity.query.get_or_404(activity_id) student = User.query.get_or_404( request.args.get('student_email', current_user.email) ) records = ( # pylint: disable=bad-continuation SportHoursRecord.query .filter_by(activity_id=activity.id, student_email=student.email) .order_by(SportHoursRecord.date.desc()) ) out_schema = SportHoursRecordSchema(many=True) return out_schema.jsonify(records.all())
def get_attendance_per_day(activity_id): if not (current_user.is_admin or current_user.is_leader_of(activity_id)): abort(403) try: print(request.args.get('date')) date = datetime.fromisoformat(request.args.get('date')) except: abort(400, 'Invalid date.') activity = SportActivity.query.get_or_404(activity_id) records = ( # pylint: disable=bad-continuation SportHoursRecord.query .filter_by(activity_id=activity.id, date=date) ) out_schema = SportHoursRecordSchema(many=True) return out_schema.jsonify(records.all())
def post(self, activity_id): if not (current_user.is_admin or current_user.is_leader_of(activity_id)): abort(403) SportActivity.query.get_or_404(activity_id) in_schema = SportHoursRecordSchema(exclude=('id', 'activity_id')) hours_record = in_schema.load(request.json) hours_record.activity_id = activity_id User.query.get_or_404(hours_record.student_email) record_in_db = SportHoursRecord.query.filter_by(student_email=hours_record.student_email, activity_id=hours_record.activity_id, date=hours_record.date).one_or_none() if record_in_db: hours_record = in_schema.load(request.json, instance=record_in_db) db.session.add(hours_record) db.session.commit() return ('', 204)
def patch(self, activity_id): '''Edit the activity.''' if not request.is_json: abort(400, 'The request must be in JSON.') if not current_user.is_admin and not current_user.is_leader_of( activity_id): abort(403) in_schema = SportActivitySchema(exclude=('id', )) record_in_db = SportActivity.query.get_or_404(activity_id) for schedule_record in record_in_db.schedule_records: db.session.delete(schedule_record) in_schema.load(request.json, session=db.session, instance=record_in_db, partial=True) db.session.commit() return ('', 204)