Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
RohanCyberOps authored Oct 20, 2024
1 parent 8947e05 commit c56e8ff
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 0 deletions.
87 changes: 87 additions & 0 deletions school_app/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
from flask import Flask, render_template, request, redirect, url_for
from flask_mysqldb import MySQL

app = Flask(__name__)

# MySQL configuration
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = 'Rohan15@'
app.config['MYSQL_DB'] = 'school_db'

mysql = MySQL(app)


# Student class to represent a student object
class Student:
def __init__(self, id, name, grade):
self.id = id
self.name = name
self.grade = grade


@app.route('/')
def index():
search_query = request.args.get('search', '')
cursor = mysql.connection.cursor()

if search_query:
cursor.execute("SELECT * FROM students WHERE name LIKE %s", (f"%{search_query}%",))
else:
cursor.execute("SELECT * FROM students")

student_tuples = cursor.fetchall()
cursor.close()

# Convert tuples to Student objects
students = [Student(id=row[0], name=row[1], grade=row[2]) for row in student_tuples]

return render_template('index.html', students=students)


@app.route('/add_student', methods=['GET', 'POST'])
def add_student():
if request.method == 'POST':
name = request.form['name']
grade = request.form['grade']
cursor = mysql.connection.cursor()
cursor.execute("INSERT INTO students (name, grade) VALUES (%s, %s)", (name, grade))
mysql.connection.commit()
cursor.close()
return redirect(url_for('index'))
return render_template('add_student.html')


@app.route('/update_student/<int:student_id>', methods=['GET', 'POST'])
def update_student(student_id):
cursor = mysql.connection.cursor()

if request.method == 'POST':
name = request.form['name']
grade = request.form['grade']
cursor.execute("UPDATE students SET name = %s, grade = %s WHERE id = %s", (name, grade, student_id))
mysql.connection.commit()
cursor.close()
return redirect(url_for('index'))

cursor.execute("SELECT * FROM students WHERE id = %s", (student_id,))
student = cursor.fetchone()
cursor.close()

# Convert the tuple to a Student object
student_obj = Student(id=student[0], name=student[1], grade=student[2]) if student else None

return render_template('update_student.html', student=student_obj)


@app.route('/delete_student/<int:student_id>', methods=['POST'])
def delete_student(student_id):
cursor = mysql.connection.cursor()
cursor.execute("DELETE FROM students WHERE id = %s", (student_id,))
mysql.connection.commit()
cursor.close()
return redirect(url_for('index'))


if __name__ == '__main__':
app.run(debug=True)
7 changes: 7 additions & 0 deletions school_app/static/bootstrap.min.css

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions school_app/static/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
body {
background-color: #f8f9fa;
}

h1 {
color: #343a40;
}

.btn {
margin: 5px;
}

.card {
margin-top: 20px;
}

.list-group-item {
background-color: #ffffff;
}

input[type="text"] {
border-radius: 0.25rem;
}
28 changes: 28 additions & 0 deletions school_app/templates/add_student.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{{ url_for('static', filename='bootstrap.min.css') }}">
<title>Add Student</title>
</head>
<body>
<div class="container mt-5">
<h1 class="text-center">Add Student</h1>
<form action="{{ url_for('add_student') }}" method="POST" class="mt-4">
<div class="mb-3">
<label for="name" class="form-label">Name:</label>
<input type="text" id="name" name="name" class="form-control" required>
</div>
<div class="mb-3">
<label for="grade" class="form-label">Grade:</label>
<input type="text" id="grade" name="grade" class="form-control" required>
</div>
<button type="submit" class="btn btn-success">Add Student</button>
</form>
<div class="text-center mt-3">
<a href="{{ url_for('index') }}" class="btn btn-secondary">Back to Student List</a>
</div>
</div>
</body>
</html>
41 changes: 41 additions & 0 deletions school_app/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{{ url_for('static', filename='bootstrap.min.css') }}">
<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
<title>School App</title>
</head>
<body>
<div class="container mt-5">
<h1 class="text-center">Student Management</h1>
<div class="text-center mb-3">
<a href="{{ url_for('add_student') }}" class="btn btn-primary">Add Student</a>
</div>
<form class="mb-3" action="{{ url_for('index') }}" method="GET">
<div class="input-group">
<input type="text" name="search" class="form-control" placeholder="Search for students" value="{{ request.args.get('search', '') }}">
<button class="btn btn-outline-secondary" type="submit">Search</button>
</div>
</form>
<div class="card">
<div class="card-body">
<ul class="list-group">
{% for student in students %}
<li class="list-group-item d-flex justify-content-between align-items-center">
{{ student.name }} - Grade: {{ student.grade }}
<div>
<a href="{{ url_for('update_student', student_id=student.id) }}" class="btn btn-warning btn-sm">Edit</a>
<form action="{{ url_for('delete_student', student_id=student.id) }}" method="POST" class="d-inline">
<button type="submit" class="btn btn-danger btn-sm">Delete</button>
</form>
</div>
</li>
{% endfor %}
</ul>
</div>
</div>
</div>
</body>
</html>
28 changes: 28 additions & 0 deletions school_app/templates/update_student.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{{ url_for('static', filename='bootstrap.min.css') }}">
<title>Update Student</title>
</head>
<body>
<div class="container mt-5">
<h1 class="text-center">Update Student</h1>
<form action="{{ url_for('update_student', student_id=student.id) }}" method="POST" class="mt-4">
<div class="mb-3">
<label for="name" class="form-label">Name:</label>
<input type="text" id="name" name="name" class="form-control" value="{{ student.name }}" required>
</div>
<div class="mb-3">
<label for="grade" class="form-label">Grade:</label>
<input type="text" id="grade" name="grade" class="form-control" value="{{ student.grade }}" required>
</div>
<button type="submit" class="btn btn-success">Update Student</button>
</form>
<div class="text-center mt-3">
<a href="{{ url_for('index') }}" class="btn btn-secondary">Back to Student List</a>
</div>
</div>
</body>
</html>

0 comments on commit c56e8ff

Please sign in to comment.