Skip to content

Commit

Permalink
feat: rename affiliate and add affiliation model admin endpoints
Browse files Browse the repository at this point in the history
- renamed sponsor_partner table and model to affiliate
- added model: affiliation
- registered admin: affiliation
- added endpoints: affiliation
  • Loading branch information
freaky4wrld authored and fyliu committed May 17, 2024
1 parent 380c409 commit 56e8462
Show file tree
Hide file tree
Showing 10 changed files with 268 additions and 31 deletions.
20 changes: 17 additions & 3 deletions app/core/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from django.contrib.auth.forms import UsernameField
from django.utils.translation import gettext_lazy as _

from .models import Affiliate
from .models import Affiliation
from .models import Event
from .models import Faq
from .models import FaqViewed
Expand All @@ -15,7 +17,6 @@
from .models import Project
from .models import Sdg
from .models import Skill
from .models import SponsorPartner
from .models import StackElementType
from .models import Technology
from .models import User
Expand Down Expand Up @@ -138,8 +139,8 @@ class PracticeAreaAdmin(admin.ModelAdmin):
)


@admin.register(SponsorPartner)
class SponsorPartnerAdmin(admin.ModelAdmin):
@admin.register(Affiliate)
class AffiliateAdmin(admin.ModelAdmin):
list_display = (
"partner_name",
"partner_logo",
Expand Down Expand Up @@ -219,3 +220,16 @@ class StackElementType(admin.ModelAdmin):
@admin.register(Sdg)
class SdgAdmin(admin.ModelAdmin):
list_display = ("name", "description", "image")


@admin.register(Affiliation)
class AffiliationAdmin(admin.ModelAdmin):
list_display = (
"affiliate",
"project",
"created_at",
"ended_at",
"is_sponsor",
"is_partner",
)
list_filter = ("is_sponsor", "is_partner", "affiliate", "project")
26 changes: 23 additions & 3 deletions app/core/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from rest_framework import serializers
from timezone_field.rest_framework import TimeZoneSerializerField

from core.models import Affiliate
from core.models import Affiliation
from core.models import Event
from core.models import Faq
from core.models import FaqViewed
Expand All @@ -11,7 +13,6 @@
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
from core.models import Technology
from core.models import User
Expand Down Expand Up @@ -122,11 +123,11 @@ class Meta:
)


class SponsorPartnerSerializer(serializers.ModelSerializer):
class AffiliateSerializer(serializers.ModelSerializer):
"""Used to retrieve Sponsor Partner info"""

class Meta:
model = SponsorPartner
model = Affiliate
fields = (
"uuid",
"partner_name",
Expand Down Expand Up @@ -296,3 +297,22 @@ class Meta:
"created_at",
"updated_at",
)


class AffiliationSerializer(serializers.ModelSerializer):
"""
Used to retrieve Affiliation
"""

class Meta:
model = Affiliation
fields = (
"uuid",
"affiliate",
"project",
"created_at",
"ended_at",
"is_sponsor",
"is_partner",
)
read_only_fields = ("uuid", "created_at", "updated_at")
10 changes: 8 additions & 2 deletions app/core/api/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from django.urls import path
from rest_framework import routers

from .views import AffiliateViewSet
from .views import AffiliationViewSet
from .views import EventViewSet
from .views import FaqViewedViewSet
from .views import FaqViewSet
Expand All @@ -11,7 +13,6 @@
from .views import ProjectViewSet
from .views import SdgViewSet
from .views import SkillViewSet
from .views import SponsorPartnerViewSet
from .views import StackElementTypeViewSet
from .views import TechnologyViewSet
from .views import UserProfileAPIView
Expand All @@ -22,7 +23,7 @@
router.register(r"projects", ProjectViewSet, basename="project")
router.register(r"events", EventViewSet, basename="event")
router.register(r"practice-areas", PracticeAreaViewSet, basename="practice-area")
router.register(r"sponsor-partners", SponsorPartnerViewSet, basename="sponsor-partner")
router.register(r"affiliates", AffiliateViewSet, basename="affiliate")
router.register(r"faqs", FaqViewSet, basename="faq")
router.register(r"faqs-viewed", FaqViewedViewSet, basename="faq-viewed")
router.register(r"locations", LocationViewSet, basename="location")
Expand All @@ -34,6 +35,11 @@
r"stack-element-types", StackElementTypeViewSet, basename="stack-element-type"
)
router.register(r"sdgs", SdgViewSet, basename="sdg")
router.register(
r"affiliations",
AffiliationViewSet,
basename="affiliation",
)
urlpatterns = [
path("me/", UserProfileAPIView.as_view(), name="my_profile"),
]
Expand Down
40 changes: 28 additions & 12 deletions app/core/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from rest_framework.permissions import IsAuthenticated
from rest_framework.permissions import IsAuthenticatedOrReadOnly

from ..models import Affiliate
from ..models import Affiliation
from ..models import Event
from ..models import Faq
from ..models import FaqViewed
Expand All @@ -21,9 +23,10 @@
from ..models import Project
from ..models import Sdg
from ..models import Skill
from ..models import SponsorPartner
from ..models import StackElementType
from ..models import Technology
from .serializers import AffiliateSerializer
from .serializers import AffiliationSerializer
from .serializers import EventSerializer
from .serializers import FaqSerializer
from .serializers import FaqViewedSerializer
Expand All @@ -34,7 +37,6 @@
from .serializers import ProjectSerializer
from .serializers import SdgSerializer
from .serializers import SkillSerializer
from .serializers import SponsorPartnerSerializer
from .serializers import StackElementTypeSerializer
from .serializers import TechnologySerializer
from .serializers import UserSerializer
Expand Down Expand Up @@ -160,17 +162,17 @@ class PracticeAreaViewSet(viewsets.ModelViewSet):


