Esempio n. 1
0
 def post(self, notebook_id):
     logging.info("CreateNotebook: %s " % notebook_id)
     n = Notebooks(
         key_name=notebook_id,
         user=self.request.get("user_ID"),
         moreinfo=self.request.get("moreinfo"),
         deleteRequested=False,
     )
     n.put()
     return webapp2.redirect("/")
Esempio n. 2
0
def save_shared(link):
    note_id = re.search('[0-9]+', link.split('-')[1])
    if note_id is None:
        flash(
            'There seems to be an error here, sorry.  We will check into this',
            'danger')
        return redirect(url_for('index'))
    note_id = int(note_id.group(0))
    n = Notes.query.get(note_id)
    key = str(n.id) + str(n.user_id)
    if link.split('-')[0] == hashlib.sha224(str(n.title) + "," +
                                            key).hexdigest():
        if not g.user.notebooks.filter_by(title='Shared Notes').first():
            nb = Notebooks(title="Shared Notes",
                           timestamp=datetime.utcnow(),
                           notebook=g.user)
            db.session.add(nb)
        else:
            #User.query.filter_by(nickname = nickname).first()
            nb = g.user.notebooks.filter_by(title='Shared Notes').first()
        # this is here due to real server having slightly different encryption
        # (padding)
        note_body = decrypt_it(n.body)
        note_body = note_body.decode('utf8', errors='ignore')
        nn = Notes(title=n.title,
                   body=encrypt_it(note_body),
                   timestamp=datetime.utcnow(),
                   notes_link=nb,
                   note=g.user)
        db.session.add(nn)
        db.session.commit()
        return redirect(url_for('members'))
    flash('There has been an error.  Sorry, we will look into the problem',
          'danger')
    return redirect(url_for('index'))
