70 lines
1.9 KiB
Python
70 lines
1.9 KiB
Python
from django.shortcuts import render
|
|
from rest_framework.views import APIView
|
|
from rest_framework.response import Response
|
|
from rest_framework import serializers
|
|
import pandas as pd
|
|
from apps.appsetting.models import City as CityModel
|
|
from apps.appsetting.selector import get_city_list
|
|
# Create your views here.
|
|
|
|
|
|
class CityListView(APIView):
|
|
class OutputSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = CityModel
|
|
fields = '__all__'
|
|
|
|
def get(self, request):
|
|
city_list = get_city_list()
|
|
output = self.OutputSerializer(city_list, many=True)
|
|
return Response(data=output.data)
|
|
|
|
|
|
class ImportExcelOstanView(APIView):
|
|
|
|
def get(self, request):
|
|
|
|
df = pd.read_excel('ostan.xlsx')
|
|
for index, row in df.iterrows():
|
|
ostan_name = row['name']
|
|
pv_id = row['pv_id']
|
|
|
|
try:
|
|
ostan = CityModel.objects.filter(pv_id=pv_id, is_ostan=True).exists()
|
|
except Exception as exp:
|
|
print(ostan_name)
|
|
print(pv_id)
|
|
|
|
if not ostan:
|
|
ostan_obj = CityModel()
|
|
ostan_obj.name = ostan_name
|
|
ostan_obj.pv_id = pv_id
|
|
ostan_obj.is_ostan = True
|
|
ostan_obj.save()
|
|
|
|
return Response(data={"result": 'ok'})
|
|
|
|
|
|
class ImportExcelCitysView(APIView):
|
|
|
|
def get(self, request):
|
|
|
|
|
|
df = pd.read_excel('cities.xlsx')
|
|
for index, row in df.iterrows():
|
|
city_name = row['name']
|
|
pv_id = row['province_id']
|
|
|
|
try:
|
|
ostan = CityModel.objects.get(pv_id=pv_id, is_ostan=True)
|
|
except Exception as exp:
|
|
print(city_name)
|
|
print(pv_id)
|
|
|
|
city_obj = CityModel()
|
|
city_obj.name = city_name
|
|
city_obj.is_ostan = False
|
|
city_obj.ostan = ostan
|
|
city_obj.save()
|
|
|
|
return Response(data={"result": 'ok'}) |