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 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 get_subreddits(): """ important and widely imported method because a list of the top 30 subreddits are present on every page in the sidebar """ # TODO: # convert to mongodb # subreddits = [] # subreddits = Subreddit.query.filter(Subreddit.id != 1)[:25] subreddits = Subreddit.find({"id": {"$ne": 1}})[:25] app.logger.debug("in frontends.views: subreddits: {}".format(subreddits)) return subreddits
def init_db(): try: User.collection.drop() except Exception as e: print(str(e)) User.ensure_indexes() try: CommentUpvote.collection.drop() except Exception as e: print(str(e)) CommentUpvote.ensure_indexes() try: ThreadUpvote.collection.drop() except Exception as e: print(str(e)) ThreadUpvote.ensure_indexes() try: Thread.collection.drop() except Exception as e: print(str(e)) Thread.ensure_indexes() try: Comment.collection.drop() except Exception as e: print(str(e)) Comment.ensure_indexes() try: Subreddit.collection.drop() except Exception as e: print(str(e)) Subreddit.ensure_indexes()
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)
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 view_all(): """ """ return render_template('subreddits/all.html', user=g.user, subreddits=Subreddit.find())
def submit(subreddit_name=None): """ """ if g.user is None: flash('You must be logged in to submit posts!', 'danger') return redirect(url_for('frontends.login', next=request.path)) user_id = g.user.id subreddits = Subreddit.find({"name": subreddit_name}) subreddit = None if subreddits.count() > 0: subreddit = subreddits[0] if not subreddit: flash('Select a subreddit!', 'danger') # abort(404) return redirect(url_for('frontends.home', next=request.path)) form = SubmitForm(request.form) if form.validate_on_submit(): title = form.title.data.strip() link = form.link.data.strip() text = form.text.data.strip() thread_data = { "title": title, "link": link, "text": text, "user_id": user_id, "subreddit_id": subreddit.id, "user": g.user, "subreddit": subreddit } thread = Thread(**thread_data) if not meets_thread_criteria(thread): return render_template('threads/submit.html', form=form, user=g.user, cur_subreddit=subreddit.name) thread.update() thread.commit() app.logger.debug("adding thread: subreddit name: {}".format( thread.subreddit)) app.logger.debug("adding thread: subreddit name: {}".format( thread.subreddit.fetch().name)) # db.threads.add_thread # db.session.add(thread) # db.session.commit() # thread.set_hotness() # thread.add_thread() flash('thanks for submitting!', 'success') return redirect( url_for('subreddits.permalink', subreddit_name=subreddit.name)) return render_template('threads/submit.html', form=form, user=g.user, cur_subreddit=subreddit, subreddits=get_subreddits())