Esempio n. 3
0
def members(page=0, booked=0):
    user = g.user
    fc, books, notes = g.user.get_books()
    form = Note_Form()
    form2 = Note_Title()
    form3_new_notebook = New_Notebook()
    form4_new_note_title = Rename_Note()
    form5_new_notebook_title = Rename_Notebook()
    form6_new_fc_title = Rename_FileCabinet()
    form7_new_fc = New_FileCabinet()
    form8_new_fcnotebook = New_FCNotebook()
    form9_move_notebook = Move_Notebook()
    attach = Attach(csrf_enabled=False)
    booked2 = ""
    fcd = 0
    if booked > 0:
        if booked == None:
            flash("Notebook not found.  If you think this is in error, please contact us.", "danger")
            return redirect(url_for("members"))
        nbid = Notebooks.query.get(int(booked))
        if notebook_check_out(nbid):
            booked2 = nbid.title
            fcd = nbid.filecabinet
    note_counter = {}
    form9_move_notebook.fc_select.choices = [(fcg.id, fcg.title) for fcg in fc]
    for book in books:  # count the notes in each book for badges
        for note in notes:
            if note.notebooks_id == book.id:
                if book.id in note_counter:
                    note_counter[book.id] += 1
                else:
                    note_counter[book.id] = 1
            elif (
                note.notebooks_id == None
            ):  # delete the orphans (might as well when we are already going through the notebooks)
                n = Notes.query.get(note.id)
                db.session.delete(n)
    db.session.commit()
    if form9_move_notebook.validate_on_submit():  # move notebooks to file cabinets
        new_fc_id = form9_move_notebook.fc_select.data
        if new_fc_id == None or type(new_fc_id) != int:
            flash("There seems to be a problem. If you think this is in error, please contact us.", "danger")
        fc = Filecabinets.query.get(int(new_fc_id))
        if filecabinet_check_out(fc):
            nb_id = re.search("[0-9]+", form9_move_notebook.hidden_nb_id2.data)
            if nb_id == None:
                flash("There seems to be a problem. If you think this is in error, please contact us.", "danger")
            nb = Notebooks.query.get(int(nb_id.group(0)))
            if notebook_check_out(nb):
                nb.notebook_link = fc
                db.session.add(nb)
                db.session.commit()
                return redirect(url_for("members", page=0, booked=nb.id))

    if form3_new_notebook.validate_on_submit():  # new notebook
        nb_title = nohtml(form3_new_notebook.book_title.data)
        if len(nb_title) > 40:
            flash("Please make the Notebook title shorter", "danger")
            return redirect(url_for("members"))
        nb = Notebooks(title=nb_title, timestamp=datetime.utcnow(), notebook=g.user)
        db.session.add(nb)
        nn_title = nohtml(form3_new_notebook.new_note_title.data)
        if len(nn_title) > 40:
            flash("Please make the Note title shorter", "danger")
        nn = Notes(title=nn_title, body=" ", timestamp=datetime.utcnow(), notes_link=nb, note=g.user)
        db.session.add(nn)
        db.session.commit()
        return redirect(url_for("members", page=nn.id, booked=nb.id))

    if form2.validate_on_submit():  # new note form
        nbid = form2.hidden2.data
        if nbid == None:
            flash("Notebook not found.  If you think this is in error, please contact us.", "danger")
            return redirect(url_for("members"))
        nb = Notebooks.query.get(int(nbid))
        if notebook_check_out(nb):
            tt = nohtml(form2.notetitle.data)
            if len(tt) > 40:
                flash("Can you make it smaller please?", "danger")
                return redirect(url_for("members"))
            nn = Notes(title=tt, body=" ", timestamp=datetime.utcnow(), notes_link=nb, note=g.user)
            db.session.add(nn)
            db.session.commit()
            return redirect(url_for("members", page=nn.id, booked=int(nbid)))

    if form4_new_note_title.validate_on_submit():  # rename note
        ids = re.search("[0-9]+", form4_new_note_title.hidden_note_id.data)
        if ids == None:
            flash("Note not found.  If you think this is in error, please contact us.", "danger")
            return redirect(url_for("members"))
        n = Notes.query.get(int(ids.group(0)))
        if note_check_out(n):
            nn_title = nohtml(form4_new_note_title.new_title.data)
            if len(nn_title) > 40:
                flash("Please make the Note title shorter", "danger")
            n.title = nn_title
            db.session.add(n)
            db.session.commit()
            return redirect(url_for("members", page=n.id, booked=n.notebooks_id))

    if form7_new_fc.validate_on_submit():  # new file cabinet
        fc_title = nohtml(form7_new_fc.fc_title.data)
        if len(fc_title) > 40:
            flash("Please make the File Cabinet title shorter", "danger")
            return redirect(url_for("members"))
        nfc = Filecabinets(title=fc_title, filecabinet=g.user)
        db.session.add(nfc)
        nb_title = nohtml(form7_new_fc.nb_title.data)
        if len(nb_title) > 40:
            flash("Please make the Notebook title shorter", "danger")
        nb = Notebooks(title=nb_title, timestamp=datetime.utcnow(), notebook=g.user, notebook_link=nfc)
        db.session.add(nb)
        db.session.commit()
        return redirect(url_for("members", page=0, booked=nb.id))

    if form5_new_notebook_title.validate_on_submit():  # Rename Notebook
        nbid = re.search("[0-9]+", form5_new_notebook_title.hidden_book_id.data)
        if nbid == None:
            flash("Notebook not found.  If you think this is in error, please contact us.", "danger")
            return redirect(url_for("members"))
        nb = Notebooks.query.get(int(nbid.group(0)))
        if notebook_check_out(nb):
            nb_title = nohtml(form5_new_notebook_title.new_nbtitle.data)
            if len(nb_title) > 40:
                flash("Please make the Notebook title shorter", "danger")
                return redirect(url_for("members"))
            nb.title = nb_title
            db.session.add(nb)
            db.session.commit()
            return redirect(url_for("members", page=0, booked=nb.id))

    if form6_new_fc_title.validate_on_submit():  # rename file cabinet title
        fcid = re.search("[0-9]+", form6_new_fc_title.hidden_fc_id.data)
        if fcid == None:
            flash("File Cabinet not found.  If you think this is in error, please contact us.", "danger")
            return redirect(url_for("members"))
        nfc = Filecabinets.query.get(int(fcid.group(0)))
        if filecabinet_check_out(nfc):
            fc_title = nohtml(form6_new_fc_title.new_fctitle.data)
            if len(fc_title) > 40:
                flash("Please make the File Cabinet title shorter", "danger")
                return redirect(url_for("members"))
            nfc.title = fc_title
            db.session.add(nfc)
            db.session.commit()
            return redirect(url_for("members"))

    if form8_new_fcnotebook.validate_on_submit():  # new file cabinet notebook
        fcid = re.search("[0-9]+", form8_new_fcnotebook.hidden_fc_id2.data)
        if fcid == None:
            flash("File Cabinet not found.  If you think this is in error, please contact us.", "danger")
            return redirect(url_for("members"))
        nfc = Filecabinets.query.get(int(fcid.group(0)))
        if filecabinet_check_out(nfc):
            nb_title = nohtml(form8_new_fcnotebook.fcbook_title.data)
            if len(nb_title) > 40:
                flash("Please make the Notebook title shorter", "danger")
                return redirect(url_for("members"))
            nb = Notebooks(title=nb_title, timestamp=datetime.utcnow(), notebook=g.user, notebook_link=nfc)
            db.session.add(nb)
            nn_title = nohtml(form8_new_fcnotebook.fcnew_note_title.data)
            if len(nn_title) > 40:
                flash("Please make the Note title shorter", "danger")
            nn = Notes(title=nn_title, body=" ", timestamp=datetime.utcnow(), notes_link=nb, note=g.user)
            db.session.add(nn)
            db.session.commit()
            return redirect(url_for("members", page=nn.id, booked=nb.id))

    return render_template(
        "members.html",
        title="Members",
        user=user,
        form=form,
        form2=form2,
        form3_new_notebook=form3_new_notebook,
        form4_new_note_title=form4_new_note_title,
        form5_new_notebook_title=form5_new_notebook_title,
        form6_new_fc_title=form6_new_fc_title,
        form7_new_fc=form7_new_fc,
        form8_new_fcnotebook=form8_new_fcnotebook,
        note_counter=note_counter,
        fc=fc,
        fcd=fcd,
        page=page,
        attach=attach,
        booked=booked,
        booked2=booked2,
        books=books,
        form9_move_notebook=form9_move_notebook,
        notes=notes,
    )
