Skip to content

Commit

Permalink
Edit student contacts (#3689)
Browse files Browse the repository at this point in the history
  • Loading branch information
falbru authored Jan 1, 2025
1 parent 2d308a0 commit 41d8412
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lego/apps/companies/fixtures/test_companies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@
pk: 1
fields:
company: 1
student: 1
user: 1
semester: 1

- model: companies.StudentCompanyContact
pk: 2
fields:
company: 1
student: 2
user: 2
semester: 2


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.16 on 2024-12-28 23:04

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("companies", "0030_alter_company_options"),
]

operations = [
migrations.RenameField(
model_name="studentcompanycontact",
old_name="student",
new_name="user",
),
]
2 changes: 1 addition & 1 deletion lego/apps/companies/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class StudentCompanyContact(BasisModel):
company = models.ForeignKey(
Company, related_name="student_contacts", on_delete=models.CASCADE
)
student = models.ForeignKey(
user = models.ForeignKey(
User, related_name="contact_for_companies", on_delete=models.CASCADE
)
semester = models.ForeignKey(
Expand Down
39 changes: 37 additions & 2 deletions lego/apps/companies/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from lego.apps.comments.serializers import CommentSerializer
from lego.apps.companies.constants import INTERESTED, NOT_CONTACTED
from lego.apps.companies.fields import SemesterField
from lego.apps.companies.models import (
Company,
CompanyContact,
Expand All @@ -15,6 +16,8 @@
StudentCompanyContact,
)
from lego.apps.files.fields import FileField, ImageField
from lego.apps.users.fields import PublicUserField
from lego.apps.users.models import User
from lego.utils.serializers import BasisModelSerializer


Expand All @@ -36,9 +39,12 @@ def create(self, validated_data):


class StudentCompanyContactSerializer(BasisModelSerializer):
user = PublicUserField(queryset=User.objects.all())
semester = SemesterField(queryset=Semester.objects.all())

class Meta:
model = StudentCompanyContact
fields = ("id", "company_id", "student_id", "semester_id")
fields = ("id", "company", "user", "semester")


class SemesterStatusDetailSerializer(SemesterStatusSerializer):
Expand Down Expand Up @@ -207,7 +213,7 @@ class CompanyAdminDetailSerializer(BasisModelSerializer):
comments = CommentSerializer(read_only=True, many=True)
content_target = CharField(read_only=True)

student_contacts = StudentCompanyContactSerializer(many=True, read_only=True)
student_contacts = StudentCompanyContactSerializer(many=True, required=False)
semester_statuses = SemesterStatusDetailSerializer(many=True, read_only=True)
company_contacts = CompanyContactSerializer(many=True, read_only=True)

Expand Down Expand Up @@ -236,6 +242,35 @@ class Meta:
"company_contacts",
)

def update(self, instance, validated_data):
updated_student_contacts = validated_data.pop("student_contacts", [])

previous_student_contacts = list(instance.student_contacts.all())
previous_ids = {contact.id for contact in previous_student_contacts}

existing_contacts_ids = set()

for student_contact in updated_student_contacts:
try:
existing_contact = StudentCompanyContact.objects.get(
company=student_contact.get("company"),
semester=student_contact.get("semester"),
user=student_contact.get("user"),
)
existing_contacts_ids.add(existing_contact.id)
except StudentCompanyContact.DoesNotExist:
StudentCompanyContact(
company=student_contact.get("company"),
semester=student_contact.get("semester"),
user=student_contact.get("user"),
).save()

delete_contacts_ids = previous_ids.difference(existing_contacts_ids)
for contact_id in delete_contacts_ids:
StudentCompanyContact.objects.get(id=contact_id).delete()

return super().update(instance, validated_data)


class CompanyInterestCreateAndUpdateSerializer(serializers.ModelSerializer):
class Meta:
Expand Down
73 changes: 73 additions & 0 deletions lego/apps/companies/tests/test_companies_admin_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.urls import reverse
from rest_framework import status

from lego.apps.companies.models import StudentCompanyContact
from lego.apps.users.models import AbakusGroup, User
from lego.utils.test_utils import BaseAPITestCase

Expand All @@ -21,6 +22,35 @@
]


def _get_test_student_contacts_data(pk):
return [
{"studentContacts": []},
{
"studentContacts": [
{
"company": pk,
"user": 1,
"semester": 1,
}
]
},
{
"studentContacts": [
{
"company": pk,
"user": 1,
"semester": 1,
},
{
"company": pk,
"user": 2,
"semester": 1,
},
]
},
]


def _get_bdb_list_url():
return reverse("api:v1:bdb-list")

Expand Down Expand Up @@ -159,6 +189,49 @@ def test_company_delete_with_bedkom_user(self):
self.assertEqual(company_response.status_code, status.HTTP_404_NOT_FOUND)


class UpdateStudentCompanyContactTestCase(BaseAPITestCase):
fixtures = ["test_abakus_groups.yaml", "test_companies.yaml", "test_users.yaml"]

def getStudentCompanyContactCount(self, pk):
return StudentCompanyContact.objects.filter(company=2).count()

def updateCompany(self, pk, body):
AbakusGroup.objects.get(name="Bedkom").add_user(self.abakus_user)
self.client.force_authenticate(self.abakus_user)
company_response = self.client.patch(_get_bdb_detail_url(pk), body)
self.assertEqual(company_response.status_code, status.HTTP_200_OK)

def setUp(self):
self.abakus_user = User.objects.all().first()

def test_add_student_company_contact(self):
self.assertEqual(self.getStudentCompanyContactCount(2), 0)

self.updateCompany(2, _get_test_student_contacts_data(2)[1])
self.assertEqual(self.getStudentCompanyContactCount(2), 1)

def test_add_multiple_student_company_contacts(self):
self.assertEqual(self.getStudentCompanyContactCount(2), 0)

self.updateCompany(2, _get_test_student_contacts_data(2)[1])
self.assertEqual(self.getStudentCompanyContactCount(2), 1)

self.updateCompany(2, _get_test_student_contacts_data(2)[2])
self.assertEqual(self.getStudentCompanyContactCount(2), 2)

def test_delete_student_company_contacts(self):
self.assertEqual(self.getStudentCompanyContactCount(2), 0)

self.updateCompany(2, _get_test_student_contacts_data(2)[2])
self.assertEqual(self.getStudentCompanyContactCount(2), 2)

self.updateCompany(2, _get_test_student_contacts_data(2)[1])
self.assertEqual(self.getStudentCompanyContactCount(2), 1)

self.updateCompany(2, _get_test_student_contacts_data(2)[0])
self.assertEqual(self.getStudentCompanyContactCount(2), 0)


class CreateSemesterStatusTestCase(BaseAPITestCase):
fixtures = ["test_abakus_groups.yaml", "test_companies.yaml", "test_users.yaml"]

Expand Down

0 comments on commit 41d8412

Please sign in to comment.