Пример #1
0
	def get_pending_approvals_for_subscriber(self, user_id, page_no=INITIAL_PAGE_NO, no_of_records=NUMBER_OF_RECORDS_PER_PAGE, show_all=False, **kwargs):
		auth_group = UserGroupManager.check_user_group(self.user)
		logged_in_employee = Subscriber.objects.get(user=self.user)
		subscriber_to_get = Subscriber.objects.get(user__id__exact=user_id)
		is_company_admin = UserGroupManager.is_company_admin(self.user)
		if not (logged_in_employee.id == subscriber_to_get.id and UserGroupManager.can_approve_leave(self.user)):
			if not is_company_admin:
				return self.respond(is_saved=False, auth_errors='Permission Denied')

		if is_company_admin:
			leaves = Leave.objects.filter(status__exact=LeaveStatus.PENDING)
		else:
			leaves = Leave.objects.filter(
				approver=subscriber_to_get,
				status__exact=LeaveStatus.PENDING
			)
		pending_approvals = self._prepare_search(leaves.order_by('-created_on'), **kwargs)

		show_all = bool(int(show_all))
		prevPageNo = 0
		nextPageNo = 0
		current_page_number = 0
		num_of_pages = pending_approvals.count()
		if not show_all:
			paginator = Paginator(pending_approvals, no_of_records)
			try:
				pending_approvals = paginator.page(page_no)
			except PageNotAnInteger:
				# If page is not an integer, deliver first page.
				pending_approvals = paginator.page(1)
			except EmptyPage:
				# If page is out of range (e.g. 9999), deliver last page of results.
				pending_approvals = paginator.page(paginator.num_pages)

			try:
				prevPageNo = pending_approvals.previous_page_number()
			except InvalidPage as e:
				prevPageNo = page_no
			try:
				nextPageNo = pending_approvals.next_page_number()
			except InvalidPage as e:
				nextPageNo = page_no
			current_page_number = pending_approvals.number
			num_of_pages = pending_approvals.paginator.num_pages

		serialized_objects = self.get(serializers.serialize(
			'json',
			pending_approvals,
			indent=4,
			extras=['period'],
			relations=['requester', 'approver', 'type_of_leave']
		))
		print serialized_objects

		return self.respond(
			pending_approvals=serialized_objects,
			previous_page_number=prevPageNo,
			next_page_number=nextPageNo,
			current_page_number=current_page_number,
			num_of_pages=num_of_pages,
			no_of_records=no_of_records,
			subscriber=subscriber_to_get.serialize(maxDepth=1)
		)