# This Python file uses the following encoding: utf-8
# !/usr/bin/env python
import os

import django

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "proj_hkcm.settings")
django.setup()

from hkcm.models import Cmdata, DistrictsForAllLocationsinLocaList

if __name__ == "__main__":
    """
    Purpose : update district fields in cmdata table.
    Author  : Pharrell.zx
    Date    : 2017 Feb 6 """

    list_two = DistrictsForAllLocationsinLocaList.objects.all()
    list_one = Cmdata.objects.all()

    for x in list_one:
        # noinspection PyBroadException
        try:
            id_one = x.location
            entry_selected = \
                DistrictsForAllLocationsinLocaList.objects.get(location=id_one)

            dist = entry_selected.Districts
            if x.district == dist:
                # print(x)
                pass
            else:
                x.district = dist

                x.save()
        except:
            print(x.id)
            # print(x.location)
            x.district = "out_of_hk"
            x.save()
