Example #1
0
def manage_desk():
    if request.args.get('selection') is None:
        flash('You did not select a desk to manage, please try again.')
        return redirect(url_for('desks.choose_desk'))
    else:
        selection = int(request.args.get('selection'))
        target_desk = Desks.query.get(selection)
        active_desk = Desks.query.get(current_user.active_desk)
        desk_notebooks = Notebooks.query.filter(
            Notebooks.associated_desks.contains(target_desk))
        form = edit_desk_form(obj=target_desk)
        if form.validate_on_submit():
            target_desk.deskname = form.deskname.data
            target_desk.desc = form.desc.data
            db.session.commit()
            flash("'{}' has been editted.".format(target_desk.deskname))
            return redirect(url_for('desks.choose_desk'))
        else:
            for field, error in form.errors.items():
                flash('{} ({} error)'.format(error[0], field))
        return render_template('managedesk.html',
                               webtitle="Manage My Desk",
                               daynight=eval_time(),
                               form=form,
                               desk_notebooks=desk_notebooks,
                               desk_id=target_desk.id)
Example #2
0
def add_page(notebook_id):
    target_notebook = Notebooks.query.get(notebook_id)
    if current_user.id == target_notebook.owner:
        form = add_page_form()
        if form.validate_on_submit():
            new_page_id = page_add(form.heading.data, form.content.data,
                                   notebook_id, current_user.id)
            flash('The new page has been created!')
            return redirect(
                url_for('pages.show',
                        notebook_id=notebook_id,
                        p=url_encode(new_page_id)))
        else:
            for field, error in form.errors.items():
                flash('{} ({} error)'.format(error[0], field))
        return render_template('addpage.html',
                               webtitle="Adding New Page to {}".format(
                                   target_notebook.title),
                               daynight=eval_time(),
                               form=form,
                               notebook=target_notebook)
    else:
        flash(
            "Sorry, you can't add pages to a notebook that doesn't belong to you!"
        )
        return redirect(url_for('pages.show', notebook_id=notebook.id))
Example #3
0
def forgot_password():
    if current_user.is_authenticated:
        flash("Actually, you're already logged in.")
        return redirect(url_for('core.index'))
    else:
        form = pw_reset_request_form()
        if form.validate_on_submit():
            target_user = Users.query.filter_by(email=form.email.data).first()
            token = target_user.get_reset_token()
            email_message = Message('Password Reset Request',
                                    sender='*****@*****.**',
                                    recipients=[target_user.email])
            email_message.body = f"""Hey {target_user.username}, \n
I heard that you forgot your password for your account at Fluid Notebook. Please click on the following link to reset it. \n
{url_for('users.reset_password', token = token, _external = True)}\n
This link will expire in 30 mins, please do not share it with anyone, as they'll be able to change your password.\n\n
Thanks for using Fluid Notebook!\n
Daryl"""
            mail.send(email_message)
            flash(
                "An email has been sent to the email address we have on file. Please check that email for instructions. If you do not see an email from [email protected], please check your junk mail. If you're experiencing problems recovering your account, please send contact us."
            )
            return redirect(url_for('users.login'))
        else:
            for field, error in form.errors.items():
                flash('{} ({} error)'.format(error[0], field))
        return render_template('forgot_password.html',
                               webtitle="Forgot Password",
                               daynight=eval_time(),
                               form=form)
Example #4
0
def choose_desk():
    all_desks = Desks.query.filter_by(owner=current_user.id)
    form = add_desk_form()
    if form.validate_on_submit():
        new_desk = Desks(deskname=form.deskname.data,
                         desc=form.desc.data,
                         owner=current_user.id)
        db.session.add(new_desk)
        db.session.commit()
        this_user = Users.query.get(current_user.id)
        this_user.active_desk = new_desk.id
        db.session.commit()
        flash(
            "The new desk has been created! I've gone ahead and switched you over by making it your active desk! If you don't want that, activate a different desk below."
        )
        return redirect(url_for('desks.choose_desk'))
    else:
        for field, error in form.errors.items():
            flash('{} ({} error)'.format(error[0], field))

    return render_template('choosedesk.html',
                           webtitle="Switch Desk",
                           daynight=eval_time(),
                           all_desks=all_desks,
                           form=form)
