diff --git a/src/organizations/repositories.py b/src/organizations/repositories.py index 50f9149..e3d968f 100644 --- a/src/organizations/repositories.py +++ b/src/organizations/repositories.py @@ -5,10 +5,28 @@ from core.repositories import BaseRepository class CategoryRepository(BaseRepository): model = Category + @classmethod + def get_by_name(cls, name): + return cls.model.objects.get(name=name) + class LocationRepository(BaseRepository): model = Location + @classmethod + def get_or_create(cls, coords, address, region=None): + if region is None: + region = 23 + if Location.objects.get(coords=coords): + return Location.objects.get(coords=coords) + else: + obj = LocationRepository.create( + coords=coords, + region=region, + address=address + ) + return obj + class OrganizationRepository(BaseRepository): model = Organization diff --git a/src/organizations/serializers.py b/src/organizations/serializers.py index 047a8d6..73ef280 100644 --- a/src/organizations/serializers.py +++ b/src/organizations/serializers.py @@ -19,7 +19,7 @@ class LocationSerializer(serializers.ModelSerializer): class Meta: model = Location - fields = ('coords', 'region', 'address') + fields = ('pk', 'coords', 'region', 'address') class OrganizationListSerializer(serializers.ModelSerializer): diff --git a/src/organizations/services.py b/src/organizations/services.py new file mode 100644 index 0000000..1cef77a --- /dev/null +++ b/src/organizations/services.py @@ -0,0 +1,55 @@ +from django.contrib.gis.geos.point import Point + +from .repositories import LocationRepository, OrganizationRepository, CategoryRepository + + +MAPPING_DATA = { + 'alpine_hut': 'Домики в горах', + 'apartment': 'Апартаменты', + 'aquarium': 'Океанариумы', + 'artwork': 'Паблик-арты', + 'attraction': 'Достопримечательности', + 'camp_pitch': 'Места для палатки', + 'camp_site': 'Кемпинги', + 'caravan_site': 'Караванинги', + 'chalet': 'Коттеджи', + 'gallery': 'Художественные галереи', + 'guest_house': 'Гостевые дома', + 'hostel': 'Хостелы', + 'hotel': 'Отели', + 'information': 'Информационные центры', + 'motel': 'Мотели', + 'museum': 'Музеи', + 'wilderness_hut': 'Лесные домики', + 'zoo': 'Зоопарки', + 'beach': 'Пляжи', + 'fuel': 'Заправки', + 'parking': 'Парковки', + 'restaurant': 'Рестораны', + 'picnic_site': 'Места для пикника', + 'viewpoint': 'Смотровые площадки', + 'shop': 'Магазины', + 'winery': 'Винодельни', + 'brewery': 'Пивоварни', + 'bicycle_rental': 'Места с арендой велосипедов', + 'theme_park': 'Парки аттракционов', + 'farm': 'Фермы', + 'trail_riding': 'Ранчо', + 'charging_station': 'Зарядные станции' +} + + +def parse_organizations_to_db(data): + for k in data.keys(): + for obj in data[k]: + OrganizationRepository.create( + location=LocationRepository.get_or_create( + coords=Point(obj['latitude'], obj['longitude']), + address=obj['address'] + ), + name=obj['name'], + phone=obj['phone'], + website=obj['website'], + description=obj['description'], + category=CategoryRepository.get_by_name(MAPPING_DATA[obj['category']]) + ) diff --git a/src/organizations/views.py b/src/organizations/views.py index 9f51ee0..2258083 100644 --- a/src/organizations/views.py +++ b/src/organizations/views.py @@ -4,11 +4,13 @@ from django.views.decorators.cache import cache_page from django.views.decorators.vary import vary_on_cookie from rest_framework.generics import ListAPIView from rest_framework.pagination import PageNumberPagination +from rest_framework.views import APIView from rest_framework.viewsets import ModelViewSet from core.permissions import IsAuthorOrReadOnly from .repositories import CategoryRepository, OrganizationRepository from .serializers import CategorySerializer, OrganizationListSerializer, OrganizationCreateSerializer +from .services import parse_organizations_to_db # Create your views here. @@ -78,3 +80,9 @@ class OrganizationByCategoryListAPIView(ListAPIView): return OrganizationRepository.get_by_category(int(category)) else: return OrganizationRepository.all() + + +class UploadAPIView(APIView): + + def post(self, request): + parse_organizations_to_db(self.request.data)