Esempio n. 4
0
def members(page=0, booked=0):
    user = g.user
    fc, books, notes = g.user.get_books()
    form = Note_Form()
    form2 = Note_Title()
    form3_new_notebook = New_Notebook()
    form4_new_note_title = Rename_Note()
    form5_new_notebook_title = Rename_Notebook()
    form6_new_fc_title = Rename_FileCabinet()
    form7_new_fc = New_FileCabinet()
    form8_new_fcnotebook = New_FCNotebook()
    form9_move_notebook = Move_Notebook()
    merge = Merge()
    attach = Attach(csrf_enabled=False)
    notebook_title = ""  # notebook title
    fcd = 0
    note_counter = {}

    # Pass if this note is passphrased or not
    if page > 0:
        page_note = Notes.query.get(page).passphrased
    else:
        page_note = ""

    # Make sure the notebook is theirs
    if booked > 0:
        if booked is None:
            flash(
                'Notebook not found.  If you think this is in error, please contact us.',
                'danger')
            return redirect(url_for('members'))
        nbid = Notebooks.query.get(int(booked))
        if notebook_check_out(nbid):
            notebook_title = nbid.title
            fcd = nbid.filecabinet
        else:
            return redirect(url_for('members'))

    # Populate the list choices
    form9_move_notebook.fc_select.choices = [(fcg.id, fcg.title) for fcg in fc]
    merge.nt_select.choices = [(nt.id, nt.title) for nt in notes]

    # Count the notes in each book for badges
    for book in books:
        for note in notes:
            if note.notebooks_id == book.id:
                if book.id in note_counter:
                    note_counter[book.id] += 1
                else:
                    note_counter[book.id] = 1
            elif note.notebooks_id is None:  # delete the orphans
                n = Notes.query.get(note.id)
                db.session.delete(n)
    db.session.commit()

    # New note
    if form2.validate_on_submit():
        nbid = form2.hidden2.data
        if nbid is None:
            flash(
                'Notebook not found.  If you think this is in error, please contact us.',
                'danger')
            return redirect(url_for('members'))
        nb = Notebooks.query.get(int(nbid))
        if notebook_check_out(nb):
            tt = nohtml(form2.notetitle.data)
            if len(tt) > 40:
                flash('Can you make it smaller please?', 'danger')
                return redirect(url_for('members'))
            nn = Notes(title=tt,
                       body=" ",
                       timestamp=datetime.utcnow(),
                       notes_link=nb,
                       note=g.user)
            db.session.add(nn)
            db.session.commit()
            return redirect(url_for('members', page=nn.id, booked=int(nbid)))

    # New notebook
    if form3_new_notebook.validate_on_submit():
        nb_title = nohtml(form3_new_notebook.book_title.data)
        if len(nb_title) > 40:
            flash('Please make the Notebook title shorter', 'danger')
            return redirect(url_for('members'))
        nb = Notebooks(title=nb_title,
                       timestamp=datetime.utcnow(),
                       notebook=g.user)
        db.session.add(nb)
        nn_title = nohtml(form3_new_notebook.new_note_title.data)
        if len(nn_title) > 40:
            flash('Please make the Note title shorter', 'danger')
        nn = Notes(title=nn_title,
                   body=" ",
                   timestamp=datetime.utcnow(),
                   notes_link=nb,
                   note=g.user)
        db.session.add(nn)
        db.session.commit()
        return redirect(url_for('members', page=nn.id, booked=nb.id))

    # Rename note
    if form4_new_note_title.validate_on_submit():
        ids = re.search('[0-9]+', form4_new_note_title.hidden_note_id.data)
        if ids is None:
            flash(
                'Note not found.  If you think this is in error, please contact us.',
                'danger')
            return redirect(url_for('members'))
        n = Notes.query.get(int(ids.group(0)))
        if note_check_out(n):
            nn_title = nohtml(form4_new_note_title.new_title.data)
            if len(nn_title) > 40:
                flash('Please make the Note title shorter', 'danger')
            n.title = nn_title
            db.session.add(n)
            db.session.commit()
            return redirect(
                url_for('members', page=n.id, booked=n.notebooks_id))
        return redirect(url_for('members'))

    # Rename notebook
    if form5_new_notebook_title.validate_on_submit():
        nbid = re.search('[0-9]+',
                         form5_new_notebook_title.hidden_book_id.data)
        if nbid is None:
            flash(
                'Notebook not found.  If you think this is in error, please contact us.',
                'danger')
            return redirect(url_for('members'))
        nb = Notebooks.query.get(int(nbid.group(0)))
        if notebook_check_out(nb):
            nb_title = nohtml(form5_new_notebook_title.new_nbtitle.data)
            if len(nb_title) > 40:
                flash('Please make the Notebook title shorter', 'danger')
                return redirect(url_for('members'))
            nb.title = nb_title
            db.session.add(nb)
            db.session.commit()
            return redirect(url_for('members', page=0, booked=nb.id))
        return redirect(url_for('members'))

    # Rename file cabinet title
    if form6_new_fc_title.validate_on_submit():
        fcid = re.search('[0-9]+', form6_new_fc_title.hidden_fc_id.data)
        if fcid is None:
            flash(
                'File Cabinet not found.  If you think this is in error, please contact us.',
                'danger')
            return redirect(url_for('members'))
        nfc = Filecabinets.query.get(int(fcid.group(0)))
        if filecabinet_check_out(nfc):
            fc_title = nohtml(form6_new_fc_title.new_fctitle.data)
            if len(fc_title) > 40:
                flash('Please make the File Cabinet title shorter', 'danger')
                return redirect(url_for('members'))
            nfc.title = fc_title
            db.session.add(nfc)
            db.session.commit()
            return redirect(url_for('members'))
        return redirect(url_for('members'))

    # New file cabinet
    if form7_new_fc.validate_on_submit():
        fc_title = nohtml(form7_new_fc.fc_title.data)
        if len(fc_title) > 40:
            flash('Please make the File Cabinet title shorter', 'danger')
            return redirect(url_for('members'))
        nfc = Filecabinets(title=fc_title, filecabinet=g.user)
        db.session.add(nfc)
        nb_title = nohtml(form7_new_fc.nb_title.data)
        if len(nb_title) > 40:
            flash('Please make the Notebook title shorter', 'danger')
        nb = Notebooks(title=nb_title,
                       timestamp=datetime.utcnow(),
                       notebook=g.user,
                       notebook_link=nfc)
        db.session.add(nb)
        db.session.commit()
        return redirect(url_for('members', page=0, booked=nb.id))

    # New notebook in a file cabinet
    if form8_new_fcnotebook.validate_on_submit():
        fcid = re.search('[0-9]+', form8_new_fcnotebook.hidden_fc_id2.data)
        if fcid is None:
            flash(
                'File Cabinet not found.  If you think this is in error, please contact us.',
                'danger')
            return redirect(url_for('members'))
        nfc = Filecabinets.query.get(int(fcid.group(0)))
        if filecabinet_check_out(nfc):
            nb_title = nohtml(form8_new_fcnotebook.fcbook_title.data)
            if len(nb_title) > 40:
                flash('Please make the Notebook title shorter', 'danger')
                return redirect(url_for('members'))
            nb = Notebooks(title=nb_title,
                           timestamp=datetime.utcnow(),
                           notebook=g.user,
                           notebook_link=nfc)
            db.session.add(nb)
            nn_title = nohtml(form8_new_fcnotebook.fcnew_note_title.data)
            if len(nn_title) > 40:
                flash('Please make the Note title shorter', 'danger')
            nn = Notes(title=nn_title,
                       body=" ",
                       timestamp=datetime.utcnow(),
                       notes_link=nb,
                       note=g.user)
            db.session.add(nn)
            db.session.commit()
            return redirect(url_for('members', page=nn.id, booked=nb.id))
        return redirect(url_for('members'))

    # Move notebooks to file cabinets
    if form9_move_notebook.validate_on_submit():
        new_fc_id = form9_move_notebook.fc_select.data
        if new_fc_id is None or not isinstance(new_fc_id, int):
            flash(
                'There seems to be a problem. If you think this is in error, please contact us.',
                'danger')
        fc = Filecabinets.query.get(int(new_fc_id))
        if filecabinet_check_out(fc):
            nb_id = re.search('[0-9]+', form9_move_notebook.hidden_nb_id2.data)
            if nb_id is None:
                flash(
                    'There seems to be a problem. If you think this is in error, please contact us.',
                    'danger')
            nb = Notebooks.query.get(int(nb_id.group(0)))
            if notebook_check_out(nb):
                nb.notebook_link = fc
                db.session.add(nb)
                db.session.commit()
                return redirect(url_for('members', page=0, booked=nb.id))
            return redirect(url_for('members'))
        return redirect(url_for('members'))

    # Merge two notes
    if merge.validate_on_submit():
        mergee_id = re.search('[0-9]+', merge.merge_note_id.data)
        if mergee_id is None:
            flash(
                'Note not found.  If you think this is in error, please contact us.',
                'danger')
            return redirect(url_for('members'))
        n = Notes.query.get(int(mergee_id.group(0)))
        merger_id = merge.nt_select.data
        if int(mergee_id.group(0)) == int(merger_id):
            flash('You can not merge a Note into itself', 'danger')
            return redirect(url_for('members'))
        if n.passphrased:
            flash('You can not merge a passphrased note', 'danger')
            return redirect(url_for('members'))
        nn = Notes.query.get(int(merger_id))
        if nn.passphrased:
            flash('You can not merge a passphrased note', 'danger')
            return redirect(url_for('members'))
        if note_check_out(n) and note_check_out(nn):
            if len(n.body) < 2 or len(nn.body) < 2:
                flash('You can not merge an empty note', 'danger')
                return redirect(url_for('members'))
            mergee = decrypt_it(n.body)
            merger = decrypt_it(nn.body)
            new_note = mergee.decode('utf8', errors='ignore') + merger.decode(
                'utf8', errors='ignore')
            if len(new_note) <= 524279:
                n.body = encrypt_it(new_note)
                db.session.add(n)
                db.session.delete(nn)
                db.session.commit()
                return redirect(
                    url_for('members', page=n.id, booked=n.notebooks_id))
        return redirect(url_for('members'))

    return render_template(
        "members.html",
        title='Members',
        user=user,
        form=form,
        form2=form2,
        form3_new_notebook=form3_new_notebook,
        form4_new_note_title=form4_new_note_title,
        form5_new_notebook_title=form5_new_notebook_title,
        form6_new_fc_title=form6_new_fc_title,
        form7_new_fc=form7_new_fc,
        form8_new_fcnotebook=form8_new_fcnotebook,
        form9_move_notebook=form9_move_notebook,
        merge=merge,
        note_counter=note_counter,
        fc=fc,
        fcd=fcd,
        page=page,  # reload with right note selected
        page_passphrased=page_note,
        attach=attach,
        booked=booked,  # reload with right notebook selected
        notebook_title=notebook_title,
        books=books,
        notes=notes)
