Esempio n. 1
0
def fetch_messages(request):

    if request.method == "POST":

        output = {}

        required_params = [
            'device_id',
            'longitude',
            'latitude',
            'worldlayer_id',
            'page',
            'timestamp',
            'secret',
        ]

        print request.POST

        # all parameters must be sent
        for param in required_params:
            if param not in request.POST.keys():
                output['success'] = False
                output['alert'] = "missing parameters"
                return HttpResponse(simplejson.dumps(output), 'application/json')

        # device id must be non-empty
        if request.POST['device_id'] == "":
                output['success'] = False
                output['alert'] = "empty device id"
                return HttpResponse(simplejson.dumps(output), 'application/json')

        # authorize secret
        if _auth_secret([
                            request.POST['device_id'],
                            request.POST['longitude'],
                            request.POST['latitude'],
                            request.POST['timestamp']], request.POST['secret']) == False:

                output['success'] = False
                output['alert'] = "malformed request"
                return HttpResponse(simplejson.dumps(output), 'application/json')

        # client must exist in system and be registered
        try:
            client = Client.objects.get(device_id=request.POST['device_id'])
        except:
            output['success'] = False
            output['alert'] = "unknown client"
            return HttpResponse(simplejson.dumps(output), 'application/json')

        # make sure worldlayer exists
        try:
            worldlayer = WorldLayer.objects.get(id=request.POST['worldlayer_id'])
        except:
            output['success'] = False
            output['alert'] = "unknown world layer"
            return HttpResponse(simplejson.dumps(output), 'application/json')

        try:
            latitude = float(request.POST['latitude'])
            longitude = float(request.POST['longitude'])
        except:
            output['success'] = False
            output['alert'] = "malformed long/lat"
            return HttpResponse(simplejson.dumps(output), 'application/json')

        lat_low_range = latitude - TOTEMS_MAX_FETCH_RANGE_LATITUDE
        long_low_range = longitude - TOTEMS_MAX_FETCH_RANGE_LONGITUDE

        lat_high_range = latitude + TOTEMS_MAX_FETCH_RANGE_LATITUDE
        long_high_range = longitude + TOTEMS_MAX_FETCH_RANGE_LONGITUDE

        messages = TotemMessage.objects.filter(
            totem__longitude__range=(long_low_range,long_high_range),
            totem__latitude__range=(lat_low_range,lat_high_range),
            totem__active=True,
        ).order_by('-created')[
            (int(request.POST['page'])*MAX_MESSAGES_PER_PAGE):
            ((int(request.POST['page'])*MAX_MESSAGES_PER_PAGE)+MAX_MESSAGES_PER_PAGE)
        ]

        output['messages'] = []

        for message in messages:
            distance = lat_long_distance((latitude,longitude),(message.totem.latitude,message.totem.longitude))*1000
            distance = "%.2f" % distance
            caller_owns = (message.owner.device_id == request.POST['device_id'])
            is_parent_totem_message = (message.parent_message == None) 
            timestamp = time.mktime(message.created.timetuple())


            # SET REPLY NOTIFICATIONS AS READ
            #if message.parent_message.owner == client:
                #message.view_reply_notification_replies()

            if message.active == True:
                message_text = message.message
            else:
                message_text = ""

            output['messages'].append({
                'id':message.id,
                'message':message_text,
                'message_count':message.totem.get_message_count(),
                'created':str(message.created),
                'created_pretty':pretty_date(int(timestamp)),
                'distance':distance,
                'is_owner':caller_owns,
                'is_totem_head':is_parent_totem_message,
                'totem_id':message.totem.id,
                'num_replies':message.get_num_replies(),
                'active':message.active,
            })

        output['total'] = len(output['messages'])

        # REPLY NOTIFICATIONS
        output['new_reply_notifications'] = []
        output['show_notification_alert'] = False
        notifications = TotemMessage.get_all_unread_reply_notifications_for_client(client)
        for notification in notifications:
            notification.view_reply_notification()
            output['new_reply_notifications'].append({'id':notification.id,'longitude':notification.totem.longitude,'latitude':notification.totem.latitude})
            output['show_notification_alert'] = True
        
        output['success'] = True

        print output
        RequestLog.add_request_log(client,longitude,latitude)
        client.save()

        return HttpResponse(simplejson.dumps(output), 'application/json')
    else:
        raise Http404
