Пример #1
0
def create_note(box_id):
    try:
        if request.method == 'POST':
            # getting forms
            share = request.form['inputGroupSelect01']

            # share label error handler
            try:
                share, share_only_with_users = share_bool_function(share)
            except ValueError:
                return render_template(
                    '/notes/create_note.html',
                    error_msg=
                    "You did not selected an Share label. Please select an Share label."
                )

            # getting title content and email
            title = request.form['title']
            content = request.form['content_'].strip('\n').strip('\r')
            author_email = session['email']

            # getting files and saving
            try:
                # getting files
                files = request.files.getlist('file')

                # # checking file size
                # file_size = 0
                # for file in files:
                #     file_size += file.seek(0, os.SEEK_END).tell()
                #
                # if file_size > 5e+8:
                #     flash('{ "message":"Too much files!", "type":"error" , "captaion":"File Overload Error", "icon_id": "fas fa-exclamation-triangle"}')

                # file length checker
                if len(files) > 5:
                    # flash("Too much files!")
                    flash(
                        '{ "message":"Too much files!", "type":"error" , "captaion":"File Overload Error", "icon_id": "fas fa-exclamation-triangle"}'
                    )

                    return render_template('/notes/create_note.html',
                                           title=title,
                                           content_=content,
                                           share=share)

                filenames = []
                for file in files:
                    if files and Note.allowed_file(file):
                        # create name for file
                        sid = shortid.ShortId()
                        # create path for file
                        file_path, file_extenstion = os.path.splitext(
                            file.filename)
                        filename = secure_filename(
                            sid.generate()) + file_extenstion

                        # os.chdir("static/img/file/")
                        # save file and add file to filenames list
                        file.save(os.path.join(filename))
                        filenames.append(filename)

                    # if extenstion is not supported
                    elif file is not None:
                        # flash("Sorry; your file's extension is supported.")
                        flash(
                            '{ "message":"Sorry; your file\'s extension is not supported", "type":"error" , "captaion":"File Extension Error", "icon_id": "fas fa-exclamation-triangle"}'
                        )

                        return render_template('/notes/create_note.html',
                                               title=title,
                                               content=content,
                                               share=share)
                    else:
                        filenames = []

            except:
                # file = None
                filenames = []

            # getting author nickname, label and user notes
            author_nickname = User.find_by_email(author_email).nick_name

            label = is_shared_validator(share, share_only_with_users)

            user_notes = Note.get_user_notes(session['email'])

            # if too much notes, then redirect
            if len(user_notes) > 20:
                # flash("You have the maximum amount of notes. Please delete your notes")
                flash(
                    '{ "message":"You have the maximum amount of notes. Please delete your notes", "type":"error" , "captaion":"Note Overload Error", "icon_id": "fas fa-exclamation-triangle"}'
                )

                return redirect(url_for(".user_notes", box_id=box_id))

            # saving note
            all_box_id = box_id
            note_id = uuid.uuid4().hex

            try:
                share_with_group = request.form['share_with_group']

                if share_with_group == 'on':
                    try:
                        user = User.find_by_id(session['_id'])
                        share_with_group = True
                        group = Group.find_by_id(user.group_id)
                        group.shared_notes.append({
                            'note_id': note_id,
                            'author': user._id
                        })
                        group.save_to_mongo()
                        group.update_to_elastic()
                    except:
                        # flash("You aren't in a group. Please join a group to share with group users.")
                        flash(
                            '{ "message":"You aren\'t in a group. Please join a group to share with group users.", "type":"info" , "captaion":"Group Share Error", "icon_id": "fas fa-exclamation-triangle"}'
                        )

                        return render_template('/notes/create_note.html',
                                               title=title,
                                               content=content,
                                               share=share)

                else:
                    share_with_group = False

            except werkzeug.exceptions.BadRequestKeyError:
                share_with_group = False

            else:
                share_with_group = False

            note_for_save = Note(_id=note_id,
                                 title=title,
                                 content=content,
                                 author_email=author_email,
                                 shared=share,
                                 author_nickname=author_nickname,
                                 share_only_with_users=share_only_with_users,
                                 share_label=label,
                                 file_name=filenames,
                                 box_id=all_box_id,
                                 share_with_group=share_with_group)
            note_for_save.save_to_mongo()
            note_for_save.save_to_elastic()
            if box_id is not None:
                box_for_save = Box.find_by_id(all_box_id)
                box_for_save.notes.append(note_id)
                box_for_save.save_to_mongo()
                box_for_save.update_to_elastic()

            # flash message and redirect
            # flash('Your note has successfully created.')
            flash(
                '{ "message":"Your note has successfully created.", "type":"success" , "captaion":"Note Saved", "icon_id": "far fa-save"}'
            )

            return redirect(url_for('.user_notes'))

        return render_template('/notes/create_note.html')

    except:
        error_msg = traceback.format_exc().split('\n')

        Error_obj = Error_(error_msg=''.join(error_msg),
                           error_location='create_note creating note')
        Error_obj.save_to_mongo()
        return render_template('base_htmls/error_page.html',
                               error_msgr='Crashed during saving your note...')
Пример #2
0
def create_note():
    try:
        if request.method == 'POST':
            share = request.form['inputGroupSelect01']

            try:
                share, share_only_with_users = share_bool_function(share)
            except ValueError:
                return render_template('/notes/create_note.html',
                                       error_msg="You did not selected an Share label. Please select an Share label.")

            title = request.form['title']
            content = request.form['content'].strip('\n').strip('\r')
            author_email = session['email']
            try:
                files = request.files.getlist('file')

                if len(files) > 5:
                    flash("Too much files!")
                    return render_template('/notes/create_note.html'
                                           , title=title, content=content, share=share)

                filenames = []
                for file in files:
                    if files and Note.allowed_file(file):
                        sid = shortid.ShortId()
                        file_path, file_extenstion = os.path.splitext(file.filename)
                        filename = secure_filename(sid.generate()) + file_extenstion

                        # os.chdir("static/img/file/")
                        file.save(os.path.join(filename))
                        filenames.append(filename)

                    elif file is not None:
                        flash("Sorry; your file's extension is supported.")
                        return render_template('/notes/create_note.html'
                                               , title=title, content=content, share=share)
                    else:
                        filenames = []

            except:
                # file = None
                filenames = []

            author_nickname = User.find_by_email(author_email).nick_name

            label = is_shared_validator(share, share_only_with_users)

            user_notes = Note.get_user_notes(session['email'])

            if len(user_notes) > 20:
                flash("You have the maximum amount of notes. Please delete your notes")
                return redirect(url_for(".user_notes"))

            note_for_save = Note(title=title, content=content, author_email=author_email, shared=share,
                                 author_nickname=author_nickname, share_only_with_users=share_only_with_users,
                                 share_label=label, file_name=filenames)
            note_for_save.save_to_mongo()
            note_for_save.save_to_elastic()

            flash('Your note has successfully created.')

            return redirect(url_for('.user_notes'))

        return render_template('/notes/create_note.html')

    except:
        error_msg = traceback.format_exc().split('\n')

        Error_obj = Error_(error_msg=''.join(error_msg), error_location='create_note creating note')
        Error_obj.save_to_mongo()
        return render_template('error_page.html', error_msgr='Crashed during saving your note...')