コード例 #1
0
ファイル: functions.py プロジェクト: VINKELA/new_version
def database(id):
    if type(id) not in [int]:
        raise TypeError('id must be an int')
    if id < 0:
        raise ValueError('id must be an Integer greater than 0')
    tables = {}
    # format class tables names
    school = select_school_by_id(session.get('user_id'))
    current_session = school[0]["current_session"]
    current_term = school[0]["current_term"]
    tables["class_id"] = id
    tables["school_id"] = session["user_id"]
    schoolId = session["user_id"]
    tables["terms"] = "terms" + "_" + str(schoolId)
    term_identifier = str(current_term) + "_" + str(
        current_session) + "_" + str(tables["school_id"])
    class_identifier = str(tables["class_id"]) + "_" + str(
        current_term) + "_" + str(current_session) + "_" + str(
            tables["school_id"])
    tables["classes"] = "classes" + "_" + term_identifier
    tables["settings"] = "settings" + "_" + term_identifier
    tables["classlist"] = "classlist" + "_" + class_identifier
    tables["ca"] = "catable" + "_" + class_identifier
    tables["test"] = "testtable" + "_" + class_identifier
    tables["exam"] = "examtable" + "_" + class_identifier
    tables["subjects"] = "subjects" + "_" + class_identifier
    tables["mastersheet"] = "mastersheet" + "_" + class_identifier
    tables["subject_position"] = "subject_position" + "_" + class_identifier
    tables["grade"] = "grade" + "_" + class_identifier
    return tables
コード例 #2
0
ファイル: functions.py プロジェクト: VINKELA/new_version
def render_portfolio(error=None):
    tables = database(0)
    rows = select_school_by_id(session['user_id'])
    classrows = select_all_from_table(tables['classes'])
    if error:
        flash(error, 'failure')
    return render_template("portfolio.html",
                           schoolInfo=rows,
                           clas=classrows,
                           error=error)
コード例 #3
0
ファイル: functions.py プロジェクト: VINKELA/new_version
def render_class(class_id, error=None):
    # format class tables names
    tables = database(class_id)
    #query database
    classrow = select_all_from_row(tables['classes'], 'id', class_id)
    schoolrow = select_school_by_id(session['user_id'])
    subjectrow = select_all_from_table(tables['subjects'])
    classlistrow = select_all_from_table(tables['classlist'])
    # render class veiw
    if error:
        flash(error, 'failure')
    return render_template("classView.html",
                           schoolInfo=schoolrow,
                           classData=classrow,
                           subjectData=subjectrow,
                           class_list=classlistrow,
                           error=error)
コード例 #4
0
ファイル: functions.py プロジェクト: VINKELA/new_version
 def decorated_function(*args, **kwargs):
     current_user = select_school_by_id(session.get('user_id'))
     if not current_user[0]["confirmed"]:
         flash('Please confirm your account!', 'warning')
         return redirect("/unconfirmed")
     return func(*args, **kwargs)
コード例 #5
0
def login():
    try:
        if request.method == "POST":
            username = str(request.form.get("username"))
            password = str(request.form.get("password"))
            if username == "":
                error = "username field cannot be empty"
                # return render_template("login.html", error = error)
                return jsonify(message=error, data=False)
            if request.form.get("password") == "":
                error = "password field cannot be empty"
                # return render_template("login.html", error = error)
                return jsonify(message=error, data=False)
            # Query database for username
            rows = select_school_by_username(username)
            # Remember which user has logged in
            # Ensure username exists and password is correct
            if len(rows) == 0:
                error = "user does not exist"
                return jsonify(message=error, data=False)
            if not check_password_hash(rows[0]["admin_password"],
                                       password) and not check_password_hash(
                                           rows[0]["password"], password):
                error = "invalid username/password"
                # return render_template("login.html", error = error)
                return jsonify(message=error, status=401, data=False)
            session["user_id"] = rows[0]["id"]
            # if rows[0]["username"] == "admin":
            #     # select all the schools
            #     all_schools = db.execute("SELECT * FROM school")
            #     # display them in admin portfolio
            #     return render_template("admin_page.html", schoolInfo = all_schools)
            # if account is confirmed render this
            if (rows[0]["confirmed"] == "true"):
                # tables = database(str(0))
                # classRows = select_all_from_table(tables["session_data"])
                # if remember me check box is checked
                if request.form.get("remember_me") == "checked":
                    # generate token
                    random_token = random_string_generator(
                        12, string.ascii_letters + string.punctuation)
                    # generate series id
                    random_series = random_string_generator(
                        12, string.ascii_letters + string.punctuation)
                    #set cookie
                    # resp = make_response(render_template("portfolio.html",schoolInfo = rows, clas = classRows))
                    resp = make_response(jsonify(message='success', data=True))
                    expire_date = datetime.datetime.now()
                    expire_date = expire_date + datetime.timedelta(days=90)
                    resp.set_cookie("series_id",
                                    random_series,
                                    expires=expire_date)
                    resp.set_cookie("main_token",
                                    random_token,
                                    expires=expire_date)
                    update_table(
                        'school', ["series", "token"],
                        [random_series,
                         generate_password_hash(random_token)], 'id',
                        session["user_id"])
                    return resp
                    # return render portfolio
                # return render_template("portfolio.html", schoolInfo = rows, clas = classRows)
                return jsonify(message='success', data=True)

            # else if account is not confirmed render unconfirmed view
            else:
                return jsonify(message='success', data='unconfirmed_user')
        else:
            try:
                session["user_id"]
            except KeyError:
                # return render_template("login.html")
                return jsonify(message='fail', data=False)
            else:
                rows = select_school_by_id(session['user_id'])
                # if account is confirmed render this
                if (rows[0]["confirmed"] == "true"):
                    # tables = database(str(0))
                    rows = select_school_by_id(session['user_id'])
                    # classRows = select_all_from_table(tables['session_data'])
                    # return render portfolio
                    # return render_template("portfolio.html", schoolInfo = rows, clas = classRows)
                    return jsonify(message='success', data=True)

                # else if account is not confirmed render unconfirmed view
                else:
                    return jsonify(message='success', data='unconfirmed_user')
    except Exception as e:
        logger.error(e)
        return jsonify(message='fail', data=False)