def _list_designs(querydict, page_size, prefix=""): """ _list_designs(querydict, page_size, prefix, user) -> (page, filter_param) A helper to gather the designs page. It understands all the GET params in ``list_designs``, by reading keys from the ``querydict`` with the given ``prefix``. If a ``user`` is specified, only the saved queries of this user will be returned. This has priority over the ``user`` in the ``querydict`` parameter. """ DEFAULT_SORT = ("-", "date") # Descending date SORT_ATTR_TRANSLATION = dict(date="mtime", name="name", desc="desc", type="type") # Filtering. Only display designs explicitly saved. db_queryset = models.SavedQuery.objects.filter(is_auto=False) user = querydict.get(prefix + "user") if user is not None: db_queryset = db_queryset.filter(owner__username=user) # Design type d_type = querydict.get(prefix + "type") if d_type: d_type = str(d_type) if d_type not in SavedQuery.TYPES_MAPPING.keys(): LOG.warn("Bad parameter to list_designs: type=%s" % (d_type,)) else: db_queryset = db_queryset.filter(type=SavedQuery.TYPES_MAPPING[d_type]) # Text search frag = querydict.get(prefix + "text") if frag: db_queryset = db_queryset.filter(Q(name__icontains=frag) | Q(desc__icontains=frag)) # Ordering sort_key = querydict.get(prefix + "sort") if sort_key: if sort_key[0] == "-": sort_dir, sort_attr = "-", sort_key[1:] else: sort_dir, sort_attr = "", sort_key if not SORT_ATTR_TRANSLATION.has_key(sort_attr): LOG.warn("Bad parameter to list_designs: sort=%s" % (sort_key,)) sort_dir, sort_attr = DEFAULT_SORT else: sort_dir, sort_attr = DEFAULT_SORT db_queryset = db_queryset.order_by(sort_dir + SORT_ATTR_TRANSLATION[sort_attr]) pagenum = int(querydict.get(prefix + "page", 1)) paginator = Paginator(db_queryset, page_size) page = paginator.page(pagenum) # We need to pass the parameters back to the template to generate links keys_to_copy = [prefix + key for key in ("user", "type", "sort")] filter_params = copy_query_dict(querydict, keys_to_copy) return page, filter_params
def tasks(request, jobid): """ We get here from /jobs/jobid/tasks?filterargs, with the options being: page=<n> - Controls pagination. Defaults to 1. tasktype=<type> - Type can be one of hadoop.job_tracker.VALID_TASK_TYPES ("map", "reduce", "job_cleanup", "job_setup") taskstate=<state> - State can be one of hadoop.job_tracker.VALID_TASK_STATES ("succeeded", "failed", "running", "pending", "killed") tasktext=<text> - Where <text> is a string matching info on the task """ # Get the filter parameters ttypes = request.GET.get("tasktype") tstates = request.GET.get("taskstate") ttext = request.GET.get("tasktext") task_types = None if ttypes: task_types = set(ttypes.split(",")) task_states = None if tstates: task_states = set(tstates.split(",")) pagenum = int(request.GET.get("page", 1)) if pagenum < 0: pagenum = 1 # Fetch the list of tasks task_list = TaskList.select( request.jt, jobid, task_types, task_states, ttext, __DEFAULT_OBJ_PER_PAGINATION, __DEFAULT_OBJ_PER_PAGINATION * (pagenum - 1), ) paginator = Paginator(task_list, __DEFAULT_OBJ_PER_PAGINATION, total=task_list.numTotalTasks) page = paginator.page(pagenum) # We need to pass the parameters back to the template to generate links filter_params = copy_query_dict(request.GET, ("tasktype", "taskstate", "tasktext")).urlencode() return render( "tasks.mako", request, { "request": request, "filter_params": filter_params, "jobid": jobid, "jobid_short": get_shorter_id(jobid), "page": page, "tasktype": ttypes, "taskstate": tstates, "tasktext": ttext, }, )
def tasks(request, jobid): """ We get here from /jobs/jobid/tasks?filterargs, with the options being: page=<n> - Controls pagination. Defaults to 1. tasktype=<type> - Type can be one of hadoop.job_tracker.VALID_TASK_TYPES ("map", "reduce", "job_cleanup", "job_setup") taskstate=<state> - State can be one of hadoop.job_tracker.VALID_TASK_STATES ("succeeded", "failed", "running", "pending", "killed") tasktext=<text> - Where <text> is a string matching info on the task """ # Get the filter parameters ttypes = request.GET.get('tasktype') tstates = request.GET.get('taskstate') ttext = request.GET.get('tasktext') task_types = None if ttypes: task_types = set(ttypes.split(',')) task_states = None if tstates: task_states = set(tstates.split(',')) pagenum = int(request.GET.get('page', 1)) if pagenum < 0: pagenum = 1 # Fetch the list of tasks task_list = TaskList.select(request.jt, jobid, task_types, task_states, ttext, __DEFAULT_OBJ_PER_PAGINATION, __DEFAULT_OBJ_PER_PAGINATION * (pagenum - 1)) paginator = Paginator(task_list, __DEFAULT_OBJ_PER_PAGINATION, total=task_list.numTotalTasks) page = paginator.page(pagenum) # We need to pass the parameters back to the template to generate links filter_params = copy_query_dict( request.GET, ('tasktype', 'taskstate', 'tasktext')).urlencode() return render( "tasks.mako", request, { 'request': request, 'filter_params': filter_params, 'jobid': jobid, 'page': page, 'tasktype': ttypes, 'taskstate': tstates, 'tasktext': ttext })
def test_paginator(): """ Test that the paginator works with partial list. """ def assert_page(page, data, start, end): assert_equal(page.object_list, data) assert_equal(page.start_index(), start) assert_equal(page.end_index(), end) # First page 1-20 obj = range(20) pgn = Paginator(obj, per_page=20, total=25) assert_page(pgn.page(1), obj, 1, 20) # Second page 21-25 obj = range(5) pgn = Paginator(obj, per_page=20, total=25) assert_page(pgn.page(2), obj, 21, 25) # Handle extra data on first page (22 items on a 20-page) obj = range(22) pgn = Paginator(obj, per_page=20, total=25) assert_page(pgn.page(1), range(20), 1, 20) # Handle extra data on second page (22 items on a 20-page) obj = range(22) pgn = Paginator(obj, per_page=20, total=25) assert_page(pgn.page(2), range(5), 21, 25) # Handle total < len(obj). Only works for QuerySet. obj = query.QuerySet() obj._result_cache = range(10) pgn = Paginator(obj, per_page=10, total=9) assert_page(pgn.page(1), range(10), 1, 10) # Still works with a normal complete list obj = range(25) pgn = Paginator(obj, per_page=20) assert_page(pgn.page(1), range(20), 1, 20) assert_page(pgn.page(2), range(20, 25), 21, 25)
def paginate_task(self, task_list, pagenum): paginator = Paginator(task_list, _DEFAULT_OBJ_PER_PAGINATION) return paginator.page(pagenum)
def _list_query_history(user, querydict, page_size, prefix=""): """ _list_query_history(user, querydict, page_size, prefix) -> (page, filter_param) A helper to gather the history page. It understands all the GET params in ``list_query_history``, by reading keys from the ``querydict`` with the given ``prefix``. """ DEFAULT_SORT = ('-', 'date') # Descending date VALID_TYPES = ('hql') # Design types SORT_ATTR_TRANSLATION = dict( date='submission_date', state='last_state', name='design__name', type='design__type', ) db_queryset = models.QueryHistory.objects.select_related() # Filtering # # Queries without designs are the ones we submitted on behalf of the user, # (e.g. view table data). Exclude those when returning query history. if not querydict.get(prefix + 'auto_query', False): db_queryset = db_queryset.filter(design__isnull=False) username = user.username user_filter = querydict.get(prefix + 'user', user.username) if user_filter != ':all': db_queryset = db_queryset.filter(owner__username=user_filter) # Design id design_id = querydict.get(prefix + 'design_id') if design_id: db_queryset = db_queryset.filter(design__id=int(design_id)) # Design type d_type = querydict.get(prefix + 'type') if d_type: if d_type not in VALID_TYPES: LOG.warn('Bad parameter to list_query_history: type=%s' % (d_type,)) else: if d_type == 'hql': d_type = models.SavedQuery.HQL db_queryset = db_queryset.filter(design__type=d_type) # Ordering sort_key = querydict.get(prefix + 'sort') if sort_key: sort_dir, sort_attr = '', sort_key if sort_key[0] == '-': sort_dir, sort_attr = '-', sort_key[1:] if not SORT_ATTR_TRANSLATION.has_key(sort_attr): LOG.warn('Bad parameter to list_query_history: sort=%s' % (sort_key,)) sort_dir, sort_attr = DEFAULT_SORT else: sort_dir, sort_attr = DEFAULT_SORT db_queryset = db_queryset.order_by(sort_dir + SORT_ATTR_TRANSLATION[sort_attr]) # Get the total return count before slicing total_count = db_queryset.count() # Slicing (must be the last filter applied) pagenum = int(querydict.get(prefix + 'page', 1)) if pagenum < 1: pagenum = 1 db_queryset = db_queryset[ page_size * (pagenum - 1) : page_size * pagenum ] paginator = Paginator(db_queryset, page_size, total=total_count) page = paginator.page(pagenum) # We do slicing ourselves, rather than letting the Paginator handle it, in order to # update the last_state on the running queries for history in page.object_list: _update_query_state(history.get_full_object()) # We need to pass the parameters back to the template to generate links keys_to_copy = [ prefix + key for key in ('user', 'type', 'sort', 'design_id', 'auto_query') ] filter_params = copy_query_dict(querydict, keys_to_copy) return page, filter_params
def _list_designs(querydict, page_size, prefix="", user=None): """ _list_designs(querydict, page_size, prefix, user) -> (page, filter_param) A helper to gather the designs page. It understands all the GET params in ``list_designs``, by reading keys from the ``querydict`` with the given ``prefix``. If a ``user`` is specified, only the saved queries of this user will be returned. This has priority over the ``user`` in the ``querydict`` parameter. """ DEFAULT_SORT = ('-', 'date') # Descending date VALID_TYPES = ('hql') # Design types SORT_ATTR_TRANSLATION = dict( date='mtime', name='name', desc='desc', type='type', ) # Filtering. Only display designs explicitly saved. db_queryset = models.SavedQuery.objects.filter(is_auto=False) if user is None: user = querydict.get(prefix + 'user') if user is not None: db_queryset = db_queryset.filter(owner__username=user) # Design type d_type = querydict.get(prefix + 'type') if d_type: d_type = str(d_type) if d_type not in VALID_TYPES: LOG.warn('Bad parameter to list_designs: type=%s' % (d_type,)) else: if d_type == 'hql': d_type = models.SavedQuery.HQL else: d_type = models.SavedQuery.REPORT db_queryset = db_queryset.filter(type=d_type) # Text search frag = querydict.get(prefix + 'text') if frag: db_queryset = db_queryset.filter(Q(name__icontains=frag) | Q(desc__icontains=frag)) # Ordering sort_key = querydict.get(prefix + 'sort') if sort_key: if sort_key[0] == '-': sort_dir, sort_attr = '-', sort_key[1:] else: sort_dir, sort_attr = '', sort_key if not SORT_ATTR_TRANSLATION.has_key(sort_attr): LOG.warn('Bad parameter to list_designs: sort=%s' % (sort_key,)) sort_dir, sort_attr = DEFAULT_SORT else: sort_dir, sort_attr = DEFAULT_SORT db_queryset = db_queryset.order_by(sort_dir + SORT_ATTR_TRANSLATION[sort_attr]) pagenum = int(querydict.get(prefix + 'page', 1)) paginator = Paginator(db_queryset, page_size) page = paginator.page(pagenum) # We need to pass the parameters back to the template to generate links keys_to_copy = [ prefix + key for key in ('user', 'type', 'sort') ] filter_params = copy_query_dict(querydict, keys_to_copy) return page, filter_params
def _list_designs(user, querydict, page_size, prefix="", is_trashed=False): """ _list_designs(user, querydict, page_size, prefix, is_trashed) -> (page, filter_param) A helper to gather the designs page. It understands all the GET params in ``list_designs``, by reading keys from the ``querydict`` with the given ``prefix``. """ DEFAULT_SORT = ('-', 'date') # Descending date SORT_ATTR_TRANSLATION = dict( date='last_modified', name='name', desc='description', type='extra', ) # Trash and security if is_trashed: db_queryset = Document.objects.trashed_docs(SavedQuery, user) else: db_queryset = Document.objects.available_docs(SavedQuery, user) # Filter by user filter_username = querydict.get(prefix + 'user') if filter_username: try: db_queryset = db_queryset.filter(owner=User.objects.get(username=filter_username)) except User.DoesNotExist: # Don't care if a bad filter term is provided pass # Design type d_type = querydict.get(prefix + 'type') if d_type and d_type in SavedQuery.TYPES_MAPPING.keys(): db_queryset = db_queryset.filter(extra=str(SavedQuery.TYPES_MAPPING[d_type])) # Text search frag = querydict.get(prefix + 'text') if frag: db_queryset = db_queryset.filter(Q(name__icontains=frag) | Q(description__icontains=frag)) # Ordering sort_key = querydict.get(prefix + 'sort') if sort_key: if sort_key[0] == '-': sort_dir, sort_attr = '-', sort_key[1:] else: sort_dir, sort_attr = '', sort_key if not SORT_ATTR_TRANSLATION.has_key(sort_attr): LOG.warn('Bad parameter to list_designs: sort=%s' % (sort_key,)) sort_dir, sort_attr = DEFAULT_SORT else: sort_dir, sort_attr = DEFAULT_SORT db_queryset = db_queryset.order_by(sort_dir + SORT_ATTR_TRANSLATION[sort_attr]) designs = [job.content_object for job in db_queryset.all() if job.content_object and job.content_object.is_auto == False] pagenum = int(querydict.get(prefix + 'page', 1)) paginator = Paginator(designs, page_size) page = paginator.page(pagenum) # We need to pass the parameters back to the template to generate links keys_to_copy = [ prefix + key for key in ('user', 'type', 'sort') ] filter_params = copy_query_dict(querydict, keys_to_copy) return page, filter_params
def _list_query_history(user, querydict, page_size, prefix=""): """ _list_query_history(user, querydict, page_size, prefix) -> (page, filter_param) A helper to gather the history page. It understands all the GET params in ``list_query_history``, by reading keys from the ``querydict`` with the given ``prefix``. """ DEFAULT_SORT = ('-', 'date') # Descending date SORT_ATTR_TRANSLATION = dict( date='submission_date', state='last_state', name='design__name', type='design__type', ) db_queryset = models.QueryHistory.objects.select_related() # Filtering # # Queries without designs are the ones we submitted on behalf of the user, # (e.g. view table data). Exclude those when returning query history. if querydict.get(prefix + 'auto_query', 'on') != 'on': db_queryset = db_queryset.exclude(design__isnull=False, design__is_auto=True) user_filter = querydict.get(prefix + 'user', user.username) if user_filter != ':all': db_queryset = db_queryset.filter(owner__username=user_filter) # Design id design_id = querydict.get(prefix + 'design_id') if design_id: db_queryset = db_queryset.filter(design__id=int(design_id)) # Search search_filter = querydict.get(prefix + 'search') if search_filter: db_queryset = db_queryset.filter(Q(design__name__icontains=search_filter) | Q(query__icontains=search_filter) | Q(owner__username__icontains=search_filter)) # Design type d_type = querydict.get(prefix + 'type') if d_type: if d_type not in SavedQuery.TYPES_MAPPING.keys(): LOG.warn('Bad parameter to list_query_history: type=%s' % (d_type,)) else: db_queryset = db_queryset.filter(design__type=SavedQuery.TYPES_MAPPING[d_type]) # Ordering sort_key = querydict.get(prefix + 'sort') if sort_key: sort_dir, sort_attr = '', sort_key if sort_key[0] == '-': sort_dir, sort_attr = '-', sort_key[1:] if not SORT_ATTR_TRANSLATION.has_key(sort_attr): LOG.warn('Bad parameter to list_query_history: sort=%s' % (sort_key,)) sort_dir, sort_attr = DEFAULT_SORT else: sort_dir, sort_attr = DEFAULT_SORT db_queryset = db_queryset.order_by(sort_dir + SORT_ATTR_TRANSLATION[sort_attr], '-id') # Get the total return count before slicing total_count = db_queryset.count() # Slicing (must be the last filter applied) pagenum = int(querydict.get(prefix + 'page', 1)) if pagenum < 1: pagenum = 1 db_queryset = db_queryset[ page_size * (pagenum - 1) : page_size * pagenum ] paginator = Paginator(db_queryset, page_size, total=total_count) page = paginator.page(pagenum) # We do slicing ourselves, rather than letting the Paginator handle it, in order to # update the last_state on the running queries for history in page.object_list: _update_query_state(history.get_full_object()) # We need to pass the parameters back to the template to generate links keys_to_copy = [ prefix + key for key in ('user', 'type', 'sort', 'design_id', 'auto_query', 'search') ] filter_params = copy_query_dict(querydict, keys_to_copy) return page, filter_params
def _list_query_history(user, querydict, page_size, prefix=""): """ _list_query_history(user, querydict, page_size, prefix) -> (page, filter_param) A helper to gather the history page. It understands all the GET params in ``list_query_history``, by reading keys from the ``querydict`` with the given ``prefix``. """ DEFAULT_SORT = ("-", "date") # Descending date SORT_ATTR_TRANSLATION = dict(date="submission_date", state="last_state", name="design__name", type="design__type") db_queryset = models.QueryHistory.objects.select_related() # Filtering # # Queries without designs are the ones we submitted on behalf of the user, # (e.g. view table data). Exclude those when returning query history. if querydict.get(prefix + "auto_query", "on") != "on": db_queryset = db_queryset.exclude(design__isnull=False, design__is_auto=True) user_filter = querydict.get(prefix + "user", user.username) if user_filter != ":all": db_queryset = db_queryset.filter(owner__username=user_filter) # Design id design_id = querydict.get(prefix + "design_id") if design_id: db_queryset = db_queryset.filter(design__id=int(design_id)) # Search search_filter = querydict.get(prefix + "search") if search_filter: db_queryset = db_queryset.filter( Q(design__name__icontains=search_filter) | Q(query__icontains=search_filter) | Q(owner__username__icontains=search_filter) ) # Design type d_type = querydict.get(prefix + "type") if d_type: if d_type not in SavedQuery.TYPES_MAPPING.keys(): LOG.warn("Bad parameter to list_query_history: type=%s" % (d_type,)) else: db_queryset = db_queryset.filter(design__type=SavedQuery.TYPES_MAPPING[d_type]) # If recent query recent = querydict.get("recent") if recent: db_queryset = db_queryset.filter(is_cleared=False) # Ordering sort_key = querydict.get(prefix + "sort") if sort_key: sort_dir, sort_attr = "", sort_key if sort_key[0] == "-": sort_dir, sort_attr = "-", sort_key[1:] if not SORT_ATTR_TRANSLATION.has_key(sort_attr): LOG.warn("Bad parameter to list_query_history: sort=%s" % (sort_key,)) sort_dir, sort_attr = DEFAULT_SORT else: sort_dir, sort_attr = DEFAULT_SORT db_queryset = db_queryset.order_by(sort_dir + SORT_ATTR_TRANSLATION[sort_attr], "-id") # Get the total return count before slicing total_count = db_queryset.count() # Slicing (must be the last filter applied) pagenum = int(querydict.get(prefix + "page", 1)) if pagenum < 1: pagenum = 1 db_queryset = db_queryset[page_size * (pagenum - 1) : page_size * pagenum] paginator = Paginator(db_queryset, page_size, total=total_count) page = paginator.page(pagenum) # We do slicing ourselves, rather than letting the Paginator handle it, in order to # update the last_state on the running queries for history in page.object_list: _update_query_state(history.get_full_object()) # We need to pass the parameters back to the template to generate links keys_to_copy = [prefix + key for key in ("user", "type", "sort", "design_id", "auto_query", "search")] filter_params = copy_query_dict(querydict, keys_to_copy) return page, filter_params
def _list_designs(querydict, page_size, prefix="", is_trashed=False): """ _list_designs(querydict, page_size, prefix, user) -> (page, filter_param) A helper to gather the designs page. It understands all the GET params in ``list_designs``, by reading keys from the ``querydict`` with the given ``prefix``. If a ``user`` is specified, only the saved queries of this user will be returned. This has priority over the ``user`` in the ``querydict`` parameter. """ DEFAULT_SORT = ('-', 'date') # Descending date SORT_ATTR_TRANSLATION = dict( date='mtime', name='name', desc='desc', type='type', ) # Filtering. Only display designs explicitly saved. db_queryset = models.SavedQuery.objects.filter(is_auto=False, is_trashed=is_trashed) user = querydict.get(prefix + 'user') if user is not None: db_queryset = db_queryset.filter(owner__username=user) # Design type d_type = querydict.get(prefix + 'type') if d_type: d_type = str(d_type) if d_type not in SavedQuery.TYPES_MAPPING.keys(): LOG.warn('Bad parameter to list_designs: type=%s' % (d_type,)) else: db_queryset = db_queryset.filter(type=SavedQuery.TYPES_MAPPING[d_type]) # Text search frag = querydict.get(prefix + 'text') if frag: db_queryset = db_queryset.filter(Q(name__icontains=frag) | Q(desc__icontains=frag)) # Ordering sort_key = querydict.get(prefix + 'sort') if sort_key: if sort_key[0] == '-': sort_dir, sort_attr = '-', sort_key[1:] else: sort_dir, sort_attr = '', sort_key if not SORT_ATTR_TRANSLATION.has_key(sort_attr): LOG.warn('Bad parameter to list_designs: sort=%s' % (sort_key,)) sort_dir, sort_attr = DEFAULT_SORT else: sort_dir, sort_attr = DEFAULT_SORT db_queryset = db_queryset.order_by(sort_dir + SORT_ATTR_TRANSLATION[sort_attr]) pagenum = int(querydict.get(prefix + 'page', 1)) paginator = Paginator(db_queryset, page_size) page = paginator.page(pagenum) # We need to pass the parameters back to the template to generate links keys_to_copy = [ prefix + key for key in ('user', 'type', 'sort') ] filter_params = copy_query_dict(querydict, keys_to_copy) return page, filter_params
def _list_query_history(user, querydict, page_size, prefix=""): """ _list_query_history(user, querydict, page_size, prefix) -> (page, filter_param) A helper to gather the history page. It understands all the GET params in ``list_query_history``, by reading keys from the ``querydict`` with the given ``prefix``. """ DEFAULT_SORT = ('-', 'date') # Descending date SORT_ATTR_TRANSLATION = dict( date='submission_date', state='last_state', name='design__name', type='design__type', ) db_queryset = models.QueryHistory.objects.select_related() # Filtering # # Queries without designs are the ones we submitted on behalf of the user, # (e.g. view table data). Exclude those when returning query history. if querydict.get(prefix + 'auto_query', 'on') != 'on': db_queryset = db_queryset.exclude(design__isnull=False, design__is_auto=True) user_filter = querydict.get(prefix + 'user', user.username) if user_filter != ':all': db_queryset = db_queryset.filter(owner__username=user_filter) # Design id design_id = querydict.get(prefix + 'design_id') if design_id: if design_id.isdigit(): db_queryset = db_queryset.filter(design__id=int(design_id)) else: raise PopupException(_('list_query_history requires design_id parameter to be an integer: %s') % design_id) # Search search_filter = querydict.get(prefix + 'search') if search_filter: db_queryset = db_queryset.filter(Q(design__name__icontains=search_filter) | Q(query__icontains=search_filter) | Q(owner__username__icontains=search_filter)) # Design type d_type = querydict.get(prefix + 'type') if d_type: if d_type not in SavedQuery.TYPES_MAPPING.keys(): LOG.warn('Bad parameter to list_query_history: type=%s' % (d_type,)) else: sort_dir, sort_attr = DEFAULT_SORT db_queryset = db_queryset.order_by(sort_dir + SORT_ATTR_TRANSLATION[sort_attr], '-id') # Get the total return count before slicing total_count = db_queryset.count() # Slicing (must be the last filter applied) pagenum = int(querydict.get(prefix + 'page', 1)) if pagenum < 1: pagenum = 1 db_queryset = db_queryset[page_size * (pagenum - 1): page_size * pagenum] paginator = Paginator(db_queryset, page_size, total=total_count) page = paginator.page(pagenum) # We do slicing ourselves, rather than letting the Paginator handle it, in order to # update the last_state on the running queries for history in page.object_list: _update_query_state(history.get_full_object()) # We need to pass the parameters back to the template to generate links keys_to_copy = [prefix + key for key in ('user', 'type', 'sort', 'design_id', 'auto_query', 'search')] filter_params = copy_query_dict(querydict, keys_to_copy) return page, filter_params