def upload_record(): """ Upload a new Radiology Record """ form = RecordForm(request.form) # Populate the Form patients = selectPersonsWhoArePatients() patientChoices = personChoicesForSelectField(patients) doctors = selectPersonsWhoAreDoctors() doctorChoices = personChoicesForSelectField(doctors) radiologists = selectPersonsWhoAreRadiologists() radiologistChoices = personChoicesForSelectField(radiologists) form.patient_id.choices = patientChoices form.doctor_id.choices = doctorChoices form.radiologist_id.choices = radiologistChoices if form.validate_on_submit(): # Create the Record record = models.Record() form.populate_obj(record) db.session.add(record) db.session.commit() # Create the images images = request.files.getlist("images") if images: for img in images: # Create Images file_name = str(uuid.uuid4()) + secure_filename(img.filename) image_file = os.path.join(app.config['UPLOAD_FOLDER'], file_name) img.save(image_file) thumb_file_name = os.path.splitext(file_name)[0] + ".thumbnail" thumb_file = os.path.splitext(image_file)[0] + ".thumbnail" regular_file_name = os.path.splitext(file_name)[0] + ".regular" regular_file = os.path.splitext(image_file)[0] + ".regular" # Resize resize_image_thumb(img, thumb_file) resize_image_regular(img, regular_file) image = models.Image( record_id=record.record_id, thumbnail=thumb_file_name.encode('utf-8'), regular_size=regular_file_name.encode('utf-8'), full_size=file_name.encode('utf-8')) db.session.add(image) db.session.commit() flash(u'Record {} has been saved'.format(record.record_id)) return redirect(url_for('list_records')) return render_template('upload_record.html', title='Upload a Record', form=form)
def record_add(): if request.method == 'POST': try: token = request.form['access_token'] if not validate.hash(token): raise Exception('Access token is not valid.') user = auxiliary_metods.get_user(token) except Exception: return jsonify({'error_code': '1', 'error_msg': 'Access token is not valid.'}) try: doctor_id = int(request.form['doctor_id']) except Exception: return jsonify( {'error_code': '2', 'error_msg': 'Value [doctor_id] is not valid, value should be a number.'}) if doctor_id == user.id: return jsonify( {'error_code': '3', 'error_msg': 'It is impossible to enroll in the reception.'}) check_doctor = db.session.query(models.Doctor).filter(models.Doctor.id == doctor_id).first() if check_doctor == None: return jsonify( {'error_code': '4', 'error_msg': 'Value [doctor_id] is not found.'}) try: date_create = datetime.now() date_record = request.form['date_record'] date_record = datetime.strptime(date_record, "%d-%m-%Y %H:%M") except Exception: return jsonify( {'error_code': '5', 'error_msg': 'Value [date_record] is not valid.'}) min_interval = (datetime.now() + timedelta(days=1)) max_interval = (datetime.now() + timedelta(days=30)) if not date_record > min_interval and date_record < max_interval: return jsonify( {'error_code': '6', 'error_msg': 'You can not enroll in the reception now.'}) check_time_record = db.session.query(models.Record) \ .filter(models.Record.date_record == date_record) \ .filter(models.Record.doctor_id == doctor_id) \ .first() if not check_time_record == None: return jsonify( {'error_code': '7', 'error_msg': 'This time is already busy.'}) try: record = models.Record(doctor_id=doctor_id, pacient_id=user.id, date_record=date_record, date_create=date_create) db.session.add(record) db.session.commit() return jsonify({'msg': 'Record successfully added.'}) except Exception as e: print(e) db.session.rollback() return jsonify({'error_code': '8', 'error_msg': 'Error connect to database.'}) return jsonify({'error_code': '0', 'error_msg': 'Use POST parameters.'})
def sign_up(): all_courses = models.Course.query.all() if current_user.is_authenticated: return redirect('/') else: user = None sign_up_form = SignUpForm() #when the sign_up button is clicked if sign_up_form.validate_on_submit(): all_users = models.Student.query.all() #check if the user already exists for user in all_users: if request.form["student_email"] == user.student_email: flash("User already exists!") user = None logger.warning("{} already exists".format(user)) return redirect('/sign_up') # Check if the email and confirm email fields match if request.form["confirm_email"] != request.form["student_email"]: flash("The two emails need to be the same!") logger.warning("Email and confrim email fields do not match") return redirect('/sign_up') else: #add the user to the db user = models.Student(sign_up_form.student_email.data, sign_up_form.student_password.data, sign_up_form.student_name.data, sign_up_form.current_year.data) selected_course = models.Course.query.filter_by( course_title=sign_up_form.course_id.data).first() user.course = selected_course db.session.add(user) db.session.commit() all_modules = selected_course.modules for module in all_modules: record = models.Record(user.id, module.id, 0, 0) user.record.append(record) module.record.append(record) db.session.commit() logger.info("{} added to the database".format(user)) return redirect('/sign_in') return render_template('/sign_up.html', title='Sign Up', user=user, form=sign_up_form, courses=all_courses)
def insert(): with open("sars_2003_complete_dataset_clean.csv", "r") as f: csv_reader = csv.DictReader(f) for row in csv_reader: db_record = models.Record( date=datetime.datetime.strptime(row["date"], "%Y-%m-%d"), country=row["country"], cases=row["cases"], deaths=row["deaths"], recoveries=row["recoveries"], ) db.add(db_record) db.commit() db.close()
import csv import datetime from app import models from app.database import SessionLocal, engine db = SessionLocal() models.Base.metadata.create_all(bind=engine) # import fresh data only if the table is empty if db.query(models.Record).count() == 0: with open("sars_2003_complete_dataset_clean.csv", "r") as f: csv_reader = csv.DictReader(f) for row in csv_reader: db_record = models.Record( date=datetime.datetime.strptime(row["date"], "%Y-%m-%d"), country=row["country"], cases=row["cases"], deaths=row["deaths"], recoveries=row["recoveries"], ) db.add(db_record) db.commit() db.close()
def module_chooser(): user = current_user user_id = user.id user_course = user.course_id records = models.Record.query.filter_by(student_id=user_id).all() course_id = models.Course.query.get(user_course) user_compul_modules = course_id.modules credits_1y = course_id.course_1y_credits credits_2y = course_id.course_2y_credits credits_3y = course_id.course_3y_credits credits_4y = 0 if course_id.course_4y_credits: credits_4y = course_id.course_4y_credits all_modules = course_id.optional_modules sel_mod_prereq_list = [] chosen_mod_prereq_list = [] list_modules = [] chosen_list_modules = [] future_choices = [] calculated_credits = {} all_user_modules = [] for record in records: user_module = models.Module.query.get(record.module_id) all_user_modules.append(user_module) calculated_credits = calculate_credits(user) needed_1y = calculated_credits['cr_1y'] needed_2y = calculated_credits['cr_2y'] needed_3y = calculated_credits['cr_3y'] needed_4y = calculated_credits['cr_4y'] if request.method == 'POST': data = request.get_json() mod_id = "" mod_code = "" for item in data: if item == "id": mod_id = data['id'] elif item == "code": mod_code = data['code'] if mod_id != "": selected_module = models.Module.query.get(mod_id) if selected_module.lecturer: lecturer = selected_module.lecturer lecturer_name = lecturer.lecturer_degree + " " + lecturer.lecturer_name else: lecturer_name = "Lecturer is not assigned" sel_mod_prereq_list = selected_module.prerequisites.split("|") future_choices = find_future_modules(mod_id) list_taken_mod = [] #Check if prerequisite is taken if selected_module.prerequisites: for mod in sel_mod_prereq_list: sel_mod = models.Module.query.filter_by( module_title=mod).first() for record in records: user_mod = models.Module.query.filter_by( id=record.module_id).first() if sel_mod.module_title == user_mod.module_title: list_taken_mod.append(sel_mod.module_title) for mod in sel_mod_prereq_list: sel_mod = models.Module.query.filter_by( module_title=mod).first() sel_mod_dict = {} sel_mod_dict['prereq_id'] = sel_mod.id sel_mod_dict['prereq_title'] = sel_mod.module_title sel_mod_dict['prereq_code'] = sel_mod.module_code if sel_mod.module_title in list_taken_mod: sel_mod_dict[ 'taken'] = "<span class=\"fa fa-check-circle\"></span>" else: sel_mod_dict[ 'taken'] = "<span class=\"fa fa-times-circle\"></span>" list_modules.append(sel_mod_dict) if future_choices: return jsonify({ "title": selected_module.module_title, "code": selected_module.module_code, "lecturer": lecturer_name, "size": selected_module.class_size, "credits": selected_module.module_credits, "description": selected_module.module_description, "url": selected_module.module_url, "prerequisites": list_modules, "future_choices": future_choices }) else: return jsonify({ "title": selected_module.module_title, "code": selected_module.module_code, "lecturer": lecturer_name, "size": selected_module.class_size, "credits": selected_module.module_credits, "description": selected_module.module_description, "url": selected_module.module_url, "prerequisites": list_modules }) else: if future_choices: return jsonify({ "title": selected_module.module_title, "code": selected_module.module_code, "lecturer": lecturer_name, "size": selected_module.class_size, "credits": selected_module.module_credits, "description": selected_module.module_description, "url": selected_module.module_url, "future_choices": future_choices }) else: return jsonify({ "title": selected_module.module_title, "code": selected_module.module_code, "lecturer": lecturer_name, "size": selected_module.class_size, "credits": selected_module.module_credits, "description": selected_module.module_description, "url": selected_module.module_url, }) elif mod_code != "": mod_code = data['code'] chosen_module = models.Module.query.filter_by( module_code=mod_code).first() chosen_mod_prereq_list = chosen_module.prerequisites.split("|") list_of_records = [] year_chosen_mod = chosen_module.module_year if year_chosen_mod == 1 and needed_1y == 0: return jsonify({ "title": "", "code": "", "err_message": "You cannot exceed 120 credits!", "suc_message": "", "info_message": "" }) elif year_chosen_mod == 2 and needed_2y == 0: return jsonify({ "title": "", "code": "", "err_message": "You cannot exceed 120 credits!", "suc_message": "", "info_message": "" }) elif year_chosen_mod == 3 and needed_3y == 0: return jsonify({ "title": "", "code": "", "err_message": "You cannot exceed 120 credits!", "suc_message": "", "info_message": "" }) elif year_chosen_mod == 4 and needed_4y == 0: return jsonify({ "title": "", "code": "", "err_message": "You cannot exceed 120 credits!", "suc_message": "", "info_message": "" }) else: assigned = update_optional_mod(user, chosen_module) if assigned == True: return jsonify({ "title": "", "code": "", "err_message": "You cannot assign this module. It is already assigned.", "suc_message": "", "info_message": "" }) else: #check if module has prerequisites if chosen_module.prerequisites: #Check if user has the required prerequisites for mod in chosen_mod_prereq_list: prereq_mod = models.Module.query.filter_by( module_title=mod).first() for record in records: user_mod = models.Module.query.filter_by( id=record.module_id).first() if prereq_mod.module_title == user_mod.module_title: list_of_records.append( user_mod.module_title) #If all prerequisites are taken then assgin module if chosen_mod_prereq_list == list_of_records: new_record = models.Record(user_id, chosen_module.id, 0, 0) user.record.append(new_record) chosen_module.record.append(new_record) db.session.commit() return jsonify({ "title": chosen_module.module_title, "code": chosen_module.module_code, "err_message": "", "suc_message": "Successfully assigned: ", "info_message": "" }) else: for mod in chosen_mod_prereq_list: if mod not in list_of_records: prereq = models.Module.query.filter_by( module_title=mod).first() return jsonify({ "title": prereq.module_title, "code": prereq.module_code, "err_message": "", "suc_message": "", "info_message": "First you need to take: " }) else: new_record = models.Record(user_id, chosen_module.id, 0, 0) user.record.append(new_record) chosen_module.record.append(new_record) db.session.commit() return jsonify({ "title": chosen_module.module_title, "code": chosen_module.module_code, "err_message": "", "suc_message": "Successfully assigned: ", "info_message": "" }) return redirect('/module_chooser') return render_template('/module_chooser.html', title='Display Modules in Module Chooser', modules=all_modules, all_user_modules=all_user_modules, user_compul_modules=user_compul_modules, credits_1y=credits_1y, credits_2y=credits_2y, credits_3y=credits_3y, credits_4y=credits_4y, needed_1y=needed_1y, needed_2y=needed_2y, needed_3y=needed_3y, needed_4y=needed_4y, user=user)
metric2_id = db.session.query(models.Metric.id).filter_by(name='Mindfulness') metric3_id = db.session.query(models.Metric.id).filter_by(name='Chinese') metric4_id = db.session.query(models.Metric.id).filter_by(name='Peacefulness') metric5_id = db.session.query(models.Metric.id).filter_by(name='Happiness') # Create dates for previous week today = date.today() todayl1 = today - timedelta(days=1) todayl2 = today - timedelta(days=2) todayl3 = today - timedelta(days=3) todayl4 = today - timedelta(days=4) todayl5 = today - timedelta(days=5) todayl6 = today - timedelta(days=6) # Create records for metric 1 r1 = models.Record(metric_id=metric1_id, value=1, date=todayl6) r2 = models.Record(metric_id=metric1_id, value=0, date=todayl5) r3 = models.Record(metric_id=metric1_id, value=1, date=todayl4) r4 = models.Record(metric_id=metric1_id, value=0, date=todayl3) r5 = models.Record(metric_id=metric1_id, value=0, date=todayl2) r6 = models.Record(metric_id=metric1_id, value=1, date=todayl1) # Add records to db db.session.add_all([r1, r2, r3, r4, r5, r6]) db.session.commit() # Create records for metric 2 r1 = models.Record(metric_id=metric2_id, value=8, date=todayl6) r2 = models.Record(metric_id=metric2_id, value=4, date=todayl5) r3 = models.Record(metric_id=metric2_id, value=3, date=todayl4) r4 = models.Record(metric_id=metric2_id, value=7, date=todayl3)
def parse_incident(report, record): case = record['Case'].split('-') if case[0] == '' and case[1] == '': return case_yr = int(case[0][1:]) case_id = int(case[1]) existing = models.Record.query.filter_by(case_id=case_id, case_yr=case_yr).first() if existing is not None: r = existing else: r = models.Record() r.case_yr = case_yr r.case_id = case_id r.date = record['DateTime'].date() r.time = record['DateTime'].time() r.incident_type = models.RecordType.incident r.report = report r.location = add_or_create_location(record['Address']) r.shift = {'I': 1, 'II': 2, 'III': 3}[record['Shift']] r.incidents = [] r.person = None r.person_res = None for inc in record['Incident']: incident = add_or_create_incident(inc)