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.title = form['post_title'] 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) fontsize = getint('post_fontsize', 0, minimum=0, form=form) if form['post_fontsize_mode'] == 'custom' and fontsize > 0: post.fontsize = fontsize else: post.fontsize = 0 post.write_date = now()
def test_validate_max(self): # input is small enough self.resp.form['GETME'] = 80 self.assertEqual(getint('GETME', 42, maximum=99), 80) # end up on default del self.resp.form['GETME'] self.assertEqual(getint('GETME', 100, maximum=200), 100) # fallback to maximum self.resp.form['GETME'] = 80 self.assertEqual(getint('GETME', 42, maximum=25), 25)
def test_validate_min(self): # input is big enough self.resp.form['GETME'] = 120 self.assertEquals(getint('GETME', 42, minimum=99), 120) # end up on default del self.resp.form['GETME'] self.assertEquals(getint('GETME', 100, minimum=99), 100) # fallback to minimum self.resp.form['GETME'] = 80 self.assertEquals(getint('GETME', 42, minimum=99), 99)
def test_validate_max(self): # input is small enough self.resp.form['GETME'] = 80 self.assertEquals(getint('GETME', 42, maximum=99), 80) # end up on default del self.resp.form['GETME'] self.assertEquals(getint('GETME', 100, maximum=200), 100) # fallback to maximum self.resp.form['GETME'] = 80 self.assertEquals(getint('GETME', 42, maximum=25), 25)
def test_validate_min(self): # input is big enough self.resp.form['GETME'] = 120 self.assertEqual(getint('GETME', 42, minimum=99), 120) # end up on default del self.resp.form['GETME'] self.assertEqual(getint('GETME', 100, minimum=99), 100) # fallback to minimum self.resp.form['GETME'] = 80 self.assertEqual(getint('GETME', 42, minimum=99), 99)
def test_validate_minmax(self): # input is small enough self.resp.form['GETME'] = 80 self.assertEquals(getint('GETME', 42, minimum=20, maximum=99), 80) # end up on default del self.resp.form['GETME'] self.assertEquals(getint('GETME', 75, minimum=20, maximum=99), 75) # fallback to maximum self.resp.form['GETME'] = 9000 self.assertEquals(getint('GETME', 42, minimum=20, maximum=99), 99) # fallback to minimum self.resp.form['GETME'] = 9 self.assertEquals(getint('GETME', 42, minimum=20, maximum=99), 20)
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_validate_minmax(self): # input is small enough self.resp.form['GETME'] = 80 self.assertEqual(getint('GETME', 42, minimum=20, maximum=99), 80) # end up on default del self.resp.form['GETME'] self.assertEqual(getint('GETME', 75, minimum=20, maximum=99), 75) # fallback to maximum self.resp.form['GETME'] = 9000 self.assertEqual(getint('GETME', 42, minimum=20, maximum=99), 99) # fallback to minimum self.resp.form['GETME'] = 9 self.assertEqual(getint('GETME', 42, minimum=20, maximum=99), 20)
def external_data_source_edit(source_id): ''' Editing a external data source ''' if not user_session.is_admin(): flash('Only Admins can do this!') return redirect(url_for('feeds')) # first find the data type: if request.method == 'DELETE': ExternalSource.delete() \ .where(ExternalSource.id == int(source_id)) \ .execute() return 'deleted' if source_id == None: try: source = ExternalSource() source.type = request.args['type'] source.name = "new " + source.type + " source" source.feed = Feed.get() # set initial feed except KeyError: return 'No type specified.', 500 else: try: source = ExternalSource.get(id=source_id) except peewee.DoesNotExist: return 'Invalid id.', 404 # Try and load the external source type ( and check it's valid): try: module = external_source_types.load(source.type) except ImportError: return 'Invalid External Source Type', 404 # if it's a post, then update the data with 'receive': if request.method == 'POST': source.post_as_user = user_session.get_user() source.settings = json.dumps(module.receive(request)) source.name = request.form.get('name', source.name) source.frequency = getint('frequency', 60) source.publish = getbool('publish', False) source.lifetime_start = getstr('active_start', source.lifetime_start, validate=DATESTR) source.lifetime_end = getstr('active_end', source.lifetime_end, validate=DATESTR) source.display_time = getint('display_time', source.display_time) source.post_template = request.form.get('post_template', source.post_template) try: source.feed = Feed.get(Feed.id == getint('feed', 100)) source.save() if source_id == None: # new source! return redirect( url_for('external_data_source_edit', source_id=source.id)) else: flash('Updated.') except Feed.DoesNotExist: flash("Can't save! Invalid Feed!{}".format(getint('feed', '-11'))) return render_template("external_source.html", source=source, feeds=Feed.select(), form=module.form(json.loads(source.settings)))
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 test_empty(self): self.resp.form['GETME'] = '' self.assertEquals(getint('GETME', 42), 42)
def test_there(self): self.resp.form['GETME'] = 999 self.assertEquals(getint('GETME', 42), 999)
def test_not_there(self): self.assertFalse('GETME' in self.resp.form) self.assertEquals(getint('GETME', 42), 42)
def external_data_source_edit(source_id): ''' Editing a external data source ''' if not user_session.is_admin(): flash('Only Admins can do this!') return redirect(url_for('feeds')) # first find the data type: if request.method == 'DELETE': ExternalSource.delete() \ .where(ExternalSource.id == int(source_id)) \ .execute() return 'deleted' if source_id == None: try: source = ExternalSource() source.type = request.args['type'] source.name = "new " + source.type + " source" source.feed = Feed.get() # set initial feed except KeyError: return 'No type specified.', 500 else: try: source = ExternalSource.get(id=source_id) except peewee.DoesNotExist: return 'Invalid id.', 404 # Try and load the external source type ( and check it's valid): try: module = external_source_types.load(source.type) except ImportError: return 'Invalid External Source Type', 404 # if it's a post, then update the data with 'receive': if request.method == 'POST': source.post_as_user = user_session.get_user() source.settings = json.dumps(module.receive(request)) source.name = request.form.get('name', source.name) source.frequency = getint('frequency', 60) source.publish = getbool('publish', False) source.lifetime_start = request.form.get('active_start', source.lifetime_start) source.lifetime_end = request.form.get('active_end', source.lifetime_end) source.display_time = getint('display_time', source.display_time) source.post_template = request.form.get('post_template', source.post_template) try: source.feed = Feed.get(Feed.id == getint('feed', 100)) source.save() if source_id == None: # new source! return redirect(url_for('external_data_source_edit', source_id=source.id)) else: flash('Updated.') except Feed.DoesNotExist: flash("Can't save! Invalid Feed!{}".format( getint('feed', '-11'))) return render_template("external_source.html", source=source, feeds=Feed.select(), form=module.form(json.loads(source.settings)))
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 test_empty(self): self.resp.form['GETME'] = '' self.assertEqual(getint('GETME', 42), 42)
def test_there(self): self.resp.form['GETME'] = 999 self.assertEqual(getint('GETME', 42), 999)
def test_not_there(self): self.assertFalse('GETME' in self.resp.form) self.assertEqual(getint('GETME', 42), 42)