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()
def home_subreddit(): #TODO: convert to mongodb # return Subreddit.query.get_or_404(1) s = Subreddit.find_one({}) if s is None: # abort(404) flash("There are no subreddits yet, create one!") return s
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 thread_permalink(subreddit_name=None, thread_id=None, title=None): """ """ thread_id = thread_id #or -99 app.logger.debug("thread_id: {}".format(thread_id)) thread = Thread.find_one({"id": ObjectId(thread_id)}) app.logger.debug(Subreddit.find({"name": subreddit_name})) subreddit = Subreddit.find_one({"name": subreddit_name}) subreddits = get_subreddits() return render_template('threads/permalink.html', user=g.user, thread=thread, cur_subreddit=subreddit, subreddits=subreddits)
def permalink(subreddit_name=""): """ """ subreddit = Subreddit.find_one({"name": subreddit_name}) app.logger.debug("subreddit: {}".format(subreddit)) if not subreddit: abort(404) trending = True if request.args.get('trending') else False thread_paginator = process_thread_paginator(trending=trending, subreddit=subreddit) subreddits = get_subreddits() return render_template('home.html', user=g.user, thread_paginator=thread_paginator, subreddits=subreddits, cur_subreddit=subreddit)