Example #5
0
def device_settings():
    if "modified_settings" in request.form:
        if request.form['daynight_pref'] != "AUTO":
            session['daynight_pref'] = request.form['daynight_pref']
        elif request.form['daynight_pref'] == "AUTO" and session.get(
                'daynight_pref'):
            session.pop('daynight_pref')

        if request.form['font_size_pref'] != "AUTO":
            session['font_size_pref'] = request.form['font_size_pref']
        session['keywords_pref'] = request.form['keywords_pref']

        if "editor_pref" in request.form:
            session['editor_pref'] = request.form['editor_pref']

    if request.args.get('clear'):
        if request.args.get('clear') == "session":
            if session.get('daynight_pref'):
                session.pop('daynight_pref')
            if session.get('font_size_pref'):
                session.pop('font_size_pref')
            if session.get('keywords_pref'):
                session.pop('keywords_pref')
            if session.get('editor_pref'):
                session.pop('editor_pref')

            flash('Your device-specific settings have been cleared.')

    return render_template('device_settings.html',
                           webtitle="Device-Specific Settings",
                           daynight=eval_time())
Example #6
0
def show_all():
    search_results = Notebooks.query.filter_by(owner = current_user.id)
    all_notebooks = search_results

    if request.args.get('query'):
        search_term = request.args.get('query')
    else:
        search_term = ""


    if request.args.get('type') and request.args.get('sort'):
        if search_term == "":
            flash('No search term entered, showing all notebooks.')

        if request.args.get('type') == 'desc':
            search_results = search_results.filter(Notebooks.desc.contains(search_term))
        else:
            search_results = search_results.filter(Notebooks.title.contains(search_term))

        if request.args.get('sort') == 'recency_old':
            search_results = search_results.order_by(Notebooks.id.asc())
        elif request.args.get('sort') == 'alphabetical':
            search_results = search_results.order_by(Notebooks.title)
        else:
            search_results = search_results.order_by(Notebooks.id.desc())

    else:
        search_results = search_results.order_by(Notebooks.id.desc())

    if search_results.count() == 0:
        search_results = 0

    return render_template('show_all.html', webtitle="All My Notebooks", daynight = eval_time(), all_notebooks = all_notebooks, search_results = search_results)
Example #7
0
def reset_password(token):
    if current_user.is_authenticated:
        flash("Actually, you're already logged in.")
        return redirect(url_for('core.index'))
    else:
        target_user = Users.verify_reset_token(token)
        if target_user is None:
            flash('The token is invalid or has expired.')
            return redirect(url_for('users.login'))
        else:
            form = pw_reset_form()
            if form.validate_on_submit():
                target_user.password = generate_password_hash(
                    form.password.data)
                db.session.commit()
                flash('Your password has been changed, please login now.')
                return redirect(url_for('users.login'))
            else:
                for field, error in form.errors.items():
                    flash('{} ({} error)'.format(error[0], field))

        return render_template('reset_password.html',
                               webtitle="Set New Password",
                               daynight=eval_time(),
                               form=form)
Example #8
0
def index():
    if current_user.role == "admin":
        all_reports = Reports.query.order_by(Reports.id.desc())
        if all_reports.count() == 0:
            all_reports = 0

        users_count = db.session.query(Users).count()
        notebooks_count = db.session.query(Notebooks).count()
        pages_count = db.session.query(Pages).count()

        newest_users = Users.query.order_by(Users.id.desc()).limit(5)
        newest_notebooks = Notebooks.query.order_by(
            Notebooks.id.desc()).limit(3)

        return render_template('dashboard.html',
                               webtitle="Admin Dashboard",
                               daynight=eval_time(),
                               all_reports=all_reports,
                               users_count=users_count,
                               notebooks_count=notebooks_count,
                               pages_count=pages_count,
                               newest_notebooks=newest_notebooks,
                               newest_users=newest_users)
    else:
        flash(
            "Sorry but you're not authorized to use the admin dashboard. It's nothing personal, I promise!"
        )
        return redirect(url_for('core.index'))
Example #9
0
def info():
    info_table = Desks.query.filter_by(owner = 2).filter_by(deskname = "Info").first()
    if info_table is not None:
        info_notebooks = Notebooks.query.filter(Notebooks.associated_desks.contains(info_table))
    else:
        info_notebooks = [Notebooks(title = "Info desk is missing.", desc = "", creation_date = "", last_update = "", visibility = "PUBLIC", access_code = "", url = "", cover_img = "", owner = 1)]
    return render_template('about.html', webtitle = "About", daynight = eval_time(), info_notebooks = info_notebooks)
