feat: implement API security measures phase 1
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
# Generated by Django 5.1.7 on 2026-05-27 00:22
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
def assign_existing_applications_to_first_superuser(apps, schema_editor):
|
||||
"""Assign existing JobApplication rows to the first superuser."""
|
||||
JobApplication = apps.get_model("jobs", "JobApplication")
|
||||
User = apps.get_model("accounts", "User")
|
||||
admin = User.objects.filter(is_superuser=True).order_by("id").first()
|
||||
if admin is not None:
|
||||
JobApplication.objects.filter(user__isnull=True).update(user=admin)
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('jobs', '0001_initial'),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
# 1. Add user FK as nullable initially so existing rows can be migrated
|
||||
migrations.AddField(
|
||||
model_name='jobapplication',
|
||||
name='user',
|
||||
field=models.ForeignKey(
|
||||
blank=True,
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name='job_applications',
|
||||
to=settings.AUTH_USER_MODEL,
|
||||
help_text='User who owns this job application.',
|
||||
),
|
||||
),
|
||||
# 2. Assign existing rows to the first superuser
|
||||
migrations.RunPython(
|
||||
assign_existing_applications_to_first_superuser,
|
||||
reverse_code=migrations.RunPython.noop,
|
||||
),
|
||||
# 3. Make user non-nullable now that all rows have a value
|
||||
migrations.AlterField(
|
||||
model_name='jobapplication',
|
||||
name='user',
|
||||
field=models.ForeignKey(
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name='job_applications',
|
||||
to=settings.AUTH_USER_MODEL,
|
||||
help_text='User who owns this job application.',
|
||||
),
|
||||
),
|
||||
# 4. Add the unique constraint
|
||||
migrations.AddConstraint(
|
||||
model_name='jobapplication',
|
||||
constraint=models.UniqueConstraint(
|
||||
fields=('user', 'company_name', 'position_title'),
|
||||
name='unique_user_job_application',
|
||||
),
|
||||
),
|
||||
]
|
||||
Reference in New Issue
Block a user