Test / dump_all.py
Zheng Qu
working for MIDTERM2a and b
11c72a0
raw
history blame
4.77 kB
import os
import logging
import zipfile
import tempfile
from datetime import datetime
from pathlib import Path
from app_utils import get_db
logger = logging.getLogger(__name__)
def create_markdown_content(submissions):
"""Generate markdown content from a list of submissions"""
content = "# Student Submissions\n\n"
# Add student info section
if submissions:
content += f"**Student Name**: {submissions[0]['name']}\n"
content += f"**Email**: {submissions[0]['email']}\n\n"
content += "---\n\n"
# Sort submissions by timestamp (newest first)
sorted_submissions = sorted(
submissions,
key=lambda x: datetime.fromisoformat(x['timestamp']),
reverse=True
)
for submission in sorted_submissions:
# Add section for each submission
content += f"## Problem: {submission['problem_id']}\n\n"
# Add timestamp
timestamp = datetime.fromisoformat(submission['timestamp'])
content += f"**Submitted**: {timestamp.strftime('%Y-%m-%d %H:%M:%S')}\n"
content += f"**Hint Used**: {'Yes' if submission['hint_requested'] else 'No'}\n\n"
# Add code section
content += "### Code Submission\n"
content += "```python\n"
content += submission['student_code']
content += "\n```\n\n"
content += "---\n\n"
return content
def group_submissions_by_student(submissions):
"""Group submissions by student name"""
student_submissions = {}
for submission in submissions:
name = submission['name']
if name not in student_submissions:
student_submissions[name] = []
student_submissions[name].append(submission)
return student_submissions
def create_markdown_files(student_submissions, output_dir):
"""Create markdown files for each student in the specified directory"""
output_dir = Path(output_dir)
output_dir.mkdir(exist_ok=True)
created_files = []
for name, submissions in student_submissions.items():
# Create safe filename from student name
safe_filename = "".join(x for x in name if x.isalnum() or x in "- ").strip()
safe_filename = safe_filename.replace(" ", "_") + ".md"
# Generate markdown content
content = create_markdown_content(submissions)
# Write to file
file_path = output_dir / safe_filename
with open(file_path, 'w', encoding='utf-8') as f:
f.write(content)
created_files.append(file_path)
logger.info(f"Created markdown file for {name}: {file_path}")
return created_files
def create_markdown_zip():
"""Create a zip file containing markdown files for all submissions"""
try:
# Get submissions from database
_, submissions_table = get_db()
all_submissions = submissions_table.all()
if not all_submissions:
logger.warning("No submissions found in database")
return None
# Create temporary directory for markdown files
with tempfile.TemporaryDirectory() as temp_dir:
# Group submissions and create markdown files
student_submissions = group_submissions_by_student(all_submissions)
markdown_files = create_markdown_files(student_submissions, temp_dir)
# Create zip file
zip_path = tempfile.mktemp(suffix='.zip')
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
for file_path in markdown_files:
zipf.write(file_path, file_path.name)
logger.info(f"Created zip file with {len(markdown_files)} markdown files")
return zip_path
except Exception as e:
logger.error(f"Error creating markdown zip: {str(e)}")
return None
def main():
"""Main function for command-line usage"""
output_dir = Path("db_dump")
try:
# Get submissions from database
_, submissions_table = get_db()
all_submissions = submissions_table.all()
if not all_submissions:
logger.warning("No submissions found in database")
return
# Group submissions and create markdown files
student_submissions = group_submissions_by_student(all_submissions)
create_markdown_files(student_submissions, output_dir)
logger.info(f"Successfully created markdown files in {output_dir}")
logger.info(f"Total number of students: {len(student_submissions)}")
except Exception as e:
logger.error(f"Error occurred: {str(e)}")
if __name__ == "__main__":
main()