def delete_entry(name):
    connect(host='mongodb://*****:*****@ds255539.mlab.com:55539/9321test')
    for a in Area.objects:
        if a.name.lower().replace(' ', '') == name.lower().replace(' ', ''):
            Area.objects(name = name.lower().replace(' ', '')).delete()
            return jsonify(LGA_name=True), 200
    return jsonify(LGA_name=False), 404
Example #2
0
def load_data_to_db(row):
    province_id = int(row['CH_ID'])
    province = Area.objects(aid=province_id).first()
    if not province:
        try:
            province = Area(level='ch',
                            th_name=row['CHANGWAT_T'].split()[-1],
                            en_name=row['CHANGWAT_E'],
                            aid=province_id)
            province.save()
        except ValidationError as e:
            print(row['CHANGWAT_T'], row['CHANGWAT_E'])

    amphoe_id = int(row['AM_ID'])
    amphoe = Area.objects(aid=amphoe_id).first()
    if not amphoe:
        try:
            amphoe = Area(level='am',
                          th_name=row['AMPHOE_T'].split()[1],
                          en_name=row['AMPHOE_E'],
                          aid=amphoe_id,
                          parent=province)
            amphoe.save()
        except ValidationError as e:
            print(row['AMPHOE_E'], row['AMPHOE_T'])

    tambon_id = int(row['TA_ID'])
    tambon = Area.objects(aid=tambon_id).first()
    if not tambon:
        try:
            tambon = Area(
                level='ta',
                th_name=row['TAMBON_T'].split()[1],
                aid=tambon_id,
                parent=amphoe,
                latlng=[float(row['LONG']),
                        float(row['LAT'])],
            )
            tambon.save()
        except ValidationError as e:
            print(row['TAMBON_E'], row['TAMBON_T'])
            sys.exit(1)
    else:
        # the tambon exists, do nothing.
        '''
        print('{}, {}, {} already in the database'.format(
            tambon.th_name.encode('utf8'),
            amphoe.th_name.encode('utf8'),
            province.th_name.encode('utf8')
        ))
        '''
        return

    amphoe.childs.append(tambon)
    amphoe.save()

    province.childs.append(amphoe)
    province.save()