def fill_posts(): """Empties all Post data, then generates fake posts using Faker.""" # empty legacy data empty_posts() # Configuring for random data generation seed(1) # for posts fake = Faker( ) # for timestamp. See https://faker.readthedocs.io/en/master/index.html Faker.seed( 0 ) # See https://faker.readthedocs.io/en/master/providers/faker.providers.date_time.html # Cycle through all users. # Generate 0-9 posts per user. # Use Faker library to randomise username generation and post timestamps. users = User.query.all() for user in users: # 0-9 posts for i in range(0, randint(0, 10)): # data for a temporary post temp_content = fake.text() temp_timestamp = fake.date_time() temp_userid = user.id # create and add temporary Post object to database temp_post = Post(content=temp_content, timestamp=temp_timestamp, user_id=temp_userid) db.session.add(temp_post) # db.session.execute('INSERT INTO post (content) VALUES (:param);', {'param':makePosts.text()}) db.session.commit() # commit the session to the database return
def post(): post_form = PostForm() if post_form.validate_on_submit(): language = guess_language(post_form.post.data) if language == "UNKNOWN" or len(language) > 5: language = "" post = Post(body=post_form.post.data, author=current_user, language=language) db.session.add(post) db.session.commit() flash(_("Your post is now live!")) return redirect(url_for("main.user", username=current_user.username)) page = request.args.get("page", 1, type=int) posts = current_user.posts.order_by(Post.timestamp.desc()).paginate( page, current_app.config["POSTS_PER_PAGE"], False ) next_url = ( url_for("main.user", username=current_user.username, page=posts.next_num) if posts.has_next else None ) prev_url = ( url_for("main.user", username=current_user.username, page=posts.prev_num) if posts.has_prev else None ) return render_template( "post.html", user=user, post_form=post_form, posts=posts.items, next_url=next_url, prev_url=prev_url, )
def create_post(): if request.method == 'POST': import code; code.interact(local=dict(globals(), **locals())) post = Post(image_url = request.form['image_url'], body = request.form['body'], user_id = current_user.id) db.session.add(post) db.session.commit() flash('Thank you for your Post!', 'success') return redirect(url_for('home'))
def post(): form = PostForm() if request.method == "GET": return render_template('post.html', form=form) post = Post(title = request.form["title"], body = request.form["body"]) db.session.add(post) db.session.commit() return redirect(url_for('index'))
def create_post(self): new_post = Post(title='Test Post', post_body='This is a test post.', tags='testing', posted_date=date(2014, 10, 03), updated_at=None, status=1, user_id='Michael') db.session.add(new_post) db.session.commit()
def user(username): postform = UserPost() if postform.validate_on_submit(): post = Post(content=postform.content.data, author=current_user) db.session.add(post) db.session.commit() return redirect(url_for('user', username=current_user.username)) user = User.query.filter_by(username=username).first() form = FollowForm() posts = Post.query.filter_by(author=user).order_by(Post.id.desc()).all() return render_template('user.html', user=user, form=form, postform=postform, posts=posts)
def add_post(): form = PostForm(request.form) if request.method == 'GET': return render_template('add_post.html', form=form) else: title = form.title.data text = form.text.data post = Post(current_user.id, title, text) db.session.add(post) db.session.commit() return redirect(url_for('feed'))
def new_post(): form = PostForm() if form.validate_on_submit(): if form.picture.data: picture_file = save_post_picture(form.picture.data) post = Post(title=form.title.data, content=form.content.data, image_file=picture_file, author=current_user) db.session.add(post) db.session.commit() else: post = Post(title=form.title.data, content=form.content.data, author=current_user) db.session.add(post) db.session.commit() flash('Your post has been created successfully!', 'success') return redirect(url_for('home')) return render_template('create_post.html', title='New Post', form=form)
def test_follow_posts(self): # Create four users u1 = User(username="******", email="*****@*****.**") u2 = User(username="******", email="*****@*****.**") u3 = User(username="******", email="*****@*****.**") u4 = User(username="******", email="*****@*****.**") db.session.add_all([u1, u2, u3, u4]) # Create four posts now = datetime.utcnow() p1 = Post(body="post from john", author=u1, timestamp=now + timedelta(seconds=1)) p2 = Post(body="post from susan", author=u2, timestamp=now + timedelta(seconds=4)) p3 = Post(body="post from mary", author=u3, timestamp=now + timedelta(seconds=3)) p4 = Post(body="post from david", author=u4, timestamp=now + timedelta(seconds=2)) db.session.add_all([p1, p2, p3, p4]) db.session.commit() # setup the Followers u1.follow(u2) u1.follow(u4) u2.follow(u3) u3.follow(u4) db.session.commit() # Check the followed posts of each user f1 = u1.followed_posts().all() f2 = u2.followed_posts().all() f3 = u3.followed_posts().all() f4 = u4.followed_posts().all() self.assertEqual(f1, [p2, p4, p1]) self.assertEqual(f2, [p2, p3]) self.assertEqual(f3, [p3, p4]) self.assertEqual(f4, [p4])
def user_home(): form = UserPost() if form.validate_on_submit(): post = Post(content=form.content.data, user_id=current_user.id) db.session.add(post) db.session.commit() return redirect(url_for('user_home')) posts = Post.query.order_by(Post.id.desc()).all() like_counts = [] for post in posts: like_counts.append(get_likes_by_post_id(post.id)) return render_template('user_home.html', posts=posts, form=form, likes=like_counts)
def add_block_content_to_db(block): block_number = int(block['previous'][:8], base=16) + 1 # add qualifying posts, and for edited posts and comments, schedule updates try: for transaction in block['transactions']: for o in transaction['operations']: # check for votes for existing video posts, and if so schedule Steem RPC update if o[0] == 'vote': try: vote = o[1] post = db.session.query(Post) \ .filter(and_(Post.author == vote['author'], Post.permlink == vote['permlink'])).first() if post: post.pending_steem_info_update = True post.steem_info_update_requested = datetime.now() db.session.commit() except Exception as e: log('Monitor error when evaluating vote...') log(str(e)) # check for existing post (or add skeleton record if not present) and schedule updates if o[0] == 'comment': try: comment = o[1] if comment['parent_author'] == '': # if top-level post post = db.session.query(Post) \ .filter(and_(Post.author == comment['author'], Post.permlink == comment['permlink'])).first() if post: post.pending_steem_info_update = True post.steem_info_update_requested = datetime.now() post.pending_video_info_update = True post.video_info_update_requested = datetime.now() db.session.commit() else: video_type, video_id, category = get_valid_video(comment) if video_type and video_id and category: post_skeleton = Post(author=comment['author'], permlink=comment['permlink'], category=category, video_type=video_type, video_id=video_id, block_number=block_number) db.session.add(post_skeleton) db.session.commit() except Exception as e: log('Monitor error when evaluating comment...') log(str(e)) except Exception as e: log('BIG ERROR:' + str(e)) time.sleep(30)
def user(username): # Post form post_form = PostForm() if post_form.validate_on_submit(): # Check what language is used to the post language = guess_language(post_form.post.data) if language == "UNKNOWN" or len(language) > 5: language = "" post = Post(body=post_form.post.data, author=current_user, language=language) db.session.add(post) db.session.commit() flash(_("Your post is now live!")) return redirect(url_for("main.user", username=current_user.username)) # Profile edit form edit_form = EditProfileForm(current_user.username) if edit_form.validate_on_submit(): current_user.username = edit_form.username.data current_user.about_me = edit_form.about_me.data current_user.location_id = edit_form.location_pref.data db.session.commit() flash(_("Your changes have been saved.")) return redirect(url_for("main.user", username=current_user.username)) if request.method == "GET": edit_form.username.data = current_user.username edit_form.about_me.data = current_user.about_me user = User.query.filter_by(username=username).first_or_404() location = Location.query.filter_by(id=user.location_id).first() page = request.args.get("page", 1, type=int) posts = user.posts.order_by(Post.timestamp.desc()).paginate( page, current_app.config["POSTS_PER_PAGE"], False ) next_url = ( url_for("main.user", username=current_user.username, page=posts.next_num) if posts.has_next else None ) prev_url = ( url_for("main.user", username=current_user.username, page=posts.prev_num) if posts.has_prev else None ) return render_template( "user.html", user=user, location=location, posts=posts.items, edit_form=edit_form, post_form=post_form, next_url=next_url, prev_url=prev_url, )
def new_post(): form = PostForm() if form.validate_on_submit(): post = Post(patient=form.patient.data, condition=form.condition.data, medication=form.medication.data, description=form.description.data, author=current_user) db.session.add(post) db.session.commit() flash('Your post has been created!', 'success') return redirect(url_for('main.home')) return render_template('create_post.html', title='New Post', form=form)
def new_post(): form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data, customer=form.customer.data, microscope=form.microscope.data, country=form.country.data, date_req = form.date_req.data, problem_disc = form.problem_disc.data, os = form.os.data, digistar = form.digistar.data, gis = form.gis.data, people_inv = form.people_inv.data, actions = form.actions.data, spares = form.spares.data, date_fix =form.date_fix.data, warranty = form.warranty.data, notes = form.notes.data, archived = form.archived.data, author=current_user) db.session.add(post) db.session.commit() flash('Post created!', 'success') return redirect(url_for('home')) return render_template('create_post.html', title='New Post', form=form, legend='New Post')
def new_post(): form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data, content=form.content.data, author=current_user) db.session.add(post) db.session.commit() flash('Your Post has been created!', 'success') return redirect(url_for('home')) return render_template('create_post.html', title='New Post', form=form, legend='New Post')
def new_post(): form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data, content=form.content.data, author=current_user) db.session.add(post) db.session.commit() flash(u"Your post has been created!", "success") return redirect(url_for("main.home")) return render_template("create_post.html", title="New Post", form=form, legend="New Post")
def create_post_paginate(): title = request.form['post-title'] content = request.form['post-content'] subreddit = request.form['subreddit'] if subreddit == 'Music': subreddit_id = 0 elif subreddit == 'Funny': subreddit_id = 1 elif subreddit == 'Programming': subreddit_id = 2 elif subreddit == 'News': subreddit_id = 3 elif subreddit == 'Design': subreddit_id = 4 elif subreddit == 'Sports': subreddit_id = 5 elif subreddit == 'Politics': subreddit_id = 6 else: subreddit_id = 7 time = datetime.now() newtime = datetime.strftime(time, '%d/%m/%Y') subreddits = Subreddit.query.all() sub = subreddit newPost = Post(title=title, description=content, sub=sub, votes=0, user=current_user, subreddit_id=subreddit_id, timestamp=newtime) db.session.add(newPost) db.session.commit() all_posts = Post.query.all() state['sort'] = 'dates_newest' page = request.args.get('page', 1, type=int) posts = Post.query.paginate(page=page, per_page=5) return render_template('index.html', name=current_user.name, all_posts=posts, subreddits=subreddits, profile=False, page_name='home', sort_by=state['sort'], paginate=True)
def new(user_id): post_form = PostForm(request.form) if request.method == 'POST': if post_form.validate_on_submit(): post_obj = Post(contactName=request.form['contactName'], phoneNo=request.form['phoneNo'], address=request.form['address'], title=request.form['title'], description=request.form['description'], user_id=user_id) db.session.add(post_obj) db.session.commit() return redirect( url_for('images.upload_files', user_id=user_id, post_id=post_obj.id)) return render_template('posts/new.html', user_id=user_id, post_form=post_form)
def add_post(): form = PostForm(request.form) if request.method == 'POST': title = form.title.data text = form.text.data test_result = test_add_post(form) if test_result == "success": post = Post(current_user.id, title, text) db.session.add(post) db.session.commit() return redirect(url_for("stories")) else: data = {'err_msg':test_result} return render_template('add_post.html',form = form, data = data) else: return render_template('add_post.html', form=form)
def index(): error = None form = MessageForm(request.form) if form.validate_on_submit(): new_idea = Post(form.title.data, form.description.data, current_user.id) db.session.add(new_idea) db.session.commit() flash('New Idea Shared!') return redirect(url_for('home.index')) else: posts = db.session.query(Post).order_by('posts.id desc') first_name = current_user.first_name last_name = current_user.last_name return render_template('index.html', posts=posts, form=form, error=error, last_name=last_name, first_name=first_name, title="Home")
newuser = User(date_joined=strftime("%Y-%m-%d"), email='*****@*****.**', username='******', password='******', verified=1) newpost_bnb1 = Post(date_posted=datetime.datetime.now(), username='******', page='bnb', viewed=0, subject='민박 샘플 포스팅입니다2', body='민박 샘플 포스팅입니다', phone='123-456-7890', email='*****@*****.**', kakaotalk='calbang', city='Fullerton, CA', price='50', image_ext=None, address='1234 Wilshire Blvd.', bedrooms=0, bathrooms=0, parking=1, utilities=None, internet=None, furniture=None, food=None, sqft=1234, year=1991) newpost_bnb2 = Post(date_posted=datetime.datetime.now(), username='******', page='bnb', viewed=0, subject='민박 샘플 포스팅입니다2',
lines = [] with open('scrapy_craigslist/craigslist.jl', 'r') as f: for line in f: lines.append(json.loads(line)) def get_craigslist_user_id(): users = User.query.filter(User.email == "craigslist@home_locals.com").all() if not users: user = User("craigslist@home_locals.com", "home_locals", 94108) db.session.add(user) db.session.commit() else: user = users[0] return user.id craigslist_user_id = get_craigslist_user_id() for line in lines: post = Post(description=line.get('description'), address=line.get('location'), title=line.get('title'), user_id=craigslist_user_id) db.session.add(post) db.session.commit() db.session.add(Image(line.get('image_url'), post.id)) db.session.commit()