Beispiel #1
0
def tutors():

    if request.method == "POST":
        className = request.form.get("subject-search")

        if not className:
            flash("Please select a subject.", "error")
            return redirect(url_for("tutors"))

        subject = [k for k, v in subject_dict.items() if className in v][0]

        DB = Db()
        query = (
            "SELECT tutors.tutorID, name, email, grade, image, status, questionName, answerText "
            "FROM tutors JOIN answers ON tutors.tutorID = answers.tutorID JOIN questions on answers.questionID = questions.questionID "
            "WHERE status = 1 AND tutors.tutorID IN (SELECT DISTINCT tutorID from answers WHERE answerText = %s) "
            "AND (questionName = 'description' OR questionName = 'motivation' OR questionName = %s);"
        )
        arguments = (className, subject + "Experience")
        tutors = DB.execute(query, arguments)

        if tutors:
            for tutor in tutors:
                tutor[tutor["questionName"]] = tutor["answerText"]
                tutor.pop("questionName")
                tutor.pop("answerText")

            tutors = merge_list(tutors, "tutorID")

            tutors = sorted(tutors, key=lambda k: k['name'].lower())

        DB.close_connection()
        return render_template("tutors.html",
                               tutors=tutors,
                               className=className,
                               subject=subject,
                               subject_dict=subject_dict,
                               subject_display_names=subject_display_names)

    DB = Db()
    query = (
        "SELECT tutors.tutorID, name, email, grade, image, status, questionName, answerText "
        "FROM tutors JOIN answers ON tutors.tutorID = answers.tutorID JOIN questions on answers.questionID = questions.questionID "
        "WHERE status = 1;")
    tutors = DB.execute(query)

    for tutor in tutors:
        tutor[tutor["questionName"]] = tutor["answerText"]
        tutor.pop("questionName")
        tutor.pop("answerText")

    tutors = merge_list(tutors, "tutorID")
    tutors = sorted(
        tutors, key=lambda k: k['name'].lower()
    )  # https://stackoverflow.com/questions/72899/how-do-i-sort-a-list-of-dictionaries-by-a-value-of-the-dictionary

    # Define list of subjects
    subjects = subject_dict.keys()

    DB.close_connection()
    return render_template("tutors.html",
                           tutors=tutors,
                           subjects=subjects,
                           subject_dict=subject_dict,
                           subject_display_names=subject_display_names)
Beispiel #2
0
def volunteer():
    form = TutorApplicationForm()

    if form.validate_on_submit():

        # Get form data
        data = form.data
        for value in data:
            value = value.strip()

        # Get form values
        name = data["name"]
        email = data["email"]
        grade = data["grade"]

        # Connect to database
        DB = Db()

        # Test if user is already registered as a tutor/potential tutor
        result = DB.execute("SELECT * from tutors WHERE email = %s", (email, ))
        if result:
            if result[0]["status"] == 1:
                flash("This email is already registered as a tutor.", "error")
            else:
                flash("This email is already registered as a potential tutor.",
                      "error")
            return render_template("volunteer.html", form=form)

        # Get user responses

        # Get classes
        subjects = subject_dict.keys()
        classes = []
        for subject in [data[i] for i in subjects]:
            for className in subject:
                classes.append(className)

        # Get experiences
        mathExperience = data["mathExperience"]
        scienceExperience = data["scienceExperience"]
        englishExperience = data["englishExperience"]
        historyExperience = data["historyExperience"]
        csExperience = data["csExperience"]
        languageExperience = data["languageExperience"]
        businessExperience = data["businessExperience"]
        speechExperience = data["speechExperience"]

        description = data["description"]
        motivation = data["motivation"]
        referral = data["referral"]
        extra = data["extra"]
        image = data["image"]
        x = data["x"]
        y = data["y"]
        w = data["w"]
        h = data["h"]

        # Store tutor profile information
        if allowed_file(image.filename):

            # Protect against forged filenames
            filename = secure_filename(image.filename)

            # Hash the file
            filename = secrets.token_urlsafe(16) + ".jpeg"

            # Save image to images folder
            image_path = os.path.join(app.config["IMG_UPLOAD_FOLDER"],
                                      filename)
            image.save(image_path)

            # If user used JCrop, crop the image
            if w:
                x = float(x)
                y = float(y)
                w = float(w)
                h = float(h)
                temp = Image.open(image_path)
                temp = image_transpose_exif(temp)

                # Get dpi of image
                dpi = (300, 300)
                if 'dpi' in temp.info:
                    dpi = temp.info['dpi']

                # Modify crop width to fit image size (instead of the size displayed in the browser)
                height = temp.size[1]
                browserH = 300  # The height of the image in the browser

                # Resize image to match browser dimensions * scale
                scale = height / browserH
                temp = temp.crop(
                    (x * scale, y * scale, (x + w) * scale, (y + h) *
                     scale))  # Crop image using JCrop browser dimensions
                temp.thumbnail(
                    (692, 400)
                )  # Make image smaller while maintaining 1.73 aspect ratio (for browser)
                temp = temp.convert('RGB')
                temp.save(image_path, 'jpeg', quality=90, dpi=dpi)

            # Define query and arguments
            date = datetime.now().strftime("%Y-%m-%d")
            query = "INSERT INTO tutors (name, email, grade, image, hours, applyDate) VALUES(%s, %s, %s, %s, %s, %s)"
            arguments = (name, email, grade, filename, 0, date)

            # Connect to DB & execute insert query
            DB.execute(query, arguments)
        else:
            flash(
                "Invalid image format (only .jpg, .jpeg, and .png are allowed). Please try again.",
                "error")
            DB.close_connection()
            return render_template("volunteer.html", form=form)

        # Store tutor answers

        tutorID = DB.execute("SELECT LAST_INSERT_ID() AS id")[0]["id"]

        # Store subjects they want to teach
        for className in classes:
            if className:
                query = "INSERT INTO answers(formID, questionID, tutorID, answerText) VALUES(%s, %s, %s, %s)"
                arguments = (1, 1, tutorID, className)
                DB.execute(query, arguments)

        # Store other answers
        for questionName in questions_dict:
            if questionName != "subjects":
                query = "INSERT INTO answers(formID, questionID, tutorID, answerText) VALUES(%s, %s, %s, %s)"
                arguments = (1, questions_dict[questionName], tutorID,
                             locals()[questionName])
                DB.execute(query, arguments)

        flash(
            "Thanks for applying! The application process typically takes up to 1 week; if you're accepted, we'll send you an email with further details on the tutoring process."
        )
        DB.close_connection()
        return redirect(url_for("volunteer"))

    return render_template("volunteer.html",
                           form=form,
                           subject_dict=subject_dict,
                           subject_display_names=subject_display_names)