Esempio n. 5
0
def members(page=0, booked=0):
    user = g.user
    fc, books, notes = g.user.get_books()
    form = Note_Form()
    form2 = Note_Title()
    form3_new_notebook = New_Notebook()
    form4_new_note_title = Rename_Note()
    form5_new_notebook_title = Rename_Notebook()
    form6_new_fc_title = Rename_FileCabinet()
    form7_new_fc = New_FileCabinet()
    form8_new_fcnotebook = New_FCNotebook()
    form9_move_notebook = Move_Notebook()
    merge = Merge()
    attach = Attach(csrf_enabled=False)
    notebook_title = ""  # notebook title
    fcd = 0
    note_counter = {}
    
    # Pass if this note is passphrased or not
    if page > 0:
        page_note = Notes.query.get(page).passphrased
    else:
        page_note = ""

    # Make sure the notebook is theirs
    if booked > 0:
        if booked is None:
            flash(
                'Notebook not found.  If you think this is in error, please contact us.',
                'danger')
            return redirect(url_for('members'))
        nbid = Notebooks.query.get(int(booked))
        if notebook_check_out(nbid):
            notebook_title = nbid.title
            fcd = nbid.filecabinet
        else:
            return redirect(url_for('members'))

    # Populate the list choices
    form9_move_notebook.fc_select.choices = [(fcg.id, fcg.title) for fcg in fc]
    merge.nt_select.choices = [(nt.id, nt.title) for nt in notes]

    # Count the notes in each book for badges
    for book in books:
        for note in notes:
            if note.notebooks_id == book.id:
                if book.id in note_counter:
                    note_counter[book.id] += 1
                else:
                    note_counter[book.id] = 1
            elif note.notebooks_id is None:  # delete the orphans
                n = Notes.query.get(note.id)
                db.session.delete(n)
    db.session.commit()

    # New note
    if form2.validate_on_submit():
        nbid = form2.hidden2.data
        if nbid is None:
            flash(
                'Notebook not found.  If you think this is in error, please contact us.',
                'danger')
            return redirect(url_for('members'))
        nb = Notebooks.query.get(int(nbid))
        if notebook_check_out(nb):
            tt = nohtml(form2.notetitle.data)
            if len(tt) > 40:
                flash('Can you make it smaller please?', 'danger')
                return redirect(url_for('members'))
            nn = Notes(title=tt,
                       body=" ",
                       timestamp=datetime.utcnow(),
                       notes_link=nb,
                       note=g.user)
            db.session.add(nn)
            db.session.commit()
            return redirect(url_for('members', page=nn.id,
                                    booked=int(nbid)))

    # New notebook
    if form3_new_notebook.validate_on_submit():
        nb_title = nohtml(form3_new_notebook.book_title.data)
        if len(nb_title) > 40:
            flash('Please make the Notebook title shorter', 'danger')
            return redirect(url_for('members'))
        nb = Notebooks(title=nb_title,
                       timestamp=datetime.utcnow(),
                       notebook=g.user)
        db.session.add(nb)
        nn_title = nohtml(form3_new_notebook.new_note_title.data)
        if len(nn_title) > 40:
            flash('Please make the Note title shorter', 'danger')
        nn = Notes(title=nn_title,
                   body=" ",
                   timestamp=datetime.utcnow(),
                   notes_link=nb,
                   note=g.user)
        db.session.add(nn)
        db.session.commit()
        return redirect(url_for('members', page=nn.id, booked=nb.id))

    # Rename note
    if form4_new_note_title.validate_on_submit():
        ids = re.search('[0-9]+', form4_new_note_title.hidden_note_id.data)
        if ids is None:
            flash(
                'Note not found.  If you think this is in error, please contact us.',
                'danger')
            return redirect(url_for('members'))
        n = Notes.query.get(int(ids.group(0)))
        if note_check_out(n):
            nn_title = nohtml(form4_new_note_title.new_title.data)
            if len(nn_title) > 40:
                flash('Please make the Note title shorter', 'danger')
            n.title = nn_title
            db.session.add(n)
            db.session.commit()
            return redirect(url_for('members', page=n.id,
                                    booked=n.notebooks_id))
        return redirect(url_for('members'))

    # Rename notebook
    if form5_new_notebook_title.validate_on_submit():
        nbid = re.search(
            '[0-9]+',
            form5_new_notebook_title.hidden_book_id.data)
        if nbid is None:
            flash(
                'Notebook not found.  If you think this is in error, please contact us.',
                'danger')
            return redirect(url_for('members'))
        nb = Notebooks.query.get(int(nbid.group(0)))
        if notebook_check_out(nb):
            nb_title = nohtml(form5_new_notebook_title.new_nbtitle.data)
            if len(nb_title) > 40:
                flash('Please make the Notebook title shorter', 'danger')
                return redirect(url_for('members'))
            nb.title = nb_title
            db.session.add(nb)
            db.session.commit()
            return redirect(url_for('members', page=0,
                                    booked=nb.id))
        return redirect(url_for('members'))

    # Rename file cabinet title
    if form6_new_fc_title.validate_on_submit():
        fcid = re.search('[0-9]+', form6_new_fc_title.hidden_fc_id.data)
        if fcid is None:
            flash(
                'File Cabinet not found.  If you think this is in error, please contact us.',
                'danger')
            return redirect(url_for('members'))
        nfc = Filecabinets.query.get(int(fcid.group(0)))
        if filecabinet_check_out(nfc):
            fc_title = nohtml(form6_new_fc_title.new_fctitle.data)
            if len(fc_title) > 40:
                flash('Please make the File Cabinet title shorter', 'danger')
                return redirect(url_for('members'))
            nfc.title = fc_title
            db.session.add(nfc)
            db.session.commit()
            return redirect(url_for('members'))
        return redirect(url_for('members'))

    # New file cabinet
    if form7_new_fc.validate_on_submit():
        fc_title = nohtml(form7_new_fc.fc_title.data)
        if len(fc_title) > 40:
            flash('Please make the File Cabinet title shorter', 'danger')
            return redirect(url_for('members'))
        nfc = Filecabinets(title=fc_title, filecabinet=g.user)
        db.session.add(nfc)
        nb_title = nohtml(form7_new_fc.nb_title.data)
        if len(nb_title) > 40:
            flash('Please make the Notebook title shorter', 'danger')
        nb = Notebooks(title=nb_title,
                       timestamp=datetime.utcnow(),
                       notebook=g.user,
                       notebook_link=nfc)
        db.session.add(nb)
        db.session.commit()
        return redirect(url_for('members', page=0,
                                booked=nb.id))

    # New notebook in a file cabinet
    if form8_new_fcnotebook.validate_on_submit():
        fcid = re.search('[0-9]+', form8_new_fcnotebook.hidden_fc_id2.data)
        if fcid is None:
            flash(
                'File Cabinet not found.  If you think this is in error, please contact us.',
                'danger')
            return redirect(url_for('members'))
        nfc = Filecabinets.query.get(int(fcid.group(0)))
        if filecabinet_check_out(nfc):
            nb_title = nohtml(form8_new_fcnotebook.fcbook_title.data)
            if len(nb_title) > 40:
                flash('Please make the Notebook title shorter', 'danger')
                return redirect(url_for('members'))
            nb = Notebooks(title=nb_title,
                           timestamp=datetime.utcnow(),
                           notebook=g.user,
                           notebook_link=nfc)
            db.session.add(nb)
            nn_title = nohtml(form8_new_fcnotebook.fcnew_note_title.data)
            if len(nn_title) > 40:
                flash('Please make the Note title shorter', 'danger')
            nn = Notes(title=nn_title,
                       body=" ",
                       timestamp=datetime.utcnow(),
                       notes_link=nb,
                       note=g.user)
            db.session.add(nn)
            db.session.commit()
            return redirect(url_for('members', page=nn.id,
                                    booked=nb.id))
        return redirect(url_for('members'))

    # Move notebooks to file cabinets
    if form9_move_notebook.validate_on_submit():
        new_fc_id = form9_move_notebook.fc_select.data
        if new_fc_id is None or not isinstance(new_fc_id, int):
            flash(
                'There seems to be a problem. If you think this is in error, please contact us.',
                'danger')
        fc = Filecabinets.query.get(int(new_fc_id))
        if filecabinet_check_out(fc):
            nb_id = re.search('[0-9]+', form9_move_notebook.hidden_nb_id2.data)
            if nb_id is None:
                flash(
                    'There seems to be a problem. If you think this is in error, please contact us.',
                    'danger')
            nb = Notebooks.query.get(int(nb_id.group(0)))
            if notebook_check_out(nb):
                nb.notebook_link = fc
                db.session.add(nb)
                db.session.commit()
                return redirect(url_for('members', page=0, booked=nb.id))
            return redirect(url_for('members'))
        return redirect(url_for('members'))

    # Merge two notes
    if merge.validate_on_submit():
        mergee_id = re.search('[0-9]+', merge.merge_note_id.data)
        if mergee_id is None:
            flash(
                'Note not found.  If you think this is in error, please contact us.',
                'danger')
            return redirect(url_for('members'))
        n = Notes.query.get(int(mergee_id.group(0)))
        merger_id = merge.nt_select.data
        if int(mergee_id.group(0)) == int(merger_id):
            flash('You can not merge a Note into itself', 'danger')
            return redirect(url_for('members'))
        if n.passphrased:
            flash('You can not merge a passphrased note', 'danger')
            return redirect(url_for('members'))            
        nn = Notes.query.get(int(merger_id))
        if nn.passphrased:
            flash('You can not merge a passphrased note', 'danger')
            return redirect(url_for('members'))          
        if note_check_out(n) and note_check_out(nn):
            if len(n.body) < 2 or len(nn.body) < 2:
                flash('You can not merge an empty note', 'danger')
                return redirect(url_for('members'))
            mergee = decrypt_it(n.body)
            merger = decrypt_it(nn.body)
            new_note = mergee.decode('utf8',
                                     errors='ignore') + merger.decode('utf8',
                                                                      errors='ignore')
            if len(new_note) <= 524279:
                n.body = encrypt_it(new_note)
                db.session.add(n)
                db.session.delete(nn)
                db.session.commit()
                return redirect(url_for('members', page=n.id,
                                        booked=n.notebooks_id))
        return redirect(url_for('members'))

    return render_template("members.html",
                           title='Members',
                           user=user,
                           form=form,
                           form2=form2,
                           form3_new_notebook=form3_new_notebook,
                           form4_new_note_title=form4_new_note_title,
                           form5_new_notebook_title=form5_new_notebook_title,
                           form6_new_fc_title=form6_new_fc_title,
                           form7_new_fc=form7_new_fc,
                           form8_new_fcnotebook=form8_new_fcnotebook,
                           form9_move_notebook=form9_move_notebook,
                           merge=merge,
                           note_counter=note_counter,
                           fc=fc,
                           fcd=fcd,
                           page=page,  # reload with right note selected
                           page_passphrased=page_note,
                           attach=attach,
                           booked=booked,  # reload with right notebook selected
                           notebook_title=notebook_title,
                           books=books,
                           notes=notes)