Example #10
0
def index():
    recent_notebooks = Notebooks.query.filter_by(visibility = "PUBLIC").order_by(Notebooks.last_update.desc()).limit(10)
    featured_table = Desks.query.filter_by(owner = 2).filter_by(deskname = "Featured").first()
    if featured_table is not None:
        featured_notebooks = Notebooks.query.filter(Notebooks.associated_desks.contains(featured_table))
    else:
        featured_notebooks = [Notebooks(title = "Featured desk is missing.", desc = "", creation_date = "", last_update = "", visibility = "PUBLIC", access_code = "", url = "", cover_img = "", owner = 1)]
    return render_template('index.html', webtitle = "Homepage", daynight = eval_time(), recent_notebooks = recent_notebooks, featured_notebooks = featured_notebooks)
Example #11
0
def check(table):
    if current_user.role == "admin":
        # table headings
        users_headings = ('id', 'username', 'email', 'password', 'bio', 'pic',
                          'role', 'active_desk', 'delete')
        desks_headings = ('id', 'deskname', 'desc', 'owner (FK)', 'delete')
        notebooks_headings = ('id', 'owner', 'title', 'desc', 'creation_date',
                              'last_update', 'V', 'access_code', 'url',
                              'cover_img', 'delete')
        pages_headings = ('id', 'prior', 'next', 'heading', 'content',
                          'last_update', 'notebook (FK)', 'author (FK)',
                          'delete')
        preferences_headings = ('id', 'timezone', 'night_time_on',
                                'night_time_off', 'night_mode_type',
                                'font_size', 'coding_addon',
                                'hyperlinks_addon', 'colors_addon', 'user_id',
                                'delete')
        relational_headings = ('desk_id', 'notebook_id')

        if table == "users":
            table_headings = users_headings
            table_contents = Users.query.all()

        elif table == "desks":
            table_headings = desks_headings
            table_contents = Desks.query.all()

        elif table == "notebooks":
            table_headings = notebooks_headings
            table_contents = Notebooks.query.all()

        elif table == "pages":
            table_headings = pages_headings
            table_contents = Pages.query.all()

        elif table == "preferences":
            table_headings = preferences_headings
            table_contents = Preferences.query.all()

        elif table == "relational":
            table_headings = relational_headings
            table_contents = db.session.query(desks_to_notebooks)

        return render_template('table.html',
                               webtitle="Checking Table",
                               daynight=eval_time(),
                               table_headings=table_headings,
                               table_contents=table_contents,
                               table=table)

    else:
        flash(
            "Sorry but you're not authorized to use the admin dashboard. It's nothing personal, I promise!"
        )
        return redirect(url_for('core.index'))
Example #12
0
def index():
    active_desk = Desks.query.get(current_user.active_desk)

    #FIND NOTEBOOKS ON THIS DESK (many-to-many filter)
    desk_notebooks = Notebooks.query.filter(
        Notebooks.associated_desks.contains(active_desk))

    return render_template('desk.html',
                           webtitle="{} (My Active Desk)".format(
                               active_desk.deskname),
                           daynight=eval_time(),
                           active_desk=active_desk,
                           desk_notebooks=desk_notebooks)
Example #13
0
def get_access(notebook_url):
    target_notebook = Notebooks.query.filter_by(url = notebook_url).first()
    form = get_access_form()
    if form.validate_on_submit():
        if form.access_code.data == target_notebook.access_code:
            session['access_key'] = "{}-{}".format(target_notebook.id, target_notebook.access_code)
            flash('Access code accepted!')
            return redirect(url_for('pages.show', notebook_id = target_notebook.id))
        else:
            flash('Sorry, the access code you entered was incorrect.')
    else:
        for field, error in form.errors.items():
            flash('{} ({} error)'.format(error[0], field))
    return render_template('access.html', daynight = eval_time(), webtitle = "Access Code Required For {}".format(target_notebook.title), form = form)
