Django(Python),Assign value stay or on_move to the field row in Model

Viewed 21
def mark_stay_location():
if TestLocation.objects.filter(processed=None):
    k = TestLocation.objects.filter(processed=None).order_by("timestamp")
    p = len(k)
    list_lat1 = []
    list_lat2 = []
    list_long1 = []
    list_long2 = []
    for i in range(p - 1):
        rowpairs = k[i : i + 2]
        lat1 = rowpairs[0].latitude
        list_lat1.append(lat1)
        lat2 = rowpairs[1].latitude
        list_lat2.append(lat2)
        long1 = rowpairs[0].longitude
        list_long1.append(long1)
        long2 = rowpairs[1].longitude
        list_long2.append(long2)
        lat = abs(lat2 - lat1)
        long = abs(long2 - long1)
        a = sin(lat / 2) ** 2 + cos(lat1) * cos(lat2) * sin(long / 2) ** 2
        c = 2 * atan2(sqrt(a), sqrt(1 - a))
        for rowpair in rowpairs:
            row_id = rowpair.id
            po = TestLocation.objects.filter(id=row_id)
            if lat < 0.0009 or long < 0.0009:
                po.update(stay="stay")
                po.update(processed=True)
            else:
                po.update(stay="on_move")
                po.update(processed=True)
    print(list_lat1)
    print(list_lat2)
    print(list_long1)
    print(list_long2)
    duration()
    return Response(status=status.HTTP_200_OK)
else:
    return Response(status=status.HTTP_404_NOT_FOUND)

in database,TestLocation model fields are getting stored with values ,In above function k is querying all rows which have processed=None so far example it gives 100,then I'm using for loop using it's range and iterating with each consecutive pair of rows .In this pair I have latitude and longitude in each item of list and I used if condition if either the difference between lat or long between both items of lists is less than 0.0009 then I should set the stay value to the stay item .

I would like to know if the loops and if conditions could be corrected.Cause it's doing the job but not accurately some fields are wrongly done.

0 Answers
Related