
- Set up project structure with FastAPI and SQLite - Implement user authentication with JWT - Create models for learning content (subjects, lessons, quizzes) - Add progress tracking and gamification features - Implement comprehensive API documentation - Add error handling and validation - Set up proper logging and health check endpoint
58 lines
2.1 KiB
Python
58 lines
2.1 KiB
Python
from __future__ import annotations
|
|
|
|
import enum
|
|
from datetime import datetime
|
|
|
|
from sqlalchemy import Boolean, Column, DateTime, Enum, ForeignKey, Integer, String, Text
|
|
from sqlalchemy.orm import relationship
|
|
|
|
from app.db.base_class import Base
|
|
|
|
|
|
class AchievementType(enum.Enum):
|
|
COMPLETION = "completion" # Complete lessons/quizzes
|
|
STREAK = "streak" # Maintain a daily learning streak
|
|
PERFORMANCE = "performance" # Get high scores or perfect quizzes
|
|
MILESTONE = "milestone" # Reach specific milestones (points, levels)
|
|
SPECIAL = "special" # Special or seasonal achievements
|
|
|
|
|
|
class Achievement(Base):
|
|
"""
|
|
Achievement model for defining available achievements in the system.
|
|
"""
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
name = Column(String, index=True, nullable=False)
|
|
description = Column(Text, nullable=False)
|
|
type = Column(Enum(AchievementType), nullable=False)
|
|
image_url = Column(String, nullable=True)
|
|
points = Column(Integer, default=0) # Points awarded for earning this achievement
|
|
criteria = Column(Text, nullable=False) # JSON string with criteria for earning
|
|
is_active = Column(Boolean, default=True)
|
|
created_at = Column(DateTime, default=datetime.utcnow)
|
|
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
|
|
|
# Relationships
|
|
user_achievements = relationship("UserAchievement", back_populates="achievement")
|
|
|
|
|
|
class UserAchievement(Base):
|
|
"""
|
|
UserAchievement model for tracking achievements earned by users.
|
|
"""
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
user_id = Column(Integer, ForeignKey("user.id"), nullable=False)
|
|
achievement_id = Column(Integer, ForeignKey("achievement.id"), nullable=False)
|
|
earned_at = Column(DateTime, default=datetime.utcnow)
|
|
|
|
# Relationships
|
|
user = relationship("User", back_populates="achievements")
|
|
achievement = relationship("Achievement", back_populates="user_achievements")
|
|
|
|
# This ensures that a user can only earn a specific achievement once
|
|
__table_args__ = (
|
|
# UniqueConstraint('user_id', 'achievement_id', name='user_achievement_uc'),
|
|
)
|