Exemplo n.º 1
0
def get_parking_information_and_show_partners_around(request):
    try:
        radius = 0.6
        radius_for_branches = 0.4

        lat = float(request.POST["lat"])
        lng = float(request.POST["lng"])

        reports = []
        time_now = datetime.datetime.now()
        before_one_hour = time_now - timedelta(hours=3)
        for x in range(0, 2):
            reports = [
                report for report in ParkingReport.objects.filter(
                    datetime__range=(before_one_hour, time_now))
                if distance([report.lat, report.lng], [lat, lng]) < radius
            ]
            if not reports:
                radius = radius + 0.3
            else:
                break

        partners_branches_around = [
            branch for branch in Branch.objects.all() if distance(
                [branch.lat, branch.lng], [lat, lng]) < radius_for_branches
        ]

        reports_and_branches = list(chain(reports, partners_branches_around))
        data = serializers.serialize("json", reports_and_branches)
        return HttpResponse(data,
                            content_type="application/json; charset=utf-8")
    except Exception:
        send_error_email(inspect.stack()[0][3])
        return HttpResponse("Error occured",
                            content_type="text/html; charset=utf-8")
Exemplo n.º 2
0
def get_stickers(request):
    try:
        lat = float(request.POST["lat"])
        lng = float(request.POST["lng"])
        encoded = int(request.POST["encoded"])

        if encoded == 0:
            dist_and_stickers = [{
                distance([sticker.lat, sticker.lng], [lat, lng]):
                sticker
            } for sticker in Sticker.objects.all() if distance(
                [sticker.lat, sticker.lng], [lat, lng]) < sticker.radius]
            stickers_dict = {}
            for i in dist_and_stickers:
                stickers_dict.update(i)

            selected_stickers = []
            for key in sorted(stickers_dict.iterkeys()):
                selected_stickers.append(stickers_dict[key])

            if not selected_stickers:
                return HttpResponse("No stickers around",
                                    content_type="text/html; charset=utf-8")

            data = serializers.serialize("json", selected_stickers)
            return HttpResponse(data,
                                content_type="application/json; charset=utf-8")
        else:
            dist_and_stickers = [{
                distance([sticker.lat, sticker.lng], [lat, lng]):
                sticker
            } for sticker in Sticker.objects.all() if distance(
                [sticker.lat, sticker.lng], [lat, lng]) < sticker.radius]
            stickers_dict = {}
            for i in dist_and_stickers:
                stickers_dict.update(i)

            selected_stickers = []
            for key in sorted(stickers_dict.iterkeys()):
                selected_stickers.append(stickers_dict[key])

            if not selected_stickers:
                return HttpResponse("No stickers around",
                                    content_type="text/html; charset=utf-8")

            stickers_encoded_urls = []
            for sticker in selected_stickers:
                stickers_encoded_urls.append(
                    (open(settings.MEDIA_ROOT + str(sticker.image),
                          "rb").read()).encode("base64"))

            #data = serializers.serialize("json", selected_stickers)
            data = simplejson.dumps(stickers_encoded_urls)
            return HttpResponse(data, mimetype='application/json')
    except Exception:
        send_error_email(inspect.stack()[0][3])
        return HttpResponse("Error occured",
                            content_type="text/html; charset=utf-8")
Exemplo n.º 3
0
def get_partner_images(request):
    try:
        partners = Partner.objects.all()
        app_parameters = ApplicationParameters.objects.all()
        partners_and_parameters = list(chain(partners, app_parameters))
        data = serializers.serialize("json", partners_and_parameters)
        return HttpResponse(data,
                            content_type="application/json; charset=utf-8")
    except Exception:
        send_error_email(inspect.stack()[0][3])
        return HttpResponse("Error occured",
                            content_type="text/html; charset=utf-8")
Exemplo n.º 4
0
def receive_email(request):
    try:
        topic = request.POST["topic"]
        message = request.POST["message"]
        email = MobileEmail.objects.create(topic=topic, message=message)
        email.save()
        return HttpResponse("Email successful",
                            content_type="text/html; charset=utf-8")
    except Exception:
        send_error_email(inspect.stack()[0][3])
        return HttpResponse("Error occured",
                            content_type="text/html; charset=utf-8")
