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

Project #46

Merged
merged 2 commits into from
Oct 20, 2023
Merged
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
100 changes: 90 additions & 10 deletions backend/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@
Skills,
Valuation,
)
from projects.models import Organization, Project, Volunteer, VolunteerSkills
from projects.models import (
Organization,
Project,
Volunteer,
VolunteerSkills,
ProjectParticipants,
ProjectIncomes,
)
from users.models import User


Expand All @@ -36,8 +43,12 @@ class PlatformAboutSerializer(serializers.ModelSerializer):
class Meta:
model = PlatformAbout
fields = (
'about_us', 'platform_email', 'valuations',
'projects_count', 'volunteers_count', 'organizers_count'
'about_us',
'platform_email',
'valuations',
'projects_count',
'volunteers_count',
'organizers_count',
)

def get_projects_count(self, obj):
Expand Down Expand Up @@ -208,9 +219,16 @@ class UserSerializer(serializers.ModelSerializer):
"""
Сериализатор для получения пользователя.
"""

class Meta:
model = User
fields = ('id', 'first_name', 'second_name', 'last_name', 'email',)
fields = (
'id',
'first_name',
'second_name',
'last_name',
'email',
)


class UserCreateSerializer(serializers.ModelSerializer):
Expand All @@ -220,14 +238,20 @@ class UserCreateSerializer(serializers.ModelSerializer):

class Meta:
model = User
fields = ('first_name', 'second_name', 'last_name',
'email', 'password',)
fields = (
'first_name',
'second_name',
'last_name',
'email',
'password',
)


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

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

Expand All @@ -240,10 +264,10 @@ class VolunteerCreateSerializer(serializers.ModelSerializer):
"""
Сериализатор для создания волонтера.
"""

user = UserCreateSerializer()
skills = serializers.PrimaryKeyRelatedField(
queryset=Skills.objects.all(),
many=True
queryset=Skills.objects.all(), many=True
)

def create_skills(self, skills, volunteer):
Expand Down Expand Up @@ -292,6 +316,7 @@ class OrganizationGetSerializer(serializers.ModelSerializer):
"""
Сериализатор для отображения организации-организатора.
"""

contact_person = UserSerializer()

class Meta:
Expand All @@ -303,15 +328,15 @@ class OgranizationCreateSerializer(serializers.ModelSerializer):
"""
Сериализатор для создания организации-организатора.
"""

contact_person = UserCreateSerializer()

@transaction.atomic
def create(self, validated_data):
user_data = validated_data.pop('contact_person')
user = User.objects.create_user(role=User.ORGANIZER, **user_data)
organization = Organization.objects.create(
contact_person=user,
**validated_data
contact_person=user, **validated_data
)

return organization
Expand All @@ -335,3 +360,58 @@ def update(self, instance, validated_data):
class Meta:
model = Organization
exclude = ('id',)


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

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


class ProjectIncomesSerializer(serializers.Serializer):
"""
Сериализатор для заявок волонтеров.
"""

class Meta:
model = ProjectIncomes
fields = (
'project',
'volunteer',
'status_incomes',
)


# в разработке
class VolunteerProfileSerializer(serializers.Serializer):
"""
Сериализатор для личного кабинета волонтера.
"""

volunteer = VolunteerGetSerializer()
user = UserSerializer()
skills = SkillsSerializer(many=True)
participating_projects = ProjectSerializer(many=True)
projects = ProjectSerializer(many=True)
participants = ProjectParticipantSerializer(many=True)
project_incomes = ProjectIncomesSerializer(many=True)
phone = serializers.CharField(source='volunteer.phone')

class Meta:
fields = (
'volunteer',
'user',
'phone',
'skills',
'participating_projects',
'projects',
'participants',
'project_incomes',
)
8 changes: 7 additions & 1 deletion backend/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@
SearchListView,
SkillsViewSet,
VolunteerViewSet,
VolunteerProfileView,
)

router = DefaultRouter()
router.register(r'news', NewsViewSet, basename='news')
router.register(r'projects', ProjectViewSet, basename='projects')
router.register(r'volunteers', VolunteerViewSet, basename='volunteers')
router.register(
r'organizations', OrganizationViewSet, basename='organizations')
r'organizations', OrganizationViewSet, basename='organizations'
)
router.register(r'cities', CityViewSet)
router.register(r'skills', SkillsViewSet)

Expand All @@ -29,4 +31,8 @@
path('platform_about/', PlatformAboutView.as_view()),
path('feedback/', FeedbackCreateView.as_view()),
path('search/', SearchListView.as_view()),
path(
'volunteer/profile/<int:volunteer_id>/',
VolunteerProfileView.as_view(),
),
]
12 changes: 12 additions & 0 deletions backend/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
SkillsSerializer,
VolunteerCreateSerializer,
VolunteerGetSerializer,
VolunteerProfileSerializer,
)

# from .filters import SearchFilter
Expand Down Expand Up @@ -143,3 +144,14 @@ class SearchListView(generics.ListAPIView):
filter_backends = [DjangoFilterBackend, filters.SearchFilter]
# filterset_class = SearchFilter
search_fields = ['name', 'description', 'event_purpose']


# в разработке
class VolunteerProfileView(generics.RetrieveAPIView):
def get(self, request, volunteer_id, format=None):
try:
volunteer = Volunteer.objects.get(id=volunteer_id)
serializer = VolunteerProfileSerializer(volunteer)
return Response(serializer.data)
except Volunteer.DoesNotExist:
return Response({'error': 'Волонтер не найден'}, status=404)
25 changes: 16 additions & 9 deletions backend/backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@
ALLOWED_HOSTS = os.getenv('ALLOWED_HOSTS', '127.0.0.1').split(',')

