Добавлена выгрузка данных

main
through-your-tears 9 months ago
parent cb361130a6
commit 61583f086c

@ -5,10 +5,28 @@ from core.repositories import BaseRepository
class CategoryRepository(BaseRepository): class CategoryRepository(BaseRepository):
model = Category model = Category
@classmethod
def get_by_name(cls, name):
return cls.model.objects.get(name=name)
class LocationRepository(BaseRepository): class LocationRepository(BaseRepository):
model = Location 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): class OrganizationRepository(BaseRepository):
model = Organization model = Organization

@ -19,7 +19,7 @@ class LocationSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Location model = Location
fields = ('coords', 'region', 'address') fields = ('pk', 'coords', 'region', 'address')
class OrganizationListSerializer(serializers.ModelSerializer): class OrganizationListSerializer(serializers.ModelSerializer):

@ -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']])
)

@ -4,11 +4,13 @@ from django.views.decorators.cache import cache_page
from django.views.decorators.vary import vary_on_cookie from django.views.decorators.vary import vary_on_cookie
from rest_framework.generics import ListAPIView from rest_framework.generics import ListAPIView
from rest_framework.pagination import PageNumberPagination from rest_framework.pagination import PageNumberPagination
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet from rest_framework.viewsets import ModelViewSet
from core.permissions import IsAuthorOrReadOnly from core.permissions import IsAuthorOrReadOnly
from .repositories import CategoryRepository, OrganizationRepository from .repositories import CategoryRepository, OrganizationRepository
from .serializers import CategorySerializer, OrganizationListSerializer, OrganizationCreateSerializer from .serializers import CategorySerializer, OrganizationListSerializer, OrganizationCreateSerializer
from .services import parse_organizations_to_db
# Create your views here. # Create your views here.
@ -78,3 +80,9 @@ class OrganizationByCategoryListAPIView(ListAPIView):
return OrganizationRepository.get_by_category(int(category)) return OrganizationRepository.get_by_category(int(category))
else: else:
return OrganizationRepository.all() return OrganizationRepository.all()
class UploadAPIView(APIView):
def post(self, request):
parse_organizations_to_db(self.request.data)

Loading…
Cancel
Save