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

Add semester_id query to bdb and make student_contact unique for all semesters #3645

Merged
merged 10 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Generated by Django 4.0.10 on 2024-09-18 18:58

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('companies', '0028_alter_companyinterest_other_offers'),
]

operations = [
migrations.RemoveField(
model_name='company',
name='previous_contacts',
),
migrations.RemoveField(
model_name='company',
name='student_contact',
),
migrations.CreateModel(
name='StudentCompanyContact',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_at', models.DateTimeField(db_index=True, default=django.utils.timezone.now, editable=False)),
('updated_at', models.DateTimeField(default=django.utils.timezone.now, editable=False)),
('deleted', models.BooleanField(db_index=True, default=False, editable=False)),
('company', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='student_contacts', to='companies.company')),
('created_by', models.ForeignKey(default=None, editable=False, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='%(class)s_created', to=settings.AUTH_USER_MODEL)),
('semester', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='student_contacts', to='companies.semester')),
('student', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='contact_for_companies', to=settings.AUTH_USER_MODEL)),
('updated_by', models.ForeignKey(default=None, editable=False, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='%(class)s_updated', to=settings.AUTH_USER_MODEL)),
],
options={
'abstract': False,
'default_manager_name': 'objects',
},
),
]
16 changes: 11 additions & 5 deletions lego/apps/companies/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,8 @@ class Meta:
unique_together = ("year", "semester")
permission_handler = SemesterPermissionHandler()


class Company(BasisModel):
name = models.CharField(max_length=100)
student_contact = models.ForeignKey(
User, related_name="companies", null=True, on_delete=models.SET_NULL
)
previous_contacts = models.ManyToManyField(User)

description = models.TextField(blank=True)
phone = models.CharField(max_length=100, blank=True)
Expand All @@ -71,6 +66,17 @@ def content_target(self) -> str:
def __str__(self) -> str:
return self.name

class StudentCompanyContact(BasisModel):
company = models.ForeignKey(
Company, related_name="student_contacts", on_delete=models.CASCADE
)
student = models.ForeignKey(
User, related_name="contact_for_companies", on_delete=models.CASCADE
)
semester = models.ForeignKey(
Semester, related_name="student_contacts", on_delete=models.CASCADE
)


class CompanyFile(models.Model):
company = models.ForeignKey(Company, related_name="files", on_delete=models.CASCADE)
Expand Down
40 changes: 33 additions & 7 deletions lego/apps/companies/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
CompanyInterest,
Semester,
SemesterStatus,
StudentCompanyContact
)
from lego.apps.files.fields import FileField, ImageField
from lego.apps.users.fields import PublicUserField
from lego.apps.users.serializers.users import PublicUserSerializer
from lego.apps.users.models import User
from lego.utils.serializers import BasisModelSerializer

Expand All @@ -36,6 +38,11 @@ def create(self, validated_data):
return super().create(validated_data)


class StudentCompanyContactSerializer(BasisModelSerializer):
class Meta:
model = StudentCompanyContact
fields = ("id", "company_id", "student_id", "semester_id")
Comment on lines +38 to +41
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the semester_id supposed to be the year? (In the example it's "1"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the ID of the Semester object


class SemesterStatusDetailSerializer(SemesterStatusSerializer):
contract = FileField(required=False, allow_null=True)
statistics = FileField(required=False, allow_null=True)
Expand Down Expand Up @@ -128,20 +135,41 @@ class Meta:


class CompanyAdminListSerializer(BasisModelSerializer):
semester_statuses = SemesterStatusSerializer(many=True, read_only=True)
student_contact = PublicUserField(required=False, queryset=User.objects.all())
semester_statuses = serializers.SerializerMethodField()
student_contacts = serializers.SerializerMethodField()

class Meta:
model = Company
fields = (
"id",
"name",
"semester_statuses",
"student_contact",
"student_contacts",
"admin_comment",
"active",
)

def get_student_contacts(self, obj):
semester_id = self.context.get('semester_id')

if semester_id == None:
queryset = StudentCompanyContact.objects.filter(company=obj)
else:
queryset = StudentCompanyContact.objects.filter(company=obj, semester_id=semester_id)

return StudentCompanyContactSerializer(queryset, many=True).data

def get_semester_statuses(self, obj):
semester_id = self.context.get('semester_id')

if semester_id == None:
queryset = SemesterStatus.objects.filter(company=obj)
else:
queryset = SemesterStatus.objects.filter(company=obj, semester_id=semester_id)

return SemesterStatusSerializer(queryset, many=True).data



class CompanyDetailSerializer(BasisModelSerializer):
logo = ImageField(required=False, options={"height": 500})
Expand Down Expand Up @@ -178,9 +206,7 @@ class CompanyAdminDetailSerializer(BasisModelSerializer):
comments = CommentSerializer(read_only=True, many=True)
content_target = CharField(read_only=True)

student_contact = PublicUserField(
required=False, allow_null=True, queryset=User.objects.all()
)
student_contacts = StudentCompanyContactSerializer(many=True)
semester_statuses = SemesterStatusDetailSerializer(many=True, read_only=True)
company_contacts = CompanyContactSerializer(many=True, read_only=True)

Expand All @@ -192,7 +218,7 @@ class Meta:
fields = (
"id",
"name",
"student_contact",
"student_contacts",
"description",
"phone",
"company_type",
Expand Down
8 changes: 7 additions & 1 deletion lego/apps/companies/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,17 @@ class AdminCompanyViewSet(AllowedPermissionsMixin, viewsets.ModelViewSet):
queryset = (
Company.objects.all()
.prefetch_related("semester_statuses", "files")
.select_related("student_contact")
)
pagination_class = None
permission_handler = CompanyAdminPermissionHandler()

def _list(self, request, *args, **kwargs):
semester_id = request.query_params.get('semester_id', None)

serializer = self.get_serializer(self.queryset, many=True, context={'semester_id': semester_id})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there some way to just update the context (so we don't lose request and the other couple things passed down by default)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! I'll look into it


return Response(serializer.data)

def get_serializer_class(self):
if self.action == "list":
return CompanyAdminListSerializer
Expand Down