def search():
    print "in search"
    name = request.form['cname']
    query = Query()
    courses = query.getCourseInfoByName(name)
    cid = courses[0][0]
    reviews = query.getReviewsByCid(cid)
    revList = [r[5] for r in reviews]
    if len(revList) == 0:
        revList.append("")
    # print revList
    combinedRev = ReviewSummarizer(revList).getSummarizedText()
    revTags = SentimentAnalyser(revList).senti_pretrained()
    # print revTags

    try:
        stat = roundVals(query.get_stats(cid)[0])
    except Exception:
        stat = (0,0,0)

    print reviews

    if request.method == 'POST':

        if len(reviews) != 0 and len(revTags) !=0:
            taggedRevs = tagRevs(reviews,revTags)
        else:
            taggedRevs = tagRevs(reviews,[])
        return render_template('course.html', course=courses[0], revs=reviews, stat=stat,
                               combRev=combinedRev, taggedRevs=taggedRevs, session = session)
    return "what"
def addCom():
    print "in add com"
    print request.method
    cid = request.args.get('courseId')
    print cid
    if request.method == 'POST':
        query = Query()
        cid2 = request.form['cid']

        query.ins_review_replWrd(session['email'], request.form['toughness'], request.form['work'], request.form['rating'],
                                 request.form['rev'], cid2,"f**k","fudge")

        reviews = query.getReviewsByCid(cid2)
        course = query.getCourseInfoByCid(cid2)
        stat = roundVals(query.get_stats(cid2)[0])

        # reviews = query.getReviewsByCid(cid2)
        revList = [r[5] for r in reviews]
        combinedRev = ReviewSummarizer(revList).getSummarizedText()
        revTags = SentimentAnalyser(revList).senti_pretrained()
        taggedRevs = tagRevs(reviews, revTags)
        return render_template('course.html', course=course[0], revs=reviews, stat=stat,
                               combRev=combinedRev, session = session, taggedRevs=taggedRevs)

    return render_template('comment.html', type='com', cid=cid, session = session)
def delete():
    print "in delete"
    postId = request.args.get('postId')
    print postId
    query = Query()
    query.deleteReview(postId)
    cid = request.args.get('cid')
    reviews = query.getReviewsByCid(cid)
    courses = query.getCourseInfoByCid(cid)
    stat = roundVals(query.get_stats(cid)[0])

    revList = [r[5] for r in reviews]
    combinedRev = ReviewSummarizer(revList).getSummarizedText()
    revTags = SentimentAnalyser(revList).senti_pretrained()
    taggedRevs = tagRevs(reviews, revTags)

    return render_template('course.html', course=courses[0], revs=reviews, stat=stat,
                           session = session, combRev=combinedRev, taggedRevs=taggedRevs)
def update():
    print "in update"
    query = Query()


    cid = request.args.get('cid')
    rev = []

    if request.method == 'GET':
        postId = request.args.get('postId')
        rev = query.getReviewById(postId)
        print postId
        print rev

    if request.method == 'POST':
        cid2 = request.form['cid']
        postId2 = request.form['revid']


        query.updateRev(postId2, request.form['rev'], request.form['toughness'],
                                 request.form['work'],
                                 request.form['rating'])

        reviews = query.getReviewsByCid(cid2)
        course = query.getCourseInfoByCid(cid2)
        stat = roundVals(query.get_stats(cid2)[0])

        revList = [r[5] for r in reviews]
        combinedRev = ReviewSummarizer(revList).getSummarizedText()
        revTags = SentimentAnalyser(revList).senti_pretrained()
        taggedRevs = tagRevs(reviews, revTags)

        return render_template('course.html', course=course[0], revs=reviews, stat=stat,
                               session = session, combRev=combinedRev, taggedRevs=taggedRevs)

    if len(rev) <= 0:
        rev = [""]
    return render_template('comment.html', type='up', cid=cid, rev=rev[0], revid=postId, session = session)