コード例 #1
0
ファイル: plots.py プロジェクト: jjmata/otm-core
def plots_closest_to_point(request, instance, lat, lng):
    point = Point(float(lng), float(lat), srid=4326)

    try:
        max_plots = int(request.GET.get('max_plots', '1'))

        if max_plots not in xrange(1, 501):
            raise ValueError()
    except ValueError:
        raise HttpBadRequestException(
            'The max_plots parameter must be a number between 1 and 500')

    try:
        distance = float(request.GET.get('distance',
                                         settings.MAP_CLICK_RADIUS))
    except ValueError:
        raise HttpBadRequestException(
            'The distance parameter must be a number')

    plots = Plot.objects.distance(point)\
                        .filter(instance=instance)\
                        .filter(geom__distance_lte=(point, D(m=distance)))\
                        .order_by('distance')[0:max_plots]

    def ctxt_for_plot(plot):
        return context_dict_for_plot(request, plot)

    return [ctxt_for_plot(plot) for plot in plots]
コード例 #2
0
def instances_closest_to_point(request, lat, lng):
    """
    Get all the info we need about instances near a given point
    Includes only public instances the user does not belong to.
    If a user has been specified instances that user belongs to will
    also be included in a separate list.

    Unlike instance_info, this does not return the field permissions for the
    instance
    """
    user = request.user
    user_instance_ids = []
    if user and not user.is_anonymous():
        user_instance_ids = InstanceUser.objects.filter(user=user)\
                                        .values_list('instance_id', flat=True)\
                                        .distinct()

    point = Point(float(lng), float(lat), srid=4326)

    try:
        max_instances = int(request.GET.get('max', '10'))

        if not (1 <= max_instances <= 500):
            raise ValueError()
    except ValueError:
        raise HttpBadRequestException(
            'The max parameter must be a number between 1 and 500')

    try:
        distance = float(
            request.GET.get('distance', settings.NEARBY_INSTANCE_RADIUS))
    except ValueError:
        raise HttpBadRequestException(
            'The distance parameter must be a number')

    instances = Instance.objects \
                        .filter(get_viewable_instances_filter())
    personal_predicate = Q(pk__in=user_instance_ids)

    def get_annotated_contexts(instances):
        results = []
        for instance in instances:
            instance_info = _instance_info_dict(instance)
            d = D(m=point.distance(instance.bounds.geom)).km
            instance_info['distance'] = d
            results.append(instance_info)
        return sorted(results, key=itemgetter('distance'))

    return {
        'nearby':
        get_annotated_contexts(
            instances.filter(is_public=True).filter(
                bounds__geom__distance_lte=(point, D(m=distance))).exclude(
                    personal_predicate)[0:max_instances]),
        'personal':
        get_annotated_contexts(instances.filter(personal_predicate))
    }
コード例 #3
0
    def wrapper(request, version, *args, **kwargs):
        api_version = int(version)
        if api_version not in API_VERSIONS:
            raise HttpBadRequestException("Version %s is not supported" %
                                          version)

        request.api_version = api_version
        return view_f(request, *args, **kwargs)
コード例 #4
0
ファイル: views.py プロジェクト: lorenanicole/OTM2
def reset_password(request):
    email = request.REQUEST["email"]
    try:
        User.objects.get(email=email)
    except User.DoesNotExist:
        return {"status": "failure", "message": "Email address not found."}

    resetform = PasswordResetForm({"email": email})
    if (resetform.is_valid()):
        opts = {
            'use_https': request.is_secure(),
            'token_generator': default_token_generator,
            'from_email': None,
            'email_template_name': 'registration/password_reset_email.html',
            'request': request}

        resetform.save(**opts)
        return {"status": "success"}
    else:
        raise HttpBadRequestException()
コード例 #5
0
def polygon_for_point(request, instance):
    lng = request.GET['lng']
    lat = request.GET['lat']
    point = Point(float(lng), float(lat), srid=4326)

    try:
        distance = float(request.GET.get('distance',
                                         settings.MAP_CLICK_RADIUS))
    except ValueError:
        raise HttpBadRequestException(
            'The distance parameter must be a number')

    features = PolygonalMapFeature.objects.distance(point)\
        .filter(instance=instance)\
        .filter(polygon__distance_lte=(point, D(m=distance)))\
        .order_by('distance')[0:1]

    if len(features) > 0:
        # This is currently only being used to complement UTF grid plot data,
        # so the structure of the response is designed to mirror the utf grid
        return {'data': {'id': features[0].pk}}

    return {'data': None}
コード例 #6
0
ファイル: tests.py プロジェクト: cwardcode/django-tinsel
def raise_bad_request_view(request):
    raise HttpBadRequestException("raised HttpBadRequestException")