示例#1
0
def unfavorite(request, slug):
    from statmap.models import FavoriteDataSet
    """
        Unfavorite is called using JS and removes the
        given DataSet to the users Favorites list.

        The response will be a JSON object

        {
            'status': 'success|error',
            'message': 'textual repr. of error',
        }

        or a 404 if the data_set does not exist.

        If the user hasnt favorited this element just
        fail silently
    """
    if not request.user.is_authenticated():
        json_response = { 'status': 'error', 'message': 'User is not signed in'}
    else:
        data_set = get_data_set(request.user, slug, allow_empty=False)

        FavoriteDataSet.objects.filter(dataset=data_set,
                                       user=request.user)\
                               .delete()
        json_response = { 'status': 'success', 'message': ''}


    response = HttpResponse()
    json.dump(json_response, response)
    return response
示例#2
0
def favorite(request, slug):
    """
        Favorite is called using JS and add the
        given DataSet to the users Favorites list.

        The response will be a JSON object

        {
            'status': 'success|error',
            'message': 'textual repr. of error',
        }

        or a 404 if the data_set does not exist
    """
    if not request.user.is_authenticated():
        json_response = { 'status': 'error', 'message': 'You are not signed in'}
    else:
        data_set = get_data_set(request.user, slug, allow_empty=False)

        FavoriteDataSet.objects.get_or_create(dataset=data_set,
                                              user=request.user)

        # User cannot favorite twice, so no need to update the
        # time record

        json_response = { 'status': 'success', 'message': ''}


    response = HttpResponse()
    json.dump(json_response, response)
    return response
示例#3
0
def data_set(request, region_set_slug, data_set_slug):
    """
        Returns a JSON file with some keys for the Meta
        data and a list with key/value pairs.

        .. code-block:: js
            {
              values:
                citySlug: value,
                citySlug: value,
                citySlug: value,
                citySlug: value,
                ...,
              ],
              meta: {
                title: value,
                dataset: value,
                public: value,
                description: value,
                author: value,
                zoom: value,
                lat: value,
                lng: value,
                region: value
              }
            };


        `empty.json` returns a list with all values set to "" and
        `slug` set to 'empty'.
    """
    data_set = get_data_set(request.user, data_set_slug, region_set_slug)

    values = {}
    for k,v in data_set.data_values.by_region(data_set.regionset).items():
        values[k] = v.value

    data_set_return = \
    {
        'values': values,
        'meta':
        {
            'title': data_set.title,
            'dataset': data_set.slug,
            'public': data_set.public,
            'description': data_set.description,
            #'author': unicode(data_set.author),
            'zoom': data_set.zoom,
            'latitude': data_set.latitude,
            'longitude': data_set.longitude,
            'regionset': region_set_slug
        }
    }

    response = HttpResponse(mimetype='application/json')
    json.dump(data_set_return, response, indent=4)    # .. todo :: remove indent
    return response
示例#4
0
def embed(request, slug):
    """
      Select the dataset requested. If the user is not authenticated,
      choose only from the public maps. Otherwise also search the
      users own maps

      .. TODO ::
        Wout: what is a user opens his own map but is not authenticated.
        For now the user gets presented a 404, but maybe its better
        to tell him the map exists but the user has not got
        permission to view it.

    """
    data_set = get_data_set(request.user, slug, allow_empty=False)

    return {'map': data_set}
示例#5
0
def download(request, region_set_slug, data_set_slug, file_type):
    """
        Returns the data_values of a `DataSet` in the give format.
        
        Currently we provide the following formats:
        
        * xls
        * csv
    """
    data_set = get_data_set(request.user, data_set_slug, region_set_slug)
    
    if file_type=='xls':
        return download_xls(data_set)
    elif file_type=='csv':
        return download_csv(data_set)
    else: # Unknown filetype
        raise Http404
示例#6
0
def mapdetail(request, slug):
    """
      Select the dataset requested. If the user is not authenticated,
      choose only from the public maps. Otherwise also search the
      users own maps

      .. TODO ::
        Wout: what is a user opens his own map but is not authenticated.
        For now the user gets presented a 404, but maybe its better
        to tell him the map exists but the user has not got
        permission to view it.

    """
    data_set = get_data_set(request.user, slug, allow_empty=False)
    data_set.update_recently_viewed_by(request.user)

    favorited_by_user = data_set.is_favorited_by(request.user)


    return {'map': data_set,
            'favorited_by_user': favorited_by_user}