def post_new_post_now(self): ''' check that things are cool now, whatever now is ''' self.login(USERNAME, USERPASS) with self.ctx(): resp = self.client.get(url_for('feedpage', feedid=self.feed.id)) self.assertEqual(resp.status_code, 200) self.assertIn(self.feed.name, resp.data) resp = self.client.post(url_for('post_new', feed_id=self.feed.id), data={"post_type":"html", "content": "{'content':'test'}", "active_start": models.now(), "active_end": models.now() + \ timedelta(minutes=1), }, follow_redirects=True) post = Post.get() post.publish(self.user) post.save() return post
def test_post_past(self): ''' check that posts in the past don't show up in the list ''' self.create_post(published=True, active_start=models.now() - timedelta(hours=2), active_end=models.now() - timedelta(minutes=1)) self.assertEqual(self.get_posts_ids([self.feed.id]), [])
def test_post_future(self): ''' test that posts in the future don't appear ''' self.create_post(published=True, active_start=models.now() + timedelta(hours=2), active_end=models.now() + timedelta(days=2)) self.assertEqual(self.get_posts_ids([self.feed.id]), [])
def external_source_run(source_id): ''' use the importer specified to see if there is any new data, and if there is, then import it. ''' try: source = ExternalSource.get(id=source_id) except ExternalSource.DoesNotExist: return 'Invalid Source', 404 time_now = now() if user_session.is_admin() and request.form.get('force', 'no') == 'yes': flash("Update forced.") else: if source.last_checked: next_check = source.last_checked + timedelta( minutes=source.frequency) if next_check > time_now: return "Nothing to do. Last: {0}, Next: {1}, Now: {2} ".format( source.last_checked, next_check, time_now) module = external_source_types.load(source.type) settings_data = json.loads(source.settings) new_posts = module.get_new(settings_data) if new_posts: for fresh_data in new_posts: post = Post(type=fresh_data.get('type', 'html'), \ author=source.post_as_user) post_type_module = post_types.load(fresh_data.get('type', 'html')) post.feed = source.feed fresh_data['active_start'] = source.current_lifetime_start() fresh_data['active_end'] = source.current_lifetime_end() post_form_intake(post, fresh_data, post_type_module) post.display_time = source.display_time if source.publish: post.publisher = source.post_as_user post.publish_date = now() post.published = True post.save() # else, no new posts! oh well! source.settings = json.dumps(settings_data) source.last_checked = now() source.save() return 'Done!'
def external_source_run(source_id): ''' use the importer specified to see if there is any new data, and if there is, then import it. ''' try: source = ExternalSource.get(id=source_id) except ExternalSource.DoesNotExist: return 'Invalid Source', 404 time_now = now() if user_session.is_admin() and request.form.get('force', 'no') == 'yes': flash("Update forced.") else: if source.last_checked: next_check = source.last_checked + timedelta(minutes=source.frequency) if next_check > time_now: return "Nothing to do. Last: {0}, Next: {1}, Now: {2} ".format( source.last_checked, next_check, time_now) module = external_source_types.load(source.type) settings_data = json.loads(source.settings) new_posts = module.get_new(settings_data) if new_posts: for fresh_data in new_posts: post = Post(type=fresh_data.get('type', 'html'), \ author=source.post_as_user) post_type_module = post_types.load(fresh_data.get('type', 'html')) post.feed = source.feed fresh_data['active_start'] = source.current_lifetime_start() fresh_data['active_end'] = source.current_lifetime_end() post_form_intake(post, fresh_data, post_type_module) post.display_time = source.display_time if source.publish: post.publisher = source.post_as_user post.publish_date = now() post.published = True post.save() # else, no new posts! oh well! source.settings = json.dumps(settings_data) source.last_checked = now() source.save() return 'Done!'
def posts_housekeeping(): ''' goes through all posts, move 'old' posts to archive status, delete reeeeealy old posts. ''' time_now = now() archive_time = time_now - \ timedelta(days=config_var('posts.archive_after_days', 7)) delete_time = time_now - \ timedelta(days=config_var('posts.delete_after_days', 30)) # first delete really old posts: delete_count = 0 archive_count = 0 if config_var('posts.delete_when_old', True): for post in Post.select().where(Post.active_end < delete_time): delete_post_and_run_callback(post, post_types.load(post.type)) delete_count += 1 # next set old-ish posts to archived: if config_var('posts.archive_when_old', True): archive_count = Post.update(status=2) \ .where((Post.active_end < archive_time) & (Post.status != 2)) \ .execute() # And done. return jsonify({"deleted": delete_count, "archived": archive_count, "delete_before": delete_time, "archive_before": archive_time, "now": time_now})
def post_form_intake(post, form, editor): ''' takes the values from 'form', passes the post contents to the editor 'receive' function, and adds all the values into the 'post' object. NOTE: this actually modifies the post it is sent! ''' post.content = json.dumps(editor.receive(form)) post.status = 0 # any time a post is edited, remove it from archive. post.time_restrictions_show = \ (form.get('times_mode', 'do_not_show') == 'only_show') post.time_restrictions = form.get('time_restrictions_json', '[]') post.display_time = \ getint('displaytime', 8, minimum=2, maximum=100, form=form) print type(form['active_start']) post.active_start = \ getstr('active_start', post.active_start, validate=DATESTR, form=form) print type(post.active_start) post.active_end = \ getstr('active_end', post.active_end, validate=DATESTR, form=form) post.write_date = now()
def post_form_intake(post, form, editor): ''' takes the values from 'form', passes the post contents to the editor 'receive' function, and adds all the values into the 'post' object. NOTE: this actually modifies the post it is sent! ''' post.content = json.dumps(editor.receive(form)) post.status = 0 # any time a post is edited, remove it from archive. post.time_restrictions_show = \ (form.get('times_mode', 'do_not_show') == 'only_show') post.time_restrictions = form.get('time_restrictions_json', '[]') post.display_time = \ getint('displaytime', 8, minimum=2, maximum=100, form=form) print(type(form['active_start'])) post.active_start = \ getstr('active_start', post.active_start, validate=DATESTR, form=form) print(type(post.active_start)) post.active_end = \ getstr('active_end', post.active_end, validate=DATESTR, form=form) post.write_date = now()
def test_future_timezone_live(self): ''' test that posts in a future do show up when the TIME_OFFSET sets the server to be in that future time. ''' models.app.config['TIME_OFFSET'] = 0 p = self.create_post(published=True, active_start=models.now() + timedelta(hours=1), active_end=models.now() + timedelta(days=2)) self.assertEqual(self.get_posts_ids([self.feed.id]), []) models.app.config['TIME_OFFSET'] = 60 self.assertEqual(self.get_posts_ids([self.feed.id]), [p.id]) models.app.config['TIME_OFFSET'] = 0
def test_zero_offset(self): models.app.config['TIME_OFFSET'] = 0 before = datetime.now() now = models.now() after = datetime.now() self.assertLess(before, now) self.assertGreater(after, now)
def test_future_timezone_future(self): ''' test that even though the TIME_OFFSET config var sets the server into the future, if we're not *enough* in the future to see a post, it still doesn't appear. ''' models.app.config['TIME_OFFSET'] = 0 self.create_post(published=True, active_start=models.now() + timedelta(hours=1), active_end=models.now() + timedelta(days=2)) self.assertEqual(self.get_posts_ids([self.feed.id]), []) models.app.config['TIME_OFFSET'] = 30 self.assertEqual(self.get_posts_ids([self.feed.id]), []) models.app.config['TIME_OFFSET'] = 0
def feedsrss(ids_raw): ''' get a bunch of feeds posts as an RSS stream ''' ids = [] feed_names = [] for i in ids_raw.split(','): try: feedid = int(i) if feedid in ids: continue feed = Feed.get(id=feedid) ids.append(feedid) feed_names.append(feed.name) except ValueError: continue except Feed.DoesNotExist: continue time_now = now() feed_posts = [p for p in \ Post.select().join(Feed).where( (Feed.id << ids) &(Post.status == 0) &(Post.active_start < time_now) &(Post.active_end > time_now) &(Post.published) )] feed = RSSFeed() feed.feed["title"] = ",".join(feed_names) feed.feed["link"] = url_for('feeds') feed.feed["description"] = "Posts from " + (','.join(feed_names)) for post in feed_posts: contents = json.loads(post.content) cleantext = bleach.clean(contents["content"], tags=[], strip=True) if len(cleantext) > 20: cleantext = cleantext[0:20] + "..." item = { "title": cleantext, "description": contents["content"], "guid": str(post.id), } feed.items.append(item) resp = make_response(feed.format_rss2_string()) resp.headers["Content-Type"] = "application/xml" return resp
def test_neg_30_mins_offset(self): models.app.config['TIME_OFFSET'] = -30 before = datetime.now() now = models.now() after = datetime.now() before_minus_hour = datetime.now() - timedelta(hours=1) self.assertGreater(before, now) self.assertGreater(after, now) self.assertLess(before_minus_hour, now)
def test_future_timezone_past(self): ''' test that if the TIME_OFFSET config var puts the server into the future, but a post is not in the future enough for the timezone to match, it doesn't show up. ''' models.app.config['TIME_OFFSET'] = 0 p = self.create_post(published=True, active_start=models.now() - timedelta(hours=9), active_end=models.now() - timedelta(hours=1)) self.assertEqual(self.get_posts_ids([self.feed.id]), []) models.app.config['TIME_OFFSET'] = -30 self.assertEqual(self.get_posts_ids([self.feed.id]), []) models.app.config['TIME_OFFSET'] = -120 self.assertEqual(self.get_posts_ids([self.feed.id]), [p.id]) models.app.config['TIME_OFFSET'] = 0
def screens_posts_from_feeds(json_feeds_list): ''' send JSON list of the posts in whichever feeds you request (as a JS array [id,id,id] type list) ''' feeds_list = json.loads(json_feeds_list) time_now = now() posts = [{"id": p.id, "changed": p.write_date, "uri": url_for('json_post', postid=p.id)} for p in \ Post.select().join(Feed) .where((Feed.id << feeds_list) &(Post.status == 0) &(Post.active_start < time_now) &(Post.active_end > time_now) &(Post.published) )] return jsonify(posts=posts)
def post_form_intake(post, form, editor): ''' takes the values from 'form', passes the post contents to the editor 'receive' function, and adds all the values into the 'post' object. NOTE: this actually modifies the post it is sent! ''' post.content = json.dumps(editor.receive(form)) post.status = 0 # any time a post is edited, remove it from archive. post.time_restrictions_show = (form.get('times_mode', \ 'do_not_show') \ == 'only_show') post.time_restrictions = form.get('time_restrictions_json', '[]') post.display_time = min(100, max(2, int(form.get('displaytime', 8)))) post.active_start = form.get('active_start', post.active_start) post.active_end = form.get('active_end', post.active_end) post.write_date = now()