first commit
This commit is contained in:
commit
3e4574dcdd
163 files changed
+3559
No files matched your search
Whitespace-only changes.
@@ -0,0 +1,10 @@
|
||||
from django.contrib import admin
|
||||
from .models import City as CityModel
|
||||
# Register your models here.
|
||||
|
||||
|
||||
@admin.register(CityModel)
|
||||
class CityAdmin(admin.ModelAdmin):
|
||||
list_display = ['name', 'is_ostan', 'ostan', 'pv_id']
|
||||
search_fields = ['name',]
|
||||
list_filter = ['name', 'is_ostan', 'ostan',]
|
||||
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class AppsettingConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'apps.appsetting'
|
||||
@@ -0,0 +1,30 @@
|
||||
# Generated by Django 4.2.1 on 2024-03-27 13:43
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='City',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('name', models.CharField(blank=True, max_length=30, null=True)),
|
||||
('is_ostan', models.BooleanField(default=False)),
|
||||
('pv_id', models.PositiveSmallIntegerField(blank=True, null=True)),
|
||||
('ostan', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='appsetting.city')),
|
||||
],
|
||||
options={
|
||||
'abstract': False,
|
||||
},
|
||||
),
|
||||
]
|
||||
Whitespace-only changes.
@@ -0,0 +1,13 @@
|
||||
from django.db import models
|
||||
from utils.base_model import BaseModel
|
||||
# Create your models here.
|
||||
|
||||
|
||||
class City(BaseModel):
|
||||
name = models.CharField(max_length=30, null=True, blank=True)
|
||||
is_ostan = models.BooleanField(default=False, null=False, blank=False)
|
||||
ostan = models.ForeignKey('self', models.CASCADE, null=True, blank=True)
|
||||
pv_id = models.PositiveSmallIntegerField(null=True, blank=True)
|
||||
|
||||
def __str__(self):
|
||||
return str(self.name)
|
||||
@@ -0,0 +1,13 @@
|
||||
from apps.appsetting.models import City
|
||||
from django.core.cache import cache
|
||||
|
||||
|
||||
def get_city_list():
|
||||
city = cache.get('city')
|
||||
|
||||
if city is None:
|
||||
city = City.objects.all()
|
||||
# 75 hour cache
|
||||
cache.set('city', city, 60 * 60 * 72)
|
||||
|
||||
return city
|
||||
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
@@ -0,0 +1,5 @@
|
||||
from django.urls import path
|
||||
from apps.appsetting.views import CityListView
|
||||
urlpatterns = [
|
||||
path('citys/', CityListView.as_view(), name='citys'),
|
||||
]
|
||||
@@ -0,0 +1,70 @@
|
||||
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'})
|
||||
Reference in new issue
Block a user