CSRF_TRUSTED_ORIGINS = [
'https://*.better-together.acceleratorpracticum.ru/', 'https://*.80.87.109.180', 'https://*.127.0.0.1',
'http://*.better-together.acceleratorpracticum.ru/', 'http://*.80.87.109.180', 'http://*.127.0.0.1',
'https://*.better-together.acceleratorpracticum.ru/',
'https://*.80.87.109.180',
'https://*.127.0.0.1',
'http://*.better-together.acceleratorpracticum.ru/',
'http://*.80.87.109.180',
'http://*.127.0.0.1',
]

# Application definition
Expand Down Expand Up @@ -162,7 +166,9 @@
],
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
'PAGE_SIZE': 10,
'DEFAULT_FILTER_BACKENDS': ['django_filters.rest_framework.DjangoFilterBackend', ],
'DEFAULT_FILTER_BACKENDS': [
'django_filters.rest_framework.DjangoFilterBackend',
],
# 'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema',
}

Expand Down Expand Up @@ -191,14 +197,12 @@

SWAGGER_SETTINGS = {
'SECURITY_DEFINITIONS': {
'Token': { # авторизация в джанго по токену
'Token': { # авторизация в джанго по токену
'type': 'apiKey',
'name': 'Authorization',
'in': 'header'
'in': 'header',
},
'Basic': { # базова авторизация
'type': 'basic'
}
'Basic': {'type': 'basic'}, # базова авторизация
},
'USE_SESSION_AUTH': True, # кнопка джанго логин можно отключить поменяв False
'JSON_EDITOR': True,
Expand All @@ -217,6 +221,7 @@
MAX_LEN_TEXT_IN_ADMIN = 50

MAX_LEN_NAME = 200
MAX_LEN_SLUG = 50
LEN_OGRN = 13
MESSAGE_PHONE_REGEX = 'Номер должен начинаться с +7 и содержать {} цифр.'
MESSAGE_EMAIL_VALID = (
Expand All @@ -234,7 +239,9 @@

MIN_LEN_NAME_USER = 2
MAX_LEN_NAME_USER = 40
MESSAGE_NAME_USER_VALID = f'Длина поля от {MIN_LEN_NAME_USER} до {MAX_LEN_NAME_USER} символов'
MESSAGE_NAME_USER_VALID = (
f'Длина поля от {MIN_LEN_NAME_USER} до {MAX_LEN_NAME_USER} символов'
)
MESSAGE_NAME_USER_CYRILLIC = 'Введите имя кириллицей'

OGRN_ERROR_MESSAGE = 'ОГРН должен состоять из 13 цифр.'
Expand Down
17 changes: 17 additions & 0 deletions backend/projects/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Project,
Volunteer,
VolunteerSkills,
ProjectIncomes,
)


Expand Down Expand Up @@ -110,3 +111,19 @@ class ProjectAdmin(ModelAdmin):
)
save_on_top = True
empty_value_display = '-пусто-'


@register(ProjectIncomes)
class ProjectIncomesAdmin(ModelAdmin):
list_display = (
'project',
'volunteer',
'status_incomes',
)
search_fields = (
'name',
'slug',
'status_incomes',
)
list_filter = ('project',)
save_on_top = True
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 4.2.6 on 2023-10-19 12:41

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('projects', '0005_alter_volunteer_date_of_birth'),
]

operations = [
migrations.AlterField(
model_name='category',
name='slug',
field=models.SlugField(unique=True, verbose_name='Слаг'),
),
migrations.CreateModel(
name='ProjectIncomes',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('status_incomes', models.CharField(blank=True, choices=[('application_submitted', 'Одобрено'), ('rejected', 'На рассмотрении'), ('accepted', 'Принята')], default=None, null=True, verbose_name='Статус заявки волонтера')),
('project', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='project_incomes', to='projects.project', verbose_name='Проект')),
('volunteer', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='project_incomes', to='projects.volunteer', verbose_name='Волонтер')),
],
),
]
41 changes: 39 additions & 2 deletions backend/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ class Category(models.Model):
)
slug = models.SlugField(
unique=True,
max_length=30,
verbose_name='Идентификатор',
max_length=settings.MAX_LEN_SLUG,
verbose_name='Слаг',
)
description = models.TextField(
blank=False,
Expand Down Expand Up @@ -308,3 +308,40 @@ def __str__(self):
return settings.PROJECTPARTICIPANTS.format(
self.project, self.volunteer
)


class ProjectIncomes(models.Model):
"""
Модель представляет собой заявки волонтеров на участие в проекте.
"""

APPLICATION_SUBMITTED = 'application_submitted'
REJECTED = 'rejected'
ACCEPTED = 'accepted'

STATUS_INCOMES = [
(APPLICATION_SUBMITTED, 'Одобрено'),
(REJECTED, 'На рассмотрении'),
(ACCEPTED, 'Принята'),
]
project = models.ForeignKey(
Project,
blank=False,
on_delete=models.CASCADE,
related_name='project_incomes',
verbose_name='Проект',
)
volunteer = models.ForeignKey(
Volunteer,
blank=False,
on_delete=models.CASCADE,
related_name='project_incomes',
verbose_name='Волонтер',
)
status_incomes = models.CharField(
choices=STATUS_INCOMES,
null=True,
blank=True,
default=None,
verbose_name='Статус заявки волонтера',
)
Loading