Exemplo n.º 1
0
def nearbyBuses(request, pUserId, pBusStop):
    """ return all information about bus stop: events and buses """

    logger = logging.getLogger(__name__)

    timeNow = timezone.now()
    theBusStop = BusStop.objects.get(code=pBusStop)

    """
    This is temporal, it has to be deleted in the future
    """
    if pUserId != 'null':
        # Register user request
        NearByBusesLog.objects.create(
            userId=pUserId,
            busStop=theBusStop,
            timeStamp=timeNow)
    else:
        logger.error('nearbybuses: null user')

    answer = {}
    """
    BUS STOP EVENTS
    """
    getEventsBusStop = EventsByBusStop()
    busStopEvent = getEventsBusStop.getEventsForBusStop(theBusStop, timeNow)
    answer["eventos"] = busStopEvent

    """
    USER BUSES
    """
    userBuses = getUserBuses(theBusStop, pUserId)

    """
    DTPM BUSES
    """
    # DTPM source
    url = "http://54.94.231.101/dtpm/busStopInfo/"
    url = "{}{}/{}".format(url, settings.SECRET_KEY, pBusStop)
    response = requests.get(url=url)

    authBuses = []
    if(response.text != ""):
        data = json.loads(response.text)
        
        if 'id' in data:
            authBuses = getAuthorityBuses(data)

        if data['error'] is not None:
            answer['DTPMError'] = data['error']
        else:
            answer['DTPMError'] = ""

    """
    MERGE USER BUSES WITH DTPM BUSES
    """
    answer['servicios'] = mergeBuses(userBuses, authBuses)

    return JsonResponse(answer, safe=False)
    def getBusStopsForService(self, pBusService):
        """this method look for all the bus stops where the service stops."""
        busStops = []

        for sbs in ServicesByBusStop.objects.filter(service=pBusService):
            data = {}
            busStop = sbs.busStop
            data['codigo'] = busStop.code
            data['nombre'] = busStop.name
            data['latitud'] = busStop.latitud
            data['longitud'] = busStop.longitud
            getEventsByBusStop = EventsByBusStop()
            data['eventos'] = getEventsByBusStop.getEventsForBusStop(
                busStop, timezone.now())
            busStops.append(data)

        return busStops