def test_models(self): data_dicts = set_up_data() self.assertTrue(Locality(data_dicts['l1'])) self.assertTrue(Locality(data_dicts['l2'])) self.assertTrue(Locality(data_dicts['l3'])) self.assertTrue(Locality(data_dicts['l4'])) self.assertTrue(Locality(data_dicts['l5']))
def set_up_data(): # model1: Locality # notice each instance is made to cover different combinations of attribute values l1 = Locality(city="Melbourne", province="New South Wales", country="AUS") # 50 letters l2 = Locality(city="Chargoggagoggmanchauggagoggchaubunagungamaugg City", state="MA") l3 = Locality(city="Los Angeles", state="CA", country="USA") l4 = Locality(city="Kitchener", state="", country="CAN") l5 = Locality(city="Virginia Beach", country="USA") return dict([('l1', l1), ('l2', l2), ('l3', l3), ('l4', l4), ('l5', l5)])
def post(self, request): data = request.data facility = Locality() try: data = self.parse_data(data) facility.update_data(data, request.user) return Response(facility.uuid, status=status.HTTP_201_CREATED) except KeyError as e: return HttpResponseBadRequest('%s is required' % e) except ValueError as e: return HttpResponseBadRequest('%s' % e) except Exception as e: return HttpResponseBadRequest('%s' % e)
def locality_create(request): if request.method == 'POST': if request.user.is_authenticated(): json_request = get_json_from_request(request) # checking mandatory if json_request['is_valid'] == True: tmp_changeset = Changeset.objects.create( social_user=request.user) # generate new uuid tmp_uuid = uuid.uuid4().hex loc = Locality() loc.changeset = tmp_changeset loc.domain = Domain.objects.get(name="Health") loc.uuid = tmp_uuid # generate unique upstream_id loc.upstream_id = u'web¶{}'.format(tmp_uuid) loc.geom = Point(float(json_request['long']), float(json_request['lat'])) loc.name = json_request['name'] loc.save() # ------------------------------------------------------ loc.set_values(json_request, request.user, tmp_changeset) loc.update_what3words(request.user, tmp_changeset) regenerate_cache.delay(tmp_changeset.pk, loc.pk) regenerate_cache_cluster.delay() return { "success": json_request['is_valid'], "uuid": tmp_uuid, "reason": "" } else: return { "success": json_request['is_valid'], "reason": json_request['invalid_key'] + " can not be empty" } else: return {"success": False, "reason": "Not Login Yet"} return {"success": False, "reason": "There is error occured"}
def locality_create(request): if request.method == 'POST': if request.user.is_authenticated(): json_request = get_json_from_request(request) # checking mandatory if json_request['is_valid'] is True: tmp_changeset = Changeset.objects.create( social_user=request.user ) # generate new uuid tmp_uuid = uuid.uuid4().hex loc = Locality() loc.changeset = tmp_changeset loc.domain = Domain.objects.get(name='Health') loc.uuid = tmp_uuid # generate unique upstream_id loc.upstream_id = u'web¶{}'.format(tmp_uuid) loc.geom = Point( float(json_request['long']), float(json_request['lat']) ) loc.name = json_request['name'] loc.save() # ------------------------------------------------------ loc.set_values(json_request, request.user, tmp_changeset) loc.update_what3words(request.user, tmp_changeset) regenerate_cache.delay(tmp_changeset.pk, loc.pk) regenerate_cache_cluster.delay() return {'success': json_request['is_valid'], 'uuid': tmp_uuid, 'reason': ''} else: return {'success': json_request['is_valid'], 'reason': json_request['invalid_key'] + ' can not be empty'} else: return {'success': False, 'reason': 'Not Login Yet'} return {'success': False, 'reason': 'There is error occured'}