示例#1
0
def create_location(request, user_id):
    received_json_data = json.loads(request.body)
    form = LocationForm(received_json_data)
    if form.is_valid():
        new_location = Location()
        new_location.latitude = form.cleaned_data['latitude']
        new_location.longitude = form.cleaned_data['longitude']
        new_location.image_url = form.cleaned_data['image_url']
        try:
            request.user.location_set.add(new_location)
        except Exception as e:
            logger.error(
                "Error associating location to user. User %s Location %s. %s",
                request.user, new_location, e.message)
            return HttpResponseBadRequest(e.message)

        # Add tags to the location if any were passed
        tag_ids = form.cleaned_data['tag_ids']
        if tag_ids:
            tags = Tag.objects.filter(id__in=tag_ids)
            for tag in tags:
                LocationTags.objects.create(tag=tag, location=new_location)

        data = json.dumps(new_location.to_dict())
        return HttpResponse(data)
    else:
        logger.error("Form invalid. %s", form.errors)
        error_data = json.dumps(form.errors)
        return HttpResponseBadRequest(error_data)
示例#2
0
def update_user_location(request, user):
    longitude = request.POST['longitude']
    latitude = request.POST['latitude']
    if longitude is None or latitude is None:
        return
    longitude = shrink_str(longitude, 15)
    latitude = shrink_str(latitude, 15)
    if len(longitude) == 0 or len(latitude) == 0:
        return
    # get the inner user information
    muser = MUser.objects.get_or_create(user=user)[0]
    if muser.user is None:
        muser.user = user
    # get the last location of the user
    location = muser.location
    if location is None:
        location = Location()
    # assign new location of the user
    location.longitude = longitude
    location.latitude = latitude
    try:
        location.save()
        muser.location = location
        muser.save()
    except Exception as e:
        print(e)
示例#3
0
def create_location(request, user_id):
    received_json_data = json.loads(request.body)
    form = LocationForm(received_json_data)
    if form.is_valid():
        new_location = Location()
        new_location.latitude = form.cleaned_data['latitude']
        new_location.longitude = form.cleaned_data['longitude']
        new_location.image_url = form.cleaned_data['image_url']
        try:
            request.user.location_set.add(new_location)
        except Exception as e:
            logger.error("Error associating location to user. User %s Location %s. %s", request.user, new_location, e.message)
            return HttpResponseBadRequest(e.message)

        # Add tags to the location if any were passed
        tag_ids = form.cleaned_data['tag_ids']
        if tag_ids:
            tags = Tag.objects.filter(id__in=tag_ids)
            for tag in tags:
                LocationTags.objects.create(tag=tag, location=new_location)

        data = json.dumps(new_location.to_dict())
        return HttpResponse(data)
    else:
        logger.error("Form invalid. %s", form.errors)
        error_data = json.dumps(form.errors)
        return HttpResponseBadRequest(error_data)