update
This commit is contained in:
123
rog/models.py
123
rog/models.py
@ -1,3 +1,6 @@
|
||||
from dataclasses import field
|
||||
from pyexpat import model
|
||||
from sre_constants import CH_LOCALE
|
||||
from typing import ChainMap
|
||||
from django.contrib.gis.db import models
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
@ -14,6 +17,7 @@ from sqlalchemy.sql.functions import mode
|
||||
from .mapping import location_mapping, location_line_mapping, location_polygon_mapping
|
||||
from .choices import LAYER_CHOICES
|
||||
from django.contrib.gis.utils import LayerMapping
|
||||
from django.apps import apps
|
||||
|
||||
env = environ.Env(DEBUG=(bool, False))
|
||||
environ.Env.read_env(env_file=".env")
|
||||
@ -84,37 +88,6 @@ class Location(models.Model):
|
||||
return self.location_name
|
||||
|
||||
|
||||
class Location(models.Model):
|
||||
location_id=models.IntegerField(_('Location id'), blank=True, null=True)
|
||||
location_name=models.CharField(_('Location Name'), max_length=255)
|
||||
category=models.CharField(_('Category'), max_length=255, blank=True, null=True)
|
||||
zip=models.CharField(_('Zip code'), max_length=12, blank=True, null=True)
|
||||
address = models.CharField(_('Address'), max_length=512, blank=True, null=True)
|
||||
prefecture = models.CharField(_('Prefecture'), max_length=255, blank=True, null=True)
|
||||
area= models.CharField(_('Area'), max_length=255, blank=True, null=True)
|
||||
city= models.CharField(_('City'), max_length=255, blank=True, null=True)
|
||||
photos=models.CharField(_('Phptos'), max_length=255, blank=True, null=True)
|
||||
videos=models.CharField(_('Videos'), max_length=255, blank=True, null=True)
|
||||
webcontents=models.CharField(_('Web Content'), max_length=255, blank=True, null=True)
|
||||
status=models.CharField(_('Status'),max_length=255, blank=True, null=True)
|
||||
portal=models.CharField(_('Status'), max_length=255,blank=True, null=True)
|
||||
group=models.CharField(_('Status'), max_length=255,blank=True, null=True)
|
||||
phone=models.CharField(_('Status'), max_length=255,blank=True, null=True)
|
||||
fax=models.CharField(_('Status'), max_length=255, blank=True, null=True)
|
||||
email=models.EmailField(_('Email'), max_length=255,blank=True, null=True)
|
||||
facility=models.CharField(_('Status'), max_length=255, blank=True, null=True)
|
||||
remark=models.CharField(_('Status'), max_length=255, blank=True, null=True)
|
||||
tags=models.CharField(_('Tags'), max_length=512, blank=True, null=True)
|
||||
parammeters=models.CharField(_('Parameters'), max_length=512, blank=True, null=True)
|
||||
created_at=models.DateTimeField(auto_now_add=True)
|
||||
last_updated_user=models.ForeignKey(User, related_name="location_updated_user", on_delete=models.DO_NOTHING,blank=True, null=True)
|
||||
last_updated_at=models.DateTimeField(auto_now=True)
|
||||
geom=models.MultiPointField(srid=4326)
|
||||
|
||||
def __str__(self):
|
||||
return self.location_name
|
||||
|
||||
|
||||
class Location_line(models.Model):
|
||||
location_id=models.IntegerField(_('Location id'), blank=True, null=True)
|
||||
location_name=models.CharField(_('Location Name'), max_length=255)
|
||||
@ -140,10 +113,11 @@ class Location_line(models.Model):
|
||||
created_at=models.DateTimeField(auto_now_add=True)
|
||||
last_updated_user=models.ForeignKey(User, related_name="location_line_updated_user", on_delete=models.DO_NOTHING,blank=True, null=True)
|
||||
last_updated_at=models.DateTimeField(auto_now=True)
|
||||
geom=models.MultiLineStringField(srid=4326)
|
||||
|
||||
geom=models.MultiLineStringField(srid=4326, blank=True, null=True)
|
||||
|
||||
def __str__(self):
|
||||
return self.location_name
|
||||
return str(self.location_id)
|
||||
|
||||
|
||||
|
||||
class Location_polygon(models.Model):
|
||||
@ -171,10 +145,10 @@ class Location_polygon(models.Model):
|
||||
created_at=models.DateTimeField(auto_now_add=True)
|
||||
last_updated_user=models.ForeignKey(User, related_name="location_polygon_updated_user", on_delete=models.DO_NOTHING,blank=True, null=True)
|
||||
last_updated_at=models.DateTimeField(auto_now=True)
|
||||
geom=models.MultiPolygonField(srid=4326)
|
||||
geom=models.MultiPolygonField(srid=4326, blank=True, null=True)
|
||||
|
||||
def __str__(self):
|
||||
return self.location_name
|
||||
return str(self.location_name)
|
||||
|
||||
|
||||
|
||||
@ -327,9 +301,9 @@ def getMappingforModel(tbl, shp):
|
||||
if tbl == 1:
|
||||
return LayerMapping(Location, shp, location_mapping, transform=False)
|
||||
elif tbl == 2:
|
||||
return LayerMapping(Location_line, location_line_mapping, transform=False)
|
||||
return LayerMapping(Location_line, shp, location_line_mapping, transform=False)
|
||||
else:
|
||||
return LayerMapping(Location_polygon, location_polygon_mapping, transform=False)
|
||||
return LayerMapping(Location_polygon, shp, location_polygon_mapping, transform=False)
|
||||
|
||||
|
||||
class ShapeLayers(models.Model):
|
||||
@ -372,11 +346,80 @@ def publish_date(sender, instance, created, **kwargs):
|
||||
if epsg is None:
|
||||
epsg=4326
|
||||
|
||||
print("### shape file is ###")
|
||||
|
||||
lm2 = getMappingforModel(instance.layerof, shp)
|
||||
print("### shape file is ###")
|
||||
lm2.save(strict=True, verbose=True)
|
||||
|
||||
os.remove(shp)
|
||||
except Exception as e:
|
||||
print('##################',e)
|
||||
|
||||
try:
|
||||
csv = glob.glob(r'{}/**/*.csv'.format(file_path), recursive=True)[0]
|
||||
|
||||
mdl = apps.get_model(app_label="rog", model_name=LAYER_CHOICES[instance.layerof -1][1])
|
||||
print(mdl)
|
||||
print(f"#### instance.layerof - {instance.layerof}")
|
||||
with open(csv, "r") as txt_file:
|
||||
lns = txt_file.readlines()
|
||||
for ln in lns:
|
||||
fields = ln.split(",")
|
||||
if instance.layerof == 12:
|
||||
updateLineTable(mdl, fields)
|
||||
if instance.layerof == 3:
|
||||
updatePolygonTable(mdl, fields)
|
||||
|
||||
except Exception as e:
|
||||
print('##################',e)
|
||||
|
||||
|
||||
def updateLineTable(mdl, fields):
|
||||
#print(f"Updated {fields[0]} - {fields[1]}")
|
||||
mdl.objects.filter(location_id = fields[0]).update(
|
||||
location_name=fields[1],
|
||||
category=fields[2],
|
||||
zip=fields[3],
|
||||
address=fields[4],
|
||||
prefecture=fields[5],
|
||||
area=fields[6],
|
||||
city=fields[7],
|
||||
photos=fields[8],
|
||||
videos=fields[9],
|
||||
webcontents=fields[10],
|
||||
status=fields[11],
|
||||
portal=fields[12],
|
||||
group=fields[13],
|
||||
phone=fields[14],
|
||||
fax=fields[15],
|
||||
email=fields[16],
|
||||
facility=fields[17],
|
||||
remark=fields[18],
|
||||
tags=fields[19],
|
||||
parammeters=fields[20]
|
||||
)
|
||||
|
||||
|
||||
def updatePolygonTable(mdl, fields):
|
||||
#print(f"Updated {fields[0]} - {fields[1]}")
|
||||
mdl.objects.filter(location_id = fields[0]).update(
|
||||
location_name=fields[1],
|
||||
category=fields[2],
|
||||
zip=fields[3],
|
||||
address=fields[4],
|
||||
prefecture=fields[5],
|
||||
area=fields[6],
|
||||
city=fields[7],
|
||||
photos=fields[8],
|
||||
videos=fields[9],
|
||||
webcontents=fields[10],
|
||||
status=fields[11],
|
||||
portal=fields[12],
|
||||
group=fields[13],
|
||||
phone=fields[14],
|
||||
fax=fields[15],
|
||||
email=fields[16],
|
||||
facility=fields[17],
|
||||
remark=fields[18],
|
||||
tags=fields[19],
|
||||
parammeters=fields[20]
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user