def update_districts(): from models import District doc = doc_from_url(BASE_URL) desc = doc.xpath('/html/body/div[4]/dl[1]//a') hlid = doc.xpath('/html/body/div[4]/dl[1]//a/@href') desc = [d.text for d in desc[1:]] hlid = [l.split('/')[-2] for l in hlid[1:]] for arg in zip(hlid, desc): dist = District(hlid=arg[0], desc=arg[1]) try: dist.save() print 'Added new district', dist.desc except: pass
def get(self, request): response_data = { 'retCode': error_constants.ERR_STATUS_SUCCESS[0], 'retMsg': error_constants.ERR_STATUS_SUCCESS[1] } for item in district: code = item.get('code') name = item.get('name') if not District.objects.filter(name=name, code=code).exists(): cur_district = District(name=name, code=code) try: with transaction.atomic(): cur_district.save() except Exception as ex: print 'function name: ', __name__ print Exception, ":", ex return generate_error_response( error_constants.ERR_SAVE_INFO_FAIL, status.HTTP_500_INTERNAL_SERVER_ERROR) return Response(response_data, status.HTTP_200_OK)