예제 #1
0
def update(id):
    student = get_students(id)

    if request.method == 'POST':
        name = request.form['name']
        age = request.form['age']
        gender = request.form['gender']
        error = None

        if not name:
            error = 'Student Name is required.'

        if error is not None:
            flash(error)
        else:
            db = get_db()
            db.execute(
                'UPDATE student SET name = ?, age = ?, gender=? WHERE id = ?',
                (
                    name,
                    age,
                    gender,
                    id,
                ))
            db.commit()
            return redirect(url_for('student.index'))

    return render_template('student/update.html', student=student)
예제 #2
0
def login():

    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']

        db = get_db()
        error = None

        user = db.execute('SELECT * FROM user WHERE username = ?',
                          (username, )).fetchone()

        if user is None:
            error = 'Invalid Username.'
        elif user['password'] != password:
            error = 'Invalid Password.'

        if error is None:
            session.clear()
            session['user_id'] = user['id']
            return redirect(url_for('student.index'))

        flash(error)

    return render_template('auth/login.html')
예제 #3
0
def load_logged_in_user():
    user_id = session.get('user_id')

    if user_id is None:
        g.user = None
    else:
        g.user = get_db().execute('SELECT * FROM user WHERE id = ?',
                                  (user_id, )).fetchone()
예제 #4
0
def get_students(id):
    student = get_db().execute(
        'SELECT id, "S0000" || CAST( id AS TEXT)  AS roll_number, name, age, gender, add_date '
        ' FROM student WHERE id=?', (id, )).fetchone()

    if student is None:
        abort(404, "Post id {0} doesn't exist.".format(id))

    return student
예제 #5
0
def create():
    if request.method == 'POST':
        name = request.form['name']
        age = request.form['age']
        gender = request.form['gender']
        error = None

        if not name:
            error = 'Name is required.'

        if error is not None:
            flash(error)
        else:
            db = get_db()
            db.execute(
                'INSERT INTO student (name, age, gender)'
                ' VALUES (?, ?, ?)', (name, age, gender))
            db.commit()
            return redirect(url_for('student.index'))

    return render_template('student/create.html')
예제 #6
0
def search():
    students = []
    if request.method == 'GET':
        pattern = '%' + request.args.get('search') + '%'
        print(pattern)
        error = None

        if not pattern:
            error = 'Search field cannot be empty.'

        if error is not None:
            flash(error)
        else:
            db = get_db()
            students = db.execute(
                'SELECT id, "S0000" || CAST( id AS TEXT)  AS roll_number, name, age, gender, add_date FROM student'
                ' WHERE name LIKE ?', (pattern, )).fetchall()
            print(students)
            return render_template('student/index.html', students=students)

    return render_template('student/index.html', students=students)
예제 #7
0
def adduser():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        db = get_db()
        error = None

        if not username:
            error = 'Username is required.'
        elif not password:
            error = 'Password is required.'
        elif db.execute('SELECT id FROM user WHERE username = ?',
                        (username, )).fetchone() is not None:
            error = 'User {} is already registered.'.format(username)

        if error is None:
            db.execute('INSERT INTO user (username, password) VALUES (?, ?)',
                       (username, password))
            db.commit()
            return redirect(url_for('student.index'))

        flash(error)

    return render_template('auth/adduser.html')
예제 #8
0
def delete(id):
    get_students(id)
    db = get_db()
    db.execute('DELETE FROM student WHERE id = ?', (id, ))
    db.commit()
    return redirect(url_for('student.index'))
예제 #9
0
def index():
    db = get_db()
    students = db.execute(
        'SELECT id, "S0000" || CAST( id AS TEXT)  AS roll_number, name, age, gender, add_date FROM student'
    ).fetchall()
    return render_template('student/index.html', students=students)