Beispiel #1
0
def fetch_page_of_provider_prospects(cursor_key=None,
                                     page_size=50,
                                     search_keyword=None):
    ''' 
        Returns three values: prospects, next_curs, prev_curs
        
        Search uses a workaround to imitate LIKE:
        http://stackoverflow.com/questions/47786/google-app-engine-is-it-possible-to-do-a-gql-like-query
    
    '''
    cursor = Cursor(urlsafe=cursor_key)
    if search_keyword:
        limit = search_keyword + u"\ufffd"
        query = ProviderProspect.query(
            ProviderProspect.last_name >= search_keyword,
            ProviderProspect.last_name < limit)

        query = query.order(ProviderProspect.last_name)
    else:
        query = ProviderProspect.query()
    # forward and back
    forward_query = query.order(ProviderProspect.category,
                                ProviderProspect.last_name,
                                ProviderProspect.key)
    backward_query = query.order(-ProviderProspect.category,
                                 -ProviderProspect.last_name,
                                 -ProviderProspect.key)
    # fetch next
    prospects, next_curs, more = forward_query.fetch_page(page_size,
                                                          start_cursor=cursor)
    logging.info('next_curs: %s  more: %s' % (next_curs, more))
    # fetch prev (just to get cursor position)
    if cursor_key:
        reversed_cursor = cursor.reversed()
        prev_prospects, prev_curs, prev_more = backward_query.fetch_page(
            page_size, start_cursor=reversed_cursor)
        # reverse the cursor (GAE docs is wrong on this)
        if prev_curs:
            prev_curs = prev_curs.reversed()
    else:
        prev_curs = None
        prev_more = None

    if not more:
        next_curs = None

    #if not prev_more:
    #    prev_curs = None
    return prospects, next_curs, prev_curs
Beispiel #2
0
    def get(self):
        try:
            cursor = Cursor.from_websafe_string(self.request.get('cursor'))
        except BadValueError:
            cursor = None

        try:
            limit = int(self.request.get('limit'))
            if limit <= 0:
                raise ValueError
        except (TypeError, ValueError):
            limit = 100

        query = Poll.query().order(-Poll.created)
        polls, next_cursor, has_more = query.fetch_page(limit,
                                                        start_cursor=cursor)

        for poll in polls:
            self.response.write(poll.render_html() + '\n\n<hr>\n\n')

        if not has_more:
            return

        more_url = '?cursor={}&limit={}'.format(
            next_cursor.to_websafe_string(), limit)
        self.response.write('<p><a href="{}">More</a></p>'.format(more_url))
def create_matches_for_user(app_user, group_id, cursor=None):
    logging.debug('debugging_news create_matches_for_user user:%s group:%s', app_user, group_id)

    user_settings = NewsSettingsUser.create_key(app_user).get()

    batch_count = 50
    qry = NewsItem.list_published_by_group_id(group_id)
    start_cursor = Cursor.from_websafe_string(cursor) if cursor else None
    items, new_cursor, has_more = qry.fetch_page(batch_count, start_cursor=start_cursor)
    si_users = []
    for news_item in items:
        if news_item.sender in si_users:
            continue
        si_users.append(news_item.sender)

    to_put = []
    for news_item in items:
        m = _create_news_item_match(user_settings, news_item)
        if not m:
            continue
        to_put.append(m)
    if to_put:
        put_in_chunks(to_put, is_ndb=True)

    if has_more and new_cursor:
        new_cursor_str = new_cursor.to_websafe_string().decode('utf-8')
        deferred.defer(create_matches_for_user, app_user, group_id, new_cursor_str, _queue=NEWS_MATCHING_QUEUE)
