Skip to content

Commit

Permalink
IDPF-305: People finder Country endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
oyeniyipa authored Feb 25, 2025
1 parent a75d21d commit faa7c63
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 8 deletions.
23 changes: 21 additions & 2 deletions core/api/people_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

from core import services as core_services
from core.schemas import Error
from core.schemas.peoplefinder import MinimalPeopleFinderProfile
from core.schemas.peoplefinder import CountrySchema, MinimalPeopleFinderProfile
from core.schemas.profiles import (
PeopleFinderProfileRequestSchema,
PeopleFinderProfileResponseSchema,
UkStaffLocationSchema,
)
from profiles.models.combined import Profile
from profiles.models.generic import UkStaffLocation
from profiles.models.generic import Country, UkStaffLocation
from profiles.models.peoplefinder import PeopleFinderProfile


Expand Down Expand Up @@ -93,6 +93,25 @@ def update_profile(
return 404, {"message": "People finder profile does not exist"}


@reference_router.get(
"countries/",
response={
200: list[CountrySchema],
404: Error,
},
)
def get_countries(request) -> tuple[int, list[Country] | dict]:
try:
# Get a list of all countries
countries = core_services.get_countries()
if len(countries) > 0:
return 200, countries
else:
return 404, {"message": "No Countries to display"}
except Exception as unknown_error:
return 404, {"message": f"Could not get Countries, reason: {unknown_error}"}


@reference_router.get(
"uk_staff_locations/",
response={
Expand Down
19 changes: 18 additions & 1 deletion core/schemas/peoplefinder.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from dataclasses import dataclass
from typing import Optional

from ninja import Schema
from ninja import ModelSchema, Schema

from profiles.models import PeopleFinderProfile
from profiles.models.generic import Country


@dataclass
Expand Down Expand Up @@ -47,3 +48,19 @@ def resolve_email(obj: PeopleFinderProfile):
@staticmethod
def resolve_contact_email(obj: PeopleFinderProfile):
return obj.contact_email.address


class CountrySchema(ModelSchema):
class Meta:
model = Country
fields = [
"reference_id",
"name",
"type",
"iso_1_code",
"iso_2_code",
"iso_3_code",
"overseas_region",
"start_date",
"end_date",
]
11 changes: 9 additions & 2 deletions core/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
from datetime import datetime
from typing import Optional

from conftest import peoplefinder_profile
from profiles import services as profile_services
from profiles.models import PeopleFinderProfile
from profiles.models.combined import Profile
from profiles.models.generic import UkStaffLocation
from profiles.models.generic import Country, UkStaffLocation
from profiles.models.peoplefinder import PeopleFinderProfile
from profiles.types import UNSET, Unset # noqa
from user import services as user_services
Expand Down Expand Up @@ -285,5 +285,12 @@ def get_peoplefinder_profile_by_slug(slug: str) -> PeopleFinderProfile:
return profile_services.get_peoplefinder_profile_by_slug(slug=slug)


def get_countries() -> list[Country]:
"""
Function for getting a list of all countries
"""
return profile_services.get_countries()


def get_uk_staff_locations() -> list[UkStaffLocation]:
return profile_services.get_uk_staff_locations()
11 changes: 8 additions & 3 deletions profiles/services/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
# This is the entrypoint service that coordinates between the sub-services
# If in doubt about what to use, you should probably be using this
import logging
import uuid
from datetime import datetime
from typing import Optional

from django.contrib.admin.models import DELETION, LogEntry
from django.db import models

from profiles.exceptions import NonCombinedProfileExists
from profiles.models.combined import Profile
from profiles.models.generic import UkStaffLocation
from profiles.models.generic import Country, UkStaffLocation
from profiles.models.peoplefinder import PeopleFinderProfile
from profiles.models.staff_sso import StaffSSOProfile
from profiles.services import combined, peoplefinder, staff_sso
Expand Down Expand Up @@ -402,5 +400,12 @@ def get_peoplefinder_profile_by_slug(slug: str) -> PeopleFinderProfile:
return peoplefinder.get_by_slug(slug=slug)


def get_countries() -> list[Country]:
"""
Gets all countries service
"""
return peoplefinder.get_countries()


def get_uk_staff_locations() -> list[UkStaffLocation]:
return peoplefinder.get_uk_staff_locations()
7 changes: 7 additions & 0 deletions profiles/services/peoplefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,5 +423,12 @@ def delete_from_database(
peoplefinder_profile.delete()


def get_countries() -> list[Country]:
"""
Gets all countries service
"""
return list(Country.objects.all())


def get_uk_staff_locations() -> list[UkStaffLocation]:
return list(UkStaffLocation.objects.all())
17 changes: 17 additions & 0 deletions profiles/tests/test_services_peoplefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,23 @@ def test_get_by_id(peoplefinder_profile):
assert str(ex.value.args[0]) == "PeopleFinderProfile matching query does not exist."


def test_get_countries():
countries = peoplefinder_services.get_countries()
# Check if country properties exist in fetched country
expected = {
"reference_id": "CTHMTC00260",
"name": "UK",
"type": "country",
"iso_1_code": "31",
"iso_2_code": "66",
"iso_3_code": "2",
"overseas_region": None,
"start_date": None,
"end_date": None,
}
assert expected.items() <= countries[0].__dict__.items()


def test_get_uk_staff_locations():
locations = peoplefinder_services.get_uk_staff_locations()
# Check if code, name, organisation and building_name exists in the location dict
Expand Down

0 comments on commit faa7c63

Please sign in to comment.