|
1 | 1 | import time
|
2 | 2 |
|
3 | 3 | from celery import shared_task
|
| 4 | +import json |
4 | 5 | from master.models import Region, SubRegion, Country, Provinsi, Regency, Distric, Village
|
5 | 6 |
|
6 | 7 |
|
@@ -85,25 +86,31 @@ def import_regency(row={}):
|
85 | 86 | print("regency saved")
|
86 | 87 |
|
87 | 88 | @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") |
98 | 102 |
|
99 | 103 | @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