Example #14
0
def change_info():
    target_user = Users.query.get(current_user.id)
    form = profile_form(obj=target_user)
    if form.validate_on_submit():
        target_user.bio = form.bio.data
        target_user.email = form.email.data
        target_user.pic = form.pic.data
        db.session.commit()
        flash('Your details have been updated.')
    else:
        for field, error in form.errors.items():
            flash('{} ({} error)'.format(error[0], field))
    return render_template('myinfo.html',
                           webtitle="My Information",
                           daynight=eval_time(),
                           form=form)
Example #15
0
def search():
    if request.args.get('query'):
        search_term = request.args.get('query')
        search_results = Notebooks.query.filter_by(visibility = "PUBLIC").filter(Notebooks.title.contains(search_term))
        if search_results.count() == 0:
            search_results = 0

        # if current_user.is_authenticated:
        #     personal_results = Notebooks.query.filter_by(owner = current_user.id).filter(Notebooks.title.contains(search_term))
        #     if personal_results is None:
        #         personal_results = 0
        # else:
        #     personal_results = 0
    else:
        search_results = 0


    return render_template('search.html', webtitle = "Search", daynight = eval_time(), search_results = search_results)
Example #16
0
def edit_page(notebook_id):
    if request.args.get('p'):
        page_id = int(url_decode(request.args.get('p')))
        target_notebook = Notebooks.query.get(notebook_id)
        if current_user.id == target_notebook.owner:
            target_page = Pages.query.get(page_id)
            form = edit_page_form(obj=target_page)
            if form.validate_on_submit():
                target_page.heading = form.heading.data
                target_page.content = form.content.data
                target_page.last_update = int(time.time())
                db.session.commit()
                flash('This page has been editted!')
                return redirect(
                    url_for('pages.show',
                            notebook_id=notebook_id,
                            p=url_encode(target_page.id)))
            else:
                for field, error in form.errors.items():
                    flash('{} ({} error)'.format(error[0], field))

            page_url = url_encode(target_page.id)
            return render_template('editpage.html',
                                   webtitle="Editting a Page in {}".format(
                                       target_notebook.title),
                                   daynight=eval_time(),
                                   form=form,
                                   notebook=target_notebook,
                                   page=target_page,
                                   page_url=page_url)
        else:
            flash(
                "Sorry, you can't edit pages on a notebook that doesn't belong to you!"
            )
            return redirect(
                url_for('pages.show',
                        notebook_id=notebook_id,
                        p=url_encode(target_page.id)))
    else:
        flash('You must provide a page ID')
        return redirect(
            url_for('pages.show',
                    notebook_id=notebook_id,
                    p=url_encode(target_page.id)))
Example #17
0
def change_password():
    this_user = Users.query.get(current_user.id)
    form = changePassword_form()
    if form.validate_on_submit():
        if this_user.check_password(form.current_pw.data):
            this_user.password = generate_password_hash(form.new_pw.data)
            db.session.commit()
            flash('Password Changed.')
            return redirect(url_for('users.settings'))
        else:
            print(generate_password_hash(form.current_pw.data))
            print(this_user.password)
            flash('Your current password is wrong, please try again.')
            return redirect(url_for('users.change_password'))
    else:
        for field, error in form.errors.items():
            flash('{} ({} error)'.format(error[0], field))
    return render_template('changepassword.html',
                           webtitle="Change Password",
                           daynight=eval_time(),
                           form=form)
Example #18
0
def insert_page(notebook_id):
    if request.args.get('p'):
        page_id = int(url_decode(request.args.get('p')))
        target_notebook = Notebooks.query.get(notebook_id)
        if current_user.id == target_notebook.owner:
            form = add_page_form()
            if form.validate_on_submit():
                if request.args.get('pos'):
                    position = request.args.get('pos')
                else:
                    position = "before"
                new_page_id = page_insert(position=position,
                                          insertion_point=page_id,
                                          rec_heading=form.heading.data,
                                          rec_content=form.content.data,
                                          rec_notebook_id=notebook_id,
                                          rec_author=current_user.id)
                flash('The new page has been created!')
                return redirect(
                    url_for('pages.show',
                            notebook_id=notebook_id,
                            p=url_encode(new_page_id)))
            else:
                for field, error in form.errors.items():
                    flash('{} ({} error)'.format(error[0], field))
            return render_template('addpage.html',
                                   webtitle="Inserting New Page to {}".format(
                                       target_notebook.title),
                                   daynight=eval_time(),
                                   form=form,
                                   notebook=target_notebook)
        else:
            flash(
                "Sorry, you can't add pages to a notebook that doesn't belong to you!"
            )
            return redirect('/notebooks/{}'.format(target_notebook.url))
    else:
        flash("You must specify a page ID.")
        return redirect(
            url_for('notebooks.show_notebook', notebook_id=notebook_id))
