Exemple #1
0
def repo_viz( request, username, repo_name ):
    '''
        View repo <repo_name> under user <username>.

        Does the checks necessary to determine whether the current user has the
        authority to view the current repository.
    '''

    # Grab the user/organization based on the username
    account = user_or_organization( username )
    if account is None:
        return HttpResponse( status=404 )

    # Grab the repository
    repo = Repository.objects.get_by_username( repo_name, username )
    if repo is None:
        return HttpResponse( status=404 )

    # Grab all the permissions!
    permissions = []
    if request.user.is_authenticated():
        permissions = get_perms( request.user, repo )
        for org in request.user.organization_users.all():
            permissions.extend( get_perms( org, repo ) )

    # Check to see if the user has access to view this survey
    if not repo.is_public and 'view_repository' not in permissions:
        return HttpResponse( 'Unauthorized', status=401 )

    # Grab the data for this repository
    data = db.data.find( { 'repo': ObjectId( repo.mongo_id ) },
                         { 'survey_label': False,
                           'user': False } )\
                  .sort( [ ('timestamp', pymongo.DESCENDING ) ] )

    data = dehydrate_survey( data )

    # Is some unknown user looking at this data?
    # TODO: Make the privatizer take into account
    # geospatial location
    # if 'view_raw' not in permissions:
    #     data = privatize_geo( repo, data )

    usePerms = get_users_with_perms( repo, attach_perms=True )
    usePerms.pop( account, None )

    if isinstance( account, User ):
        account_name = account.username
    else:
        account_name = account.name

    return render_to_response( 'visualize.html',
                               { 'repo': repo,
                                 'sid': repo.mongo_id,
                                 'data': json.dumps( data ),
                                 'permissions': permissions,
                                 'account': account,
                                 'account_name': account_name,
                                 'users_perms': usePerms },
                               context_instance=RequestContext(request) )
Exemple #2
0
    def get_list( self, request, **kwargs ):

        startWith = request.GET['term__istartswith']
        startRegex = re.compile( ('^' + startWith), re.IGNORECASE)
        cursor = db.vocab.find( { 'term': startRegex } )\
                         .limit( 10 )\
                         .sort( 'term', pymongo.ASCENDING )
        return self.create_response( request, dehydrate_survey( cursor ) )
Exemple #3
0
    def get_list(self, request, **kwargs):

        startWith = request.GET['term__istartswith']
        startRegex = re.compile(('^' + startWith), re.IGNORECASE)
        cursor = db.vocab.find( { 'term': startRegex } )\
                         .limit( 10 )\
                         .sort( 'term', pymongo.ASCENDING )
        return self.create_response(request, dehydrate_survey(cursor))
Exemple #4
0
    def get_detail( self, request, **kwargs ):

        # Grab the survey that we're querying survey data for
        repo_id = kwargs[ 'pk' ]

        try:
            basic_bundle = self.build_bundle( request=request )
            repo = Repository.objects.get( mongo_id=repo_id )

            if not self.authorized_read_detail( repo, basic_bundle ):
                return HttpUnauthorized()

            query_parameters = self._build_filters( request )
            query_parameters['repo'] = ObjectId( repo_id )

            # Query the database for the data
            cursor = db.data.find( query_parameters )

            if 'sort' in request.GET:
                sort_parameter = 'data.%s' % ( request.GET['sort'] )
                sort_type = pymongo.DESCENDING
                if 'sort_type' in request.GET:
                    if request.GET['sort_type'] == 'ascending':
                        sort_type = pymongo.ASCENDING
                cursor = cursor.sort( sort_parameter, sort_type )

            offset = max( int( request.GET.get( 'offset', 0 ) ), 0 )

            limit = 50
            if request.GET.get( 'format', None ) == 'csv':
                limit = cursor.count()

            meta = {
                'form_name': repo.name,
                'fields': repo.flatten_fields(),
                'limit': limit,
                'offset': offset,
                'count': cursor.count(),
                'pages': cursor.count() / limit
            }

            data = {
                'meta': meta,
                'data': dehydrate_survey( cursor.skip( offset * limit ).limit( limit ) ) }

            return self.create_response( request, data )
        except ValueError as e:
            return HttpBadRequest( e )