def notifications_index(request): # get the page page = int(request.GET.get('page', 1)) # Get the total pages (rounding up, ex: 1.2 pages means 2 pages) total_pages = int(math.ceil(float(Notification.count(request.user)) / NOTIFICATIONS_PER_PAGE)) # If the page doesn't exists then 404 if page > total_pages and total_pages > 0: raise Http404 # Get the notifications offset = NOTIFICATIONS_PER_PAGE * (page - 1) limit = offset + NOTIFICATIONS_PER_PAGE notifications = Notification.find(request.user, offset, limit-1) context = { "total_pages": total_pages, "actual_page": page, "notifications": notifications } return render_to_response('notifications/notifications-index.html', context, context_instance=RequestContext(request))
def user_dashboard(request): # Notifications # Rest one beacause is the upper limit inclusive notifs = Notification.find(user=request.user, limit=NOTIFS_IN_DASHBOARD-1) # Stats total_links = UserLink.objects.filter(user=request.user).count() total_achieves = UserAchievement.objects.filter(user=request.user).count() # Latest links links_aux = UserLink.objects.filter( user=request.user).order_by('-id')[:LINKS_IN_DASHBOARD] links = [ShortLink.find(token=i.token) for i in links_aux] # Latest achievements achievements = UserAchievement.objects.filter( user=request.user).order_by('-id')[:ACHIEV_IN_DASHBOARD] context = { "notifications": notifs, "links": links, "stats": { "Total Links": total_links, "Achievements": total_achieves, "Level": request.user.profile.level.level_number, "Points": request.user.profile.points, }, "achievements": achievements } return render_to_response('userprofile/dashboard.html', context, context_instance=RequestContext(request))
def test_achievement_notification_find_all_with_limits(self): achieves = Achievement.objects.all()[:5] user = User.objects.get(id=1) r = get_redis_connection() for i in achieves: notif = AchievementNotification(achievement=i, user=user) time.sleep(1) if random.randrange(100) % 2: key = Notification.STORE_KEY_UNREAD_FORMAT.format(user.id) else: key = Notification.STORE_KEY_READ_FORMAT.format(user.id) r.zadd(key, notif.date, notif.to_json()) # Get notifications res = Notification.find(user, offset=2, limit=3, desc=False, mode=2) achieves = achieves[2:4] # 2 and 3 only a_len = len(achieves) self.assertEquals(a_len, len(res)) for i in range(len(res)): before = achieves[i] after = res[i] self.assertEquals(before.id, after.achievement_id)