def submit(): """ """ if g.user is None: flash('You must be logged in to submit subreddits!', 'danger') return redirect(url_for('frontends.login', next=request.path)) form = SubmitForm(request.form) app.logger.debug(g.user) user_id = g.user.id if form.validate_on_submit(): name = form.name.data.strip() desc = form.desc.data.strip() subreddit = Subreddit.find_one({'name': name}) if subreddit: flash('subreddit already exists!', 'danger') return render_template('subreddits/submit.html', form=form, user=g.user, subreddits=get_subreddits()) _new_subreddit = { "name": name, "desc": desc, "admin_id": user_id, "admin": g.user } new_subreddit = Subreddit(**_new_subreddit) new_subreddit.commit() if not meets_subreddit_criterea(subreddit): return render_template('subreddits/submit.html', form=form, user=g.user, subreddits=get_subreddits()) # db.session.add(new_subreddit) # db.session.commit() flash( 'Thanks for starting a community! Begin adding posts to your community\ by clicking the red button to the right.', 'success') return redirect( url_for('subreddits.permalink', subreddit_name=new_subreddit.name)) return render_template('subreddits/submit.html', form=form, user=g.user, subreddits=get_subreddits())
def populate_db(): # User.collection.drop() # User.ensure_indexes() for data in [{ 'username': '******', 'email': '*****@*****.**', 'lastname': 'Mao', 'firstname': 'Zedong', 'birthday': datetime.datetime(1993, 12, 26), 'password': generate_password_hash('1') }, { 'username': '******', 'email': '*****@*****.**', 'lastname': 'Xi', 'firstname': 'Jinping', 'birthday': datetime.datetime(1953, 6, 15), 'password': generate_password_hash('1'), 'date_created': datetime.datetime.utcnow() }]: User(**data).commit() a_user = User.find_one() _new_subreddit = { "name": "test_subreddit", "desc": "test desc", "admin_id": a_user.id, "admin": a_user } new_subreddit = Subreddit(**_new_subreddit) new_subreddit.commit() thread_data = { "title": "test thread", "link": "disxss.com", "text": "not alive", "user_id": a_user.id, "subreddit_id": Subreddit.find_one().id, "user": a_user, "subreddit": Subreddit.find_one() } thread = Thread(**thread_data) thread.update() thread.commit()