def footprint_readstorage():
    if not check_logged_in():
        return "Unauthorized!", 403
    if "footprintcipher" not in session or "footprintrows" not in session:
        return "No stored footprints", 403

    cipher = session.get("footprintcipher")
    rows_raw = session.get("footprintrows")
    table_data = base64.b64decode(cipher.encode("ascii")).decode("ascii")
    print(table_data)
    table_data = urllib.parse.parse_qs(table_data)

    # Delete stored footprint (remove if draft storage becomes a feature)
    session.pop("footprintcipher", None)
    session.pop("footprintrows", None)

    keys = ["row", "date", "time", "duration", "lat", "lon"]
    keys_2 = ["uuid", "date", "time", "duration", "lat", "lng"]
    rows = base64.b64decode(rows_raw.encode("ascii")).decode("ascii")
    rows = str.split(rows, ",")
    rows = sorted(rows)

    standard_data = []
    for (i, row) in enumerate(rows):
        standard_data.append({})
        for (j, key) in enumerate(keys):
            data = table_data.get(key + row, [""])[0]
            if key == "row":
                standard_data[i][keys_2[j]] = "anything_" + data
            else:
                standard_data[i][keys_2[j]] = data

    return jsonify(standard_data)
def footprint_location_change():
    if not check_logged_in():
        return "Unauthorized!", 403

    lat = request.form.get("lat")
    lng = request.form.get("lng")
    rowId = request.form.get("row", "")

    if "footprintcipher" not in session:
        return "No stored footprints", 403

    cipher = session.get("footprintcipher")
    table_data = base64.b64decode(cipher.encode("ascii")).decode("ascii")
    print(table_data)
    table_data = urllib.parse.parse_qs(table_data, keep_blank_values=True)

    for key in table_data:
        table_data[key] = table_data[key][0]

    if "lat" + rowId in table_data:
        table_data["lat" + rowId] = lat
        table_data["lon" + rowId] = lng
    print(table_data)

    table_data = urllib.parse.urlencode(table_data)
    cipher = base64.b64encode(table_data.encode("ascii")).decode("ascii")
    session["footprintcipher"] = cipher

    return "OK"
def footprint_load():
    if (check_logged_in()):
        user_id = session["user"]
        print(user_id)
        olddata = get_user_footprint(user_id)

        return jsonify(olddata)
def footprint_storage():
    if check_logged_in():
        cipher = request.form.get("cipher")
        rows = request.form.get("rows")
        if "footprintcipher" in session:
            session.pop("footprintcipher", None)
        session["footprintcipher"] = cipher
        session["footprintrows"] = rows

        return "OK"
    else:
        return "Unauthorized!", 403
def footprint_delete():
    if request.method == "POST":
        remove_list = request.json["data"]
        print(remove_list)
        if (check_logged_in()):
            user_id = session["user"]
            print("start deleting user{}".format(user_id))
            dynamo.batch_remove("footprint", user_id, remove_list)
        else:
            print("Error: NOT LOGGED IN ")

    return redirect(url_for("footprint_upload"))
Beispiel #6
0
def view_assessment_history():
    if (check_logged_in()):
        logged, username = get_login_status_webpage()
        filename = username + ".json"
        if (S3.check_file_exists(filename)):
            obj = json.load(S3.download_file_to_object(filename))
            q1 = obj.get(
                "Have you had in-person closed contact with patients who diagnosed with coronavirus?",
                "unknown")
            q2 = obj.get(
                "Have you visited a place that patients who diagnosed with coronavirus has been to?",
                "unknown")
            q3 = obj.get(
                "Have you traveled to foreign countries in the last 14 days?",
                "unknown")
            q4 = obj.get("symptoms", [])
            result = obj.get("result", "unknown")

            if "suspicious" in result or "emergency" in result:
                risk_level = "danger"
            elif "isolation" in result:
                risk_level = "warning"
            else:
                risk_level = "success"

            return render_template("assessment_history.html",
                                   assessment_exist=True,
                                   q1=q1,
                                   q2=q2,
                                   q3=q3,
                                   q4=q4,
                                   result=result,
                                   risk_level=risk_level,
                                   username=username,
                                   logged=logged,
                                   title="Assessment History")
        else:
            return render_template("assessment_history.html",
                                   assessment_exist=False,
                                   username=username,
                                   logged=logged,
                                   title="Assessment History")

    else:
        return redirect(url_for("main"))
def footprint_upload():
    logged, username = get_login_status_webpage()
    validation_error = False

    if "validation_error" in session:
        validation_error = True
        session.pop("validation_error", None)

    if (check_logged_in()):
        user_id = session["user"]
        if "footprintcipher" in session:
            with_storage = True
        else:
            with_storage = False
        return render_template("footprint_collect.html",
                               user_id=user_id,
                               username=username,
                               logged=logged,
                               title="Footprint Submission",
                               with_storage=with_storage,
                               validation_error=validation_error)
    else:
        return redirect(url_for("main"))
