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 sdg xref 66 #385

Open
wants to merge 4 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
6 changes: 6 additions & 0 deletions 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"""

sdgs = serializers.StringRelatedField(many=True)

class Meta:
model = Project
fields = (
Expand All @@ -117,6 +119,7 @@ class Meta:
"image_logo",
"image_hero",
"image_icon",
"sdgs",
)
read_only_fields = (
"uuid",
Expand Down Expand Up @@ -309,13 +312,16 @@ class SdgSerializer(serializers.ModelSerializer):
Used to retrieve Sdg
"""

projects = serializers.StringRelatedField(many=True)

class Meta:
model = Sdg
fields = (
"uuid",
"name",
"description",
"image",
"projects",
)
read_only_fields = (
"uuid",
Expand Down
67 changes: 67 additions & 0 deletions app/core/migrations/0028_projectsdgxref_project_sdgs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Generated by Django 4.2.11 on 2024-09-28 05:30

from django.db import migrations, models
import django.db.models.deletion
import uuid


class Migration(migrations.Migration):

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

operations = [
migrations.CreateModel(
name="ProjectSdgXref",
fields=[
(
"uuid",
models.UUIDField(
default=uuid.uuid4,
editable=False,
primary_key=True,
serialize=False,
unique=True,
),
),
(
"created_at",
models.DateTimeField(auto_now_add=True, verbose_name="Created at"),
),
(
"updated_at",
models.DateTimeField(auto_now=True, verbose_name="Updated at"),
),
(
"ended_on",
models.DateField(blank=True, null=True, verbose_name="Ended on"),
),
(
"project_id",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="core.project"
),
),
(
"sdg_id",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="core.sdg"
),
),
],
options={
"abstract": False,
},
),
migrations.AddField(
model_name="project",
name="sdgs",
field=models.ManyToManyField(
blank=True,
related_name="projects",
through="core.ProjectSdgXref",
to="core.sdg",
),
),
]
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_projectsdgxref_project_sdgs
13 changes: 13 additions & 0 deletions app/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ class Project(AbstractBaseModel):
image_logo = models.URLField(blank=True)
image_hero = models.URLField(blank=True)
image_icon = models.URLField(blank=True)
sdgs = models.ManyToManyField(
"Sdg", related_name="projects", blank=True, through="ProjectSdgXref"
)

def __str__(self):
return f"{self.name}"
Expand Down Expand Up @@ -424,3 +427,13 @@ class SocMajor(AbstractBaseModel):

def __str__(self):
return self.title


class ProjectSdgXref(AbstractBaseModel):
"""
Joins an SDG to a project
"""

sdg_id = models.ForeignKey(Sdg, on_delete=models.CASCADE)
project_id = models.ForeignKey(Project, on_delete=models.CASCADE)
ended_on = models.DateField("Ended on", null=True, blank=True)
12 changes: 12 additions & 0 deletions app/core/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
pytestmark = pytest.mark.django_db

USER_PERMISSIONS_URL = reverse("user-permission-list")
PROJECT_URL = reverse("project-list")
ME_URL = reverse("my_profile")
USERS_URL = reverse("user-list")
EVENTS_URL = reverse("event-list")
Expand Down Expand Up @@ -381,3 +382,14 @@ 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_sdg_xref(auth_client, project, sdg):
project.sdgs.add(sdg)
project.save()

proj_res = auth_client.get(PROJECT_URL)
sdg_res = auth_client.get(SDG_URL)

assert filter(lambda proj: str(proj["uuid"]) == str(project.pk), proj_res.data)
assert filter(lambda _sdg: str(_sdg["uuid"]) == str(sdg.pk), sdg_res)
22 changes: 22 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 ProjectSdgXref
from ..models import Sdg

pytestmark = pytest.mark.django_db

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

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


def test_project_sdg_relationship(project):
climate_action_sdg = Sdg.objects.get(name="Climate Action")

project.sdgs.add(climate_action_sdg)
assert project.sdgs.count() == 1
assert project.sdgs.contains(climate_action_sdg)
assert climate_action_sdg.projects.contains(project)

climate_action_sdg_xref = ProjectSdgXref.objects.get(
project_id=project,
sdg_id=climate_action_sdg,
)
assert climate_action_sdg_xref.ended_on is None

project.sdgs.remove(climate_action_sdg)
assert project.sdgs.count() == 0
assert not project.sdgs.contains(climate_action_sdg)
assert not climate_action_sdg.projects.contains(project)
146 changes: 146 additions & 0 deletions docs/how-to/add-model-and-api-endpoints.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ Let's start!
assert str(recurring_event) == payload["name"]
```

For testing many-to-many relationships, we can add

```python title="app/core/tests/test_models.py" linenums="1"
def test_project_recurring_event_relationship(project):
recurring_event = RecurringEvent.objects.get(name="{Name of Recurring Event}")