Beispiel #4
0
def fetch_page_of_provider_prospects(cursor_key=None, page_size=50, search_keyword=None):
    ''' 
        Returns three values: prospects, next_curs, prev_curs
        
        Search uses a workaround to imitate LIKE:
        http://stackoverflow.com/questions/47786/google-app-engine-is-it-possible-to-do-a-gql-like-query
    
    '''
    cursor = Cursor(urlsafe=cursor_key)
    if search_keyword:
        limit = search_keyword + u"\ufffd"
        query = ProviderProspect.query(ProviderProspect.last_name >= search_keyword, ProviderProspect.last_name < limit)
        
        query = query.order(ProviderProspect.last_name)
    else:
        query = ProviderProspect.query()
    # forward and back
    forward_query = query.order(ProviderProspect.category, ProviderProspect.last_name, ProviderProspect.key)
    backward_query = query.order(-ProviderProspect.category, -ProviderProspect.last_name, -ProviderProspect.key)
    # fetch next
    prospects, next_curs, more = forward_query.fetch_page(page_size, start_cursor=cursor)
    logging.info('next_curs: %s  more: %s' % (next_curs, more))
    # fetch prev (just to get cursor position)
    if cursor_key:
        reversed_cursor = cursor.reversed()
        prev_prospects, prev_curs, prev_more = backward_query.fetch_page(page_size, start_cursor=reversed_cursor)
        # reverse the cursor (GAE docs is wrong on this)
        if prev_curs:
            prev_curs = prev_curs.reversed()
    else:
        prev_curs = None
        prev_more = None
    
    if not more:
        next_curs = None

    #if not prev_more:
    #    prev_curs = None
    return prospects, next_curs, prev_curs
    def get_context(self, **kwargs):
        """
        Get the context for this view.
        """
        query = kwargs.pop('query')
        if not self.page_size:
            context = {'list': query}
        else:
            cursor = self.request.GET.get('cursor')
            offset = self.request.GET.get('offset')
            if cursor:
                cursor = Cursor(urlsafe=cursor)
                # TODO: 05.06.12 wooyek named tyiple seems to be empty in template, ivestigate
#            paginator = namedtuple('paginator', 'list cursor more')
            list, cursor, more = query.fetch_page(self.page_size, start_cursor=cursor)
            if cursor:
                cursor = cursor.urlsafe()
            offset = int(offset) + self.page_size if offset else 1
            context = {
                'list': list,
                'paginator': {'cursor': cursor, 'more': more, 'offset': offset},
            }
        context.update(kwargs)
        return context
def _get_jobs(qry, cursor, language):
    fetch_size = 10
    items, new_cursor, has_more = qry.fetch_page(fetch_size, start_cursor=Cursor.from_websafe_string(cursor) if cursor else None, keys_only=True)

    result = GetJobsResponseTO()
    result.has_criteria = True
    result.items = []
    for m in ndb.get_multi(items):
        job_offer = JobOffer.get_by_id(m.job_id)
        timestamp = get_epoch_from_datetime(m.created)
        interested = m.interested if hasattr(m, 'interested') else False
        job_offer_to = JobOfferTO.fromJobOffer(job_offer, timestamp, interested, language)
        if not job_offer_to:
            continue
        result.items.append(job_offer_to)
    result.cursor = new_cursor.to_websafe_string().decode('utf-8') if new_cursor else None
    result.has_more = has_more
    return result
Beispiel #7
0
def list_view(request, query, context={}, page_size=50, template_name=None):
    template_name = get_list_template_name(query, template_name, request.is_ajax())
    start_cursor = request.GET.get('page')
    page_size = int(request.GET.get('page_size', page_size))
    if start_cursor:
        start_cursor = Cursor.from_websafe_string(start_cursor)
    col, cursor, more = query.fetch_page(page_size, start_cursor=start_cursor)
    d = {
        'list': col,
        'page_obj': {'has_next': more, 'next_cursor': cursor.to_websafe_string() if cursor else None }
    }
    context.update(d)
    context = RequestContext(request, context)
    template = loader.get_template(template_name)
    response = HttpResponse(template.render(context))
    if request.is_ajax() and more:
        response['X-Cursor'] = cursor.to_websafe_string()
    return response
