def test_paginate(self): # this dictionary will serve as template context context = {} # prepare test request request = HttpRequest() # check PageNotAnInteger case: ab string request.GET['page'] = 'ab' result = paginate([1, 2, 3, 4], 3, request, context, var_name='object_list') self.assertEqual(len(result['object_list']), 3) self.assertEqual(result['is_paginated'], True) self.assertEqual(len(result['page_obj']), 3) self.assertEqual(result['object_list'][0], 1) self.assertEqual(result['object_list'][1], 2) self.assertEqual(result['object_list'][2], 3) # check empty page case: should be last page request.GET['page'] = '9999' result = paginate([1, 2, 3, 4], 3, request, context, var_name='object_list') self.assertEqual(len(result['object_list']), 1) self.assertEqual(result['object_list'][0], 4) # check valid page: second page request.GET['page'] = '2' result = paginate([1, 2, 3, 4, 5], 3, request, context, var_name='my_list') self.assertEqual(len(result['my_list']), 2) self.assertEqual(result['my_list'][0], 4) self.assertEqual(result['my_list'][1], 5)
def test_paginate(self): request = HttpRequest() objects = Student.objects.all() # try without page in request context = paginate(objects, 3, request, {}, var_name='students') self.assertTrue(isinstance(context, dict)) self.assertEqual(len(context.get('students')), 3) self.assertEqual(context.get('is_paginated'), True) self.assertTrue(context.has_key('page_obj')) self.assertTrue(context.has_key('paginator')) # try with page=2 in request request.GET.update({'page':'2'}) context = paginate(objects, 3, request, {}, var_name='students') self.assertTrue(isinstance(context, dict)) self.assertEqual(len(context.get('students')), 1) self.assertEqual(context.get('is_paginated'), True) self.assertTrue(context.has_key('page_obj')) self.assertTrue(context.has_key('paginator')) # try with page='x' - not an integer request.GET.update({'page':'x'}) context = paginate(objects, 3, request, {}, var_name='students') self.assertTrue(isinstance(context, dict)) self.assertEqual(len(context.get('students')), 3) self.assertEqual(context.get('is_paginated'), True) self.assertTrue(context.has_key('page_obj')) self.assertTrue(context.has_key('paginator'))
def test_paginate(self): # prepare list of students for function objects = Student.objects.all() # prepare our clean request request = HttpRequest() # test with clean page attribute paginate1 = paginate(objects, 3, request, {}) self.assertEqual(len(paginate1['page_obj']), 3) self.assertEqual(paginate1['page_obj'].number, 1) # test with page attribute 999 request = HttpRequest() request.GET.__setitem__('page', '999') paginate1 = paginate(objects, 3, request, {}) self.assertEqual(len(paginate1['page_obj']), 2) self.assertEqual(paginate1['page_obj'].number, 2) # test with page attribute not integer request = HttpRequest() request.GET.__setitem__('page', 'robusta') paginate1 = paginate(objects, 3, request, {}) self.assertEqual(len(paginate1['page_obj']), 3) self.assertEqual(paginate1['page_obj'].number, 1) # test with page attribute 2 request = HttpRequest() request.GET.__setitem__('page', '2') paginate1 = paginate(objects, 3, request, {}) self.assertEqual(len(paginate1['page_obj']), 2) self.assertEqual(paginate1['page_obj'].number, 2)
def test_paginate(self): request = HttpRequest() request.GET._mutable = True request.GET['page']=2 objects = Group.objects.all() size = 2 context = {} var_name = 'list' self.assertEqual(1,len(paginate(objects,size,request,context,var_name)['list'])) size = 3 self.assertEqual(False,paginate(objects,size,request,context,var_name)['is_paginated'])
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) current_group = get_current_group(self.request) if current_group: groups = Group.objects.filter(title=current_group) else: groups = Group.objects.all() # try to order groups list order_by = self.request.GET.get('order_by', '') if order_by in ('title', 'leader', 'id'): if order_by == 'leader': order_by = 'leader__first_name' groups = groups.order_by(order_by) if self.request.GET.get('reverse', '') == '1': groups = groups.reverse() else: groups = groups.order_by('title') context = paginate(groups, 10, self.request, context, var_name='groups') return context
def users_list(request): users = User.objects.all() #apply pagination, 10 users per page context = paginate(users, 10, request, {}, var_name = 'users') return render(request, 'registration/users_list.html', context)
def test_paginate(self): self.request.GET['page'] = 'test' context = paginate([1, 2, 3], 2, self.request, {}) self.assertEqual(len(context['object_list']), 2) self.assertEqual(len(context['page_obj']), 2) self.assertEqual(context['is_paginated'], True) self.assertEqual(context['object_list'][0], 1) self.assertEqual(context['object_list'][1], 2) self.request.GET['page'] = '999' context = paginate([1, 2, 3], 2, self.request, {}) self.assertEqual(len(context['object_list']), 1) self.assertEqual(len(context['page_obj']), 1) self.assertEqual(context['is_paginated'], True) self.assertEqual(context['object_list'][0], 3) self.request.GET['page'] = '2' context = paginate([1, 2, 3], 2, self.request, {}) self.assertEqual(len(context['object_list']), 1) self.assertEqual(len(context['page_obj']), 1) self.assertEqual(context['is_paginated'], True) self.assertEqual(context['object_list'][0], 3)
def test_pagenav(self): # prepare list of students for function objects = Student.objects.all() # prepare our clean request request = HttpRequest() # prepare data for templatetag paginate_data = paginate(objects, 3, request, {}) out = Template( "{% load pagenav %}" "{% pagenav context.page_obj context.is_paginated context.paginator %}" ).render(Context({'context': paginate_data})) # check our tag for obvious content self.assertIn('<!-- Pagination -->', out) self.assertIn('>1</a>\n', out) self.assertIn('<nav aria-label="Page navigation">', out) self.assertIn('<span aria-hidden="true">»</span>', out)
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) current_group = get_current_group(self.request) if current_group: exams = Exam.objects.filter(exams_group=current_group) else: exams = Exam.objects.all() # try to order exams list order_by = self.request.GET.get('order_by', '') if order_by in ('exams_name', 'exams_date', 'professor', 'id'): exams = exams.order_by(order_by) if self.request.GET.get('reverse', '') == '1': exams = exams.reverse() else: exams = exams.order_by('exams_date') # paginate exams context = paginate(exams, 10, self.request, context, var_name='exams') return context
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) results = Result.objects.all() # try to order results list order_by = self.request.GET.get('order_by', '') if order_by in ('result', 'id'): results = results.order_by(order_by) if self.request.GET.get('reverse', '') == '1': results = results.reverse() else: results = results.order_by('result') # paginate result context = paginate(results, 10, self.request, context, var_name='results') return context
def students_list(request): # check if we need to show only one group of students current_group = get_current_group(request) if current_group: students = Student.objects.filter(student_group=current_group) else: # otherwise show all students students = Student.objects.all() # try to order students list order_by = request.GET.get('order_by', '') if order_by in ('last_name', 'first_name', 'ticket'): students = students.order_by(order_by) if request.GET.get('reverse', '') == '1': students = students.reverse() # apply pagination, 3 students per page context = paginate(students, 8, request, {}, var_name='students') return render(request, 'students/students_list.html', context)
def test_paginate(self): request = HttpRequest() paginator = Paginator request.GET['page'] = 2 size = 1 objects = Group.objects.all() dict_context = {} paginator = paginator(objects, size) page = paginator.page(2) context = paginate(objects, size, request, dict_context) # self.assertTrue(page == context['object_list']) self.assertTrue(context['is_paginated']) # self.assertEqual(page, context['page_obj']) # self.assertEqual(paginator, context['paginator']) self.assertEqual(paginator.object_list, context['paginator'].object_list) self.assertRaises(EmptyPage, paginator.page, 1212) self.assertRaises(PageNotAnInteger, paginator.page, "number")
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) current_group = get_current_group(self.request) if current_group: students = Student.objects.filter(student_group=current_group) else: students = Student.objects.all() # try to order students list order_by = self.request.GET.get('order_by', '') if order_by in ('last_name', 'first_name', 'ticket', 'id'): students = students.order_by(order_by) if self.request.GET.get('reverse', '') == '1': students = students.reverse() else: students = students.order_by('last_name') context = paginate(students, 10, self.request, context, var_name='students') return context
def get_context_data(self, **kwargs): # get context data from TemplateView class context = super(JournalView, self).get_context_data(**kwargs) # check if we need to display some specific month if self.request.GET.get('month'): month = datetime.strptime(self.request.GET['month'], '%Y-%m-%d' ).date() else: # otherwise just displaying current month data today = datetime.today() month = date(today.year, today.month, 1) # calculate current, previous and next month details; # we need this for month navigation element in template next_month = month + relativedelta(months=1) prev_month = month - relativedelta(months=1) context['prev_month'] = prev_month.strftime('%Y-%m-%d') context['next_month'] = next_month.strftime('%Y-%m-%d') context['year'] = month.year context['month_verbose'] = month.strftime('%B') # we'll use this variable in students pagination context['cur_month'] = month.strftime('%Y-%m-%d') # prepare variable for template to generate # journal table header elements myear, mmonth = month.year, month.month number_of_days = monthrange(myear, mmonth)[1] context['month_header'] = [{'day': d, 'verbose': day_abbr[weekday(myear, mmonth, d)][:2]} for d in range(1, number_of_days+1)] # get all students from database, or just one if we need to # display journal for one student; also check if we need to # filter by group current_group = get_current_group(self.request) if current_group: queryset = Student.objects.filter(student_group=current_group ).order_by('last_name') elif kwargs.get('pk'): queryset = [Student.objects.get(pk=kwargs['pk'])] else: queryset = Student.objects.all().order_by('last_name') # url to update student presence, for form post update_url = reverse('journal') # go over all students and collect data about presence # during selected month students = [] for student in queryset: # try to get journal object by month selected # month and current student try: journal = MonthJournal.objects.get(student=student, date=month) except Exception: journal = None # fill in days presence list for current student days = [] for day in range(1, number_of_days+1): days.append({ 'day': day, 'present': journal and getattr(journal, 'present_day%d' % day, False) or False, 'date': date(myear, mmonth, day).strftime( '%Y-%m-%d'), }) # prepare metadata for current student students.append({ 'fullname': u'%s %s' % (student.last_name, student.first_name), 'days': days, 'id': student.id, 'update_url': update_url, }) # apply pagination, 10 students per page context = paginate(students, 10, self.request, context, var_name='students') # finally return updated context # with paginated students return context