Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create table project program area xref 45 #398

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion app/core/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ class Meta:
class ProjectSerializer(serializers.ModelSerializer):
"""Used to retrieve project info"""

program_areas = serializers.StringRelatedField(many=True)

class Meta:
model = Project
fields = (
Expand All @@ -117,6 +119,7 @@ class Meta:
"image_logo",
"image_hero",
"image_icon",
"program_areas",
)
read_only_fields = (
"uuid",
Expand Down Expand Up @@ -227,9 +230,11 @@ class Meta:
class ProgramAreaSerializer(serializers.ModelSerializer):
"""Used to retrieve program_area info"""

projects = serializers.StringRelatedField(many=True)

class Meta:
model = ProgramArea
fields = ("uuid", "name", "description", "image")
fields = ("uuid", "name", "description", "image", "projects")
read_only_fields = ("uuid", "created_at", "updated_at")


Expand Down
20 changes: 20 additions & 0 deletions app/core/migrations/0028_project_program_areas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 4.2.11 on 2024-10-04 18:26

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("core", "0027_socmajor"),
]

operations = [
migrations.AddField(
model_name="project",
name="program_areas",
field=models.ManyToManyField(
blank=True, related_name="projects", to="core.programarea"
),
),
]
2 changes: 1 addition & 1 deletion app/core/migrations/max_migration.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0027_socmajor
0028_project_program_areas
6 changes: 6 additions & 0 deletions app/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ class Project(AbstractBaseModel):
image_logo = models.URLField(blank=True)
image_hero = models.URLField(blank=True)
image_icon = models.URLField(blank=True)
program_areas = models.ManyToManyField(
"ProgramArea",
related_name="projects",
blank=True,
# through="ProjectProgramAreaXref"
)

def __str__(self):
return f"{self.name}"
Expand Down
15 changes: 15 additions & 0 deletions app/core/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
SKILL_URL = reverse("skill-list")
STACK_ELEMENT_URL = reverse("stack-element-list")
PERMISSION_TYPE = reverse("permission-type-list")
PROJECT_URL = reverse("project-list")
STACK_ELEMENT_TYPE_URL = reverse("stack-element-type-list")
SDG_URL = reverse("sdg-list")
AFFILIATION_URL = reverse("affiliation-list")
Expand Down Expand Up @@ -381,3 +382,17 @@ def test_create_soc_major(auth_client):
res = auth_client.post(SOC_MAJOR_URL, payload)
assert res.status_code == status.HTTP_201_CREATED
assert res.data["title"] == payload["title"]


def test_project_program_area_xref(auth_client, project, program_area):
project.program_areas.add(program_area)
project.save()

proj_res = auth_client.get(PROJECT_URL)
program_area_res = auth_client.get(PROGRAM_AREA_URL)

assert filter(lambda proj: str(proj["uuid"]) == str(project.pk), proj_res.data)
assert filter(
lambda _program_area: str(_program_area["uuid"]) == str(program_area.pk),
program_area_res,
)
32 changes: 32 additions & 0 deletions app/core/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import pytest

from ..models import Event
from ..models import ProgramArea
from ..models import Project

pytestmark = pytest.mark.django_db

Expand Down Expand Up @@ -145,3 +147,33 @@ def test_check_type(check_type):

def test_soc_major(soc_major):
assert str(soc_major) == "Test Soc Major"


def test_project_program_area_relationship(project):
civic_tech_infrastructure_program_area = ProgramArea.objects.get(
name="Civic Tech Infrastructure"
)
workforce_program_area = ProgramArea.objects.get(name="Workforce Development")
hack_for_la_project = Project.objects.create(name="Hack for LA Site")
hack_for_la_project.program_areas.add(
civic_tech_infrastructure_program_area, workforce_program_area
)
assert hack_for_la_project.program_areas.count() == 2
assert hack_for_la_project.program_areas.contains(
civic_tech_infrastructure_program_area
)
assert hack_for_la_project.program_areas.contains(workforce_program_area)
assert civic_tech_infrastructure_program_area.projects.contains(hack_for_la_project)
assert workforce_program_area.projects.contains(hack_for_la_project)

hack_for_la_project.program_areas.remove(civic_tech_infrastructure_program_area)
assert hack_for_la_project.program_areas.count() == 1
assert not hack_for_la_project.program_areas.contains(
civic_tech_infrastructure_program_area
)
assert hack_for_la_project.program_areas.contains(workforce_program_area)

assert not civic_tech_infrastructure_program_area.projects.contains(
hack_for_la_project
)
assert workforce_program_area.projects.contains(hack_for_la_project)
2 changes: 1 addition & 1 deletion scripts/createsuperuser.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ set -x

# This command requires the DJANGO_SUPERUSER_USERNAME and
# DJANGO_SUPERUSER_PASSWORD environmental variables to be set when django starts
echo "DJANGO_SUPERUSER_USERNAME: $DJANGO_SUPERUSER_USERNAME"
# echo "DJANGO_SUPERUSER_USERNAME: $DJANGO_SUPERUSER_USERNAME"
docker-compose exec web python manage.py createsuperuser --no-input