Beispiel #3
0
def confirm_tutors():
    if request.method == "POST":
        decision = request.form.get("submit-btn")
        tutorID = request.form.get("tutorID")
        DB = Db()

        if decision == "Accept":
            status = 1
        elif decision == "Move to Pending":
            status = 0
        elif decision == "Reject":
            status = -1
        elif decision == "Remove":
            status = -2
        else:
            flash(
                Markup(
                    f"ERROR: Invalid Status. Please report this incident <a href={ url_for('contact') }>here</a>."
                ), "error")
            return redirect(url_for("confirm_tutors"))

        # Remove tutor
        if status == -2:
            tutor = DB.execute("SELECT * from tutors WHERE tutorID = %s",
                               (tutorID, ))[0]

            DB.execute(
                "INSERT INTO removedTutors (name, email, grade, image, hours, applyDate) VALUES (%s, %s, %s, %s, %s, %s)",
                (tutor['name'], tutor['email'], tutor['grade'], tutor['image'],
                 tutor['hours'], tutor['applyDate']))
            DB.execute("DELETE FROM answers WHERE tutorID = %s", (tutorID, ))
            DB.execute("DELETE FROM tutors WHERE tutorID = %s", (tutorID, ))

        # Change tutor status
        else:
            # If accepted, send acceptance email
            if status == 1:
                tutor = DB.execute(
                    "SELECT name, email from tutors WHERE tutorID = %s",
                    (tutorID, ))[0]
                first_name = tutor['name'].split()[0]
                send_email(tutor['email'], "Interact Tutors Acceptance",
                           acceptance_email_default % (first_name),
                           acceptance_email_html % (first_name))

            DB.execute("UPDATE tutors SET status = %s WHERE tutorID = %s",
                       (status, tutorID))

        flash("Status updated!")
        DB.close_connection()
        return redirect(url_for("confirm_tutors"))

    DB = Db()
    query = (
        "SELECT tutors.tutorID, name, email, grade, image, hours, applyDate, status, questionName, answerText "
        "FROM tutors JOIN answers ON tutors.tutorID = answers.tutorID JOIN questions on answers.questionID = questions.questionID;"
    )
    tutors = DB.execute(query)

    for tutor in tutors:
        tutor[tutor["questionName"]] = tutor["answerText"]
        tutor.pop("questionName")
        tutor.pop("answerText")

    tutors = merge_list(tutors, "tutorID")

    tutors = sorted(
        tutors, key=lambda k: k['name'].lower()
    )  # https://stackoverflow.com/questions/72899/how-do-i-sort-a-list-of-dictionaries-by-a-value-of-the-dictionary

    # Define list of subjects
    subjects = subject_dict.keys()

    DB.close_connection()
    return render_template("confirmTutors.html",
                           tutors=tutors,
                           subjects=subjects)
Beispiel #4
0
from database import Db
from helpers import send_email
from email_templates import mass_email_default, mass_email_html

DB = Db()

tutors = DB.execute("SELECT email FROM tutors WHERE status = 1")
emails = [tutor["email"] for tutor in tutors]
emails.append("*****@*****.**")

for email in emails:
    send_email(email, "Interact Tutors Update", mass_email_default,
               mass_email_html)

DB.close_connection()