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

Feature/makeoverviewdynamic #1652

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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,19 @@
# Generated by Django 5.1.1 on 2024-12-22 01:25

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("samfundet", "0011_recruitmentposition_file_description_en_and_more"),
]

operations = [
migrations.AddField(
model_name="recruitmentstatistics",
name="total_rejected",
field=models.PositiveIntegerField(
blank=True, null=True, verbose_name="Total rejected applicants"
),
),
]
35 changes: 31 additions & 4 deletions backend/samfundet/models/recruitment.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django.db import models, transaction
from django.utils import timezone
from django.core.exceptions import ValidationError
from django.contrib.auth.models import UserManager

from root.utils.mixins import CustomBaseModel, FullCleanSaveMixin

Expand Down Expand Up @@ -39,11 +40,25 @@ def resolve_org(self, *, return_id: bool = False) -> Organization | int:
return self.organization_id
return self.organization

def get_applicants(self) -> UserManager[User]:
return User.objects.filter(id__in=self.applications.values_list('user__id'))

def get_unprocessed_applications(self) -> models.BaseManager[RecruitmentApplication]:
return self.applications.filter(recruiter_status=RecruitmentStatusChoices.NOT_SET)

def get_processed_applications(self) -> models.BaseManager[RecruitmentApplication]:
return self.applications.exclude(recruiter_status=RecruitmentStatusChoices.NOT_SET)

def get_unprocessed_applicants(self) -> UserManager[User]:
return self.get_applicants().filter(id__in=self.get_unprocessed_applications().values_list('user__id'))

def get_processed_applicants(self) -> UserManager[User]:
return self.get_applicants().exclude(id__in=self.get_unprocessed_applications().values_list('user__id'))

def recruitment_progress(self) -> float:
applications = RecruitmentApplication.objects.filter(recruitment=self)
if applications.count() == 0:
if self.applications.count() == 0:
return 1
return applications.exclude(recruiter_status=RecruitmentStatusChoices.NOT_SET).count() / applications.count()
return self.get_processed_applications().count() / self.applications.count()

def is_active(self) -> bool:
return self.visible_from < timezone.now() < self.actual_application_deadline
Expand Down Expand Up @@ -506,6 +521,9 @@ class RecruitmentStatistics(FullCleanSaveMixin):
# Total accepted applicants
total_accepted = models.PositiveIntegerField(null=True, blank=True, verbose_name='Total accepted applicants')

# Total rejected applicants
total_rejected = models.PositiveIntegerField(null=True, blank=True, verbose_name='Total rejected applicants')

# Average amount of different gangs an applicant applies for
average_gangs_applied_to_per_applicant = models.FloatField(null=True, blank=True, verbose_name='Gang diversity')

Expand All @@ -514,11 +532,20 @@ class RecruitmentStatistics(FullCleanSaveMixin):

