def read(sid, cnum): conn = lookup.getConn(CONN) # print("sid: "+str(sid)) # print("cnum: "+str(cnum)) try: chapter = lookup.getChapter(conn, sid, cnum) print('Chapter dict:') print(chapter) cid = chapter['cid'] # print(cid) try: uid = session['uid'] #add to history print(lookup.addToHistory(conn, uid, sid)) comments = lookup.getComments(conn, uid, cid) # print('Comments:') # print(comments) infile = open(chapter['filename'], 'r') story = infile.read() infile.close() allch = lookup.getChapters(conn, sid) numChap = lookup.getNumChaps(conn, sid)['count(cid)'] # print(numChap) work = lookup.getStory(conn, sid) print(work) if uid == work['uid']: allComments = lookup.getAllComments(conn, cid) else: allComments = None if 'username' not in session: return redirect(url_for('index')) if session['username'] == work['username']: isUpdate = True else: isUpdate = False return render_template('read.html', title=work['title'], story=story, chapter=chapter, author=work['username'], cnum=cnum, sid=sid, update=isUpdate, allch=allch, comments=comments, uid=uid, maxCh=numChap, allComments=allComments) except Exception as err: print(err) return redirect(url_for('index')) except Exception as err: return redirect(url_for('notFound'))
def update(sid, cnum): try: conn = lookup.getConn(CONN) authorid = lookup.getAuthorId(conn, sid)[0] print(authorid, session['uid']) if 'uid' in session and session['uid'] == authorid: if request.method == "GET": chapter = lookup.getChapter(conn, sid, cnum) story = "" if chapter: infile = open(chapter['filename'], 'r') story = infile.read() infile.close() allch = lookup.getChapters(conn, sid) return render_template('write.html', title='Update Story', sid=sid, cnum=cnum, story=story, allch=allch) if request.method == "POST": sometext = request.form['write'] somehtml = bleach.clean( sometext, #allowed tags, attributes, and styles tags=[ 'b', 'blockquote', 'i', 'em', 'strong', 'p', 'ul', 'br', 'li', 'ol', 'span' ], attributes=['style'], styles=['text-decoration', 'text-align']) dirname = os.path.dirname(__file__) relative = 'uploaded/' + 'sid' + str(sid) + 'cnum' + str( cnum) + '.html' filename = os.path.join(dirname, relative) outfile = open(filename, 'w') outfile.write(somehtml) outfile.close() chapter = lookup.getChapter(conn, sid, cnum) if not chapter: lookup.setChapter(conn, sid, cnum, filename) return redirect(url_for('read', sid=sid, cnum=cnum)) else: flash('''You are not authorized to edit this work. Please log in with the account associated with this work''' ) return redirect(url_for('index')) except Exception as err: flash('some kind of error ' + str(err)) return redirect(url_for('index'))
def read(sid, cnum): conn = lookup.getConn(CONN) chapter = lookup.getChapter(conn, sid, cnum) print(chapter) try: infile = open(chapter['filename'], 'r') story = infile.read() infile.close() allch = lookup.getChapters(conn, sid) work = lookup.getStory(conn, sid) print(work) if 'username' not in session: return redirect(url_for('index')) if session['username'] == work['username']: return render_template('read.html', title=work['title'], story=story, chapter=chapter, author=work['username'], cnum=cnum, sid=sid, update=True, allch=allch) else: return render_template('read.html', title=work['title'], story=story, chapter=chapter, author=work['username'], cnum=cnum, sid=sid, update=False, allch=allch) except Exception as err: print(err) return redirect(url_for('index'))
def read(sid, cnum): conn = lookup.getConn(CONN) try: chapter = lookup.getChapter(conn, sid, cnum) cid = chapter['cid'] try: #check if they're logged in if 'username' not in session: return redirect(url_for('index')) uid = session['uid'] #add to history print(lookup.addToHistory(conn, uid, sid, cid)) #check if they've rated the piece already rating = lookup.getRating(conn, sid, uid) if rating is not None: rating = rating['rating'] avgRating = float( lookup.calcAvgRating(conn, sid)['avg(rating)']) else: avgRating = None #these are the comments the user has posted on this chapter comments = lookup.getComments(conn, uid, cid) story = "" with open(chapter['filename'], 'r') as infile: story = infile.read() # print("Read for Reading:" + story) isBookmarked = lookup.isBookmarked(conn, sid, uid) allch = lookup.getChapters(conn, sid) numChap = lookup.getNumChaps(conn, sid)['count(cid)'] work = lookup.getStory(conn, sid) #only display all comments on a work if the author is viewing #otherwise, users can only see the comments they've written if uid == work['uid']: allComments = lookup.getAllComments(conn, cid) else: allComments = None if session['username'] == work['username']: isUpdate = True else: isUpdate = False return render_template('read.html', page_title=work['title'], story=story, chapter=chapter, author=work['username'], cnum=cnum, isBookmark=isBookmarked, sid=sid, update=isUpdate, allch=allch, comments=comments, uid=uid, maxCh=numChap, allComments=allComments, old_rating=rating, avgRating=avgRating) except Exception as err: # print(err) return redirect(url_for('index')) except Exception as err: return redirect(url_for('notFound'))
def update(sid, cnum): try: conn = lookup.getConn(CONN) authorid = lookup.getAuthorId(conn, sid)[0] # print(authorid, session['uid']) if 'uid' in session and session['uid'] == authorid: if request.method == "GET": chapter = lookup.getChapter(conn, sid, cnum) story = "" if chapter: with open(chapter['filename'], 'r') as infile: # print("From db: "+chapter['filename']) story = infile.read() # print("Read for Update" + story) allch = lookup.getChapters(conn, sid) title = lookup.getTitle(conn, sid) return render_template('write.html', sid=sid, cnum=cnum, story=story, allch=allch, title=title['title'], page_title="Update '{}'".format( title['title'])) if request.method == "POST": sometext = request.form['write'] somehtml = bleach.clean( sometext, #allowed tags, attributes, and styles tags=[ 'b', 'blockquote', 'i', 'em', 'strong', 'p', 'ul', 'br', 'li', 'ol', 'span', 'pre' ], attributes=['style'], styles=['text-decoration', 'text-align']) dirname = os.path.dirname(__file__) relative = 'uploaded/' + 'sid' + str(sid) + 'cnum' + str( cnum) + '.html' filename = os.path.join(dirname, relative) # print(filename) with open(filename, 'w') as outfile: outfile.write(somehtml) # print("Where it's written:" + filename) # print("Write for Update" + somehtml) lock.acquire() chapter = lookup.getChapter(conn, sid, cnum) if chapter: cid = chapter['cid'] if not chapter: cid = None lookup.setChapter(conn, sid, cnum, cid, filename) # print("ok i got this") lock.release() return redirect(url_for('read', sid=sid, cnum=cnum)) else: flash('''You are not authorized to edit this work. Please log in with the account associated with this work''' ) return redirect(url_for('index')) except Exception as err: flash('Some kind of error ' + str(err)) return redirect(url_for('index'))