def addcomment(post_id): post = db_session.query(Post).filter(Post.id == post_id).one() text = "" popup = None if request.method == 'POST': if session['logged_in']: text = request.form['text'] if len(text) == 0: flash('Empty comment', 'addcom') elif len(text) > 140: flash('Too long comment (Under 140 characters)', 'addcom') messages = get_flashed_messages(category_filter=['addcom']) if len(messages) == 0: now = datetime.datetime.now() comment = Comment(text, now) comment.user = db_session.query(User).\ filter(User.username==session['username']).one() comment.post = post db_session.add(comment) db_session.commit() text = "" else: popup = 'alert' ctx = update_stats() ctx['post'] = post ctx['comtext'] = text ctx['popup'] = popup return render_template('showpost.html', **ctx)
def showpost(post_id, popup): post = db_session.query(Post).filter(Post.id==post_id).first() ctx = update_stats() ctx['post'] = post ctx['popup'] = popup return render_template('showpost.html', **ctx)
def showentries(popup): #print_table(User) #print_table(Post) #print_table(Tag) ctx = update_stats() ctx['popup'] = popup return render_template('main.html', **ctx)
def addpost(): title = "" text = "" taginput = "" popup = None if request.method == 'POST': if session['logged_in']: title = request.form['title'] text = request.form['text'] tagtextlist = parse_taginput(request.form['tags']) taginput = "; ".join(tagtextlist) now = datetime.datetime.now() if len(title) == 0: flash('Empty title', 'addpost') elif len(title) > 50: flash('Too long title (Under 50 charaters)', 'addpost') if len(text) == 0: flash('Empty text', 'addpost') elif len(text) > 2000: flash('Too long text (Under 2000 charaters)', 'addpost') for tagtext in tagtextlist: if len(tagtext) > 20: flash('Too long tag(s) (Under 20 characters)', 'addpost') break messages = get_flashed_messages(category_filter=['addpost']) if len(messages) == 0: post = Post(title, text, now) post.user = db_session.query(User).\ filter(User.username==session['username']).one() for tagtext in tagtextlist: tag = db_session.query(Tag).filter(Tag.text==tagtext).first() if tag is None: tag = Tag(tagtext) tag.posts.append(post) db_session.add(post) db_session.commit() return redirect(url_for('showpost', post_id=post.id)) else: popup = 'alert' ctx = update_stats() ctx['title'] = title ctx['text'] = text ctx['tags'] = taginput ctx['popup'] = popup return render_template('addpost.html', **ctx)
def searchposts(): # get input keyword = request.args.get('keyword') # compute posts = search_posts_with_keyword(keyword) # template context ctx = update_stats() ctx['keyword'] = keyword ctx['posts'] = posts return render_template('searchposts.html', **ctx)
def deleteuser(): popup = None if request.method == 'POST': query = db_session.query(User) user = query.filter(User.username==session['username']).one() if user.password == request.form['password']: db_session.delete(user) db_session.commit() session['logged_in'] = False return redirect(url_for('showentries')) else: flash('Wrong password', 'deluser') popup = 'alert' ctx = update_stats() ctx['popup'] = popup return render_template('deleteuser.html', **ctx)
def signup(): popup = None if request.method == 'POST': username = request.form['username'] password = request.form['password'] cfpassword = request.form['cfpassword'] u = db_session.query(User).filter(User.username==username).first() if not u is None: flash('Existing username', 'signup') else: if len(username.split()) != 1: flash('Whitespace is Not allowed in a valid username', 'signup') elif len(username)<4 or 20<len(username): flash('Invalid length of username', 'signup') elif len(password)<4 or 20<len(password): flash('Invalid length of password', 'signup') elif password != cfpassword: flash('Invalid confirm password', 'signup') messages = get_flashed_messages(category_filter=['signup']) if len(messages) == 0: user = User(username, hash_password(password)) db_session.add(user) db_session.commit() session['logged_in'] = True session['username'] = request.form['username'] return redirect(url_for('showentries')) else: popup = 'alert' ctx = update_stats() ctx['popup'] = popup return render_template('signup.html', **ctx)
def showtaggedlist(tag_id): tag = db_session.query(Tag).filter(Tag.id==tag_id).first() ctx = update_stats() ctx['tag'] = tag return render_template('taggedlist.html', **ctx)
def modifypost(post_id): post = db_session.query(Post).filter(Post.id==post_id).one() title = post.title text = post.text taginput = get_taginput(post.tags) popup = None if request.method == 'POST': if session['logged_in'] and \ post.user.username==session['username']: title = request.form['title'] text = request.form['text'] tagtextlist = parse_taginput(request.form['tags']) taginput = "; ".join(tagtextlist) if len(title) == 0: flash('Empty title', 'addpost') elif len(title) > 50: flash('Too long title (Under 50 charaters)', 'modpost') if len(text) == 0: flash('Empty text', 'addpost') elif len(text) > 2000: flash('Too long text (Under 2000 charaters)', 'modpost') for tagtext in tagtextlist: if len(tagtext) > 20: flash('Too long tag(s) (Under 20 characters)', 'modpost') break messages = get_flashed_messages(category_filter=['modpost']) if len(messages) == 0: now = datetime.datetime.now() post.title = title post.text = text post.date = now.strftime("%y-%m-%d") post.time = now.strftime("%H:%M:%S") for tag in post.tags: post.tags.remove(tag) if len(tag.posts)==0: db_session.delete(tag) db_session.commit() for tagtext in tagtextlist: tag = db_session.query(Tag).filter(Tag.text==tagtext).first() if tag is None: tag = Tag(tagtext) tag.posts.append(post) db_session.add(post) db_session.commit() return redirect(url_for('showpost', post_id=post_id)) else: popup = 'alert' ctx = update_stats() ctx['post_id'] = post_id ctx['title'] = title ctx['text'] = text ctx['tags'] = taginput ctx['popup'] = popup return render_template('modpost.html', **ctx)