Example #19
0
def register():
    form = registration_form()
    if form.validate_on_submit():
        new_user = Users(
            username=form.username.data.lower(),
            email=form.email.data,
            password=form.password.data,
            bio="",
            pic="https://www.fluidnotebook.com/static/images/defaultavatar.png",
            active_desk=0)
        db.session.add(new_user)
        db.session.commit()
        new_desk = Desks(deskname="Main Desk",
                         desc=("The primary desk that's setup for {}.".format(
                             new_user.username)),
                         owner=new_user.id)
        db.session.add(new_desk)
        db.session.commit()
        new_user.active_desk = new_desk.id
        user_prefs = Preferences(timezone="Etc/GMT+8",
                                 night_on=21,
                                 night_off=7,
                                 night_type="dark",
                                 font_size="18",
                                 coding_addon=0,
                                 hyperlinks_addon=0,
                                 colors_addon=0,
                                 user_id=new_user.id)
        db.session.add(user_prefs)
        db.session.commit()
        flash("Your account has been registered, please log in!")
        return redirect(url_for('users.login'))
    else:
        for field, error in form.errors.items():
            flash('{} ({} error)'.format(error[0], field))
    return render_template('register.html',
                           webtitle="Register an Account",
                           daynight=eval_time(),
                           form=form)
Example #20
0
def preferences():
    user_prefs = Preferences.query.filter_by(user_id=current_user.id).first()
    form = preferences_form(obj=user_prefs)
    if form.validate_on_submit():
        user_prefs.timezone = form.timezone.data
        user_prefs.night_time_on = form.night_time_on.data
        user_prefs.night_time_off = form.night_time_off.data
        user_prefs.night_mode_type = form.night_mode_type.data
        user_prefs.font_size = form.font_size.data
        user_prefs.coding_addon = form.coding_addon.data
        user_prefs.hyperlinks_addon = form.hyperlinks_addon.data
        user_prefs.colors_addon = form.colors_addon.data
        db.session.commit()
        flash('Settings Updated.')
        return redirect(url_for('users.settings'))
    else:
        for field, error in form.errors.items():
            flash('{} ({} error)'.format(error[0], field))
    return render_template('preferences.html',
                           webtitle="Account Preferences",
                           daynight=eval_time(),
                           form=form)
Example #21
0
def email_login():
    form = email_login_form()
    if form.validate_on_submit():
        this_user = Users.query.filter_by(
            email=form.email.data.lower()).first()
        if this_user is not None and this_user.check_password(
                form.password.data):
            login_user(this_user)
            flash('Logged In')
            next = request.args.get('next')
            if next == None or not next[0] == '/':
                next = url_for('desks.index')
            return redirect(next)
        else:
            flash('You have provided a wrong email or password.')
            return redirect(url_for('users.email_login'))
    else:
        for field, error in form.errors.items():
            flash('{} ({} error)'.format(error[0], field))
    return render_template('email_login.html',
                           webtitle="Log In",
                           daynight=eval_time(),
                           form=form)
Example #22
0
def report():
    form = report_form()
    if form.validate_on_submit():
        if current_user.is_authenticated:
            reporter = current_user.username
        else:
            reporter = "unauthenticated"
        new_report = Reports(type=form.type.data,
                             url=form.url.data,
                             content=form.content.data,
                             user=reporter,
                             date=int(time.time()))
        db.session.add(new_report)
        db.session.commit()
        flash('Your report has been submitted, thank you!')
        return redirect(url_for('users.report'))
    else:
        for field, error in form.errors.items():
            flash('{} ({} error)'.format(error[0], field))
    return render_template('report.html',
                           webtitle="Report",
                           daynight=eval_time(),
                           form=form)
Example #23
0
def index():
    return render_template('featured.html', webtitle = "Featured Notebooks", daynight = eval_time())
