Skip to content

Commit

Permalink
Merge pull request #180 from volunteers-for-city-projects/destroy-pro…
Browse files Browse the repository at this point in the history
…ject

Add draft serializer, change model project without validation
  • Loading branch information
1yunker authored Nov 12, 2023
2 parents 41b0904 + 1452e0e commit 809870c
Show file tree
Hide file tree
Showing 8 changed files with 595 additions and 85 deletions.
13 changes: 9 additions & 4 deletions backend/api/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ def filter_draft(self, queryset):
Фильтр для таба "Черновик".
"""
return queryset.filter(
Q(status_approve__in=['editing', 'rejected', 'pending'])
Q(status_approve__in=[
Project.EDITING, Project.REJECTED, Project.PENDING
]
)
)

def filter_active(self, queryset):
Expand All @@ -113,7 +116,8 @@ def filter_active(self, queryset):
"""
now = timezone.now()
return queryset.filter(
Q(status_approve='approved'), end_datetime__gt=now
Q(status_approve=Project.APPROVED),
end_datetime__gt=now
)

def filter_completed(self, queryset):
Expand All @@ -122,15 +126,16 @@ def filter_completed(self, queryset):
"""
now = timezone.now()
return queryset.filter(
Q(status_approve='approved'), end_datetime__lte=now
Q(status_approve=Project.APPROVED),
end_datetime__lte=now
)

def filter_archive(self, queryset):
"""
Фильтр для таба "Архив".
"""
return queryset.filter(
Q(status_approve='canceled_by_organizer')
Q(status_approve=Project.CANCELED_BY_ORGANIZER)
)

# TODO фильтры по статусам проекта в ЛК Волонтера
Expand Down
298 changes: 261 additions & 37 deletions backend/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from django.conf import settings
from django.db import transaction
from django.utils import timezone
from djoser.serializers import UserCreateSerializer, UserSerializer
from drf_extra_fields.fields import Base64ImageField
from rest_framework import serializers
from taggit.models import Tag

from api.utils import create_user
from api.utils import NonEmptyBase64ImageField, create_user
from content.models import (
City,
Feedback,
Expand All @@ -24,6 +25,7 @@
Volunteer,
VolunteerSkills,
)
from projects.validators import LengthValidator, regex_string_validator
from users.models import User

from .validators import validate_status_incomes
Expand Down Expand Up @@ -174,19 +176,34 @@ def get_status(self, data):
COMPLETED = 'project_completed'
CANCELED = 'canceled_by_organizer'

now = timezone.now()
# now = timezone.now()
# if data.status_approve == Project.CANCELED_BY_ORGANIZER:
# return CANCELED
# elif data.start_datetime <= now < data.start_date_application:
# return OPEN
# elif data.start_date_application <= now < data.end_date_application:
# return READY
# elif data.end_date_application <= now < data.end_datetime:
# return CLOSED
# elif data.end_datetime <= now:
# return COMPLETED
# else:
# return 'Статус проекта не определен'

if data.status_approve == Project.CANCELED_BY_ORGANIZER:
return CANCELED
elif data.start_datetime <= now < data.start_date_application:
return OPEN
elif data.start_date_application <= now < data.end_date_application:
return READY
elif data.end_date_application <= now < data.end_datetime:
return CLOSED
elif data.end_datetime <= now:
return COMPLETED
else:
return 'Статус проекта не определен'
elif data.status_approve == Project.APPROVED:
now = timezone.now()
if data.start_datetime <= now < data.start_date_application:
return OPEN
elif (data.start_date_application <= now <
data.end_date_application):
return READY
elif data.end_date_application <= now < data.end_datetime:
return CLOSED
elif data.end_datetime <= now:
return COMPLETED
return 'Статус проекта не определен'

class Meta:
model = Project
Expand Down Expand Up @@ -217,16 +234,156 @@ class Meta:
read_only_fields = fields


# =====================================start====================================

# ЭТО ЧЕРНОВИК без валидации!!!!!!!!!!!!!!!!!!!!


class DraftProjectSerializer(serializers.ModelSerializer):
"""
Сериализатор для создания/редоктирования черновика - проекта.
"""

event_address = AddressSerializer(required=False)
skills = serializers.PrimaryKeyRelatedField(
required=False, queryset=Skills.objects.all(), many=True
)
picture = Base64ImageField(required=False)

def create(self, validated_data):
project_instance = super().create(validated_data)
project_instance.status_approve = Project.EDITING
project_instance.save()

return project_instance

def update(self, instance, validated_data):
status_approve = instance.status_approve
if (
status_approve != Project.EDITING
and status_approve != Project.REJECTED
):
raise serializers.ValidationError(
'Редактирование разрешено только для проектов '
'со статусом "Editing" или "Rejected".'
)
return super().update(instance, validated_data)

class Meta:
model = Project
fields = (
'name',
'description',
'picture',
'start_datetime',
'end_datetime',
'start_date_application',
'end_date_application',
'event_purpose',
'event_address',
'project_tasks',
'project_events',
'organizer_provides',
'organization',
'city',
'categories',
'photo_previous_event',
'status_approve',
'skills',
)
read_only_fields = ('organization', 'status_approve',)


class ProjectSerializer(serializers.ModelSerializer):

"""
Сериализатор для проекта.
Сериализатор для создания/редактирования проекта.
"""

event_address = AddressSerializer()
skills = serializers.PrimaryKeyRelatedField(
queryset=Skills.objects.all(), many=True
)
picture = Base64ImageField()
picture = NonEmptyBase64ImageField()
description = serializers.CharField(
validators=[
regex_string_validator,
LengthValidator(
min_length=settings.MIN_LEN_TEXT_FIELD_V2,
max_length=settings.MAX_LEN_TEXT_FIELD,
),
],
)
event_purpose = serializers.CharField(
validators=[
regex_string_validator,
LengthValidator(
min_length=settings.MIN_LEN_TEXT_FIELD_V2,
max_length=settings.MAX_LEN_TEXT_FIELD,
),
],
)
project_tasks = serializers.CharField(
validators=[
regex_string_validator,
LengthValidator(
min_length=settings.MIN_LEN_TEXT_FIELD_V1,
max_length=settings.MAX_LEN_TEXT_FIELD,
),
],
)
project_events = serializers.CharField(
validators=[
regex_string_validator,
LengthValidator(
min_length=settings.MIN_LEN_TEXT_FIELD_V1,
max_length=settings.MAX_LEN_TEXT_FIELD,
),
],
)
organizer_provides = serializers.CharField(
allow_blank=True,
required=False,
validators=[
regex_string_validator,
LengthValidator(
min_length=settings.MIN_LEN_TEXT_FIELD_V1,
max_length=settings.MAX_LEN_TEXT_FIELD,
),
],
)
city = serializers.PrimaryKeyRelatedField(
queryset=City.objects.all(),
required=True,
allow_null=False, # Запретить отправку значения None. если удасться
# убрать из модели null=True и чтоб не ломалась админка то можно удалить полностью city из сериализатор
)

class Meta:
model = Project
fields = (
'name',
'description',
'picture',
'start_datetime',
'end_datetime',
'start_date_application',
'end_date_application',
'event_purpose',
'event_address',
'project_tasks',
'project_events',
'organizer_provides',
'organization',
'city',
'categories',
'photo_previous_event',
'status_approve',
'skills',
)
read_only_fields = ('organization', 'status_approve',)
extra_kwargs = {field: {'required': True} for field in fields}
extra_kwargs['photo_previous_event'] = {'required': False}

# def validate(self, data):
# start_datetime = data['start_datetime']
Expand Down Expand Up @@ -259,29 +416,96 @@ def create(self, validated_data):
project_instanse.categories.set(categories)
return project_instanse

class Meta:
model = Project
fields = (
'name',
'description',
'picture',
'start_datetime',
'end_datetime',
'start_date_application',
'end_date_application',
'event_purpose',
'event_address',
'project_tasks',
'project_events',
'organizer_provides',
'organization',
'city',
'categories',
'photo_previous_event',
'status_approve',
'skills',
)
read_only_fields = ('organization',)
def update(self, instance, validated_data):
address_data = validated_data.pop('event_address', None)
if address_data:
address = instance.event_address
for attr, value in address_data.items():
setattr(address, attr, value)
address.save()
return super(ProjectSerializer, self).update(instance, validated_data)


# =====================================END====================================

# class ProjectSerializer(serializers.ModelSerializer):
# """
# Сериализатор для проекта.
# """

