This file provides guidance to WARP (warp.dev) when working with code in this repository.
This is a Django backend for an academic timetable generation system that creates class schedules for university departments. It features sophisticated constraint-based scheduling algorithms, user management, and data isolation capabilities.
Django Apps:
timetable- Main scheduling functionality with models, algorithms, and APIsusers- Custom user management with Firebase authenticationbackend- Django project settings and configuration
Key Models (timetable/models.py):
Subject- Academic subjects with credits and practical/theory classificationTeacher- Faculty with availability constraints and subject assignmentsClassroom- Rooms with building information and lab classificationBatch- Student groups (21SW, 22SW, etc.) with semester informationTeacherSubjectAssignment- Links teachers to subjects by batch and sectionsTimetableEntry- Generated schedule entries with time slots and constraintsScheduleConfig- Scheduling parameters and time configurationsDepartment/UserDepartment- Multi-tenant data isolation system
Scheduling Algorithms (timetable/algorithms/):
working_scheduler.py- Fast, deterministic scheduler (primary)advanced_scheduler.py- Genetic algorithm with complex constraintsconstraint_enforced_scheduler.py- Active constraint enforcement during generationfinal_scheduler.py- Universal scheduler with enhanced features
Multi-Tenant Design:
- Data isolation by department and user ownership
- Shared access controls between users
- Firebase UID integration for authentication
Constraint System:
- Teacher availability (unavailable periods with time ranges)
- Room type matching (labs for practicals, regular for theory)
- Subject frequency based on credits
- Cross-semester conflict detection
- Building priority for room allocation
# Install dependencies (requirements.txt not present - use pip install as needed)
pip install django djangorestframework django-cors-headers celery
# Database setup
python manage.py migrate
# Create superuser
python manage.py createsuperuser
# Run development server
python manage.py runserver# Populate database with sample data
python scripts/populate_fall_semester_data.py
python scripts/populate_spring_semester_data.py
# Clean all data (DESTRUCTIVE)
python scripts/cleanup_all.py
# Clean only timetable entries (preserves subjects/teachers)
python scripts/cleanup_timetable.py
# Generate timetables for all batches
python generate_all_batches.py# Make migrations after model changes
python manage.py makemigrations
# Apply migrations
python manage.py migrate
# Database shell
python manage.py dbshell
# Django shell for data exploration
python manage.py shell# Check for issues
python manage.py check
# Run with debug toolbar (already installed)
python manage.py runserver
# Test constraint validation
python check_constraint_status.py
# Fix specific constraint violations
python fix_duplicates.py
python fix_room_allocation.py
python fix_building_violations.py# Start Celery worker (for async timetable generation)
celery -A backend worker --loglevel=info
# Monitor Celery tasks
celery -A backend flower- Primary:
WorkingTimetableScheduler- Fast, reliable, conflict-free - Advanced:
AdvancedTimetableScheduler- Genetic algorithm with complex constraints - Constraint-Enforced:
ConstraintEnforcedScheduler- Active constraint enforcement
- Teacher Availability - Hard constraint, zero tolerance for violations
- Room Type Matching - Labs for practicals, regular rooms for theory
- Subject Frequency - Credits determine weekly class count
- Practical Blocks - 3 consecutive periods for practical subjects
- Cross-Semester Conflicts - Prevent teacher double-booking across batches
- Building Priority - Lab Block > Main Block > Academic Building
- Time Limits - 8:00 AM to 3:00 PM working hours
# Via API endpoint
POST /api/timetables/generate-fast/
{
"config_id": 1,
"algorithm": "working" # or "advanced", "constraint_enforced"
}
# Or direct algorithm usage
from timetable.algorithms.working_scheduler import WorkingTimetableScheduler
scheduler = WorkingTimetableScheduler(config)
result = scheduler.generate_timetable()/api/subjects/- Subject management/api/teachers/- Teacher management with availability/api/classrooms/- Classroom management/api/batches/- Student batch management/api/teacher-assignments/- Teacher-subject assignments/api/schedule-configs/- Scheduling configurations/api/timetables/- Timetable entries and generation
- JWT tokens via
rest_framework_simplejwt - Firebase UID integration
- Custom User model extending AbstractUser
- Database: SQLite for development (
db.sqlite3) - Celery: SQLite broker for async tasks
- CORS: Enabled for localhost:3000 (React frontend)
- Debug Toolbar: Enabled for development
- JWT: 60-minute access tokens, 1-day refresh tokens
- Default start time: 8:00 AM
- Class duration: 60 minutes
- Working days: Monday-Friday
- Periods: Configurable via ScheduleConfig
- Update constraint validation in
enhanced_constraint_validator.py - Modify scheduling algorithms to enforce during generation
- Add constraint checking in
_evaluate_solution()methods - Update constraint weights in scheduler initialization
- Create new file in
timetable/algorithms/ - Implement base interface with
generate_timetable()method - Register in
views.pyscheduling endpoints - Add algorithm selection in generation APIs
- Modify models in
timetable/models.py - Create migrations:
python manage.py makemigrations - Apply migrations:
python manage.py migrate - Update serializers in
serializers.py - Update any affected algorithms
# Check all constraints status
python check_constraint_status.py
# Validate specific timetable
python enhanced_constraint_validator.py
# Test teacher availability
python -c "from timetable.models import Teacher; t = Teacher.objects.first(); print(t.unavailable_periods)"# Test scheduling algorithms
from timetable.algorithms.working_scheduler import WorkingTimetableScheduler
from timetable.models import ScheduleConfig
config = ScheduleConfig.objects.first()
scheduler = WorkingTimetableScheduler(config)
result = scheduler.generate_timetable()
print(f"Generation time: {result['generation_time']:.2f}s")- Migration conflicts - Reset migrations with
python manage.py migrate --fake-initial - Teacher availability violations - Check
unavailable_periodsJSON format - Room allocation failures - Ensure lab rooms exist for practical subjects
- Cross-semester conflicts - Clear conflicting timetable entries
- Celery tasks stuck - Restart Celery worker
- Enable detailed logging in algorithms by setting logger level to DEBUG
- Use Django Debug Toolbar for SQL query optimization
- Check constraint violation messages in timetable generation responses
- Constraint-First Design - All scheduling respects hard constraints absolutely
- Algorithm Modularity - Multiple scheduling approaches for different needs
- Data Isolation - Multi-tenant architecture with department-based separation
- Performance Optimization - Fast deterministic scheduling for real-time use
- Extensible Constraints - Easy to add new scheduling requirements
- Cross-Semester Awareness - Prevents conflicts across different academic terms