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 _ from django.contrib.auth.models import User from django.db.models.signals import post_save, post_delete, pre_save from django.dispatch import receiver import geopandas as gpd from sqlalchemy import * from geoalchemy2 import Geometry, WKTElement import os, zipfile, glob import environ from geo.Postgres import Db 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 from django.db import transaction from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager import csv import codecs import sys import time import uuid env = environ.Env(DEBUG=(bool, False)) environ.Env.read_env(env_file=".env") db = Db(dbname=env("POSTGRES_DBNAME"), user=env("POSTGRES_USER"), password=env("POSTGRES_PASS"), host="postgres-db", port=env("PG_PORT")) class CustomUserManager(BaseUserManager): def create_user(self, email, password, **other_fields): if not email: raise ValueError(_("You must provide an email address")) email = self.normalize_email(email) user=self.model(email=email, **other_fields) user.set_password(password) user.save() return user def create_superuser(self, email, password, **other_fields): other_fields.setdefault('is_staff', True) other_fields.setdefault('is_superuser', True) other_fields.setdefault('is_active', True) if other_fields.get('is_staff') is not True: raise ValueError(_('Supperuser must assigned to staff')) if other_fields.get('is_superuser') is not True: raise ValueError(_('Supperuser must assigned to superuser=True')) return self.create_user(email, password, **other_fields) class JpnAdminMainPerf(models.Model): geom = models.MultiPolygonField(blank=True, null=True) adm0_en = models.CharField(max_length=254, blank=True, null=True) adm0_ja = models.CharField(max_length=254, blank=True, null=True) adm0_pcode = models.CharField(max_length=254, blank=True, null=True) adm1_en = models.CharField(max_length=254, blank=True, null=True) adm1_ja = models.CharField(max_length=254, blank=True, null=True) adm1_pcode = models.CharField(max_length=254, blank=True, null=True) class Meta: managed = False db_table = 'jpn_admin_main_perf' class JpnAdminPerf(models.Model): geom = models.MultiLineStringField(blank=True, null=True) et_id = models.IntegerField(blank=True, null=True) et_right = models.CharField(max_length=80, blank=True, null=True) et_left = models.CharField(max_length=80, blank=True, null=True) adm2_l = models.CharField(max_length=50, blank=True, null=True) adm1_l = models.CharField(max_length=50, blank=True, null=True) adm0_l = models.CharField(max_length=50, blank=True, null=True) adm0_r = models.CharField(max_length=50, blank=True, null=True) adm1_r = models.CharField(max_length=50, blank=True, null=True) adm2_r = models.CharField(max_length=50, blank=True, null=True) admlevel = models.IntegerField(blank=True, null=True) class Meta: managed = False db_table = 'jpn_admin_perf' ### ### Cities ### class JpnSubPerf(models.Model): geom = models.MultiPolygonField(blank=True, null=True) adm0_en = models.CharField(max_length=254, blank=True, null=True) adm0_ja = models.CharField(max_length=254, blank=True, null=True) adm0_pcode = models.CharField(max_length=254, blank=True, null=True) adm1_en = models.CharField(max_length=254, blank=True, null=True) adm1_ja = models.CharField(max_length=254, blank=True, null=True) adm1_pcode = models.CharField(max_length=254, blank=True, null=True) adm2_ja = models.CharField(max_length=254, blank=True, null=True) adm2_en = models.CharField(max_length=254, blank=True, null=True) adm2_pcode = models.CharField(max_length=254, blank=True, null=True) name_modified = models.CharField(max_length=254, blank=True, null=True) area_name = models.CharField(max_length=254, blank=True, null=True) list_order =models.IntegerField(default=0) class Meta: managed = False db_table = 'jpn_sub_perf' ### ### Gifu Areas ### class GifuAreas(models.Model): geom = models.MultiPolygonField(blank=True, null=True) adm0_en = models.CharField(max_length=254, blank=True, null=True) adm0_ja = models.CharField(max_length=254, blank=True, null=True) adm0_pcode = models.CharField(max_length=254, blank=True, null=True) adm1_en = models.CharField(max_length=254, blank=True, null=True) adm1_ja = models.CharField(max_length=254, blank=True, null=True) adm1_pcode = models.CharField(max_length=254, blank=True, null=True) adm2_ja = models.CharField(max_length=254, blank=True, null=True) adm2_en = models.CharField(max_length=254, blank=True, null=True) adm2_pcode = models.CharField(max_length=254, blank=True, null=True) area_nm = models.CharField(max_length=254, blank=True, null=True) class Meta: managed = False db_table = 'gifu_areas' class CustomUser(AbstractBaseUser, PermissionsMixin): email = models.EmailField(_("email address"), unique=True) is_staff = models.BooleanField(default=False) is_active = models.BooleanField(default=True) USERNAME_FIELD = 'email' objects = CustomUserManager() def __str__(self): return self.email class RogUser(models.Model): user=models.OneToOneField(CustomUser, on_delete=models.CASCADE) phone=models.CharField(_('Phone Number'), max_length=55) first_name=models.CharField(_('First Name'), max_length=255) middle_name=models.CharField(_('Middle Name'), max_length=255, blank=True, null=True) last_name=models.CharField(_('last_name'), max_length=255) nickname=models.CharField(_('Nickname'), max_length=255, blank=True, null=True) country=models.CharField(_('Country'), max_length=255, default='Japan') language=models.CharField(_('Language'), max_length=255, default='Japanese') prefecture=models.CharField(_('Prefecture'), max_length=255, blank=True, null=True) sex=models.CharField(_('Sex'), max_length=255, default='unknown', blank=True, null=True) birthyear=models.IntegerField(_('Birth year'), blank=True, null=True) family_structure =models.IntegerField(_('Family Structure'), blank=True, null=True) introducer = models.ForeignKey(CustomUser, related_name='introduced_uesr', on_delete=models.DO_NOTHING) level= models.IntegerField(_('Level'), blank=True, null=True, default=0) paid=models.BooleanField(_("Is Paid"),default=False) parammeters=models.CharField(_('Parameters'), max_length=512, blank=True, null=True) created_at=models.DateTimeField(auto_now_add=True) last_updated_user=models.ForeignKey(CustomUser, related_name="roguser_updated_user", on_delete=models.DO_NOTHING) last_updated_at=models.DateTimeField(auto_now=True) class SystemSettings(models.Model): setting_name=models.CharField(_('Settings Name'), max_length=255) version=models.CharField(_('Version'), max_length=10, blank=True, null=True) effective_date=models.DateTimeField() end_date=models.DateTimeField() parammeters=models.CharField(_('Parameters'), max_length=512) created_at=models.DateTimeField(auto_now_add=True) last_updated_user=models.ForeignKey(CustomUser, related_name="system_setting_updated_user", on_delete=models.DO_NOTHING) last_updated_at=models.DateTimeField(auto_now=True) class Location(models.Model): location_id=models.IntegerField(_('Location id'), blank=True, null=True) sub_loc_id=models.IntegerField(_('Sub location id'), blank=True, null=True) cp=models.FloatField(_('Check Point'), blank=True, null=True) location_name=models.CharField(_('Location Name'), max_length=2048, default="--- 場所をお願いします --") category=models.CharField(_('Category'), max_length=2048, blank=True, null=True) subcategory=models.CharField(_('Sub Category'), max_length=2048, blank=True, null=True) zip=models.CharField(_('Zip code'), max_length=12, blank=True, null=True) address = models.CharField(_('Address'), max_length=2048, blank=True, null=True) prefecture = models.CharField(_('Prefecture'), max_length=2048, blank=True, null=True) area= models.CharField(_('Area'), max_length=2048, blank=True, null=True) city= models.CharField(_('City'), max_length=2048, blank=True, null=True) latitude = models.FloatField('Latitude', blank=True, null=True) longitude = models.FloatField('Latitude', blank=True, null=True) photos=models.CharField(_('Photos'), max_length=2048, blank=True, null=True) videos=models.CharField(_('Videos'), max_length=2048, blank=True, null=True) webcontents=models.CharField(_('Web Content'), max_length=2048, blank=True, null=True) status=models.CharField(_('Status'),max_length=2048, blank=True, null=True) portal=models.CharField(_('Portal'), max_length=2048,blank=True, null=True) group=models.CharField(_('Group'), max_length=2048,blank=True, null=True) phone=models.CharField(_('Phone'), max_length=2048,blank=True, null=True) fax=models.CharField(_('Fax'), max_length=2048, blank=True, null=True) email=models.EmailField(_('Email'), max_length=2048,blank=True, null=True) facility=models.CharField(_('Facility'), max_length=2048, blank=True, null=True) remark=models.CharField(_('Remarks'), max_length=2048, blank=True, null=True) tags=models.CharField(_('Tags'), max_length=2048, blank=True, null=True) event_name = models.CharField(_('Event name'), max_length=2048, blank=True, null=True) event_active = models.BooleanField(_("Is Event active"),default=True) hidden_location = models.BooleanField(_("Is Hidden Location"),default=False) auto_checkin = models.BooleanField(_("Is AutoCheckin"),default=False) checkin_radius = models.FloatField(_("Checkin radious"), blank=True, null=True, default=15.0) checkin_point = models.FloatField(_("Checkin Point"), blank=True, null=True, default=10) buy_point = models.FloatField(_("buy Point"), blank=True, null=True, default=0) evaluation_value = models.CharField(_('Evaluation value (評価)'),max_length=2048, blank=True, null=True) shop_closed = models.BooleanField(_("Shop Closed (休業)"),default=False) shop_shutdown = models.BooleanField(_("Shop Shutdown (閉業)"),default=False) opening_hours_mon = models.CharField(_("Opening hours monday (月曜)"),max_length=512, blank=True, null=True) opening_hours_tue = models.CharField(_("Opening hours tuesday (火曜)"), max_length=512,blank=True, null=True) opening_hours_wed = models.CharField(_("Opening hours wednesday (水曜)"), max_length=512,blank=True, null=True) opening_hours_thu = models.CharField(_("Opening hours thursday (木曜)"), max_length=512, blank=True, null=True) opening_hours_fri = models.CharField(_("Opening hours frinday (金曜)"), max_length=512,blank=True, null=True) opening_hours_sat = models.CharField(_("Opening hours saturday (土曜)"), max_length=512,blank=True, null=True) opening_hours_sun = models.CharField(_("Opening hours sunday (日曜)"),max_length=512, blank=True, null=True) parammeters=models.CharField(_('Parameters'), max_length=2048, blank=True, null=True) created_at=models.DateTimeField(auto_now_add=True) last_updated_user=models.ForeignKey(CustomUser, 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 templocation(models.Model): location_id=models.IntegerField(_('Location id'), blank=True, null=True) sub_loc_id=models.IntegerField(_('Sub location id'), blank=True, null=True) cp=models.FloatField(_('Check Point'), blank=True, null=True) location_name=models.CharField(_('Location Name'), max_length=2048, default="--- 場所をお願いします --") category=models.CharField(_('Category'), max_length=2048, blank=True, null=True) subcategory=models.CharField(_('Sub Category'), max_length=2048, blank=True, null=True) zip=models.CharField(_('Zip code'), max_length=12, blank=True, null=True) address = models.CharField(_('Address'), max_length=2048, blank=True, null=True) prefecture = models.CharField(_('Prefecture'), max_length=2048, blank=True, null=True) area= models.CharField(_('Area'), max_length=2048, blank=True, null=True) city= models.CharField(_('City'), max_length=2048, blank=True, null=True) latitude = models.FloatField('Latitude', blank=True, null=True) longitude = models.FloatField('Latitude', blank=True, null=True) photos=models.CharField(_('Photos'), max_length=2048, blank=True, null=True) videos=models.CharField(_('Videos'), max_length=2048, blank=True, null=True) webcontents=models.CharField(_('Web Content'), max_length=2048, blank=True, null=True) status=models.CharField(_('Status'),max_length=2048, blank=True, null=True) portal=models.CharField(_('Portal'), max_length=2048,blank=True, null=True) group=models.CharField(_('Group'), max_length=2048,blank=True, null=True) phone=models.CharField(_('Phone'), max_length=2048,blank=True, null=True) fax=models.CharField(_('Fax'), max_length=2048, blank=True, null=True) email=models.EmailField(_('Email'), max_length=2048,blank=True, null=True) facility=models.CharField(_('Facility'), max_length=2048, blank=True, null=True) remark=models.CharField(_('Remarks'), max_length=2048, blank=True, null=True) tags=models.CharField(_('Tags'), max_length=2048, blank=True, null=True) event_name = models.CharField(_('Event name'), max_length=2048, blank=True, null=True) event_active = models.BooleanField(_("Is Event active"),default=True) hidden_location = models.BooleanField(_("Is Hidden Location"),default=False) auto_checkin = models.BooleanField(_("Is AutoCheckin"),default=False) checkin_radius = models.FloatField(_("Checkin radious"), blank=True, null=True, default=15.0) checkin_point = models.FloatField(_("Checkin Point"), blank=True, null=True, default=10) buy_point = models.FloatField(_("buy Point"), blank=True, null=True, default=0) evaluation_value = models.CharField(_('Evaluation value (評価)'),max_length=2048, blank=True, null=True) shop_closed = models.BooleanField(_("Shop Closed (休業)"),default=False) shop_shutdown = models.BooleanField(_("Shop Shutdown (閉業)"),default=False) opening_hours_mon = models.CharField(_("Opening hours monday (月曜)"),max_length=512, blank=True, null=True) opening_hours_tue = models.CharField(_("Opening hours tuesday (火曜)"), max_length=512,blank=True, null=True) opening_hours_wed = models.CharField(_("Opening hours wednesday (水曜)"), max_length=512,blank=True, null=True) opening_hours_thu = models.CharField(_("Opening hours thursday (木曜)"), max_length=512, blank=True, null=True) opening_hours_fri = models.CharField(_("Opening hours frinday (金曜)"), max_length=512,blank=True, null=True) opening_hours_sat = models.CharField(_("Opening hours saturday (土曜)"), max_length=512,blank=True, null=True) opening_hours_sun = models.CharField(_("Opening hours sunday (日曜)"),max_length=512, blank=True, null=True) parammeters=models.CharField(_('Parameters'), max_length=2048, blank=True, null=True) created_at=models.DateTimeField(auto_now_add=True) last_updated_user=models.ForeignKey(CustomUser, related_name="temp_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) 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(_('Portal'), max_length=255,blank=True, null=True) group=models.CharField(_('Group'), max_length=255,blank=True, null=True) phone=models.CharField(_('Phone'), max_length=255,blank=True, null=True) fax=models.CharField(_('Fax'), max_length=255, blank=True, null=True) email=models.EmailField(_('Email'), max_length=255,blank=True, null=True) facility=models.CharField(_('Facility'), max_length=255, blank=True, null=True) remark=models.CharField(_('Remarks'), 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(CustomUser, 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, blank=True, null=True) def __str__(self): return str(self.location_id) class Location_polygon(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(_('Portal'), max_length=255,blank=True, null=True) group=models.CharField(_('Group'), max_length=255,blank=True, null=True) phone=models.CharField(_('Phone'), max_length=255,blank=True, null=True) fax=models.CharField(_('Fax'), max_length=255, blank=True, null=True) email=models.EmailField(_('Email'), max_length=255,blank=True, null=True) facility=models.CharField(_('Facility'), max_length=255, blank=True, null=True) remark=models.CharField(_('Remarks'), 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(CustomUser, 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, blank=True, null=True) def __str__(self): return str(self.location_name) class UserTracks(models.Model): user=models.ForeignKey(CustomUser, on_delete=models.DO_NOTHING) geom=models.MultiPointField(srid=4326) created_at=models.DateTimeField(auto_now_add=True) EVENT_STATUS = ( ("PREPARING", _("Preparing")), ("PROMOTION", _("Promotion")), ("EVENT", _("Event")), ("END", _("End")) ) class Event(models.Model): tagname=models.CharField(_('Parameters'), max_length=512, blank=True, null=True) status=models.CharField(max_length=256, choices=EVENT_STATUS) price=models.IntegerField(_('Paid Amount'), default=0) promotion_date=models.DateTimeField(_('Promotion date'), blank=True, null=True) event_start=models.DateTimeField(_('Promotion date'), blank=True, null=True) event_end=models.DateTimeField(_('Promotion date'), blank=True, null=True) remark=models.CharField(max_length=256, 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(CustomUser, related_name="event_updated_user", on_delete=models.DO_NOTHING,blank=True, null=True) last_updated_at=models.DateTimeField(auto_now=True) ROG_STATUS = ( ("REGISTERED", _("Registered")), ("ACCEPTED", _("accepted")), ("PAID", _("paid")), ("JOINED", _("joined")), ("CANCELED", _("Canceled")) ) class JoinedEvent(models.Model): user=models.ForeignKey(CustomUser, on_delete=models.DO_NOTHING) tagname=models.CharField(_('Tag Name'), max_length=255, blank=True, null=True) status=models.CharField(max_length=256, choices=ROG_STATUS) registrationid=models.CharField(_('Registration Id'), max_length=56) payment_code=models.CharField(_('Payment Code'), max_length=255) paid=models.IntegerField(_('Paid Amount'), default=0) remark=models.CharField(_('Remark'), max_length=255, blank=True, null=True) parammeters=models.CharField(_('Parameters'), max_length=512) created_at=models.DateTimeField(auto_now_add=True) last_updated_user=models.ForeignKey(CustomUser, related_name="joined_event_updated_user", on_delete=models.DO_NOTHING) last_updated_at=models.DateTimeField(auto_now=True) class Favorite(models.Model): user=models.ForeignKey(CustomUser, on_delete=models.DO_NOTHING) location=models.ForeignKey(Location, on_delete=models.CASCADE) good=models.IntegerField(_('Good'), default=0) favorite=models.IntegerField(_('Favorite'), default=0) evaluation=models.IntegerField(_('Evaluation'), default=0) number_visit=models.IntegerField(_('Good'), default=0) last_visited=models.DateTimeField(_('Last Visited'), 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(CustomUser, related_name="favorite_updated_user", on_delete=models.DO_NOTHING) last_updated_at=models.DateTimeField(auto_now=True) TRAVEL_CATEGORY = ( ("PRIVATE", _("Private")), ("GROUP", _("Group")), ("AGENT", _("Agent")), ("ROGAINING", _("Rogaining")) ) class TravelList(models.Model): travel_id= models.IntegerField(_('Travel Id')) user=models.ForeignKey(CustomUser, on_delete=models.DO_NOTHING) start_date=models.DateTimeField(_('Start date') ,blank=True, null=True) finish_date=models.DateTimeField(_('End date') ,blank=True, null=True) category=models.CharField(max_length=256, choices=TRAVEL_CATEGORY) title=models.CharField(_('Title'), max_length=255) transportation=models.CharField(_('Transpotation'), max_length=255 ,blank=True, null=True) moving_distance=models.IntegerField(blank=True, null=True) duration=models.DurationField(_('Duration') ,blank=True, null=True) eta=models.DateTimeField(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(CustomUser, related_name="travel_list_updated_user", on_delete=models.DO_NOTHING) last_updated_at=models.DateTimeField(auto_now=True) class TravelPoint(models.Model): travel_list= models.ForeignKey(TravelList, on_delete=models.DO_NOTHING) location=models.ForeignKey(Location, on_delete=models.CASCADE) distance=models.FloatField(blank=True, null=True) transportation=models.CharField(_('Transpotation'), max_length=255 ,blank=True, null=True) eta=models.DateTimeField(blank=True, null=True) order_number=models.IntegerField(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(CustomUser, related_name="travelpoint_updated_user", on_delete=models.DO_NOTHING) last_updated_at=models.DateTimeField(auto_now=True) class Useractions(models.Model): user=models.ForeignKey(CustomUser, related_name="action_user", on_delete=models.CASCADE) location=models.ForeignKey(Location, related_name="action_location", on_delete=models.CASCADE) wanttogo=models.BooleanField(default=False) like=models.BooleanField(default=False) checkin=models.BooleanField(default=False) checkinimage=models.FileField(upload_to='%y%m%d', blank=True, null=True) order =models.IntegerField(default=0) created_at=models.DateTimeField(auto_now_add=True) last_updated_at=models.DateTimeField(auto_now=True) class TestModel(models.Model): testbane=models.CharField(_("test field"), max_length=355) wanttogo=models.BooleanField(default=False) like=models.BooleanField(default=False) checkin=models.BooleanField(default=False) created_at=models.DateTimeField(auto_now_add=True) last_updated_at=models.DateTimeField(auto_now=True) def getTableForModel(tbl): if tbl == 1: return templocation.objects.model._meta.db_table; elif tbl == 2: return Location_line.objects.model._meta.db_table; else: return Location_polygon.objects.model._meta.db_table; def getTempMappingforModel(tbl, shp): if tbl == 1: return LayerMapping(templocation, shp, location_mapping, transform=False) elif tbl == 2: return LayerMapping(Location_line, shp, location_line_mapping, transform=False) else: return LayerMapping(Location_polygon, shp, location_polygon_mapping, transform=False) def get_file_path(instance, filename): ext = filename.split('.')[-1] filename = "%s/%s.%s" % (uuid.uuid4(), uuid.uuid4(), ext) return os.path.join('uploads/geoms', filename) class ShapeLayers(models.Model): name = models.CharField(_("Shape Layer"), max_length=255) file = models.FileField(upload_to=get_file_path, blank=True) uploaded_date = models.DateField(auto_now_add=True) layerof = models.IntegerField(choices=LAYER_CHOICES, default=1) table_name = models.CharField(_("Table name"), max_length=255, blank=True) def __str__(self): return self.name class ShapeFileLocations(models.Model): shapefile=models.CharField(_('Shapelayer'), max_length=2048 ,blank=True, null=True) locid=models.IntegerField(blank=True, null=True) def remove_bom_inplace(path): """Removes BOM mark, if it exists, from a file and rewrites it in-place""" buffer_size = 4096 bom_length = len(codecs.BOM_UTF8) with open(path, mode="r+b") as fp: chunk = fp.read(buffer_size) if chunk.startswith(codecs.BOM_UTF8): i = 0 chunk = chunk[bom_length:] while chunk: fp.seek(i) fp.write(chunk) i += len(chunk) fp.seek(bom_length, os.SEEK_CUR) chunk = fp.read(buffer_size) fp.seek(-bom_length, os.SEEK_CUR) fp.truncate() @receiver(pre_save, sender=Location) def location_presave(sender, instance, *args, **kwargs): #print("------############------------", instance.location_id) templocation.objects.filter(location_id = instance.location_id).delete() @receiver(pre_save, sender=Location_line) def location_presave(sender, instance, *args, **kwargs): Location_line.objects.filter(location_id = instance.location_id).delete() @receiver(pre_save, sender=Location_polygon) def location_presave(sender, instance, *args, **kwargs): Location_polygon.objects.filter(location_id = instance.location_id).delete() @receiver(pre_save, sender=ShapeLayers) def my_callback(sender, instance, *args, **kwargs): instance.table_name = getTableForModel(instance.layerof) def deletePrevious(mdl, fields): with transaction.atomic(): mdl.objects.filter(location_id = int(fields[0])).delete(); @receiver(post_delete,sender=ShapeLayers) def deleteShapelocation(sender,instance,*args,**kwargs): locids = ShapeFileLocations.objects.filter(shapefile=instance.name).values_list('locid') print("------- name----") print(locids) print("------- name----") templocation.objects.all().delete() ShapeFileLocations.objects.filter(shapefile=instance.name).delete() @receiver(post_save, sender=ShapeLayers) def publish_data(sender, instance, created, **kwargs): file = instance.file.path file_format = os.path.basename(file).split('.')[-1] file_name = os.path.basename(file).split('.')[0] file_path = os.path.dirname(file) name = instance.name conn_str = f'postgresql://{env("POSTGRES_USER")}:{env("POSTGRES_PASS")}@{env("PG_HOST")}:{env("PG_PORT")}/{env("POSTGRES_DBNAME")}' with zipfile.ZipFile(file, 'r') as zip_ref: zip_ref.extractall(file_path) os.remove(file) try: print("before reading the file") shp = glob.glob(r'{}/**/*.shp'.format(file_path), recursive=True)[0] print("this is the read file",shp) gdf = gpd.read_file(shp) crs_name = str(gdf.crs.srs) print(crs_name, 'crs - name') epsg = int(crs_name.replace('epsg:','')) if epsg is None: epsg=4326 lm2 = getTempMappingforModel(instance.layerof, shp) print("### shape file is ###") lm2.save(strict=True, verbose=True) os.remove(shp) except Exception as e: print('######## shape file##########',e) try: csv_f = glob.glob(r'{}/**/*.csv'.format(file_path), recursive=True)[0] remove_bom_inplace(csv_f) 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_f, mode="r", encoding="utf-8") as txt_file: #heading = next(txt_file) reader = csv.reader(txt_file, delimiter=",") for fields in reader: print("@@@@@@@@@@@@") print(fields[0]) print("@@@@@@@@@@@@") if instance.layerof == 1: #insertShapeLayerLocation(instance.name, fields) updateLocation(mdl, fields) if instance.layerof == 2: updateLineTable(mdl, fields) if instance.layerof == 3: updatePolygonTable(mdl, fields) with open(csv_f, mode="r", encoding="utf-8") as txt_file: reader_2 = csv.reader(txt_file, delimiter=",") for fields in reader_2: print("@@@@@@@@@@@@") print(fields[0]) print("@@@@@@@@@@@@") if instance.layerof == 1: insertShapeLayerLocation(instance.name, fields) except Exception as e: print('######## csv file ##########',e) def insertShapeLayerLocation(name, fields): sll = ShapeFileLocations(shapefile=name, locid=fields[0]) sll.save(); def updateLocation(mdl, fields): print(f"Updating {fields[0]} - {fields[1]}") print(mdl.objects.filter(location_id = int(fields[0]))) print("-------") with transaction.atomic(): mdl.objects.filter(location_id = int(fields[0])).update( sub_loc_id = fields[1] if len(fields[1]) > 1 else 0, cp = fields[2] if len(fields[2]) > 1 else 0, location_name = fields[3] if len(fields[3]) > 1 else '', category = fields[4] if len(fields[4]) > 1 else '', subcategory = fields[5] if len(fields[5]) > 1 else '', zip = fields[6] if len(fields[6]) > 1 else '', address = fields[7] if len(fields[7]) > 1 else '', prefecture = fields[8] if len(fields[8]) > 1 else '', area = fields[9] if len(fields[9]) > 1 else '', city = fields[10] if len(fields[10]) > 1 else '', latitude = fields[11] if len(fields[11]) > 1 else '', longitude = fields[12] if len(fields[12]) > 1 else '', photos = fields[13] if len(fields[13]) > 1 else '', videos = fields[14] if len(fields[14]) > 1 else '', webcontents = fields[15] if len(fields[15]) > 1 else '', status = fields[16] if len(fields[16]) > 1 else '', portal = fields[17] if len(fields[17]) > 1 else '', group = fields[18] if len(fields[18]) > 1 else '', phone = fields[19] if len(fields[19]) > 1 else '', fax = fields[20] if len(fields[20]) > 1 else '', email = fields[21] if len(fields[21]) > 1 else '', facility = fields[22] if len(fields[22]) > 1 else '', remark = fields[23] if len(fields[23]) > 1 else '', tags = fields[24] if len(fields[24]) > 1 else '', hidden_location = fields[25] if len(fields[25]) > 1 else False, auto_checkin = fields[26] if len(fields[26]) > 1 else False, checkin_radius = fields[27] if len(fields[27]) > 1 else 15, checkin_point = fields[28] if len(fields[28]) > 1 else 10, buy_point = fields[29] if len(fields[29]) > 1 else 0, evaluation_value = fields[30] if len(fields[30]) > 1 else '', shop_closed = fields[31] if len(fields[31]) > 1 else False, shop_shutdown = fields[32] if len(fields[32]) > 1 else False, opening_hours_mon = fields[33] if len(fields[33]) > 1 else '', opening_hours_tue = fields[34] if len(fields[34]) > 1 else '', opening_hours_wed = fields[35] if len(fields[35]) > 1 else '', opening_hours_thu = fields[36] if len(fields[36]) > 1 else '', opening_hours_fri = fields[37] if len(fields[37]) > 1 else '', opening_hours_sat = fields[38] if len(fields[38]) > 1 else '', opening_hours_sun = fields[39] if len(fields[39]) > 1 else '' ) def updateLineTable(mdl, fields): print(f"Updating {fields[0]} - {fields[1]}") print(mdl.objects.filter(location_id = int(fields[0]))) print("-------") with transaction.atomic(): mdl.objects.filter(location_id = int(fields[0])).update( location_name= fields[1] if len(fields) > 1 else '', category=fields[2] if len(fields) > 2 else '', zip=fields[3] if len(fields) > 3 else '', address=fields[4] if len(fields) > 4 else '', prefecture=fields[5] if len(fields) > 5 else '', area=fields[6] if len(fields) > 6 else '', city=fields[7] if len(fields) > 7 else '', photos=fields[8] if len(fields) > 8 else '', videos=fields[9] if len(fields) > 9 else '', webcontents=fields[10] if len(fields) > 10 else '', status=fields[11] if len(fields) > 11 else '', portal=fields[12] if len(fields) > 12 else '', group=fields[13] if len(fields) > 13 else '', phone=fields[14] if len(fields) > 14 else '', fax=fields[15] if len(fields) > 15 else '', email=fields[16] if len(fields) > 16 else '', facility=fields[17] if len(fields) > 17 else '', remark=fields[18] if len(fields) > 18 else '', tags=fields[19] if len(fields) > 19 else '', parammeters=fields[20] if len(fields) > 20 else '' ) def updatePolygonTable(mdl, fields): #print(f"Updated {fields[0]} - {fields[1]}") with transaction.atomic(): mdl.objects.filter(location_id = fields[0]).update( location_name= fields[1] if len(fields) > 1 else '', category=fields[2] if len(fields) > 2 else '', zip=fields[3] if len(fields) > 3 else '', address=fields[4] if len(fields) > 4 else '', prefecture=fields[5] if len(fields) > 5 else '', area=fields[6] if len(fields) > 6 else '', city=fields[7] if len(fields) > 7 else '', photos=fields[8] if len(fields) > 8 else '', videos=fields[9] if len(fields) > 9 else '', webcontents=fields[10] if len(fields) > 10 else '', status=fields[11] if len(fields) > 11 else '', portal=fields[12] if len(fields) > 12 else '', group=fields[13] if len(fields) > 13 else '', phone=fields[14] if len(fields) > 14 else '', fax=fields[15] if len(fields) > 15 else '', email=fields[16] if len(fields) > 16 else '', facility=fields[17] if len(fields) > 17 else '', remark=fields[18] if len(fields) > 18 else '', tags=fields[19] if len(fields) > 19 else '', parammeters=fields[20] if len(fields) > 20 else '' )