|
| 1 | +""" |
| 2 | +Submission Export Plugin for CTFd |
| 3 | +Allows admins to view and export submission status for all users |
| 4 | +""" |
| 5 | +import csv |
| 6 | +from io import StringIO |
| 7 | +from flask import Blueprint, render_template, Response |
| 8 | +from CTFd.models import db, Users, Challenges, Solves, UserFieldEntries, UserFields |
| 9 | +from CTFd.utils.decorators import admins_only |
| 10 | +from CTFd.plugins import register_admin_plugin_menu_bar |
| 11 | +from sqlalchemy import func |
| 12 | + |
| 13 | + |
| 14 | +def load(app): |
| 15 | + """Load the submission export plugin""" |
| 16 | + |
| 17 | + # Register menu item in admin panel |
| 18 | + register_admin_plugin_menu_bar( |
| 19 | + title='Submission Export', |
| 20 | + route='/admin/submission_export/' |
| 21 | + ) |
| 22 | + |
| 23 | + # Create blueprint for the plugin |
| 24 | + submission_export = Blueprint( |
| 25 | + 'submission_export', |
| 26 | + __name__, |
| 27 | + template_folder='templates', |
| 28 | + static_folder='static', |
| 29 | + url_prefix='/admin/submission_export' |
| 30 | + ) |
| 31 | + |
| 32 | + @submission_export.route('/') |
| 33 | + @admins_only |
| 34 | + def index(): |
| 35 | + """Display submission status page""" |
| 36 | + # Get all challenges ordered by ID |
| 37 | + challenges = Challenges.query.order_by(Challenges.id).all() |
| 38 | + |
| 39 | + # Get all users with their information |
| 40 | + users = Users.query.filter_by(type='user', banned=False, hidden=False).order_by(Users.id).all() |
| 41 | + |
| 42 | + # Get Student ID Number field |
| 43 | + student_id_field = UserFields.query.filter_by(name="Student ID Number").first() |
| 44 | + |
| 45 | + # Build user data with submissions |
| 46 | + user_data = [] |
| 47 | + for user in users: |
| 48 | + # Get student ID |
| 49 | + student_id = "" |
| 50 | + if student_id_field: |
| 51 | + student_id_entry = UserFieldEntries.query.filter_by( |
| 52 | + user_id=user.id, |
| 53 | + field_id=student_id_field.id |
| 54 | + ).first() |
| 55 | + if student_id_entry: |
| 56 | + student_id = student_id_entry.value |
| 57 | + |
| 58 | + # Get solves for this user |
| 59 | + user_solves = { |
| 60 | + solve.challenge_id: solve |
| 61 | + for solve in Solves.query.filter_by(user_id=user.id).all() |
| 62 | + } |
| 63 | + |
| 64 | + # Build challenge scores |
| 65 | + challenge_scores = {} |
| 66 | + for challenge in challenges: |
| 67 | + if challenge.id in user_solves: |
| 68 | + # Get the score (value) for the challenge |
| 69 | + challenge_scores[challenge.id] = challenge.value or 0 |
| 70 | + else: |
| 71 | + challenge_scores[challenge.id] = 0 |
| 72 | + |
| 73 | + user_data.append({ |
| 74 | + 'id': user.id, |
| 75 | + 'name': user.name, |
| 76 | + 'email': user.email, |
| 77 | + 'student_id': student_id, |
| 78 | + 'challenge_scores': challenge_scores |
| 79 | + }) |
| 80 | + |
| 81 | + return render_template( |
| 82 | + 'submission_export.html', |
| 83 | + users=user_data, |
| 84 | + challenges=challenges |
| 85 | + ) |
| 86 | + |
| 87 | + @submission_export.route('/export.csv') |
| 88 | + @admins_only |
| 89 | + def export_csv(): |
| 90 | + """Export submission data as CSV""" |
| 91 | + # Get all challenges ordered by ID |
| 92 | + challenges = Challenges.query.order_by(Challenges.id).all() |
| 93 | + |
| 94 | + # Get all users |
| 95 | + users = Users.query.filter_by(type='user', banned=False, hidden=False).order_by(Users.id).all() |
| 96 | + |
| 97 | + # Get Student ID Number field |
| 98 | + student_id_field = UserFields.query.filter_by(name="Student ID Number").first() |
| 99 | + |
| 100 | + # Create CSV in memory with UTF-8 BOM for Excel compatibility |
| 101 | + output = StringIO() |
| 102 | + # Write UTF-8 BOM for proper Korean encoding in Excel |
| 103 | + output.write('\ufeff') |
| 104 | + writer = csv.writer(output) |
| 105 | + |
| 106 | + # Write header |
| 107 | + header = ['Name', 'Email', 'Student ID'] |
| 108 | + for challenge in challenges: |
| 109 | + header.append(f'{challenge.name} (ID: {challenge.id})') |
| 110 | + header.append('Total Score') |
| 111 | + writer.writerow(header) |
| 112 | + |
| 113 | + # Write user data |
| 114 | + for user in users: |
| 115 | + # Get student ID |
| 116 | + student_id = "" |
| 117 | + if student_id_field: |
| 118 | + student_id_entry = UserFieldEntries.query.filter_by( |
| 119 | + user_id=user.id, |
| 120 | + field_id=student_id_field.id |
| 121 | + ).first() |
| 122 | + if student_id_entry: |
| 123 | + student_id = student_id_entry.value |
| 124 | + |
| 125 | + # Get solves for this user |
| 126 | + user_solves = { |
| 127 | + solve.challenge_id: solve |
| 128 | + for solve in Solves.query.filter_by(user_id=user.id).all() |
| 129 | + } |
| 130 | + |
| 131 | + # Build row with total score |
| 132 | + row = [user.name, user.email, student_id] |
| 133 | + total_score = 0 |
| 134 | + for challenge in challenges: |
| 135 | + if challenge.id in user_solves: |
| 136 | + score = challenge.value or 0 |
| 137 | + row.append(score) |
| 138 | + total_score += score |
| 139 | + else: |
| 140 | + row.append(0) |
| 141 | + row.append(total_score) |
| 142 | + |
| 143 | + writer.writerow(row) |
| 144 | + |
| 145 | + # Prepare response |
| 146 | + output.seek(0) |
| 147 | + return Response( |
| 148 | + output.getvalue(), |
| 149 | + mimetype='text/csv; charset=utf-8', |
| 150 | + headers={ |
| 151 | + 'Content-Disposition': 'attachment; filename=submission_export.csv' |
| 152 | + } |
| 153 | + ) |
| 154 | + |
| 155 | + # Register blueprint |
| 156 | + app.register_blueprint(submission_export) |
| 157 | + |
| 158 | + print("[Submission Export Plugin] Loaded successfully") |
0 commit comments