Beispiel #1
0
def test_get_land_number():
    # 120.1074406, 23.2353021
    # 臺南市北門區 溪底寮段三寮灣小段 (5404) 1681地號
    result = easymap.get_land_number(120.1074406, 23.2353021, proxies=None)
    assert result["towncode"] == "D24"
    assert result["townname"] == "臺南市北門區"
    assert result["sectno"] == "5404"
    assert result["sectName"] == "溪底寮段三寮灣小段"
    assert result["landno"] == "1681"
Beispiel #2
0
    def save_model(self, request, obj, form, change):
        landinfo = easymap.get_land_number(obj.lng, obj.lat)
        landcode = landinfo.get('landno')

        obj.landcode = landcode
        obj.sectno = landinfo.get('sectno')
        obj.sectname = landinfo.get('sectName')
        obj.towncode = landinfo.get('towncode')
        obj.townname = landinfo.get('townname')

        super().save_model(request, obj, form, change)
Beispiel #3
0
def update_landcode_with_custom_factory_model(factory_id, factory_model):
    factory = factory_model.objects.get(pk=factory_id)
    landinfo = easymap.get_land_number(factory.lng, factory.lat)
    landcode = landinfo.get("landno")

    LOGGER.info(f"Factory {factory_id} retrieved land number {landcode}")
    factory_model.objects.filter(pk=factory_id).update(
        landcode=landcode,
        sectcode=landinfo.get("sectno"),
        sectname=landinfo.get("sectName"),
        towncode=landinfo.get("towncode"),
        townname=landinfo.get("townname"),
    )
Beispiel #4
0
    def save_model(self, request, obj, form, change):
        try:
            landinfo = easymap.get_land_number(obj.lng, obj.lat)
            landcode = landinfo.get("landno")

            obj.landcode = landcode
            obj.sectno = landinfo.get("sectno")
            obj.sectname = landinfo.get("sectName")
            obj.towncode = landinfo.get("towncode")
            obj.townname = landinfo.get("townname")
        except Exception as e:
            LOGGER.error("Can't get landcode from easymap")
            LOGGER.error(e)

        super().save_model(request, obj, form, change)
Beispiel #5
0
def update_landcode(factory_id):
    factory = Factory.objects.get(pk=factory_id)
    landinfo = easymap.get_land_number(factory.lng, factory.lat)
    landcode = landinfo.get('landno')
    sectname = landinfo.get('sectName')
    sectcode = landinfo.get('sectno')
    townname = landinfo.get('townname')
    towncode = landinfo.get('towncode')
    LOGGER.info(f"Factory {factory_id} retrieved land number {landcode}")
    Factory.objects.filter(pk=factory_id).update(
        landcode=landcode,
        sectcode=sectcode,
        sectname=sectname,
        towncode=towncode,
        townname=townname,
    )
Beispiel #6
0
def update_landcode(factory_id):
    factory = Factory.objects.get(pk=factory_id)
    land_number = easymap.get_land_number(factory.lng, factory.lat)['landno']
    LOGGER.info(f"Factory {factory_id} retrieved land number {land_number}")
    Factory.objects.filter(pk=factory_id).update(landcode=land_number)
Beispiel #7
0
def get_nearby_or_create_factories(request):
    # TODO
    if request.method == "GET":
        try:
            latitude = request.GET["lat"]  # 緯度: y
            longitude = request.GET["lng"]  # 經度: x
            radius = request.GET["range"]  # km
        except MultiValueDictKeyError:
            missing_params = [
                p
                for p in ("lat", "lng", "range")
                if p not in request.GET
            ]
            missing_params = ", ".join(missing_params)
            return HttpResponse(
                f"Missing query parameter: {missing_params}.",
                status=400,
            )

        latitude = float(latitude)
        longitude = float(longitude)
        if _not_in_taiwan(latitude, longitude):
            return HttpResponse(
                "The query position is not in the range of Taiwan."
                "Valid query parameters should be: "
                "{settings.TAIWAN_MIN_LONGITUDE} < lng < {settings.TAIWAN_MAX_LONGITUDE}, "
                "{settings.TAIWAN_MIN_LATITUDE} < lat < {settings.TAIWAN_MAX_LATITUDE}.",
                status=400,
            )

        radius = float(radius)
        if _radius_strange(radius):
            return HttpResponse(
                f"`range` should be within 0.01 to 100 km, but got {radius}",
                status=400,
            )

        nearby_factories = _get_nearby_factories(
            latitude=latitude,
            longitude=longitude,
            radius=radius,
        )

        serializer = FactorySerializer(nearby_factories, many=True)
        return JsonResponse(serializer.data, safe=False)
    elif request.method == "POST":
        post_body = json.loads(request.body)
        # print(post_body)
        serializer = FactorySerializer(data=post_body)

        if not serializer.is_valid():
            return JsonResponse(
                serializer.errors,
                status=400,
            )
        longitude = post_body['lng']
        latitude = post_body['lat']
        image_ids = post_body.get('images', [])

        if not _all_image_id_exist(image_ids):
            return HttpResponse(
                "please check if every image id exist",
                status=400,
            )

        try:
            land_number = easymap.get_land_number(longitude, latitude)['landno']
        except Exception:
            return HttpResponse(
                "Something wrong happened when getting land number, please try later.",
                status=400,
            )
        user_ip = _get_client_ip(request)
        new_factory_field = {
            'name': post_body["name"],
            'lat': post_body["lat"],
            'lng': post_body["lng"],
            'factory_type': post_body["type"],
            'status_time': datetime.datetime.now(),
            'landcode': land_number,
        }
        new_report_record_field = {
            'user_ip': user_ip,
            'action_type': "POST",
            "action_body": post_body,
            'nickname': post_body.get("nickname"),
            'contact': post_body.get("contact"),
            "others": post_body.get("others", ""),
        }

        with transaction.atomic():
            new_factory = Factory.objects.create(**new_factory_field)
            report_record = ReportRecord.objects.create(
                factory=new_factory,
                **new_report_record_field,
            )
            Image.objects.filter(id__in=image_ids).update(
                factory=new_factory,
                report_record=report_record
            )
        serializer = FactorySerializer(new_factory)
        return JsonResponse(serializer.data, safe=False)