Exemplo n.º 5
0
def get_stickers(request):
    try:
        lat = float(request.POST["lat"])
        lng = float(request.POST["lng"])
        encoded = int(request.POST["encoded"])
        
        if encoded == 0:
            dist_and_stickers = [{distance([sticker.lat, sticker.lng], [lat, lng]):sticker} for sticker in Sticker.objects.all()
                            if distance([sticker.lat, sticker.lng], [lat, lng]) < sticker.radius]
            stickers_dict = {}
            for i  in dist_and_stickers:
                stickers_dict.update(i)
            
            selected_stickers = []
            for key in sorted(stickers_dict.iterkeys()):
                selected_stickers.append(stickers_dict[key])
                
            if not selected_stickers:
                return HttpResponse("No stickers around", content_type="text/html; charset=utf-8")
            
            data = serializers.serialize("json", selected_stickers)
            return HttpResponse(data, content_type="application/json; charset=utf-8")
        else:
            dist_and_stickers = [{distance([sticker.lat, sticker.lng], [lat, lng]):sticker} for sticker in Sticker.objects.all()
                            if distance([sticker.lat, sticker.lng], [lat, lng]) < sticker.radius]
            stickers_dict = {}
            for i  in dist_and_stickers:
                stickers_dict.update(i)
            
            selected_stickers = []
            for key in sorted(stickers_dict.iterkeys()):
                selected_stickers.append(stickers_dict[key])
                
            if not selected_stickers:
                return HttpResponse("No stickers around", content_type="text/html; charset=utf-8")
            
            stickers_encoded_urls = []
            for sticker in selected_stickers:
                stickers_encoded_urls.append((open(settings.MEDIA_ROOT + str(sticker.image), "rb").read()).encode("base64"))
            
            #data = serializers.serialize("json", selected_stickers)
            data = simplejson.dumps(stickers_encoded_urls)
            return HttpResponse(data, mimetype='application/json')
    except Exception:
        send_error_email(inspect.stack()[0][3])
        return HttpResponse("Error occured", content_type="text/html; charset=utf-8")
Exemplo n.º 6
0
def show_recent_reports(request):
    try:
        start_minutes = int(request.POST["start_minutes"])
        end_minutes = int(request.POST["end_minutes"])

        start_time = datetime.datetime.now() - timedelta(minutes=start_minutes)
        end_time = datetime.datetime.now() - timedelta(minutes=end_minutes)

        reports = ParkingReport.objects.filter(datetime__range=(start_time,
                                                                end_time))

        data = serializers.serialize("json", reports)
        return HttpResponse(data,
                            content_type="application/json; charset=utf-8")
    except Exception:
        send_error_email(inspect.stack()[0][3])
        return HttpResponse("Error occured",
                            content_type="text/html; charset=utf-8")
Exemplo n.º 7
0
def report_parking_availability(request):
    try:
        #line = str(request.POST["lat"])+str(request.POST["lng"])+str(request.POST["availability"])
        #send_log_message(line)
        lat = float(request.POST["lat"])
        lng = float(request.POST["lng"])
        availability = int(request.POST["availability"])
        time = datetime.datetime.now().replace(tzinfo=None)

        report = ParkingReport.objects.create(lat=lat,
                                              lng=lng,
                                              availability=availability,
                                              datetime=time)
        report.save()
        return HttpResponse("Report successful",
                            content_type="text/html; charset=utf-8")
    except Exception:
        send_error_email(inspect.stack()[0][3])
        return HttpResponse("Error occured",
                            content_type="text/html; charset=utf-8")
Exemplo n.º 8
0
def get_parking_information_around_nearest_selected_partner(request):
    try:
        radius = 0.6

        lat = float(request.POST["lat"])
        lng = float(request.POST["lng"])
        branch_key = int(request.POST["branch_key"])

        partner_branches = Branch.objects.filter(branch_key=branch_key)
        distances_to_partner_branches = [(distance([branch.lat, branch.lng],
                                                   [lat, lng]), branch)
                                         for branch in partner_branches]
        # sort tuples and get first one value which is the closest branch object
        closest_branch = sorted(distances_to_partner_branches)[0][1]

        reports_around_closest_branch = []
        time_now = datetime.datetime.now()
        before_one_hour = time_now - timedelta(hours=3)
        for x in range(0, 2):
            reports_around_closest_branch = [
                report for report in ParkingReport.objects.filter(
                    datetime__range=(before_one_hour, time_now))
                if distance([report.lat, report.lng],
                            [closest_branch.lat, closest_branch.lng]) < radius
            ]
            if not reports_around_closest_branch:
                radius = radius + 0.3
            else:
                break

        closest_branch_and_reports_around_it = list(
            chain(reports_around_closest_branch, [closest_branch]))
        data = serializers.serialize("json",
                                     closest_branch_and_reports_around_it)
        return HttpResponse(data,
                            content_type="application/json; charset=utf-8")
    except Exception:
        send_error_email(inspect.stack()[0][3])
        return HttpResponse("Error occured",
                            content_type="text/html; charset=utf-8")
Exemplo n.º 9
0
def add_scheduled_reports(request):
    try:
        minutes_from_now = int(request.POST["minutes_from_now"])
        lat = float(request.POST["lat"])
        lng = float(request.POST["lng"])
        availability = int(request.POST["availability"])

        scheduled_time = (datetime.datetime.now() -
                          timedelta(minutes=minutes_from_now)).replace(
                              tzinfo=None)

        report = ParkingReport.objects.create(lat=lat,
                                              lng=lng,
                                              availability=availability,
                                              datetime=scheduled_time)
        report.save()
        return HttpResponse("Report successful",
                            content_type="text/html; charset=utf-8")
    except Exception:
        send_error_email(inspect.stack()[0][3])
        return HttpResponse("Error occured",
                            content_type="text/html; charset=utf-8")