def footprint_upload_submit():
    if (check_logged_in()):
        user_id = session["user"]

        print(request.form)
        length = request.form.get("rowCount")
        rowarray = request.form.get("rowList").split(",")
        print(rowarray)
        row_list = []
        key_list = []
        data = []
        # if length != None:
        for i in rowarray:
            ######ADD INVALID INPUT CHECK !!!! AND ERROR HANDLING ###
            uuid = user_id + "_" + str(i)
            date = request.form.get("date" + str(i))
            print(date)
            time = request.form.get("time" + str(i))
            print(time)
            duration = request.form.get("duration" + str(i))
            lat = request.form.get("lat" + str(i))
            lon = request.form.get("lon" + str(i))

            # Validation
            if (not isdate(date) or not istime(time) or not isfloat(duration)
                    or not isfloat(lat) or not isfloat(lon)):
                session["validation_error"] = True
                return redirect(url_for("footprint_upload"))

            # need further consideration

            cur_list = []
            rowdata = {
                "time": {
                    "Value": time,
                    "Action": 'PUT'
                },
                "duration": {
                    "Value": duration,
                    "Action": 'PUT'
                },
                "lat": {
                    "Value": lat,
                    "Action": 'PUT'
                },
                "lng": {
                    "Value": lon,
                    "Action": 'PUT'
                }
            }
            key = {"date": date, "uuid": uuid}
            # rowdata = {"uuid": uuid, "date": date, "time": time, "duration": duration, "lat": lat, "lng": lon}
            row_list.append(rowdata)
            key_list.append(key)
            # cur_list.append(uuid)
            cur_list.append(date)
            cur_list.append(time)
            cur_list.append(duration)
            cur_list.append(lat)
            cur_list.append(lon)
            data.append(cur_list)

        # for j in range(len(date_json_list)):
        #     S3.upload_file(file_path+date_json_list[j],date_json_list[j])

        print(data)
        filename = date + ".json"
        keypair = {"user_id": user_id}
        updatelist = {"footprint_path": {"Value": filename, "Action": 'PUT'}}
        dynamo.update_data("user", keypair, updatelist)
        batch_helper("footprint", row_list, key_list)
        # dynamo.batch_put_items("footprint", row_list)
        #make sure user_id is UNIQUE otherwise overwirte

        return redirect(url_for("footprint_upload"))
    else:
        return redirect(url_for("main"))
Beispiel #9
0
def evaluate_results():
    logged, username = get_login_status_webpage()
    if (check_logged_in()):
        user = session["user"]
        fever = request.form.get('fever')
        cough = request.form.get('cough')
        breath = request.form.get('breath')
        chest_pain = request.form.get('chest_pain')
        other_symptoms = request.form.get('other_symptoms')

        one = request.form.get('one')
        two = request.form.get('two')
        three = request.form.get('three')
        if one is None or two is None or three is None:
            session["validation_error"] = True
            return redirect(url_for("submit_assessment"))

        #Analyze the user's answer
        if (fever or cough or breath or chest_pain) and (one == "yes"):
            respond_type = 0
        elif (breath or chest_pain) and (one == "no"):
            respond_type = 1
        elif (fever or cough or other_symptoms) or (one == "yes") or (
                one == "unknown") or (two == "yes") or (three == "yes"):
            respond_type = 2
        else:
            respond_type = 3

        #find assessment result according to respond_type
        comment = [
            "Your status is suspicious and you should test for COVID-19",
            "Please call 911 or go directly to nearest emergency",
            "Please do a self-isolation for 14 days and keep monitoring on your health status",
            "You are safe for now. Please keep staying at home"
        ]
        response = comment[respond_type]

        #write result file
        file_path = user + ".json"
        symptoms = []
        if (fever != None):
            symptoms.append(fever)

        if (cough != None):
            symptoms.append(cough)

        if (breath != None):
            symptoms.append(breath)

        if (chest_pain != None):
            symptoms.append(chest_pain)

        if (other_symptoms != ""):
            symptoms.append(other_symptoms)

        json_object = {
            "Have you had in-person closed contact with patients who diagnosed with coronavirus?":
            one,
            "Have you visited a place that patients who diagnosed with coronavirus has been to?":
            two,
            "Have you traveled to foreign countries in the last 14 days?":
            three,
            "symptoms":
            symptoms,
            "result":
            response
        }

        #check if the result file already exists
        if S3.check_file_exists(file_path):
            s3_response = S3.delete_file(file_path)
        #upload new result file to S3 bucket
        data = json.dumps(json_object)
        s3_response2 = S3.upload_file_from_object(data, file_path)

        #dynamodb
        keypair = {"user_id": user}
        updatelist = {
            "survey_result": {
                "Value": respond_type,
                "Action": 'PUT'
            }
        }
        updatelist_filepath = {
            "file_path": {
                "Value": file_path,
                "Action": 'PUT'
            }
        }
        dynamo.update_data("user", keypair, updatelist)
        dynamo.update_data("user", keypair, updatelist_filepath)

        # Risk level
        if "suspicious" in response or "emergency" in response:
            risk_level = "danger"
        elif "isolation" in response:
            risk_level = "warning"
        else:
            risk_level = "success"

        return render_template('assessment_result.html',
                               response=response,
                               risk_level=risk_level,
                               logged=logged,
                               username=username,
                               title="Self-Assessment Results")
    else:
        return redirect(url_for("main"))