Beispiel #8
0
 def __init__(self,
              query,
              page_size=100,
              start_cursor=None,
              offset=0,
              use_cache=True,
              cache_begin=True,
              **kwargs):
     self.cache_begin = cache_begin
     self.use_cache = use_cache
     self.page_size = page_size
     self.query = query
     self.offset = offset
     self.__future = None
     self.__cached_keys = None
     self.cursor = None
     self.more = None
     if isinstance(start_cursor, basestring):
         start_cursor = Cursor(urlsafe=start_cursor)
     self.start_cursor = start_cursor
     super(ModelSearchCommand, self).__init__(**kwargs)
Beispiel #9
0
def index(_write_tmpl, pagina_origem=1, direcao_busca='FRENTE',
          cursor_urlsafe='None', pagina_destino=1):
    BUSCA_FRENTE = 'FRENTE'
    BUSCA_TRAS = 'TRAS'
    #tranformacao e calculo de parametros
    pagina_origem = int(pagina_origem)
    pagina_destino = int(pagina_destino)
    TAMANHO_DA_PAGINA = 2
    nova_direcao_busca = BUSCA_TRAS if (pagina_destino - pagina_origem) < 0 else BUSCA_FRENTE

    if cursor_urlsafe == 'None':
        cursor_urlsafe = None
    cursor = Cursor(urlsafe=cursor_urlsafe)

    # Funcao para definir se o usuario esta decrementando ou incrimentando a pagina
    def direcao_pesquisa_mudou():
        return direcao_busca != nova_direcao_busca

    #Calculo do offset de acordo com mudanca de direcao na busca
    offset = 0
    if cursor_urlsafe is not None:
        offset = abs(pagina_destino - pagina_origem)
        offset = offset if direcao_pesquisa_mudou() else max(offset - 1, 0)
        offset *= TAMANHO_DA_PAGINA

    #Funcao de pesquisa de usuarios
    def pesquisar_usuarios(query, cursor, offset):
        if direcao_pesquisa_mudou() and cursor:
            cursor = cursor.reversed()
        return query.fetch_page_async(TAMANHO_DA_PAGINA,
                                      offset=offset,
                                      start_cursor=cursor)

    #Pesquisa assincrona de usuarios de acordo com direcao da busca
    if nova_direcao_busca == BUSCA_TRAS:
        query = Usuario.query().order(-Usuario.nome)
        usuarios_future = pesquisar_usuarios(query, cursor, offset)
    else:
        query = Usuario.query().order(Usuario.nome)
        usuarios_future = pesquisar_usuarios(query, cursor, offset)

    #Setup de numeração de páginas
    pagina_inicial = max(1, pagina_destino - 2)
    pagina_final = pagina_inicial + 4
    pagina_anterior = max(pagina_inicial, pagina_destino - 1)

    #Construção de parâmetros a serem renderizados pelo template
    dct = {'adicionar_usuario_google_path': router.to_path(google),
           'adicionar_usuario_face_path': router.to_path(face),
           'pagina_atual': pagina_destino,
           'pagina_inicial': pagina_inicial,
           'pagina_final': pagina_final,
           'pagina_anterior': pagina_anterior,
           'pagina_posterior': pagina_destino + 1}

    usuarios, cursor, mais_resultados = usuarios_future.get_result()

    # revertando resultado para manter ordem crescente na visualizacao de busca
    if nova_direcao_busca == BUSCA_TRAS:
        usuarios.reverse()

    cursor_urlsafe = cursor.urlsafe() if cursor else 'None'
    dct['home_path'] = router.to_path(index,
                                      pagina_destino,
                                      nova_direcao_busca, cursor_urlsafe)
    dct['usuarios'] = usuarios
    _write_tmpl("/templates/usuario_home.html", dct)