Skip to content

Commit

Permalink
Created model SDG (#249)
Browse files Browse the repository at this point in the history
* feat: add model: Sdg
* feat: register admin: sdg
* feat: add endpoints: sdg
  • Loading branch information
freaky4wrld authored Feb 9, 2024
1 parent e600e62 commit 6c0bd26
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 2 deletions.
6 changes: 6 additions & 0 deletions app/core/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from .models import PracticeArea
from .models import ProgramArea
from .models import Project
from .models import Sdg
from .models import Skill
from .models import SponsorPartner
from .models import StackElementType
Expand Down Expand Up @@ -213,3 +214,8 @@ class StackElementType(admin.ModelAdmin):
"name",
"description",
)


@admin.register(Sdg)
class SdgAdmin(admin.ModelAdmin):
list_display = ("name", "description", "image")
21 changes: 21 additions & 0 deletions app/core/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from core.models import PracticeArea
from core.models import ProgramArea
from core.models import Project
from core.models import Sdg
from core.models import Skill
from core.models import SponsorPartner
from core.models import StackElementType
Expand Down Expand Up @@ -275,3 +276,23 @@ class Meta:
"created_at",
"updated_at",
)


class SdgSerializer(serializers.ModelSerializer):
"""
Used to retrieve Sdg
"""

class Meta:
model = Sdg
fields = (
"uuid",
"name",
"description",
"image",
)
read_only_fields = (
"uuid",
"created_at",
"updated_at",
)
3 changes: 2 additions & 1 deletion app/core/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .views import PracticeAreaViewSet
from .views import ProgramAreaViewSet
from .views import ProjectViewSet
from .views import SdgViewSet
from .views import SkillViewSet
from .views import SponsorPartnerViewSet
from .views import StackElementTypeViewSet
Expand All @@ -32,7 +33,7 @@
router.register(
r"stack-element-types", StackElementTypeViewSet, basename="stack-element-type"
)

router.register(r"sdgs", SdgViewSet, basename="sdg")
urlpatterns = [
path("me/", UserProfileAPIView.as_view(), name="my_profile"),
]
Expand Down
16 changes: 16 additions & 0 deletions app/core/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from ..models import PracticeArea
from ..models import ProgramArea
from ..models import Project
from ..models import Sdg
from ..models import Skill
from ..models import SponsorPartner
from ..models import StackElementType
Expand All @@ -31,6 +32,7 @@
from .serializers import PracticeAreaSerializer
from .serializers import ProgramAreaSerializer
from .serializers import ProjectSerializer
from .serializers import SdgSerializer
from .serializers import SkillSerializer
from .serializers import SponsorPartnerSerializer
from .serializers import StackElementTypeSerializer
Expand Down Expand Up @@ -298,3 +300,17 @@ class StackElementTypeViewSet(viewsets.ModelViewSet):
permission_classes = [IsAuthenticated]
queryset = StackElementType.objects.all()
serializer_class = StackElementTypeSerializer


@extend_schema_view(
list=extend_schema(description="Return a list of all the recurring events"),
create=extend_schema(description="Create a new recurring event"),
retrieve=extend_schema(description="Return the details of a recurring event"),
destroy=extend_schema(description="Delete a recurring event"),
update=extend_schema(description="Update a recurring event"),
partial_update=extend_schema(description="Patch a recurring event"),
)
class SdgViewSet(viewsets.ModelViewSet):
permission_classes = [IsAuthenticated]
queryset = Sdg.objects.all()
serializer_class = SdgSerializer
28 changes: 28 additions & 0 deletions app/core/migrations/0021_sdg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 4.0.10 on 2024-01-26 05:45

from django.db import migrations, models
import uuid


class Migration(migrations.Migration):

dependencies = [
('core', '0020_rename_is_sponsor_sponsorpartner_is_org_partner_and_more'),
]

operations = [
migrations.CreateModel(
name='Sdg',
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')),
('name', models.CharField(max_length=255)),
('description', models.TextField(blank=True)),
('image', models.URLField(blank=True)),
],
options={
'abstract': False,
},
),
]
2 changes: 1 addition & 1 deletion app/core/migrations/max_migration.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0020_rename_is_sponsor_sponsorpartner_is_org_partner_and_more
0021_sdg
15 changes: 15 additions & 0 deletions app/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,18 @@ class StackElementType(AbstractBaseModel):

def __str__(self):
return f"{self.name}"


class Sdg(AbstractBaseModel):
"""
Dictionary of SDGs
"""

name = models.CharField(max_length=255)
description = models.TextField(blank=True)
image = models.URLField(blank=True)

# PK of this model is the ForeignKey for sdg_target_indicator

def __str__(self):
return f"{self.name}"
6 changes: 6 additions & 0 deletions app/core/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from ..models import PracticeArea
from ..models import ProgramArea
from ..models import Project
from ..models import Sdg
from ..models import Skill
from ..models import SponsorPartner
from ..models import StackElementType
Expand Down Expand Up @@ -133,3 +134,8 @@ def permission_type2():
@pytest.fixture
def stack_element_type():
return StackElementType.objects.create(name="Test Stack Element Type")


@pytest.fixture
def sdg():
return Sdg.objects.create(name="Test SDG name")
12 changes: 12 additions & 0 deletions app/core/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
TECHNOLOGY_URL = reverse("technology-list")
PERMISSION_TYPE = reverse("permission-type-list")
STACK_ELEMENT_TYPE_URL = reverse("stack-element-type-list")
SDG_URL = reverse("sdg-list")

CREATE_USER_PAYLOAD = {
"username": "TestUserAPI",
Expand Down Expand Up @@ -302,3 +303,14 @@ def test_create_stack_element_type(auth_client):
res = auth_client.post(STACK_ELEMENT_TYPE_URL, payload)
assert res.status_code == status.HTTP_201_CREATED
assert res.data["name"] == payload["name"]


def test_create_sdg(auth_client):
payload = {
"name": "Test SDG name",
"description": "Test SDG description",
"image": "https://unsplash.com",
}
res = auth_client.post(SDG_URL, payload)
assert res.status_code == status.HTTP_201_CREATED
assert res.data["name"] == payload["name"]
4 changes: 4 additions & 0 deletions app/core/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,7 @@ def test_permission_type2(permission_type2):

def test_stack_element_type(stack_element_type):
assert str(stack_element_type) == "Test Stack Element Type"


def test_sdg(sdg):
assert str(sdg) == "Test SDG name"

0 comments on commit 6c0bd26

Please sign in to comment.