Example #24
0
def edit_notebook(notebook_url):
    target_notebook = Notebooks.query.filter_by(url = notebook_url).first()
    if current_user.id == target_notebook.owner:
        form = edit_notebook_form(obj = target_notebook)
        if form.validate_on_submit():
            target_notebook.title = form.title.data
            target_notebook.desc = form.desc.data
            target_notebook.last_update = int(time.time())
            if form.access_code.data == "":
                target_notebook.visibility = "PUBLIC"
            else:
                target_notebook.visibility = "PRIVATE"
            target_notebook.access_code = form.access_code.data
            target_notebook.url = form.url.data
            target_notebook.cover_img = form.cover_img.data
            db.session.commit()
            flash('The notebook has been editted.')
            return redirect('/nb/{}'.format(target_notebook.id))
        else:
            for field, error in form.errors.items():
                flash('{} ({} error)'.format(error[0], field))
        return render_template('edit.html', webtitle = "Editting a Notebook", daynight = eval_time(), form = form, notebook = target_notebook)
    else:
        flash("Sorry pal, I can't let you edit someone else's notebook. Your login session may have timed out.")
        return redirect('/notebooks/{}'.format(notebook_url))
Example #25
0
def profile(username):
    target_user = Users.query.filter_by(username = username).first()
    user_notebooks = Notebooks.query.filter_by(owner = target_user.id).filter_by(visibility = "PUBLIC").order_by(Notebooks.id.desc())

    if current_user.is_authenticated:
        if current_user.id == target_user.id:
            private_notebooks = Notebooks.query.filter_by(owner = current_user.id).filter_by(visibility = "PRIVATE").order_by(Notebooks.id.desc())
            if private_notebooks.count() == 0:
                private_notebooks = 0
        else:
            private_notebooks = 0
    else:
        private_notebooks = 0

    return render_template('profile.html', webtitle = "{}'s Notebooks".format(username), daynight = eval_time(), username = username, notebooks = user_notebooks, private_notebooks = private_notebooks, target_user = target_user)
Example #26
0
def add_notebook():
    if request.args.get('notebook_id') is None:
        # create a new notebook
        recent_notebooks = Notebooks.query.filter_by(owner = current_user.id).order_by(Notebooks.id.desc())
        form = add_notebook_form()
        if form.validate_on_submit():
            if form.cover_img.data == "":
                form.cover_img.data = "https://www.fluidnotebook.com/static/images/default-cover.png"
            if form.access_code.data != "":
                visibility = "PRIVATE"
            else:
                visibility = "PUBLIC"
            active_desk = Desks.query.get(current_user.active_desk)
            new_notebook = Notebooks(title = form.title.data, desc = form.desc.data, creation_date = int(time.time()), last_update = int(time.time()), visibility = visibility, access_code = form.access_code.data, url = form.url.data, cover_img = form.cover_img.data, owner = current_user.id)
            db.session.add(new_notebook)
            active_desk.notebooks.append(new_notebook)
            db.session.commit()
            new_page = Pages(prior = 0, next = 0, heading = "Content Page", content = "", last_update = int(time.time()), notebook = new_notebook.id, author = current_user.id)
            db.session.add(new_page)
            db.session.commit()
            flash('Your new notebook has been created and is added to your active desk!')
            return redirect(url_for('desks.index'))
        else:
            for field, error in form.errors.items():
                flash('{} ({} error)'.format(error[0], field))
        return render_template('add.html', webtitle = "Adding New Notebook to Desk", daynight = eval_time(), form = form, recent_notebooks = recent_notebooks)
    else:
        # add to active desk
        selection = request.args.get('notebook_id')
        target_notebook = Notebooks.query.get(selection)
        active_desk = Desks.query.get(current_user.active_desk)
        if target_notebook in active_desk.notebooks:
            flash('The notebook you selected already exists on your active desk ({})'.format(active_desk.deskname))
            return redirect(url_for('desks.index'))
        else:
            active_desk.notebooks.append(target_notebook)
            db.session.commit()
            flash('The selected notebook has been added to your active desk!')
            return redirect(url_for('desks.index'))
Example #27
0
def settings():
    return render_template('settings.html',
                           webtitle="Settings",
                           daynight=eval_time())
