Exemple #1
0
def add_note():
    form = NoteForm(request.form)
    if AuthHelper.check_session_validation(current_user) and form.validate():
        # Create new note
        note = Note()
        note.title = form.title.data
        note.content = form.content.data
        note.isprivate = form.isprivate.data
        # Get categories
        splitted_list = CategoryHelper.split_and_filter(
            form.categories.data, '')
        print(splitted_list)
        new_categories = CategoryHelper.get_new_categories(
            splitted_list, note.isprivate)
        print(new_categories)
        # Relations
        current_user.categories.extend(new_categories)
        note.categories.extend(new_categories)
        # Encrypt
        note.encrypt(AuthHelper.get_random_key())
        # Database operations
        db.session.add(note)
        current_user.notes.append(note)
        db.session.commit()
        return redirect(
            url_for('app_notes.notes', username=current_user.username))
    else:
        abort(404)
Exemple #2
0
def notes(username):
    if current_user.is_authenticated and AuthHelper.check_username(current_user, username) \
            and AuthHelper.check_session_validation(current_user):
        note_list = NoteHelper.get_user_notes(current_user)
        return render_template("notes.html.j2",
                               notes=note_list,
                               edit_form=NoteForm(),
                               delete_form=DeleteNoteForm(),
                               title="{} @ Librenotes".format(username),
                               description="Welcome, {}".format(username))
    else:
        note_list, searched_user = NoteHelper.get_searched_user_notes(username)
        if searched_user is not None:
            flash("You are seeing public notes of {}".format(username),
                  "warning")
            description = "See public notes of {}".format(username)
            if searched_user.description is not None:
                description = searched_user.description
            return render_template("notes.html.j2",
                                   notes=note_list,
                                   edit_form=None,
                                   delete_form=None,
                                   title="{} @ Librenotes".format(username),
                                   description=description)
        else:
            abort(404)
Exemple #3
0
def register_post():
    form = RegisterForm(request.form)
    if form.validate():
        if not AuthHelper.check_user_exist(form.email.data,
                                           form.username.data):
            # Create new user
            user = User()
            user.username = form.username.data
            user.password = generate_password_hash(form.password.data)
            user.email = form.email.data
            user.generate_encryption_keys(form.password.data)
            # Add to db
            db.session.add(user)
            db.session.commit()
            mailer.send_confirmation_mail(form.username.data, form.email.data)
            Flasher.flash(
                "Register Successful, please check your mail address for confirmation",
                "success")
            return redirect(url_for('app_login.login_get'))
        else:
            Flasher.flash("This username or email address is already in use",
                          "warning")
    else:
        Flasher.flash_errors(form, "danger")
    return redirect(url_for('app_register.register_get'))
Exemple #4
0
def change_pass_post():
    form = ChangePasswordForm(request.form)
    if form.validate():
        if AuthHelper.check_password(current_user, form.password.data) and AuthHelper.check_session_validation(
                current_user):
            current_user.password = generate_password_hash(form.new_password.data)
            current_user.encrypt_rand_key(form.new_password.data, AuthHelper.get_random_key())
            db.session.commit()
            Flasher.flash("Your password is successfully changed", "success")
            return redirect(url_for("app_notes.notes", username=current_user.username))
        else:
            Flasher.flash("Your current password doesn't match with entered password or you are fake!",
                          category='warning')
            return redirect(url_for("app_notes.notes", username=current_user.username))
    else:
        Flasher.flash_errors(form, "danger")
        return redirect(url_for("app_notes.notes", username=current_user.username))
Exemple #5
0
def delete_note():
    form = DeleteNoteForm(request.form)
    note = NoteHelper.get_user_note_with_id(current_user, form.id.data)
    if note and AuthHelper.check_session_validation(current_user):
        db.session.delete(note)
        db.session.commit()
        return redirect(
            url_for('app_notes.notes', username=current_user.username))
    else:
        return abort(404)
Exemple #6
0
def change_description_post():
    form = ChangeDescription(request.form)
    if form.validate():
        if AuthHelper.check_session_validation(current_user):
            current_user.description = form.description.data
            db.session.commit()
            Flasher.flash("Your description is successfully changed", "success")
        else:
            Flasher.flash("Are you fake?",
                          category='warning')
    else:
        Flasher.flash_errors(form, "danger")
    return redirect(url_for("app_notes.notes", username=current_user.username))
Exemple #7
0
def login_post():
    form = LoginForm(request.form)
    if form.validate():
        user_ = User.query.filter_by(username=form.username.data).first()
        if user_ and AuthHelper.check_password(user_, form.password.data):
            if not user_.is_confirmed:
                Flasher.flash("Did you confirm your email adress?", "danger")
                return redirect(url_for("app_login.login_get"))
            else:
                login_user(user_)
                AuthHelper.set_random_key(
                    user_.get_random_key(form.password.data))
                Flasher.flash("Login Successful! Welcome back, {}".format(
                    user_.username),
                              category="success")
            return redirect(
                url_for('app_notes.notes', username=form.username.data))
        else:
            Flasher.flash("Password or Username does not match", "danger")
            return redirect(url_for("app_login.login_get"))
    else:
        Flasher.flash_errors(form, "danger")
        return redirect(url_for("app_login.login_get"))
Exemple #8
0
def edit_note():
    form = NoteForm(request.form)
    note = NoteHelper.get_user_note_with_id(current_user, form.id.data)
    if note and form.validate() and AuthHelper.check_session_validation(
            current_user):
        # Update note
        note.title = form.title.data
        note.content = form.content.data
        note.isprivate = form.isprivate.data
        # Update note categoires
        splitted_list = CategoryHelper.split_and_filter(
            form.categories.data, '')
        new_categories = CategoryHelper.get_new_categories(
            splitted_list, note.isprivate)
        # Delete categories of note
        note.categories = []
        # Append it
        current_user.categories.extend(new_categories)
        note.categories.extend(new_categories)
        note.encrypt(AuthHelper.get_random_key())
        db.session.commit()
        return redirect(url_for('app_notes.notes', username=current_user))
    else:
        return abort(404)