diff --git a/app.py b/app.py index 7755bb2..8a6b7b4 100644 --- a/app.py +++ b/app.py @@ -3,6 +3,7 @@ import csv import io import re from flask import Flask, render_template, request, redirect, url_for, flash, send_from_directory +import datetime # Import datetime for current date # --- App Setup --- app = Flask(__name__) @@ -30,12 +31,14 @@ def init_db(): grade TEXT NOT NULL, school_name TEXT NOT NULL, address TEXT NOT NULL, - memorizing TEXT NOT NULL + memorizing TEXT NOT NULL, + notes TEXT, -- New: Optional notes field + registration_date TEXT NOT NULL -- New: Registration date ) ''') # Add indexes for faster search conn.execute('CREATE INDEX idx_student_name ON students(student_name)') - conn.execute('CREATE INDEX idx_parent_name ON students(parent_name)') + conn.execute('CREATE INDEX idx_parent_name ON students(parent_name)') # Changed from idx_parent_phone print("Database initialized with schema constraints and indexes") @app.cli.command('init-db') @@ -49,8 +52,8 @@ def validate_phone(phone): def validate_student_data(form_data, is_csv=False): errors = [] - required_fields = ['student_name', 'age', 'parent_name', - 'parent_phone_1', 'grade', 'school_name', + required_fields = ['student_name', 'age', 'parent_name', + 'parent_phone_1', 'grade', 'school_name', 'address', 'memorizing'] # Check required fields @@ -64,7 +67,7 @@ def validate_student_data(form_data, is_csv=False): ('parent_phone_2', form_data.get('parent_phone_2')), ('student_phone', form_data.get('student_phone')) ] - + for field, value in phones: if value and not validate_phone(value): errors.append(f"رقم الهاتف '{field}' غير صالح. يجب أن يكون 10 أرقام ويبدأ بـ 09") @@ -76,7 +79,16 @@ def validate_student_data(form_data, is_csv=False): errors.append("العمر يجب أن يكون بين 5 و 25 سنة") except ValueError: errors.append("العمر يجب أن يكون رقماً صحيحاً") - + + # Validate registration_date format if provided + reg_date_str = form_data.get('registration_date') + if reg_date_str: + try: + # Attempt to parse the date. Format 'YYYY-MM-DD' is common for HTML date input + datetime.datetime.strptime(reg_date_str, '%Y-%m-%d') + except ValueError: + errors.append("تاريخ التسجيل غير صالح. يجب أن يكون بالصيغة YYYY-MM-DD") + return errors # --- App Routes --- @@ -85,7 +97,7 @@ def index(): try: with get_db_connection() as conn: students = conn.execute(''' - SELECT * FROM students + SELECT * FROM students ORDER BY student_name ASC ''').fetchall() return render_template('index.html', students=students) @@ -97,12 +109,17 @@ def index(): def add_student(): form_data = request.form validation_errors = validate_student_data(form_data) - + if validation_errors: for error in validation_errors: flash(error, 'danger') return redirect(url_for('index')) - + + # Get current date if registration_date is not provided + registration_date = form_data.get('registration_date') + if not registration_date: + registration_date = datetime.date.today().isoformat() # YYYY-MM-DD format + try: student_data = ( form_data['student_name'], @@ -114,28 +131,30 @@ def add_student(): form_data['grade'], form_data['school_name'], form_data['address'], - form_data['memorizing'] + form_data['memorizing'], + form_data.get('notes') or None, # New: notes + registration_date # New: registration_date ) - + with get_db_connection() as conn: conn.execute(''' INSERT INTO students ( - student_name, age, parent_name, - parent_phone_1, parent_phone_2, - student_phone, grade, school_name, - address, memorizing - ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + student_name, age, parent_name, + parent_phone_1, parent_phone_2, + student_phone, grade, school_name, + address, memorizing, notes, registration_date + ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ''', 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('/modify_student/', methods=['GET', 'POST']) @@ -145,21 +164,21 @@ def modify_student(student_id): 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') @@ -171,7 +190,13 @@ def modify_student(student_id): except sqlite3.Error as e: flash(f'خطأ في قاعدة البيانات: {str(e)}', 'danger') return redirect(url_for('index')) - + + # Get current date if registration_date is not provided during modification + registration_date = form_data.get('registration_date') + if not registration_date: + registration_date = datetime.date.today().isoformat() # YYYY-MM-DD format + + try: student_data = ( form_data['student_name'], @@ -184,9 +209,11 @@ def modify_student(student_id): form_data['school_name'], form_data['address'], form_data['memorizing'], + form_data.get('notes') or None, # New: notes + registration_date, # New: registration_date student_id # The ID is the last parameter for the WHERE clause ) - + with get_db_connection() as conn: conn.execute(''' UPDATE students SET @@ -199,21 +226,142 @@ def modify_student(student_id): grade = ?, school_name = ?, address = ?, - memorizing = ? + memorizing = ?, + notes = ?, -- New + registration_date = ? -- New 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: + flash('لم يتم تقديم ملف', 'danger') + return redirect(url_for('index')) + + file = request.files['file'] + if file.filename == '': + flash('لم يتم اختيار ملف', 'warning') + return redirect(url_for('index')) + + if not file.filename.lower().endswith('.csv'): + flash('صيغة الملف غير مدعومة. يجب أن يكون CSV', 'danger') + return redirect(url_for('index')) + + try: + stream = io.TextIOWrapper(file.stream, encoding='utf-8-sig') + csv_reader = csv.reader(stream) + valid_rows = [] + row_errors = [] + + # Assuming CSV now has 12 columns: existing 10 + notes + registration_date + expected_columns = 12 + + for i, row in enumerate(csv_reader, 1): + if len(row) != expected_columns: + row_errors.append(f'السطر {i}: عدد الأعمدة غير صحيح ({expected_columns} مطلوبة)') + continue + + # Map CSV columns to form fields + student_data = { + 'student_name': row[0].strip(), + 'age': row[1].strip(), + 'parent_name': row[2].strip(), + 'parent_phone_1': row[3].strip(), + 'parent_phone_2': row[4].strip(), + 'student_phone': row[5].strip(), + 'grade': row[6].strip(), + 'school_name': row[7].strip(), + 'address': row[8].strip(), + 'memorizing': row[9].strip(), + 'notes': row[10].strip(), # New: notes + 'registration_date': row[11].strip() # New: registration_date + } + + # If registration_date is empty in CSV, set to current date + if not student_data['registration_date']: + student_data['registration_date'] = datetime.date.today().isoformat() + + # Validate row + errors = validate_student_data(student_data, is_csv=True) + if errors: + row_errors.append(f'السطر {i}: {"; ".join(errors)}') + continue + + # Prepare for insertion + valid_rows.append(( + student_data['student_name'], + int(student_data['age']), + student_data['parent_name'], + student_data['parent_phone_1'], + student_data['parent_phone_2'] or None, + student_data['student_phone'] or None, + student_data['grade'], + student_data['school_name'], + student_data['address'], + student_data['memorizing'], + student_data['notes'] or None, # New: notes + student_data['registration_date'] # New: registration_date + )) + + # Process validation results + if row_errors: + flash(f'تم العثور على أخطاء في {len(row_errors)} سطراً', 'warning') + 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(''' + INSERT INTO students ( + student_name, age, parent_name, + parent_phone_1, parent_phone_2, + student_phone, grade, school_name, + address, memorizing, notes, registration_date + ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + ''', valid_rows) + conn.commit() + flash(f'تم استيراد {len(valid_rows)} طالب بنجاح', 'success') + else: + flash('لم يتم استيراد أي سجلات', 'warning') + + except csv.Error as e: + flash(f'خطأ في معالجة CSV: {str(e)}', 'danger') + except Exception as e: + flash(f'خطأ غير متوقع: {str(e)}', 'danger') + + return redirect(url_for('index')) + +# Route to download the CSV template +@app.route('/download_csv_template') +def download_csv_template(): + # The directory where the template.csv is located (your templates folder) + # The second argument is the filename to be sent + return send_from_directory(app.template_folder, 'template.csv', as_attachment=True) + +# Route for the "تسجيل حضور أو حفظ" page +@app.route('/record') +def record(): + return render_template('record.html') + +# Route for the "النقاط" page +@app.route('/points') +def points(): + return render_template('points.html') + @app.route('/delete_student/', methods=['POST']) def delete_student(student_id): try: @@ -234,114 +382,6 @@ def delete_student(student_id): return redirect(url_for('index')) -@app.route('/import_csv', methods=['POST']) -def import_csv(): - if 'file' not in request.files: - flash('لم يتم تقديم ملف', 'danger') - return redirect(url_for('index')) - - file = request.files['file'] - if file.filename == '': - flash('لم يتم اختيار ملف', 'warning') - return redirect(url_for('index')) - - if not file.filename.lower().endswith('.csv'): - flash('صيغة الملف غير مدعومة. يجب أن يكون CSV', 'danger') - return redirect(url_for('index')) - - try: - stream = io.TextIOWrapper(file.stream, encoding='utf-8-sig') - csv_reader = csv.reader(stream) - valid_rows = [] - row_errors = [] - - for i, row in enumerate(csv_reader, 1): - if len(row) != 10: - row_errors.append(f'السطر {i}: عدد الأعمدة غير صحيح (10 مطلوبة)') - continue - - # Map CSV columns to form fields - student_data = { - 'student_name': row[0].strip(), - 'age': row[1].strip(), - 'parent_name': row[2].strip(), - 'parent_phone_1': row[3].strip(), - 'parent_phone_2': row[4].strip(), - 'student_phone': row[5].strip(), - 'grade': row[6].strip(), - 'school_name': row[7].strip(), - 'address': row[8].strip(), - 'memorizing': row[9].strip() - } - - # Validate row - errors = validate_student_data(student_data, is_csv=True) - if errors: - row_errors.append(f'السطر {i}: {"; ".join(errors)}') - continue - - # Prepare for insertion - valid_rows.append(( - student_data['student_name'], - int(student_data['age']), - student_data['parent_name'], - student_data['parent_phone_1'], - student_data['parent_phone_2'] or None, - student_data['student_phone'] - or None, - student_data['grade'], - student_data['school_name'], - student_data['address'], - student_data['memorizing'] - )) - - # Process validation results - if row_errors: - flash(f'تم العثور على أخطاء في {len(row_errors)} سطراً', 'warning') - 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(''' - INSERT INTO students ( - student_name, age, parent_name, - parent_phone_1, parent_phone_2, - student_phone, grade, school_name, - address, memorizing - ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - ''', valid_rows) - conn.commit() - flash(f'تم استيراد {len(valid_rows)} طالب بنجاح', 'success') - else: - flash('لم يتم استيراد أي سجلات', 'warning') - - except csv.Error as e: - flash(f'خطأ في معالجة CSV: {str(e)}', 'danger') - except Exception as e: - flash(f'خطأ غير متوقع: {str(e)}', 'danger') - - return redirect(url_for('index')) - -# Route to download the CSV template -@app.route('/download_csv_template') -def download_csv_template(): - # The directory where the template.csv is located (your templates folder) - # The second argument is the filename to be sent - return send_from_directory(app.template_folder, 'template.csv', as_attachment=True) - -# Route for the "تسجيل حضور أو حفظ" page -@app.route('/record') -def record(): - return render_template('record.html') - -# Route for the "النقاط" page -@app.route('/points') -def points(): - return render_template('points.html') if __name__ == '__main__': - app.run(debug=True) - + app.run(debug=True) \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index 54ea812..e392ffa 100644 --- a/templates/index.html +++ b/templates/index.html @@ -69,6 +69,14 @@ +
+ + +
+
+ + +
+
+ + +
+
+ + +