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 feedpage(feedid): ''' the back end settings for one feed. ''' try: feed = Feed.get(id=feedid) user = user_session.get_user() except user_session.NotLoggedIn: user = User() except: flash('invalid feed id! (' + str(feedid) + ')') return redirect(url_for('feeds')) if request.method == 'POST': if not user_session.logged_in(): flash("You're not logged in!") return redirect(url_for('feeds')) if not user.is_admin: flash('Sorry! Only Admins can change these details.') return redirect(request.referrer) action = request.form.get('action', 'none') if action == 'edit': feed.name = request.form.get('title', feed.name).strip() inlist = request.form.getlist feed.post_types = ', '.join(inlist('post_types')) feed.set_authors(by_id(User, inlist('authors'))) feed.set_publishers(by_id(User, inlist('publishers'))) feed.set_author_groups(by_id(Group, inlist('author_groups'))) feed.set_publisher_groups(by_id(Group, inlist('publisher_groups'))) feed.save() flash('Saved') elif action == 'delete': for post in feed.posts: post_type_module = post_types.load(post.type) delete_post_and_run_callback(post, post_type_module) feed.delete_instance(True, True) # cascade/recursive delete. flash('Deleted') return redirect(url_for('feeds')) return render_template('feed.html', feed=feed, user=user, all_posttypes=post_types.types(), allusers=User.select(), allgroups=Group.select() )
def get_new(data): ''' ok, actually go get us some new posts, alright? (return new posts, and update data with any hidden fields updated that we need to (current_posts, for instance))''' feed = feedparser.parse(data['url']) new_posts = [] templater = make_templater(data) for post in Post.select().where((Post.type == 'news')): delete_post_and_run_callback(post, post_types.load(post.type)) for entry in feed.entries: new_posts.append({ 'type': 'news', 'color': None, 'content': templater(entry) }) data['current_posts'] = [e.id for e in feed.entries] return new_posts
def postpage(postid): ''' Edit a post. ''' if not user_session.logged_in(): flash("You're not logged in!") return redirect(url_for('posts')) try: post = Post.get(Post.id == postid) post_type_module = post_types.load(post.type) user = user_session.get_user() except Post.DoesNotExist: flash('Sorry! Post id:{0} not found!'.format(postid)) return redirect(url_for('posts')) if request.method == 'POST': try: # check for write permission, and if the post is # already published, publish permission. if_i_cant_write_then_i_quit(post, user) # if the user is allowed to set the feed to what they've # requested, then do it. post.feed = try_to_set_feed(post, request.form.get('post_feed', False), user) except PleaseRedirect as e: flash(str(e.msg)) redirect(e.url) # if it's a publish or delete request, handle that instead: action = request.form.get('action', 'edit') if action == 'delete': # don't need extra guards, as if_i_cant... deals with it above delete_post_and_run_callback(post, post_type_module) flash('Deleted') elif action == 'publish': try: post.publish(user) flash("Published") except PermissionDenied: flash("Sorry, you don't have permission to publish" " posts in this feed.") elif action == 'unpublish': try: post.publish(user, False) flash("Published!") except PermissionDenied: flash('Sorry, you do NOT have permission' \ ' to unpublish on this feed.') elif action == 'move': if not user_session.is_admin(): flash('Sorry! You are not an admin!') return jsonify({'error': 'permission denied'}) post.feed = Feed.get(Feed.id == getint('feed', post.feed)) post.save() return jsonify({'message': 'Moved to ' + post.feed.name}) if action not in ('edit', 'update'): return redirect(request.referrer if request.referrer else '/') # finally get around to editing the content of the post... try: post_form_intake(post, request.form, post_type_module) post.save() flash('Updated.') except Exception as e: flash('invalid content for this data type!') flash(str(e)) # Should we bother displaying 'Post' button, and editable controls # if the user can't write to this post anyway? #can_write, can_publish = can_user_write_and_publish(user, post) return render_template('post_editor.html', post=post, current_feed=post.feed.id, feedlist=user.writeable_feeds(), user=user, form_content=post_type_module.form(json.loads(post.content)))
def postpage(postid): ''' Edit a post. ''' if not user_session.logged_in(): flash("You're not logged in!") return redirect(url_for('posts')) try: post = Post.get(Post.id == postid) post_type_module = post_types.load(post.type) user = user_session.get_user() except Post.DoesNotExist: flash('Sorry! Post id:{0} not found!'.format(postid)) return redirect(url_for('posts')) if request.method == 'POST': try: # check for write permission, and if the post is # already published, publish permission. if_i_cant_write_then_i_quit(post, user) # if the user is allowed to set the feed to what they've # requested, then do it. post.feed = try_to_set_feed(post, request.form.get('post_feed', False), user) except PleaseRedirect as e: flash(str(e.msg)) redirect(e.url) # if it's a publish or delete request, handle that instead: action = request.form.get('action', 'edit') if action == 'delete': # don't need extra guards, as if_i_cant... deals with it above delete_post_and_run_callback(post, post_type_module) flash('Deleted') elif action == 'publish': try: post.publish(user) flash("Published") except PermissionDenied: flash("Sorry, you don't have permission to publish" " posts in this feed.") elif action == 'unpublish': try: post.publish(user, False) flash("Published!") except PermissionDenied: flash('Sorry, you do NOT have permission' \ ' to unpublish on this feed.') elif action == 'move': if not user_session.is_admin(): flash('Sorry! You are not an admin!') return jsonify({'error': 'permission denied'}) post.feed = Feed.get(Feed.id == getint('feed', post.feed)) post.save() return jsonify({'message': 'Moved to ' + post.feed.name}) if action not in ('edit', 'update'): return redirect(request.referrer if request.referrer else '/') # finally get around to editing the content of the post... try: post_form_intake(post, request.form, post_type_module) post.save() flash('Updated.') except Exception as e: flash('invalid content for this data type!') flash(str(e)) # Should we bother displaying 'Post' button, and editable controls # if the user can't write to this post anyway? #can_write, can_publish = can_user_write_and_publish(user, post) return render_template('post_editor.html', post=post, current_feed=post.feed.id, feedlist=user.writeable_feeds(), user=user, form_content=post_type_module.form( json.loads(post.content)))