def update(id): entry = get_db().execute('SELECT * FROM Entry' ' WHERE Entry.id = ?', (id, )).fetchone() if request.method == 'POST': print(request.form) bookTitle = request.form['bookTitle'] entryText = request.form['entryText'] chapterNumber = request.form['chapterNumber'] error = None if not bookTitle: error = 'Title is required.' elif not entryText: error = 'Entry text is required.' elif not chapterNumber: error = 'Chapter number is required.' elif not representsInt(chapterNumber): error = 'Chapter number requires a valid int input.' elif int(chapterNumber) <= 0: error = 'Chapter number requires a valid int input.' if error is not None: flash(error) else: db = get_db() # First check if book already exists, if not add it bookId = db.execute('SELECT id FROM Book WHERE name = ?', (bookTitle, )).fetchone() if bookId is None: db.execute('INSERT INTO Book (name) VALUES (?)', (bookTitle, )) db.commit() bookId = db.execute('SELECT id FROM Book WHERE name = ?', (bookTitle, )).fetchone()['id'] else: bookId = bookId['id'] db.execute( 'INSERT INTO EntryData (entryText, modified, entryNumber, chapterNumber, bookId) ' 'VALUES (?, CURRENT_TIMESTAMP, ?, ?, ?)', ( entryText, id, chapterNumber, bookId, )) # Since we edited the content of an entry, change the lastModified value for it so that it is updated db.execute( 'UPDATE Entry SET lastModified = CURRENT_TIMESTAMP WHERE id = ?', (id, )) db.commit() return redirect(url_for('fwiki.index')) return render_template('wiki-pages/update.html', entry=entry)
def create(): if request.method == 'POST': entry = request.form['entry'] error = None if not entry: error = 'Title is required.' if error is not None: flash(error) else: db = get_db() check = db.execute('SELECT title FROM Entry WHERE title=?', (entry, )).fetchone() if check is None: db.execute( 'INSERT INTO Entry (title, lastModified)' ' VALUES (?, CURRENT_TIMESTAMP)', (entry, )) db.commit() return redirect(url_for('fwiki.index')) else: error = 'The entry with that title already exists.' flash(error) return render_template('wiki-pages/newentry.html')
def getBook(): book = get_db().execute( 'SELECT name FROM Book ORDER BY Book.name').fetchall() bookEntry = [] for e in book: bookEntry.append(e['name']) return bookEntry
def register(): if request.method == 'POST': username = escape(request.form['email']) password = escape(request.form['password']) first = escape(request.form['first']) last = escape(request.form['last']) db = get_db() error = None if not username: error = 'Username is required.' elif not password: error = 'Password is required.' if not first: error = 'First name is required.' elif not last: error = 'Last name is required.' elif db.execute('SELECT id FROM User WHERE email = ?', (username, )).fetchone() is not None: error = 'User {} is already registered.'.format(username) if error is None: db.execute( 'INSERT INTO User (email, password, first, last) VALUES (?, ?, ?, ?)', (username, generate_password_hash(password), first, last)) db.commit() return redirect(url_for('auth.login')) flash(error) return render_template('auth/register.html')
def load_logged_in_user(): user_id = session.get('user_id') if user_id is None: g.user = None else: g.user = get_db().execute('SELECT * FROM User WHERE id = ?', (user_id, )).fetchone()
def getEntry(title): entry = get_entry(title) entryInfo = [] userReadTo = get_db().execute( 'SELECT chapterNumber, book ' 'FROM ReadTo WHERE user = ?', (g.user['id'], )).fetchall() for e in entry: canAddOutside = True for rt in userReadTo: if rt['book'] == e['bookId']: canAddOutside = False if int(rt['chapterNumber']) >= int(e['chapterNumber']): entryInfo.append(e['entryText']) if canAddOutside: entryInfo.append(e['entryText']) print(entryInfo) entry = get_db().execute('SELECT * FROM Entry' ' WHERE Entry.title = ?', (title, )).fetchall() return render_template('wiki-pages/entry.html', entry=entry[0], info=entryInfo)
def get_entry(title, check_author=True): entry = get_db().execute( 'SELECT * FROM Entry' ' INNER JOIN EntryData ON Entry.id = EntryData.entryNumber' ' WHERE Entry.title = ?', (title, )).fetchall() if entry is None: abort(404, "Entry titled {0} doesn't exist.".format(id)) # if check_author and entry['author_id'] != g.user['id']: # abort(403) return entry
def changeReadTo(): bookEntry = getBook() if request.method == 'POST': db = get_db() book = request.form.get('book') chapter = escape(request.form['chapter']) error = None num = get_book_id(book) if not chapter: error = "You must enter a chapter." flash(error) elif not representsInt(chapter): error = 'Chapter number requires a valid int input.' flash(error) elif int(chapter) <= 0: error = 'Chapter number requires a valid int input.' flash(error) else: check = db.execute( 'SELECT ReadTo.book FROM' ' ReadTo INNER JOIN Book ON Book.id = ReadTo.book' ' WHERE Book.name = ? AND ReadTo.user = ?', (book, g.user['id'])).fetchone() if check is None: db.execute( 'INSERT INTO ReadTo' ' (book, chapterNumber, user)' ' VALUES' ' (?, ?, ?)', (num, chapter, g.user['id'])) db.commit() else: db.execute( 'UPDATE ReadTo' ' SET chapterNumber=? ' ' WHERE book=? AND user = ?', (chapter, num, g.user['id'])) db.commit() return render_template('wiki-pages/change.html', entry=bookEntry)
def login(): if request.method == 'POST': username = escape(request.form['email']) password = escape(request.form['password']) db = get_db() error = None user = db.execute( 'SELECT id, email, password FROM User WHERE email =?', (username, )).fetchone() if user is None: error = 'Incorrect username.' elif not check_password_hash(user['password'], password): error = 'Incorrect password.' if error is None: session.clear() session['user_id'] = user['id'] return redirect(url_for('index')) flash(error) return render_template('auth/login.html')
def deleteEntryData(entryText): db = get_db() db.execute('DELETE FROM EntryData WHERE entryText = ?', (entryText, )) db.commit() return redirect(url_for('fwiki.index'))
def delete(id): db = get_db() db.execute('DELETE FROM Entry WHERE id = ?', (id, )) db.commit() return redirect(url_for('fwiki.index'))
def get_book_id(title): db = get_db() num = db.execute('SELECT id FROM Book WHERE name = ?', (title, )).fetchone()['id'] return num
def index(): db = get_db() posts = db.execute('SELECT id, title, lastModified' ' FROM Entry' ' ORDER BY lastModified DESC').fetchall() return render_template('wiki-pages/index.html', posts=posts)