Skip to content

Commit 508e20d

Browse files
committed
update task
1 parent bd5395b commit 508e20d

File tree

2 files changed

+29
-28
lines changed

2 files changed

+29
-28
lines changed

master/management/commands/sync_alamat.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,7 @@ def handle(self, *args, **options):
5959
import_regency.delay(row)
6060

6161
Distric.objects.all().delete()
62-
with open('data/district.json', encoding='utf-8', errors='ignore') as read_district:
63-
spamreader = json.load(read_district)
64-
for row in spamreader:
65-
import_district.delay(row)
62+
import_district.delay()
6663

6764
Village.objects.all().delete()
68-
with open('data/village.json', encoding='utf-8', errors='ignore') as read_village:
69-
spamreader = json.load(read_village)
70-
for row in spamreader:
71-
import_village.delay(row)
65+
import_village.delay()

projekpi/tasks.py

+27-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import time
22

33
from celery import shared_task
4+
import json
45
from master.models import Region, SubRegion, Country, Provinsi, Regency, Distric, Village
56

67

@@ -85,25 +86,31 @@ def import_regency(row={}):
8586
print("regency saved")
8687

8788
@shared_task(name="import_district")
88-
def import_district(row={}):
89-
code = row.get('code')
90-
regency_code = row.get('regency')
91-
name = row.get("name")
92-
province_model = Distric.objects.filter(id=code, code=code, regency_code_id=regency_code, name=name).first()
93-
if province_model:
94-
print("regency exist")
95-
else:
96-
province_model = Distric.objects.create(id=code, code=code, regency_code_id=regency_code, name=name)
97-
print("regency saved")
89+
def import_district():
90+
with open('data/district.json', encoding='utf-8', errors='ignore') as read_district:
91+
spamreader = json.load(read_district)
92+
for row in spamreader:
93+
code = row.get('code')
94+
regency_code = row.get('regency')
95+
name = row.get("name")
96+
province_model = Distric.objects.filter(id=code, code=code, regency_code_id=regency_code, name=name).first()
97+
if province_model:
98+
print("District exist")
99+
else:
100+
province_model = Distric.objects.create(id=code, code=code, regency_code_id=regency_code, name=name)
101+
print("District saved")
98102

99103
@shared_task(name="import_village")
100-
def import_village(row={}):
101-
code = row.get('code')
102-
district_code = row.get('distric')
103-
name = row.get('name')
104-
village_model = Village.objects.filter(code=code, district_code_id=district_code, name=name).first()
105-
if village_model:
106-
print("Village Exists")
107-
else:
108-
province_model = Village.objects.create(code=code, district_code_id=district_code, name=name)
109-
print("Village saved")
104+
def import_village():
105+
with open('data/village.json', encoding='utf-8', errors='ignore') as read_village:
106+
spamreader = json.load(read_village)
107+
for row in spamreader:
108+
code = row.get('code')
109+
district_code = row.get('distric')
110+
name = row.get('name')
111+
village_model = Village.objects.filter(code=code, district_code_id=district_code, name=name).first()
112+
if village_model:
113+
print("Village Exists")
114+
else:
115+
province_model = Village.objects.create(code=code, district_code_id=district_code, name=name)
116+
print("Village saved")

0 commit comments

Comments
 (0)