py4u-opensource / usercontroller.py

from flask import Blueprint, render_template,session,request,redirect
from configure import configure,Message
from news import Post, Comment
from blinker import signal
import sqlite3
userapp = Blueprint('useraction', __name__, static_folder='static',template_folder='templates')

@userapp.before_request
def require_login():
    if 'user' not in session:
        if request.method == 'PUT':
            return 'login'
        temp = Message("You need to login first","alert-error")
        temp.showed = False
        session['message'] = temp
        return redirect('/login')

@userapp.context_processor
def inject_configure():
    return dict(configure.__class__.__dict__)
@userapp.context_processor
def inject_user():
    return dict(user=session.get('user',None))


@userapp.route('/newpost', methods = ['GET','POST'])
def submit_post():
    if(request.method == "POST"):
        title = request.form['title']
        link = request.form['link']
        des = request.form['des']
        post = Post(title,link,des)
        post.save()
        session['user'].vote_for(post.id)
        user = session['user']
        user.reputation += 2
        session['user']=user
        user.update()
        user.create_post_activity(2,post.id)
        return redirect('/latest')

    return render_template('newpost.html', news_type='news_sn')

@userapp.route('/addcommentforpost',methods =['POST'])
def addcommentforpost():
    if(request.method == "POST"):
        postid = request.form['postid']
        content = request.form['content']
        comment = Comment(postid, content, session['user'].id)
        comment.save()
        return redirect('/showpost?id='+str(postid))
        
@userapp.route('/addcommentforbook',methods =['POST'])
def addcommentforbook():
    if(request.method == "POST"):
        postid = request.form['postid']
        bookid = request.form['bookid']
        content = request.form['content']
        comment = Comment(postid, content, session['user'].id)
        comment.save()
        return redirect('/pybook/showbook?id='+str(bookid))
        
@userapp.route('/addcommentforcomment',methods =['POST'])
def addcommentforcomment():
    if(request.method == "POST"):
        postid = request.form['postid']
        content = request.form['content']
        parentid = request.form['parentid']
        comment = Comment(postid, content, session['user'].id, parentid = parentid)
        comment.save()
        return 'true'

@userapp.route('/voteforpost',methods =['PUT'])
def voteforpost():
    postid = request.form['postid']
    session['user'].vote_for(postid)
    return 'true'

@userapp.route('/star_pybook',methods =['PUT'])
def star_pybook():
    bookid = request.form['bookid']
    session['user'].toggle_star_book(bookid)
    return 'true'
    
def reset_password():
    pass
def reset_nickname():
    pass
def reset_email():
    pass
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.