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)
def home_page(username=None): if not username: abort(404) # TODO: port to mongodb query # user = User.query.filter_by(username=username).first() cursor = User.find({'username': username}) user = cursor[0] # user['thread_karma'] = Thread.find({"user_id":user.id}) app.logger.debug("user cursor: {}".format(cursor)) app.logger.debug("user dict: {}".format(user)) app.logger.debug("thread karma: {}".format(user.get_thread_karma())) # user = db.users.find_one_or_404({"username": username}) # threads = Thread.find({'user_id': user.id}) if not user: abort(404) return render_template( 'users/profile.html', user=user, current_user=user, subreddits=get_subreddits(), )
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())