# event_address = AddressSerializer()
# skills = serializers.PrimaryKeyRelatedField(
# queryset=Skills.objects.all(), many=True
# )
# picture = Base64ImageField()

# # def validate(self, data):
# # start_datetime = data['start_datetime']
# # end_datetime = data['end_datetime']
# # application_date = data['application_date']

# # validate_dates(start_datetime, end_datetime, application_date)
# # validate_reception_status(
# # application_date, start_datetime, end_datetime
# # )
# # return data


# def create(self, validated_data):
# status_approve = validated_data.get('status_approve')
# if status_approve is not None and status_approve not in (
# Project.EDITING,
# Project.PENDING,
# ):
# validated_data.pop('status_approve')
# categories = validated_data.pop('categories')
# skills = validated_data.pop('skills')
# with transaction.atomic():
# address, _ = Address.objects.get_or_create(
# **validated_data.pop('event_address')
# )
# project_instanse = Project.objects.create(
# event_address=address, **validated_data
# )
# project_instanse.skills.set(skills)
# project_instanse.categories.set(categories)
# return project_instanse

# def update(self, instance, validated_data):
# address_data = validated_data.pop('event_address', None)
# if address_data:
# address = instance.event_address
# for attr, value in address_data.items():
# setattr(address, attr, value)
# address.save()

# # Handle other nested fields in a similar fashion if necessary

# return super(ProjectSerializer, self).update(instance, validated_data)

# class Meta:
# model = Project
# fields = (
# 'name',
# 'description',
# 'picture',
# 'start_datetime',
# 'end_datetime',
# 'start_date_application',
# 'end_date_application',
# 'event_purpose',
# 'event_address',
# 'project_tasks',
# 'project_events',
# 'organizer_provides',
# 'organization',
# 'city',
# 'categories',
# 'photo_previous_event',
# 'status_approve',
# 'skills',
# )
# read_only_fields = ('organization', 'status_approve',)


class TagSerializer(serializers.ModelSerializer):
Expand Down
Loading

0 comments on commit 809870c

Please sign in to comment.