From 92f3222e1bc46d4d1c7b2f5686280e7731ba767b Mon Sep 17 00:00:00 2001 From: Khaled Mahfouz Date: Wed, 11 Jun 2025 19:22:28 +0300 Subject: [PATCH] - version : 0.9 - description : adding an option to modify any student's info with seperate modify_info.html file and seperate route . --- app.py | 84 ++++++++++++++++++++++++++++++++-- templates/index.html | 9 +++- templates/modify_info.html | 93 ++++++++++++++++++++++++++++++++++++++ templates/template.html | 2 + 4 files changed, 184 insertions(+), 4 deletions(-) create mode 100644 templates/modify_info.html diff --git a/app.py b/app.py index dc944c6..31d647c 100644 --- a/app.py +++ b/app.py @@ -7,7 +7,7 @@ from flask import Flask, render_template, request, redirect, url_for, flash, sen # --- App Setup --- app = Flask(__name__) app.secret_key = 'your_super_secret_key_12345' -app.config['PHONE_REGEX'] = re.compile(r'^09\d{8}$') # Syrian phone format +app.config['PHONE_REGEX'] = re.compile(r'^09\d{8}$') # Syrian phone format # --- Database Functions --- def get_db_connection(): @@ -138,6 +138,83 @@ def add_student(): return redirect(url_for('index')) +@app.route('/modify_student/', methods=['GET', 'POST']) +def modify_student(student_id): + if request.method == 'GET': + # Display the modification form + try: + with get_db_connection() as conn: + student = conn.execute('SELECT * FROM students WHERE id = ?', (student_id,)).fetchone() + + if student is None: + flash('الطالب غير موجود.', 'danger') + return redirect(url_for('index')) + + return render_template('modify_info.html', student=student) + except sqlite3.Error as e: + flash(f'خطأ في قاعدة البيانات: {str(e)}', 'danger') + return redirect(url_for('index')) + + elif request.method == 'POST': + # Process the modification form submission + form_data = request.form + validation_errors = validate_student_data(form_data) + + if validation_errors: + for error in validation_errors: + flash(error, 'danger') + # Fetch student again to re-render form with current data and errors + try: + with get_db_connection() as conn: + student = conn.execute('SELECT * FROM students WHERE id = ?', (student_id,)).fetchone() + return render_template('modify_info.html', student=student) + except sqlite3.Error as e: + flash(f'خطأ في قاعدة البيانات: {str(e)}', 'danger') + return redirect(url_for('index')) + + try: + student_data = ( + form_data['student_name'], + int(form_data['age']), + form_data['parent_name'], + form_data['parent_phone_1'], + form_data.get('parent_phone_2') or None, + form_data.get('student_phone') or None, + form_data['grade'], + form_data['school_name'], + form_data['address'], + form_data['memorizing'], + student_id # The ID is the last parameter for the WHERE clause + ) + + with get_db_connection() as conn: + conn.execute(''' + UPDATE students SET + student_name = ?, + age = ?, + parent_name = ?, + parent_phone_1 = ?, + parent_phone_2 = ?, + student_phone = ?, + grade = ?, + school_name = ?, + address = ?, + memorizing = ? + WHERE id = ? + ''', student_data) + conn.commit() + + flash('تم تحديث بيانات الطالب بنجاح!', 'success') + return redirect(url_for('index')) + + except sqlite3.IntegrityError as e: + flash(f'خطأ في قاعدة البيانات: {str(e)}', 'danger') + except Exception as e: + flash(f'خطأ غير متوقع: {str(e)}', 'danger') + + return redirect(url_for('index')) + + @app.route('/import_csv', methods=['POST']) def import_csv(): if 'file' not in request.files: @@ -202,11 +279,11 @@ def import_csv(): # Process validation results if row_errors: flash(f'تم العثور على أخطاء في {len(row_errors)} سطراً', 'warning') - for error in row_errors[:5]: # Show first 5 errors + for error in row_errors[:5]: # Show first 5 errors flash(error, 'danger') if len(row_errors) > 5: flash(f'...و {len(row_errors)-5} أخطاء إضافية', 'danger') - + if valid_rows: with get_db_connection() as conn: conn.executemany(''' @@ -248,3 +325,4 @@ def points(): if __name__ == '__main__': app.run(debug=True) + diff --git a/templates/index.html b/templates/index.html index a09009a..5cb1f20 100644 --- a/templates/index.html +++ b/templates/index.html @@ -139,6 +139,7 @@ المدرسة العنوان المحفوظات + الإجراءات {# New header for actions #} @@ -159,6 +160,13 @@ {{ student['school_name'] }} {{ student['address'] }} {{ student['memorizing'] }} + {# New column for actions #} + + + {# Pen icon #} + + {# You can add a delete button here later if needed #} + {% else %} @@ -183,7 +191,6 @@ const addManuallyButton = document.getElementById('add-manually-button'); const addStudentSection = document.getElementById('add-student-section'); const addStudentForm = document.getElementById('add-student-form'); - const importForm = document.getElementById('import-form'); const submitAddButton = document.getElementById('submit-add-button'); // New elements for search functionality diff --git a/templates/modify_info.html b/templates/modify_info.html new file mode 100644 index 0000000..f9ccdfc --- /dev/null +++ b/templates/modify_info.html @@ -0,0 +1,93 @@ +{% extends 'template.html' %} + +{% block title %}تعديل معلومات الطالب{% endblock %} + +{% block content %} +
+

تعديل معلومات الطالب

+ + {# Navigation Bar #} + +
+ +
+

تعديل معلومات الطالب: {{ student.student_name }}

+
+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+
+ + + إلغاء + +
+
+
+ + +{% endblock %} diff --git a/templates/template.html b/templates/template.html index cd055a8..9e2b49a 100644 --- a/templates/template.html +++ b/templates/template.html @@ -9,6 +9,8 @@ + {# Font Awesome for icons #} +