project.recurring_events.add(recurring_event)
assert project.recurring_events.count() == 1
assert project.recurring_events.contains(recurring_event)
assert recurring_event.projects.contains(project)

project.sdgs.remove(recurring_event)
assert project.recurring_events.count() == 0
assert not project.recurring_events.contains(recurring_event)
assert not recurring_event.projects.contains(project)
```

1. See it fail

```bash
Expand Down Expand Up @@ -99,6 +116,45 @@ class RecurringEvent(AbstractBaseModel): # (1)!
1. Try to add the relationships to non-existent models, but comment them out. Another developer will complete them when they go to implement those models.
1. Always override the `__str__` function to output something more meaningful than the default. It lets us do a quick test of the model by calling `str([model])`. It's also useful for the admin site model list view.

??? note "Updating models.py for many-to-many relationships"
For adding many-to-many relationships with additional fields, such as `ended_on`, we can add

```python title="app/core/tests/test_models.py" linenums="1"
class Project(AbstractBaseModel):
...
recurring_events = models.ManyToManyField(
"RecurringEvent",
related_name="projects",
blank=True,
through="ProjectRecurringEventXref",
)
...


class ProjectRecurringEventXref(AbstractBaseModel):
"""
Joins a recurring event to a project
"""

recurring_event_id = models.ForeignKey(RecurringEvent, on_delete=models.CASCADE)
project_id = models.ForeignKey(Project, on_delete=models.CASCADE)
ended_on = models.DateField("Ended on", null=True, blank=True)
```

For adding many-to-many relationships without additional fields, we can just add

```python title="app/core/tests/test_models.py" linenums="1"
class Project(AbstractBaseModel):
...
recurring_events = models.ManyToManyField(
"RecurringEvent",
related_name="projects",
blank=True,
through="ProjectRecurringEventXref",
)
...
```

### Run migrations

This generates the database migration files
Expand Down Expand Up @@ -247,6 +303,67 @@ This is code that serializes objects into strings for the API endpoints, and des

In `app/core/api/serializers.py`

??? note "Updating serializers.py for many-to-many relationships"
Following the many-to-many relationship between project and recurring event from above,

Update the existing serializer classes

```python title="app/core/api/serializers.py" linenums="1"
class ProjectSerializer(serializers.ModelSerializer):
"""Used to retrieve project info"""

recurring_events = serializers.StringRelatedField(many=True)

class Meta:
model = Project
fields = (
"uuid",
"name",
"description",
"created_at",
"updated_at",
"completed_at",
"github_org_id",
"github_primary_repo_id",
"hide",
"google_drive_id",
"image_logo",
"image_hero",
"image_icon",
"recurring_events",
)
read_only_fields = (
"uuid",
"created_at",
"updated_at",
"completed_at",
)


class RecurringEventSerializer(serializers.ModelSerializer):
"""Used to retrieve recurring_event info"""

projects = serializers.StringRelatedField(many=True)

class Meta:
model = RecurringEvent
fields = (
"uuid",
"name",
"start_time",
"duration_in_min",
"video_conference_url",
"additional_info",
"project",
"projects",
)
read_only_fields = (
"uuid",
"created_at",
"updated_at",
)
```

1. Import the new model

```python title="app/core/api/serializers.py" linenums="1"
Expand Down Expand Up @@ -491,6 +608,35 @@ In `app/core/api/urls.py`
./scripts/test.sh
```

??? note "Test many-to-many relationships"
In `app/core/tests/test_api.py`

1. Import API URL

```python title="app/core/tests/test_api.py" linenums="1"
PROJECT_URL = reverse("project-list")
```

1. Add test case

```python title="app/core/tests/test_api.py" linenums="1"
def test_project_sdg_xref(auth_client, project, sdg):
project.sdgs.add(sdg)
project.save()

proj_res = auth_client.get(PROJECT_URL)
sdg_res = auth_client.get(SDG_URL)

assert filter(lambda proj: str(proj["uuid"]) == str(project.pk), proj_res.data)
assert filter(lambda _sdg: str(_sdg["uuid"]) == str(sdg.pk), sdg_res)
```

1. Run the test script to show it passing

```bash
./scripts/test.sh
```

??? note "Check and commit"
This is a good place to pause, check, and commit progress.

Expand Down