@extend_schema_view(
list=extend_schema(description="Return a list of all the sponsor partners"),
create=extend_schema(description="Create a new sponsor partner"),
retrieve=extend_schema(description="Return the details of a sponsor partner"),
destroy=extend_schema(description="Delete a sponsor partner"),
update=extend_schema(description="Update a sponsor partner"),
partial_update=extend_schema(description="Patch a sponsor partner"),
list=extend_schema(description="Return a list of all the affiliates"),
create=extend_schema(description="Create a new affiliate"),
retrieve=extend_schema(description="Return the details of a affiliate"),
destroy=extend_schema(description="Delete a affiliate"),
update=extend_schema(description="Update a affiliate"),
partial_update=extend_schema(description="Patch a affiliate"),
)
class SponsorPartnerViewSet(viewsets.ModelViewSet):
class AffiliateViewSet(viewsets.ModelViewSet):
permission_classes = [IsAuthenticated]
queryset = SponsorPartner.objects.all()
serializer_class = SponsorPartnerSerializer
queryset = Affiliate.objects.all()
serializer_class = AffiliateSerializer

# The following code can be uncommented and used later, but it's being left out
# for simplicity's sake during initial model creation
Expand All @@ -179,7 +181,7 @@ class SponsorPartnerViewSet(viewsets.ModelViewSet):
# """
# Optionally filter sponsor partners by name, is_active, and/or is_sponsor query parameters in the URL
# """
# queryset = SponsorPartner.objects.all()
# queryset = Affiliate.objects.all()
# partner_name = self.request.query_params.get("partner_name")
# if partner_name is not None:
# queryset = queryset.filter(partner_name=partner_name)
Expand Down Expand Up @@ -314,3 +316,17 @@ class SdgViewSet(viewsets.ModelViewSet):
permission_classes = [IsAuthenticated]
queryset = Sdg.objects.all()
serializer_class = SdgSerializer


@extend_schema_view(
list=extend_schema(description="Return a list of all the affiliations"),
create=extend_schema(description="Create a new affiliation"),
retrieve=extend_schema(description="Return the details of an affiliation"),
destroy=extend_schema(description="Delete an affiliation"),
update=extend_schema(description="Update an affiliation"),
partial_update=extend_schema(description="Patch an affiliation"),
)
class AffiliationViewSet(viewsets.ModelViewSet):
permission_classes = [IsAuthenticated]
queryset = Affiliation.objects.all()
serializer_class = AffiliationSerializer
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Generated by Django 4.2.11 on 2024-05-09 02:32

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


class Migration(migrations.Migration):

dependencies = [
("core", "0021_sdg"),
]

operations = [
migrations.RenameModel(
old_name="SponsorPartner",
new_name="Affiliate",
),
migrations.CreateModel(
name="Affiliation",
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_at",
models.DateTimeField(
blank=True, null=True, verbose_name="Ended at"
),
),
("is_sponsor", models.BooleanField(null=True)),
("is_partner", models.BooleanField(null=True)),
(
"affiliate",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="core.affiliate"
),
),
(
"project",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="core.project"
),
),
],
options={
"db_table": "project_affiliate_xref",
},
),
migrations.AddConstraint(
model_name="affiliation",
constraint=models.UniqueConstraint(
fields=("project", "affiliate"), name="unique_project_affiliate"
),
),
]
2 changes: 1 addition & 1 deletion app/core/migrations/max_migration.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0021_sdg
0022_rename_sponsorpartner_affiliate_affiliation_and_more
37 changes: 35 additions & 2 deletions app/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def __str__(self):
return f"{self.name}"


class SponsorPartner(AbstractBaseModel):
class Affiliate(AbstractBaseModel):
"""
Dictionary of sponsors and partners
"""
Expand All @@ -186,7 +186,7 @@ class SponsorPartner(AbstractBaseModel):
is_org_sponsor = models.BooleanField(null=True)
is_org_partner = models.BooleanField(null=True)

# PK of this model is the ForeignKey for project_partner_xref
# PK of this model is the ForeignKey for project_affiliate_xref

def __str__(self):
return f"{self.partner_name}"
Expand Down Expand Up @@ -333,3 +333,36 @@ class Sdg(AbstractBaseModel):

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


class Affiliation(AbstractBaseModel):
"""
Sponsor/partner relationships stored in this table are project-dependent.
They can be both a sponsor and a partner for the same project,
so if is_sponsor is true, they are a project partner,
if is_sponsor is true, they are a project sponsor.
"""

affiliate = models.ForeignKey(Affiliate, on_delete=models.CASCADE)
project = models.ForeignKey(Project, on_delete=models.CASCADE)
ended_at = models.DateTimeField("Ended at", null=True, blank=True)
is_sponsor = models.BooleanField(null=True)
is_partner = models.BooleanField(null=True)

class Meta:
constraints = [
models.UniqueConstraint(
fields=["project", "affiliate"], name="unique_project_affiliate"
)
]
db_table = "project_affiliate_xref"

def __str__(self):
if self.is_sponsor is True and self.is_partner is True:
return f"Sponsor {self.project} and Partner {self.affiliate}"
elif self.is_sponsor is True and self.is_partner is False:
return f"Sponsor {self.project}"
elif self.is_sponsor is False and self.is_partner is True:
return f"Partner {self.affiliate}"
else:
return "Neither a partner or a sponsor"
Loading

0 comments on commit 56e8462

Please sign in to comment.