def test_timeify_filter(self): """Test the timeify filter """ self.assertEqual(timeify_filter(timestamp()), 'Less than a second ago') # Check one year ago time_yearago = timestamp() - 31536000 self.assertEqual(timeify_filter(time_yearago), '1 year ago') # Check two months ago time_yearago = timestamp() - 5184000 self.assertEqual(timeify_filter(time_yearago), '2 months ago') # Check 3 weeks ago time_yearago = timestamp() - 1814400 self.assertEqual(timeify_filter(time_yearago), '3 weeks ago')
def get_stats(): """Provides post level statistics. """ total_posts = m.db.posts.count() total_uploads = m.db.posts.find( {'upload': {'$exists': True}}).count() last_post = m.db.posts.find().sort( [('created', pymongo.DESCENDING)]).limit(1) try: last_post_time = timeify_filter(last_post[0].get('created')) except IndexError: last_post_time = None # Get the 10 highest flagged posts if there is any flagged_posts_cur = m.db.posts.find( {'flags': {'$gt': 0}}).sort('flags', pymongo.DESCENDING).limit(10) flagged_posts = [] for flagged_post in flagged_posts_cur: s = '{0} - <a href="{1}">{2}</a>: <a href="{3}">{4}</a>' + \ ' [<a href="{5}">Unflag</a>]' if flagged_post.get('reply_to') is not None: reply_to = get_post(flagged_post.get('reply_to')) username = reply_to.get('username') post_id = reply_to.get('_id') else: username = flagged_post.get('username') post_id = flagged_post.get('_id') link = s.format( flagged_post.get('flags'), url_for('users.profile', username=flagged_post.get('username')), flagged_post.get('username'), url_for('posts.view_post', username=username, post_id=post_id), flagged_post.get('_id'), url_for('posts.unflag_post', post_id=flagged_post.get('_id')) ) # Highligh comments if flagged_post.get('reply_to') is not None: link = link + ' (comment)' flagged_posts.append(link) if len(flagged_posts) == 0: flagged_posts.append('Empty') return [ ('Total posts', total_posts), ('Total uploads', total_uploads), ('Last post', last_post_time), ('Flagged posts', flagged_posts) ]
def get_stats(): """Provides post level statistics. """ total_posts = m.db.posts.count() total_uploads = m.db.posts.find({"upload": {"$exists": True}}).count() last_post = m.db.posts.find().sort([("created", pymongo.DESCENDING)]).limit(1) try: last_post_time = timeify_filter(last_post[0].get("created")) except IndexError: last_post_time = None # Get the 10 highest flagged posts if there is any flagged_posts_cur = m.db.posts.find({"flags": {"$gt": 0}}).sort("flags", pymongo.DESCENDING).limit(10) flagged_posts = [] for flagged_post in flagged_posts_cur: s = '{0} - <a href="{1}">{2}</a>: <a href="{3}">{4}</a>' + ' [<a href="{5}">Unflag</a>]' if flagged_post.get("reply_to") is not None: reply_to = get_post(flagged_post.get("reply_to")) username = reply_to.get("username") post_id = reply_to.get("_id") else: username = flagged_post.get("username") post_id = flagged_post.get("_id") link = s.format( flagged_post.get("flags"), url_for("users.profile", username=flagged_post.get("username")), flagged_post.get("username"), url_for("posts.view_post", username=username, post_id=post_id), flagged_post.get("_id"), url_for("posts.unflag_post", post_id=flagged_post.get("_id")), ) # Highligh comments if flagged_post.get("reply_to") is not None: link = link + " (comment)" flagged_posts.append(link) if len(flagged_posts) == 0: flagged_posts.append("Empty") return [ ("Total posts", total_posts), ("Total uploads", total_uploads), ("Last post", last_post_time), ("Flagged posts", flagged_posts), ]
def test_template_filters(self): """ Small tests for the template filters. There is only a couple which are not tested by the rest of the unit tests. We will just test these by calling them directly not through the site. """ # Test timeify with an invalid value (can't be converted to int) time_str = timeify_filter("None") self.assertEqual(time_str, 'Err') # Test timeify with an invaid type time_str = timeify_filter(None) self.assertEqual(time_str, 'Err') # Test that it works correctly # With an int time_str = timeify_filter(1412271814) self.assertNotEqual(time_str, 'Err') # With a string time_str = timeify_filter('1412271814') self.assertNotEqual(time_str, 'Err') # Test millify # Check incorrect type num_str = millify_filter(None) self.assertEqual(num_str, 'Err') # Check value's that can't be turned in to ints # Str num_str = millify_filter("None") self.assertEqual(num_str, 'Err') # Check it does actually work # Positive num_str = millify_filter(1000) self.assertEqual(num_str, "1K") # Negative num_str = millify_filter(-1000) self.assertEqual(num_str, "-1K")
def get_stats(): """Provides post level statistics. """ total_posts = m.db.posts.count() total_uploads = m.db.posts.find( {'upload': {'$exists': True}}).count() last_post = m.db.posts.find().sort( [('created', pymongo.DESCENDING)]).limit(1) try: last_post_time = timeify_filter(last_post[0].get('created')) except IndexError: last_post_time = None return [ ('Total posts', total_posts), ('Total uploads', total_uploads), ('Last post', last_post_time), ]