Skip to content

Commit

Permalink
Merge pull request #408 from PROCOLLAB-github/feature/flag_for_programs
Browse files Browse the repository at this point in the history
PRO-383: Flag and filter for projects
  • Loading branch information
sh1nkey authored Jul 31, 2024
2 parents eee9d10 + 9f30691 commit d628308
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 4.2.11 on 2024-07-31 14:09

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("partner_programs", "0005_partnerprogram_draft"),
]

operations = [
migrations.AddField(
model_name="partnerprogram",
name="projects_availability",
field=models.CharField(
choices=[
("all_users", "Всем пользователям"),
("experts_only", "Только экспертам"),
],
default="all_users",
max_length=25,
verbose_name="Доступность к дочерним проектам",
),
),
]
10 changes: 10 additions & 0 deletions partner_programs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class PartnerProgram(models.Model):
datetime_created: A DateTimeField indicating date of creation.
datetime_updated: A DateTimeField indicating date of update.
"""
PROJECTS_AVAILABILITY_CHOISES = [
("all_users", "Всем пользователям"),
("experts_only", "Только экспертам"),
]

name = models.TextField(
verbose_name="Название",
Expand Down Expand Up @@ -74,6 +78,12 @@ class PartnerProgram(models.Model):
through="PartnerProgramUserProfile",
)
draft = models.BooleanField(blank=False, default=True)
projects_availability = models.CharField(
choices=PROJECTS_AVAILABILITY_CHOISES,
max_length=25,
default="all_users",
verbose_name="Доступность к дочерним проектам",
)
datetime_registration_ends = models.DateTimeField(
verbose_name="Дата окончания регистрации",
)
Expand Down
25 changes: 15 additions & 10 deletions projects/filters.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.db.models import Count
from django_filters import rest_framework as filters

from users.models import Expert
from partner_programs.models import PartnerProgram, PartnerProgramUserProfile
from projects.models import Project

Expand Down Expand Up @@ -51,20 +52,24 @@ def vacancy_filter(cls, queryset, name, value):
).distinct()
return queryset

@classmethod
def filter_by_partner_program(cls, queryset, name, value):
def filter_by_partner_program(self, queryset, name, value):
program_id = value
user = self.request.user
try:
program = PartnerProgram.objects.get(pk=program_id)
profiles_qs = (
PartnerProgramUserProfile.objects.filter(
partner_program=program, project__isnull=False
program_status = program.projects_availability
# If available to all users or request.user is an expert of this program.
if program_status == "all_users" or Expert.objects.filter(user=user, programs=program).exists():
profiles_qs = (
PartnerProgramUserProfile.objects.filter(
partner_program=program, project__isnull=False
)
.select_related("project")
.only("project")
)
.select_related("project")
.only("project")
)

return queryset.filter(pk__in=[profile.project.pk for profile in profiles_qs])
return queryset.filter(pk__in=[profile.project.pk for profile in profiles_qs])
else:
return Project.objects.none()

except PartnerProgram.DoesNotExist:
return Project.objects.none()
Expand Down

0 comments on commit d628308

Please sign in to comment.