Skip to content

Commit

Permalink
Merge pull request #42 from volunteers-for-city-projects/api-updates
Browse files Browse the repository at this point in the history
Add User propreties, add update methods in VolunteersCreate and OgranizationCreate serializers
  • Loading branch information
1yunker authored Oct 19, 2023
2 parents 7b9f720 + 11d5236 commit f15ba07
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 13 deletions.
13 changes: 5 additions & 8 deletions backend/api/management/commands/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@

from django.core.management.base import BaseCommand

from content.models import City, Skills, News, Valuation
from projects.models import Category

# import os # путь на сервер расскоментировать


from content.models import City, Valuation, Skills # , News
# from projects.models import Category


def load_cities():
Expand Down Expand Up @@ -44,7 +40,7 @@ def load_skills():
Skills.objects.bulk_create(skills)
print('skills loaded!')

#def load_categories():
# def load_categories():
# print('loading categories...')
# categories = []
# # путь для заливки на сервер не проверен
Expand All @@ -62,7 +58,7 @@ def load_skills():
# Category.objects.bulk_create(categories)
# print('categories loaded!')

#def load_news():
# def load_news():
# print('loading news...')
# news = []
# # путь для заливки на сервер не проверен
Expand All @@ -79,6 +75,7 @@ def load_skills():
# News.objects.bulk_create(news)
# print('news loaded!')


def load_valuations():
print('loading valuations...')
valuations = []
Expand Down
36 changes: 36 additions & 0 deletions backend/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,26 @@ def create(self, validated_data):

return volunteer

@transaction.atomic
def update(self, instance, validated_data):
skills = validated_data.pop('skills')
VolunteerSkills.objects.filter(volunteer=instance).delete()
self.create_skills(skills, instance)

user_data = validated_data.pop('user')
user = User.objects.get(email=user_data.get('email'))
user.first_name = user_data.get('first_name')
user.second_name = user_data.get('second_name')
user.last_name = user_data.get('last_name')
user.save()

for attr, value in validated_data.items():
if hasattr(instance, attr):
setattr(instance, attr, value)

instance.save()
return instance

class Meta:
model = Volunteer
exclude = ('id',)
Expand Down Expand Up @@ -296,6 +316,22 @@ def create(self, validated_data):

return organization

@transaction.atomic
def update(self, instance, validated_data):
user_data = validated_data.pop('contact_person')
user = User.objects.get(email=user_data.get('email'))
user.first_name = user_data.get('first_name')
user.second_name = user_data.get('second_name')
user.last_name = user_data.get('last_name')
user.save()

for attr, value in validated_data.items():
if hasattr(instance, attr):
setattr(instance, attr, value)

instance.save()
return instance

class Meta:
model = Organization
exclude = ('id',)
4 changes: 2 additions & 2 deletions backend/backend/settings.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
from pathlib import Path

from dotenv import load_dotenv
from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
Expand Down Expand Up @@ -162,7 +162,7 @@
],
'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
1 change: 0 additions & 1 deletion backend/backend/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from .yasg import urlpatterns as doc_urls



urlpatterns = [
path('api/', include('api.urls')),
path('admin/', admin.site.urls),
Expand Down
1 change: 0 additions & 1 deletion backend/notifications/models.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

1 change: 0 additions & 1 deletion backend/notifications/views.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

8 changes: 8 additions & 0 deletions backend/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ class User(AbstractUser):
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ('first_name', 'second_name', 'last_name',)

@property
def is_organizer(self):
return self.role == self.ORGANIZER

@property
def is_volunteer(self):
return self.role == self.VOLUNTEER

class Meta:
verbose_name = 'Пользователь'
verbose_name_plural = 'Пользователи'
Expand Down

0 comments on commit f15ba07

Please sign in to comment.