Example #28
0
def show(notebook_id):
    target_notebook = Notebooks.query.get(notebook_id)
    if target_notebook is None:
        flash('Sorry, a notebook with that ID does not exist.')
        return redirect(url_for('core.index'))
    if target_notebook.access_code == "" or (current_user.is_authenticated
                                             and target_notebook.owner
                                             == current_user.id):
        access = True
    else:
        if session.get('access_key'):
            if session['access_key'] == "{}-{}".format(
                    target_notebook.id, target_notebook.access_code):
                access = True
            else:
                flash('Please enter the access code again.')
                access = False
        else:
            access = False

    if access == True:
        if "daynight_pref" in request.form:
            session['daynight_pref'] = request.form['daynight_pref']

        if request.args.get('p'):
            try:
                page_id = url_decode(request.args.get('p'))
            except:
                flash(
                    'There was something wrong with the page ID. Please do not type page IDs manually.'
                )
                return redirect(url_for('pages.show', notebook_id=notebook_id))
            target_page = Pages.query.get(page_id)
            if target_page in target_notebook.pages:
                if target_page.next == 0:
                    next_page_url = 0
                else:
                    next_page_url = url_encode(target_page.next)

                if target_page.prior == 0:
                    prior_page_url = 0
                    return redirect(
                        url_for('pages.show', notebook_id=target_notebook.id))
                else:
                    prior_page_url = url_encode(target_page.prior)

                if current_user.is_authenticated:
                    page_last_update = datetime.datetime.fromtimestamp(
                        int(target_page.last_update)).astimezone(
                            pytz.timezone(
                                current_user.preferences.timezone)).strftime(
                                    '%d %B, %Y')
                else:
                    page_last_update = datetime.datetime.fromtimestamp(
                        int(target_page.last_update)).astimezone(
                            pytz.timezone('Etc/GMT+7')).strftime('%d %B, %Y')

                return render_template('page.html',
                                       webtitle="{} - {}".format(
                                           target_notebook.title,
                                           target_page.heading),
                                       daynight=eval_time(),
                                       notebook=target_notebook,
                                       page=target_page,
                                       next_page_url=next_page_url,
                                       prior_page_url=prior_page_url,
                                       page_url=url_encode(target_page.id),
                                       page_last_update=page_last_update)
            else:
                flash(
                    'The page you requested does not exist within this notebook.'
                )
                return redirect(
                    url_for('pages.show', notebook_id=target_notebook.id))
        else:
            # check if notebook is on active desk
            if current_user.is_authenticated:
                active_desk = Desks.query.get(current_user.active_desk)
                if active_desk in target_notebook.associated_desks:
                    added_to_desk = True
                else:
                    added_to_desk = False
            else:
                added_to_desk = False

            # show contents page
            all_pages = Pages.query.filter_by(notebook=target_notebook.id)

            page_dict = {}
            for page in all_pages:
                page_dict[page.id] = {
                    'heading': page.heading,
                    'next': page.next,
                    'url': url_encode(page.id)
                }
            num_pages = len(page_dict)

            content_objects = []
            content_objects.append(page_dict[all_pages[0].id])

            for i in range(0, num_pages - 1):
                content_objects.append(page_dict[content_objects[i]['next']])

            content_objects.pop(0)
            if len(content_objects) == 0:
                content_objects = 0
                next_page_url = 0
            else:
                next_page_url = content_objects[0]['url']

            if current_user.is_authenticated:
                creation_date = datetime.datetime.fromtimestamp(
                    int(target_notebook.creation_date)).astimezone(
                        pytz.timezone(
                            current_user.preferences.timezone)).strftime(
                                '%d %B, %Y')
            else:
                creation_date = datetime.datetime.fromtimestamp(
                    int(target_notebook.creation_date)).astimezone(
                        pytz.timezone('Etc/GMT+7')).strftime('%d %B, %Y')

            return render_template('contents.html',
                                   webtitle="{} - Content Page".format(
                                       target_notebook.title),
                                   daynight=eval_time(),
                                   notebook=target_notebook,
                                   page=all_pages[0],
                                   all_pages=content_objects,
                                   next_page_url=next_page_url,
                                   creation_date=creation_date,
                                   added_to_desk=added_to_desk)
    else:
        return redirect(
            url_for('notebooks.get_access', notebook_url=target_notebook.url))
Example #29
0
def error_404(error):
    return render_template('404.html', daynight=eval_time())
Example #30
0
def error_500(error):
    return render_template('500.html', daynight=eval_time())