Skip to content

Migrate flight-blender from Django to FastAPI (partial)#1

Draft
Copilot wants to merge 8 commits intofastapifrom
copilot/convert-django-to-fastapi
Draft

Migrate flight-blender from Django to FastAPI (partial)#1
Copilot wants to merge 8 commits intofastapifrom
copilot/convert-django-to-fastapi

Conversation

Copy link

Copilot AI commented Feb 26, 2026

Partial migration of flight-blender from Django/DRF to FastAPI/SQLAlchemy. This PR establishes the core infrastructure and converts a subset of modules. The migration is incomplete — remaining view modules, app entry point, and cleanup are still needed.

What's done

  • Dependencies (pyproject.toml): Replaced Django, DRF, django-celery-beat, dj-database-url, channels/channels-redis with FastAPI, SQLAlchemy 2.0, Alembic, websockets
  • Core infrastructure:
    • flight_blender/config.py — env-based config replacing settings.py
    • flight_blender/db.py — SQLAlchemy engine, SessionLocal, Base, get_db() dependency
    • flight_blender/celery.py — decoupled from Django settings, explicit task discovery
  • All 28 Django ORM models → SQLAlchemy across 8 models.py files
  • common/database_operations.py (1361 lines) — full rewrite from Django ORM to SQLAlchemy session-based queries; FlightBlenderDatabaseReader/Writer now accept db: Session in constructor
  • auth_helper/utils.pyrequires_scopes decorator now uses fastapi.HTTPException instead of django.http.JsonResponse, extracts auth from request.headers instead of request.META
  • common/data_definitions.py — removed gettext_lazy dependency, plain strings
  • common/utils.py — removed DjangoJSONEncoder dependency
  • Converted view modules: flight_feed_operations, rid_operations — Django views → FastAPI APIRouter endpoints with Depends(get_db) injection

What's NOT done

  • View conversions for: scd_operations, uss_operations, geo_fence_operations, flight_declaration_operations, weather_monitoring_operations, surveillance_monitoring_operations, conformance_monitoring_operations
  • FastAPI main app entry point (asgi.py with router mounting)
  • Alembic migration setup
  • Docker/entrypoint updates
  • Test migration (Django TestCase → pytest + TestClient)
  • Cleanup of Django artifacts (admin.py, apps.py, migrations/, manage.py)

Pattern

# Before (Django)
@api_view(["POST"])
@requires_scopes(["flightblender.write"])
def set_air_traffic(request, session_id):
    body = request.data
    my_database_writer = FlightBlenderDatabaseWriter()
    return JsonResponse(result, status=200)

# After (FastAPI)
@router.post("/set_air_traffic/{session_id}")
@requires_scopes(["flightblender.write"])
async def set_air_traffic(request: Request, session_id: str, db: Session = Depends(get_db)):
    body = json.loads(await request.body())
    my_database_writer = FlightBlenderDatabaseWriter(db)
    return JSONResponse(content=result, status_code=200)

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 7 commits February 26, 2026 23:46
- Add flight_blender/config.py: Pydantic-style config replacing Django settings
- Add flight_blender/db.py: SQLAlchemy engine, session, and Base setup
- Update flight_blender/celery.py: Remove Django dependency, use dotenv and explicit conf
- Update flight_blender/__init__.py: Remove Django-style import, use wildcard logging import

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Convert 8 models.py files from Django ORM to SQLAlchemy:
- flight_feed_operations/models.py
- flight_declaration_operations/models.py
- geo_fence_operations/models.py
- conformance_monitoring_operations/models.py
- constraint_operations/models.py
- rid_operations/models.py
- surveillance_monitoring_operations/models.py
- notification_operations/models.py

Key changes:
- Replace Django models.Model with SQLAlchemy Base (DeclarativeBase)
- Convert all field types to SQLAlchemy Column types
- Convert ForeignKey/OneToOneField to ForeignKey + relationship()
- Replace auto_now_add/auto_now with default/onupdate datetime.utcnow
- Remove django_celery_beat dependency from TaskScheduler
- Rename metadata field to metadata_ (mapped to 'metadata' column) to
  avoid conflict with SQLAlchemy reserved attribute

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Replace Django ORM queries with SQLAlchemy Session-based queries
- Both FlightBlenderDatabaseReader and FlightBlenderDatabaseWriter now
  accept a db: Session parameter in their constructor
- Replace Model.objects.get() with db.query(Model).filter().first()
- Replace Model.objects.filter().exists() with .first() is not None
- Replace .save() with db.add(); db.commit(); db.refresh()
- Replace .delete() with db.delete() or db.query().delete()
- Replace django.db.utils.IntegrityError with sqlalchemy.exc.IntegrityError
- Replace QuerySet return types with list[Model]
- Replace Django signals with direct function call to handler
- Replace TaskScheduler.schedule_every/start/terminate with DB record
  creation/deletion and Celery beat scheduler warning
- Add db.rollback() in exception handlers for transaction safety

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Use FK column assignments (e.g., declaration_id=x.id) instead of
  relationship assignments for SQLAlchemy model construction
- Fix metadata -> metadata_ to match FlightObservation model attribute
- Use .is_(True)/.is_(False) for boolean comparisons in SQLAlchemy filters

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…rations to FastAPI/SQLAlchemy

Co-authored-by: RomanPszonka <57806+RomanPszonka@users.noreply.github.com>
…go/DRF to FastAPI

- Replace Django views/DRF views with FastAPI route handlers using APIRouter
- Convert HomeView/ASGIHomeView TemplateViews to simple JSON endpoints
- Replace JsonResponse with JSONResponse, request.data with json.loads(await request.body())
- Add request: Request and db: Session = Depends(get_db) parameters
- Convert DRF ListCreateAPIView/RetrieveUpdateDestroyAPIView to individual CRUD endpoints
- Convert DRF ModelSerializer to Pydantic BaseModel
- Simplify urls.py to just import and export the router

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Replace @api_view decorators with FastAPI @router.get/post/put/delete
- Replace JsonResponse/HttpResponse with JSONResponse/Response from fastapi
- Replace request.data with json.loads(await request.body())
- Add request: Request, db: Session = Depends(get_db) parameters
- Pass db to FlightBlenderDatabaseReader/Writer and SubscriptionsHelper
- Make view functions async
- Simplify urls.py to re-export the router

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI changed the title [WIP] Convert flight-blender from Django to FastAPI Migrate flight-blender from Django to FastAPI (partial) Feb 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants