Commit 24865e76 authored by Luis García's avatar Luis García
Browse files

csv import for suburbs

parent 50a7382f
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import status
from rest_framework.viewsets import ModelViewSet
from rest_framework import viewsets, mixins
from rest_framework.response import Response
from rest_framework.decorators import action
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.settings import api_settings
from rest_framework.viewsets import ModelViewSet
from rest_framework_csv import renderers as r
from core.models import Profile, Map, Country, State, City, Suburb
from core.serializers import UserExtendSerializer, ProfileSerializer, MapSerializer, CountrySerializer, StateSerializer, CitySerializer, SuburbSerializer
from api.permissions import ProfilePermission, IsStaffOrReadOnly
from api.mixins import DestroyToInvisibleMixin
import codecs
import pandas as pd
############################################################
#REST API for User
......@@ -95,4 +100,54 @@ class SuburbViewSet(DestroyToInvisibleMixin, ModelViewSet):
serializer_class = SuburbSerializer
permission_classes = (IsStaffOrReadOnly,)
filter_backends = (DjangoFilterBackend,)
filterset_fields = ('name', 'city', 'city__name')
\ No newline at end of file
filterset_fields = ('name', 'city', 'city__name')
def get_renderer_context(self):
context = super().get_renderer_context()
if self.action == 'example_csv':
context['header'] = (['Country', 'State', 'City', 'Suburb'])
return context
@action(detail=False, methods=['post'], permission_classes=[IsAdminUser])
def upload_csv(self, request, pk=None):
file = request.FILES['csv']
data = pd.read_csv(file)
#Iter country data
for country in data.groupby(['Country']):
#Insert countries data
try:
country_obj = Country.objects.get(name=country[0])
except Country.DoesNotExist:
country_obj = None
country_obj = Country(name=country[0])
country_obj.save()
#Iter states data
for state in country[1].groupby(['State']):
try:
state_obj = State.objects.get(name=state[0])
except State.DoesNotExist:
state_obj = State(name=state[0], country=country_obj)
state_obj.save()
#Iter cities data
for city in state[1].groupby(['City']):
try:
city_obj = City.objects.get(name=city[0])
except City.DoesNotExist:
city_obj = City(name=city[0], state=state_obj)
city_obj.save()
#Insert subarb
for suburb in city[1].groupby(['Suburb']):
suburb_obj = Suburb(name=suburb[0], city=city_obj)
suburb_obj.save()
return Response('ok')
#This action return a csv file for tamplate
@action(detail=False, methods=['get'], renderer_classes=[r.CSVRenderer])
def example_csv(self, request, pk=None):
response =[{
'Country': 'Mexico',
'State': 'Ciudad de mexico',
'City': 'Miguel Hidalgo',
'Suburb': 'Polanco'
}]
return Response(response)
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment