def initLocation():        
     print("*****************************************")
     print("Start to analyze ip raw data...")
     
     import os
     if not os.path.exists('./IP2LOCATION-LITE-DB5.CSV'):
         print("No location csv file")
         return
     
     with open('./IP2LOCATION-LITE-DB5.CSV', newline='', encoding="utf8") as f:
         maxline = 100
         linenum = 0
         locations = {}
         reader = csv.reader(f)
         for row in reader:
             row[3] = row[3].strip()
             row[4] = row[4].strip()
             row[5] = row[5].strip()
             linenum = linenum + 1
             if linenum%10000==0:
                 print(linenum)
             nextone = False
             if row[3]!="-" and row[3] not in locations:
                 locations[row[3]] = {}
             
             if row[4]!='-' and row[4] not in locations[row[3]]:
                 locations[row[3]][row[4]] = []
             elif row[4] == row[3]:
                 continue
            
             if row[5] != row[4]:
                 cities = [c for c in locations[row[3]][row[4]] if c==row[5]]
                 if len(cities) == 0 and row[5]!='-':
                     locations[row[3]][row[4]].append(row[5])    
                                 
     print("Stop to analyze ip raw data and start to init country/province/city db...")
     
     cindex =0
     for (country,provinces) in locations.items():
         cindex = cindex + 1
         ccode = '{code:03d}'.format(code=cindex)
         cloc = Location()
         cloc.code = ccode
         cloc.name = country
         cloc.save()
     
         pindex =0
         for (province,cities) in provinces.items():
             print("country: {} province: {}".format(country,province))
             pindex = pindex + 1
             pcode = ccode + '{code:03d}'.format(code=pindex)
             ploc = Location()
             ploc.code = pcode
             ploc.name = province
             ploc.pid = cloc
             ploc.save()
             
             cityindex =0
             for city in cities:
                 cityindex = cityindex + 1
                 citycode = pcode + '{code:03d}'.format(code=cityindex)
                 cityloc = Location()
                 cityloc.code = citycode
                 cityloc.name = city
                 cityloc.pid = ploc
                 cityloc.save()
                 
     print("Finished operation")
    def getUpdateLocationWithName(country,province,city):
        conLoc = None
        proLoc = None
        ctLoc = None
        
        if country and len(country) > 0:
            try:
                conLoc = Location.objects.get(name__iexact=country,pid_id__isnull=True)
            except:
                pass
            if conLoc is None:
                concode = 0
                try:
                    preConLoc = Location.objects.filter(pid_id__isnull=True).latest('code')
                    if preConLoc:
                        concode = int(preConLoc.code) + 1
                    else:
                        concode = int("001")
                except:
                    concode = int("001")
                    pass
                
                if concode > 0:
                    conLoc = Location()
                    conLoc.code = '{code:03d}'.format(code=concode)
                    conLoc.name = country
                    conLoc.save()
        else:
            return None

                
        if conLoc and province and len(province) > 0:
            proLoc = None
            try:
                proLoc = Location.objects.get(name__iexact=province,pid__id__exact=conLoc.id)
            except:
                pass
            if proLoc is None:
                procode = 0       
                try:
                    preProLoc = Location.objects.filter(pid__id__exact=conLoc.id).latest('code')
                    if preProLoc:
                        procode = int(preProLoc.code) + 1
                    else:
                        procode = int(conLoc.code + "001")
                except:
                    procode = int(conLoc.code + "001")
                    pass

                if procode > 0:
                    proLoc = Location()
                    proLoc.code = '{code:06d}'.format(code=procode)
                    proLoc.name = province
                    proLoc.pid = conLoc
                    proLoc.save()
        else:
            return conLoc
                
                
        if proLoc and city and len(city) > 0:            
            try:
                ctLoc = Location.objects.get(name__iexact=city,pid__id__exact=proLoc.id)
            except:
                pass
            if ctLoc is None:
                ctcode = 0
                try:
                    preCtLoc = Location.objects.filter(pid__id__exact=proLoc.id).latest('code')
                    if preCtLoc:
                        ctcode = int(preCtLoc.code) + 1
                    else:
                        ctcode = int(proLoc.code + "001")
                except:
                    ctcode = int(proLoc.code + "001")
                    pass

                if ctcode > 0:
                    ctLoc = Location()
                    ctLoc.code = '{code:09d}'.format(code = ctcode)
                    ctLoc.name = city
                    ctLoc.pid = proLoc
                    ctLoc.save()
        else:
            return proLoc
        
        return ctLoc