Skip to content

Commit

Permalink
update alamat
Browse files Browse the repository at this point in the history
  • Loading branch information
roniahmadi committed Jun 16, 2024
1 parent 508e20d commit 4f07050
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 106 deletions.
43 changes: 3 additions & 40 deletions master/management/commands/sync_alamat.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
from django.core.management.base import BaseCommand, CommandError
from master.models import Region, SubRegion, Country, Provinsi, Regency, Distric, Village
import json
import csv
import sys
from django.core.management.base import BaseCommand
from projekpi.tasks import import_all_alamat


class Command(BaseCommand):
Expand All @@ -28,38 +25,4 @@ class Command(BaseCommand):


def handle(self, *args, **options):
from projekpi.tasks import import_region, import_subregion, import_country, import_provinsi, import_regency, import_district, import_village
Region.objects.all().delete()
with open("data/regions.json", encoding='utf-8', errors='ignore') as read_file:
data = json.load(read_file)
for region in data:
import_region.delay(region)
SubRegion.objects.all().delete()
with open("data/subregions.json", encoding='utf-8', errors='ignore') as read_subregion:
data_subregion = json.load(read_subregion)
for subregion in data_subregion:
import_subregion.delay(subregion)

Country.objects.all().delete()
with open('data/countries.json', encoding='utf-8', errors='ignore') as read_countries:
data_countries = json.load(read_countries)
for countries in data_countries:
import_country.delay(countries)

Provinsi.objects.all().delete()
with open("data/provinces.csv") as read_provice:
spamreader = csv.reader(read_provice)
for row in spamreader:
import_provinsi.delay(row)

Regency.objects.all().delete()
with open('data/regencies.json', encoding='utf-8', errors='ignore') as read_regency:
spamreader = json.load(read_regency)
for row in spamreader:
import_regency.delay(row)

Distric.objects.all().delete()
import_district.delay()

Village.objects.all().delete()
import_village.delay()
import_all_alamat.delay(True)
173 changes: 107 additions & 66 deletions projekpi/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,88 +2,129 @@

from celery import shared_task
import json
import csv
from master.models import Region, SubRegion, Country, Provinsi, Regency, Distric, Village


@shared_task(name="api_check_availability")
def check_availability():
time.sleep(20)
print("hello")

@shared_task(name="import_all")
def import_all_alamat(reset=False):
if reset:
Village.objects.all().delete()
Distric.objects.all().delete()
Regency.objects.all().delete()
Provinsi.objects.all().delete()
Country.objects.all().delete()
SubRegion.objects.all().delete()
Region.objects.all().delete()
import_region()
print("done import region")
import_subregion()
print("done import subregion")
import_country()
print("done import Country")
import_provinsi()
print("done import provinsi")
import_regency()
print("done import regency")
import_district()
print("done import district")
import_village()
print("done import village")

@shared_task(name="import_region")
def import_region(region={}):
region_model = Region.objects.filter(id=region.get("id"),name=region.get("name", "-"), wiki_id=region.get("wikiDataId", "-")).first()
if region_model:
print("Is Exists")
else:
region_model = Region.objects.create(id=region.get("id"),name=region.get("name", "-"), wiki_id=region.get("wikiDataId", "-"))
print("Saved Done ")
def import_region():
with open("data/regions.json", encoding='utf-8', errors='ignore') as read_file:
data = json.load(read_file)
for region in data:
region_model = Region.objects.filter(id=region.get("id"),name=region.get("name", "-"), wiki_id=region.get("wikiDataId", "-")).first()
if region_model:
print("Is Exists")
else:
region_model = Region.objects.create(id=region.get("id"),name=region.get("name", "-"), wiki_id=region.get("wikiDataId", "-"))
print("Saved Done ")