Esempio n. 2
0
def fetch_totems(request):

    if request.method == "POST":

        output = {}

        required_params = [
            'device_id',
            'longitude',
            'latitude',
            'worldlayer_id',
            'timestamp',
            'secret',
        ]

        # all parameters must be sent
        for param in required_params:
            if param not in request.POST.keys():
                output['success'] = False
                output['alert'] = "missing parameters"
                return HttpResponse(simplejson.dumps(output), 'application/json')

        # device id must be non-empty
        if request.POST['device_id'] == "":
                output['success'] = False
                output['alert'] = "empty device id"
                return HttpResponse(simplejson.dumps(output), 'application/json')

        # authorize secret
        if _auth_secret([
                            request.POST['device_id'],
                            request.POST['longitude'],
                            request.POST['latitude'],
                            request.POST['timestamp']], request.POST['secret']) == False:

                output['success'] = False
                output['alert'] = "malformed request"
                return HttpResponse(simplejson.dumps(output), 'application/json')

        # client must exist in system and be registered
        try:
            client = Client.objects.get(device_id=request.POST['device_id'])
        except:
            output['success'] = False
            output['alert'] = "unknown client"
            return HttpResponse(simplejson.dumps(output), 'application/json')

        # make sure worldlayer exists
        try:
            worldlayer = WorldLayer.objects.get(id=request.POST['worldlayer_id'])
        except:
            output['success'] = False
            output['alert'] = "unknown world layer"
            return HttpResponse(simplejson.dumps(output), 'application/json')

        try:
            latitude = float(request.POST['latitude'])
            longitude = float(request.POST['longitude'])
        except:
            output['success'] = False
            output['alert'] = "malformed long/lat"
            return HttpResponse(simplejson.dumps(output), 'application/json')

        lat_low_range = latitude - TOTEMS_MAX_FETCH_RANGE_LATITUDE
        long_low_range = longitude - TOTEMS_MAX_FETCH_RANGE_LONGITUDE

        lat_high_range = latitude + TOTEMS_MAX_FETCH_RANGE_LATITUDE
        long_high_range = longitude + TOTEMS_MAX_FETCH_RANGE_LONGITUDE

        totems = Totem.objects.filter(
            longitude__range=(long_low_range,long_high_range),
            latitude__range=(lat_low_range,lat_high_range),
            active=True,
        )

        output['totems'] = []

        for totem in totems:
            distance = lat_long_distance((latitude,longitude),(totem.latitude,totem.longitude))*1000
            distance = "%.2f" % distance
            caller_owns = (totem.owner.device_id == request.POST['device_id'])
            timestamp_created = time.mktime(totem.created.timetuple())
            timestamp_last_activity = time.mktime(totem.last_activity.timetuple())

            parent_message = totem.get_parent_message()
            # don't pass content of inactive messages
            if parent_message.active == True:
                message_text = parent_message.message
            else:
                message_text = ""

            output['totems'].append({
                'id':totem.id,
                'message':message_text,
                'message_count':totem.get_message_count(),
                'last_activity':str(totem.last_activity),
                'created':str(totem.created),
                'last_activity_pretty':pretty_date(int(timestamp_last_activity)),
                'created_pretty':pretty_date(int(timestamp_created)),
                'distance':distance,
                'is_owner':caller_owns,
                'longitude':totem.longitude,
                'latitude':totem.latitude,
            })

        output['total'] = len(output['totems'])
        
        # REPLY NOTIFICATIONS
        output['new_reply_notifications'] = []
        output['show_notification_alert'] = False
        notifications = TotemMessage.get_all_unread_reply_notifications_for_client(client)
        for notification in notifications:
            notification.view_reply_notification()
            output['new_reply_notifications'].append({'id':notification.id,'longitude':notification.totem.longitude,'latitude':notification.totem.latitude})
            output['show_notification_alert'] = True

        output['success'] = True

        RequestLog.add_request_log(client,longitude,latitude)
        client.save()

        print output
        return HttpResponse(simplejson.dumps(output), 'application/json')
    else:
        raise Http404