def save(self, *args: tuple, **kwargs: dict) -> None:
self.total_applications = self.recruitment.applications.count()
self.total_applicants = self.recruitment.applications.values('user').distinct().count()
self.total_applicants = self.recruitment.get_applicants().count()
self.total_withdrawn = self.recruitment.applications.filter(withdrawn=True).count()
self.total_accepted = (
self.recruitment.applications.filter(recruiter_status=RecruitmentStatusChoices.CALLED_AND_ACCEPTED).values('user').distinct().count()
)
self.total_rejected = (
self.recruitment.get_applicants()
.exclude(
id__in=self.recruitment.applications.filter(
recruiter_status__in=[RecruitmentStatusChoices.CALLED_AND_ACCEPTED, RecruitmentStatusChoices.NOT_SET]
).values_list('user__id')
)
.count()
)
if self.total_applicants > 0:
self.average_gangs_applied_to_per_applicant = (
self.recruitment.applications.values('user', 'recruitment_position__gang').distinct().count() / self.total_applicants
Expand Down
26 changes: 22 additions & 4 deletions backend/samfundet/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,15 +705,12 @@ def get_applicant_percentage(self, stat: RecruitmentCampusStat) -> float:


class RecruitmentGangStatSerializer(serializers.ModelSerializer):
gang = serializers.SerializerMethodField(method_name='gang_name', read_only=True)
gang = GangSerializer(read_only=True)

class Meta:
model = RecruitmentGangStat
exclude = ['id', 'recruitment_stats']

def gang_name(self, stat: RecruitmentGangStat) -> str:
return stat.gang.name_nb


class RecruitmentStatisticsSerializer(serializers.ModelSerializer):
time_stats = RecruitmentTimeStatSerializer(read_only=True, many=True)
Expand Down Expand Up @@ -837,6 +834,12 @@ def to_representation(self, instance: Recruitment) -> dict:
class RecruitmentForRecruiterSerializer(CustomBaseSerializer):
separate_positions = RecruitmentSeparatePositionSerializer(many=True, read_only=True)
recruitment_progress = serializers.SerializerMethodField(method_name='get_recruitment_progress', read_only=True)
total_applicants = serializers.SerializerMethodField(method_name='get_total_applicants', read_only=True)
total_processed_applicants = serializers.SerializerMethodField(method_name='get_total_processed_applicants', read_only=True)
total_unprocessed_applicants = serializers.SerializerMethodField(method_name='get_total_unprocessed_applicants', read_only=True)
total_processed_applications = serializers.SerializerMethodField(method_name='get_total_processed_applications', read_only=True)
total_unprocessed_applications = serializers.SerializerMethodField(method_name='get_total_unprocessed_applications', read_only=True)

statistics = RecruitmentStatisticsSerializer(read_only=True)

class Meta:
Expand All @@ -846,6 +849,21 @@ class Meta:
def get_recruitment_progress(self, instance: Recruitment) -> float:
return instance.recruitment_progress()

def get_total_applicants(self, instance: Recruitment) -> int:
return instance.get_applicants().count()

def get_total_processed_applicants(self, instance: Recruitment) -> int:
return instance.get_processed_applicants().count()

def get_total_unprocessed_applicants(self, instance: Recruitment) -> int:
return instance.get_unprocessed_applicants().count()

def get_total_processed_applications(self, instance: Recruitment) -> int:
return instance.get_processed_applications().count()

def get_total_unprocessed_applications(self, instance: Recruitment) -> int:
return instance.get_unprocessed_applications().count()


class RecruitmentPositionSerializer(CustomBaseSerializer):
total_applicants = serializers.SerializerMethodField(method_name='get_total_applicants', read_only=True)
Expand Down
7 changes: 7 additions & 0 deletions backend/samfundet/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,13 @@ class RecruitmentForRecruiterView(ModelViewSet):
serializer_class = RecruitmentForRecruiterSerializer
queryset = Recruitment.objects.all()

def retrieve(self, request: Request, pk: int) -> Response:
recruitment = get_object_or_404(self.queryset, pk=pk)
recruitment.statistics.save()
stats = get_object_or_404(self.queryset, pk=pk)
serializer = self.serializer_class(stats)
return Response(serializer.data)


@method_decorator(ensure_csrf_cookie, 'dispatch')
class RecruitmentStatisticsView(ModelViewSet):
Expand Down
9 changes: 8 additions & 1 deletion frontend/src/Components/TabView/TabView.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type ReactElement, type ReactNode, useState } from 'react';
import { type ReactElement, type ReactNode, useEffect, useState } from 'react';
import { type Tab, TabBar } from '../TabBar/TabBar';

export type TabViewProps = {
Expand All @@ -8,6 +8,13 @@ export type TabViewProps = {

export function TabView({ tabs, className }: TabViewProps) {
const [currentTab, setCurrentTab] = useState<Tab<ReactElement | ReactNode>>(tabs[0] ?? undefined);

useEffect(() => {
if (currentTab) {
setCurrentTab(tabs.filter((tab) => tab.key === currentTab.key)[0]);
}
}, [tabs, currentTab]);

return (
<div className={className}>
<TabBar tabs={tabs} selected={currentTab} onSetTab={setCurrentTab} />
Expand Down
Loading
Loading