Skip to content

Commit

Permalink
Merge pull request #265 from volunteers-for-city-projects/fixbag-insomes
Browse files Browse the repository at this point in the history
fix add safe participants when accept incomes
  • Loading branch information
Olga-koml authored Dec 5, 2023
2 parents 86a85f3 + 0a28806 commit 2ed455c
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 29 deletions.
115 changes: 86 additions & 29 deletions backend/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,43 @@ class Meta:
fields = ('id', 'project', 'photo')


class VolunteerGetSerializer(serializers.ModelSerializer):
"""
Сериализатор для отображения волонтера.
"""

user = UserSerializer(read_only=True)
skills = SkillsSerializer(many=True)

class Meta:
model = Volunteer
fields = (
'id',
'user',
'city',
'telegram',
'skills',
'photo',
'date_of_birth',
'phone',
)


class ProjectParticipantSerializer(serializers.ModelSerializer):
"""
Сериализатор для списка участников.
"""
volunteer = VolunteerGetSerializer()

class Meta:
model = ProjectParticipants
fields = (
# 'project',
'id',
'volunteer',
)


class ProjectGetSerializer(serializers.ModelSerializer):
"""
Сериализатор для чтения данных проекта.
Expand All @@ -187,6 +224,8 @@ class ProjectGetSerializer(serializers.ModelSerializer):
status = serializers.SerializerMethodField()
city = serializers.SlugRelatedField(slug_field='name', read_only=True)
photos = ProjectImageSerializer(many=True, read_only=True)
# participants = ProjectParticipantSerializer(read_only=True, many=True)
participants = serializers.SerializerMethodField()

def get_is_favorited(self, obj):
request = self.context.get('request')
Expand Down Expand Up @@ -221,6 +260,23 @@ def get_status(self, data):
return COMPLETED
return EDITING

def get_participants(self, obj):
participants_data = ProjectParticipantSerializer(
obj.participants.all(), many=True
).data
return [
{
'id': participant.get('id'),
'volunteer_id': participant['volunteer']['id'],
'full_name': (
f'{participant["volunteer"]["user"]["last_name"]} '
f'{participant["volunteer"]["user"]["first_name"]} '
f'{participant["volunteer"]["user"]["second_name"]}'
)
}
for participant in participants_data
]

class Meta:
model = Project
fields = (
Expand Down Expand Up @@ -532,26 +588,26 @@ class Meta:
)


class VolunteerGetSerializer(serializers.ModelSerializer):
"""
Сериализатор для отображения волонтера.
"""
# class VolunteerGetSerializer(serializers.ModelSerializer):
# """
# Сериализатор для отображения волонтера.
# """

user = UserSerializer(read_only=True)
skills = SkillsSerializer(many=True)
# user = UserSerializer(read_only=True)
# skills = SkillsSerializer(many=True)

class Meta:
model = Volunteer
fields = (
'id',
'user',
'city',
'telegram',
'skills',
'photo',
'date_of_birth',
'phone',
)
# class Meta:
# model = Volunteer
# fields = (
# 'id',
# 'user',
# 'city',
# 'telegram',
# 'skills',
# 'photo',
# 'date_of_birth',
# 'phone',
# )


class VolunteerCreateSerializer(
Expand Down Expand Up @@ -782,6 +838,7 @@ def accept_incomes(self, instance):
participiants = ProjectParticipants.objects.create(
project=instance.project, volunteer=instance.volunteer
)
instance.project.participants.add(participiants) # добавила
incomes_approve_send_email.delay(
participiants.pk, get_site_data(self.context.get('request', {}))
)
Expand Down Expand Up @@ -896,18 +953,18 @@ def update(self, instance, validated_data):
return instance


class ProjectParticipantSerializer(serializers.ModelSerializer):
"""
Сериализатор для списка участников.
"""
volunteer = VolunteerGetSerializer()
# class ProjectParticipantSerializer(serializers.ModelSerializer):
# """
# Сериализатор для списка участников.
# """
# volunteer = VolunteerGetSerializer()

class Meta:
model = ProjectParticipants
fields = (
# 'project',
'volunteer',
)
# class Meta:
# model = ProjectParticipants
# fields = (
# # 'project',
# 'volunteer',
# )


# class ProjectFavoriteGetSerializer(serializers.ModelSerializer):
Expand Down
1 change: 1 addition & 0 deletions backend/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@ def get_queryset(self):
favorite_projects = Project.objects.filter(
Q(project_favorite__user=self.request.user)
)
# return (volunteer_in_projects).distinct()
return (favorite_projects | volunteer_in_projects).distinct()

# if self.request.user.is_organizer:
Expand Down
20 changes: 20 additions & 0 deletions backend/projects/migrations/0004_alter_volunteer_date_of_birth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 4.2.6 on 2023-12-04 19:04

import datetime
import django.core.validators
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('projects', '0003_alter_address_address_line_and_more'),
]

operations = [
migrations.AlterField(
model_name='volunteer',
name='date_of_birth',
field=models.DateField(validators=[django.core.validators.MinValueValidator(limit_value=datetime.date(1900, 1, 1)), django.core.validators.MaxValueValidator(limit_value=datetime.date(2023, 12, 4))], verbose_name='Дата рождения'),
),
]

0 comments on commit 2ed455c

Please sign in to comment.