@shared_task(name="import_subregion")
def import_subregion(subregion={}):
subregion_model = SubRegion.objects.filter(name=subregion.get("name", "-"), region_id_id=subregion.get('region_id'), wiki_id=subregion.get("wikiDataId", "-")).first()
if subregion_model:
print("is Exists")
else:
region = Region.objects.get(id=subregion.get('region_id'))
subregion_model = SubRegion.objects.create(id=subregion.get("id") ,name=subregion.get("name", "-"), region_id=region, wiki_id=subregion.get("wikiDataId", "-"))
print("Success saved")
def import_subregion():
with open("data/subregions.json", encoding='utf-8', errors='ignore') as read_subregion:
data_subregion = json.load(read_subregion)
for subregion in data_subregion:
subregion_model = SubRegion.objects.filter(name=subregion.get("name", "-"), region_id_id=subregion.get('region_id'), wiki_id=subregion.get("wikiDataId", "-")).first()
if subregion_model:
print("is Exists")
else:
region = Region.objects.get(id=subregion.get('region_id'))
subregion_model = SubRegion.objects.create(id=subregion.get("id") ,name=subregion.get("name", "-"), region_id=region, wiki_id=subregion.get("wikiDataId", "-"))
print("Success saved")

@shared_task(name="import_country")
def import_country(countries={}):
region = countries.get('region_id', None)
subregion = countries.get('subregion_id', None)
name = countries.get('name')
countries_model = Country.objects.filter(
region_id_id = region,
subregion_id_id = subregion,
name = name
).first()
if countries_model:
print("is exist countri")
else:
countries_model = Country.objects.create(
id = countries.get('id'),
region_id_id = region,
subregion_id_id = subregion,
name = name,
iso3 = countries.get('iso3'),
iso2 = countries.get('iso2'),
numeric_code = countries.get('numeric_code'),
phone_code = countries.get('phone_code'),
capital = countries.get('capital'),
currency = countries.get('currency'),
currency_name = countries.get('currency_name'),
currency_symbol = countries.get('currency_symbol'),
tld = countries.get('tld'),
native = countries.get('native'),
nationality = countries.get('nationality'),
timezones = countries.get('timezones', None)
)
print("saved country")

def import_country():
with open('data/countries.json', encoding='utf-8', errors='ignore') as read_countries:
data_countries = json.load(read_countries)
for countries in data_countries:
region = countries.get('region_id', None)
subregion = countries.get('subregion_id', None)
name = countries.get('name')
countries_model = Country.objects.filter(
region_id_id = region,
subregion_id_id = subregion,
name = name
).first()
if countries_model:
print("is exist countri")
else:
countries_model = Country.objects.create(
id = countries.get('id'),
region_id_id = region,
subregion_id_id = subregion,
name = name,
iso3 = countries.get('iso3'),
iso2 = countries.get('iso2'),
numeric_code = countries.get('numeric_code'),
phone_code = countries.get('phone_code'),
capital = countries.get('capital'),
currency = countries.get('currency'),
currency_name = countries.get('currency_name'),
currency_symbol = countries.get('currency_symbol'),
tld = countries.get('tld'),
native = countries.get('native'),
nationality = countries.get('nationality'),
timezones = countries.get('timezones', None)
)
print("saved country")

@shared_task(name="import_provinsi")
def import_provinsi(row=[]):
code = row[0]
name = row[1]
province_model = Provinsi.objects.filter(id=code, code=code, nama=name).first()
if province_model:
print("Province exists")
else:
province_model = Provinsi.objects.create(id=code, code=code, nama=name)
print("Province saved")
def import_provinsi():
with open("data/provinces.csv") as read_provice:
spamreader = csv.reader(read_provice)
for row in spamreader:
code = row[0]
name = row[1]
province_model = Provinsi.objects.filter(id=code, code=code, nama=name).first()
if province_model:
print("Province exists")
else:
province_model = Provinsi.objects.create(id=code, code=code, nama=name)
print("Province saved")

@shared_task(name="import_regency")
def import_regency(row={}):
code = row.get('code')
province_code = row.get("province")
name = row.get("name")
province_model = Regency.objects.filter(id=code, code=code, province_code_id=province_code, name=name).first()
if province_model:
print("regency exist")
else:
province_model = Regency.objects.create(id=code, code=code, province_code_id=province_code, name=name)
print("regency saved")
def import_regency():
with open('data/regencies.json', encoding='utf-8', errors='ignore') as read_regency:
spamreader = json.load(read_regency)
for row in spamreader:
code = row.get('code')
province_code = row.get("province")
name = row.get("name")
province_model = Regency.objects.filter(id=code, code=code, province_code_id=province_code, name=name).first()
if province_model:
print("regency exist")
else:
province_model = Regency.objects.create(id=code, code=code, province_code_id=province_code, name=name)
print("regency saved")

@shared_task(name="import_district")
def import_district():
Expand Down

0 comments on commit 4f07050

Please sign in to comment.