def create_note():

    try:
        if request.method == 'POST':
            share = request.form.get("share") != None
            title = request.form['title']
            content = request.form['content']
            author_email = session['email']
            author_nickname = User.find_by_email(author_email).nick_name

            note_for_save = Note(title=title,
                                 content=content,
                                 author_email=author_email,
                                 shared=share,
                                 author_nickname=author_nickname)
            note_for_save.save_to_mongo()

            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...')
Beispiel #2
0
def user_notes(box_id=None):
    try:

        boxs = Box.get_user_boxes(session['_id'])
        user = User.find_by_email(session['email'])
        user_name = user.email

        if box_id is None:
            user_notes = User.get_notes(user)
            box_name = ''
            search = True

        else:
            box = Box.find_by_id(box_id)
            user_notes = box.get_notes()
            box_name = box.name
            search = False

        if request.method == 'POST':
            form_ = request.form['form']
            if box_id is None:
                notes = Note.search_with_elastic(form_,
                                                 user_nickname=user.nick_name,
                                                 box_id=False)
            else:
                notes = Note.search_with_elastic(form_,
                                                 user_nickname=user.nick_name,
                                                 box_id=box_id)

            return render_template('/notes/my_notes_sidebar.html',
                                   user_notes=notes,
                                   user_name=user_name,
                                   form=form_,
                                   boxs=boxs,
                                   box_name=box_name,
                                   search=search,
                                   box_id=box_id)

        else:
            return render_template('/notes/my_notes_sidebar.html',
                                   user_name=user_name,
                                   user_notes=user_notes,
                                   boxs=boxs,
                                   box_name=box_name,
                                   search=search,
                                   box_id=box_id)

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

        Error_obj = Error_(error_msg=''.join(error_msg),
                           error_location='user note reading USER:'******'email'])
        Error_obj.save_to_mongo()
        return render_template(
            'base_htmls/error_page.html',
            error_msgr='Crashed during reading your notes...')
def share_note(note_id):

    try:
        Note.find_by_id(note_id).share_or_unshare()
    finally:
        return redirect(
            url_for('.note',
                    note_id=note_id,
                    msg='Your note is shared!!',
                    msg_=True))
Beispiel #4
0
def create_box():
    try:
        all_notes = Note.get_user_notes(session['email'])
        if request.method == 'POST':
            notes_selected = request.form.getlist('notes')
            name = request.form['name']
            maker_id = session['_id']

            box_for_save = Box(name=name,
                               notes=notes_selected,
                               maker_id=maker_id)
            box_for_save.save_to_elastic()
            box_for_save.save_to_mongo()

            return redirect(url_for('boxs.boxs'))

        return render_template('boxs/create_box.html', all_notes=all_notes)

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

        Error_obj = Error_(error_msg=''.join(error_msg),
                           error_location='user box creating USER:'******'email'])
        Error_obj.save_to_mongo()
        return render_template(
            'error_page.html',
            error_msgr='Crashed during creating your box...')
Beispiel #5
0
def user_page(user_id):

    try:
        user = User.find_by_id(user_id)
        if user is None:
            user = User.find_by_email(user_id)
        user_notes = Note.find_shared_notes_by_user(user.email)
        try:
            filepath = url_for('static', filename=user.picture)
        except werkzeug.routing.BuildError:
            filepath = None

        return render_template('/users/user.html',
                               user=user,
                               user_notes=user_notes,
                               user_note_count=len(user_notes),
                               filepath=filepath)

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

        Error_obj = Error_(
            error_msg=''.join(error_msg),
            error_location='user_page while loading user{} info'.format(
                user_id))
        Error_obj.save_to_mongo()
        return render_template(
            'base_htmls/error_page.html',
            error_msgr='Crashed during reading information...')
Beispiel #6
0
def delete_multiple():
    try:
        user = User.find_by_email(session['email'])
        user_notes = User.get_notes(user)
        user_name = user.email

        if request.method == 'POST':
            notes_id = request.form.getlist('delete')

            for note_id in notes_id:
                note = Note.find_by_id(note_id)
                note.delete_on_elastic()
                note.delete_img()
                note.delete()

            flash('Your notes has successfully deleted.')
            return redirect(url_for('.user_notes'))

        return render_template("/notes/delete_multiple.html", user_notes=user_notes, user_name=user_name)

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

        Error_obj = Error_(error_msg=''.join(error_msg), error_location='notes public note reading')
        Error_obj.save_to_mongo()
        return render_template('error_page.html', error_msgr='Crashed during reading users notes...')
Beispiel #7
0
    def delete_user_notes(self):
        notes = Note.find_by_user_email(self.email)

        for note in notes:
            note.delete_on_elastic()
            note.delete_img()
            note.delete()
Beispiel #8
0
def delete_note(note_id, redirect_to='.user_notes'):
    try:
        note = Note.find_by_id(note_id)
        try:
            note.delete_on_elastic()
            note.delete_img()
        except:
            pass
        note.delete()
        user_ = User.find_by_id(session['_id'])
        user_group = Group.find_by_id(user_.group_id)
        try:
            user_group.shared_notes.remove(note_id)
            user_group.save_to_mongo()
            user_group.save_to_elastic()
        except:
            pass

        # flash('Your note has successfully deleted.')
        flash(
            '{ "message":"Your note has successfully deleted.", "type":"success" , "captaion":"Note Deleted", "icon_id": "far fa-check-circle"}'
        )

        return redirect(url_for(redirect_to))

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

        Error_obj = Error_(error_msg=''.join(error_msg),
                           error_location='notes deleting note')
        Error_obj.save_to_mongo()
        return render_template(
            'base_htmls/error_page.html',
            error_msgr='Crashed during deleting your note...')
Beispiel #9
0
def boxs():
    all_boxs = Box.get_user_boxes(session['_id'])
    all_notes = Note.get_user_notes(session['email'])

    return render_template('boxs/boxs_page.html',
                           all_boxs=all_boxs,
                           all_notes=all_notes)
Beispiel #10
0
def user_page(user_id):

    try:
        try:
            user = User.find_by_id(user_id)
        except:
            user = User.find_by_email(user_id)
        user_notes = Note.find_shared_notes_by_user(user.email)

        return render_template('/users/user.html',
                               user=user,
                               user_notes=user_notes,
                               user_note_count=len(user_notes))

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

        Error_obj = Error_(
            error_msg=''.join(error_msg),
            error_location='user_page while loading user{} info'.format(
                user_id))
        Error_obj.save_to_mongo()
        return render_template(
            'error_page.html',
            error_msgr='Crashed during reading information...')
Beispiel #11
0
def edit_note(note_id):

    try:
        note = Note.find_by_id(note_id)

        if request.method == 'POST':
            share = request.form.get("share") != None
            title = request.form['title']
            content = request.form['content']

            note.shared = share
            note.title = title
            note.content = content
            note.save_to_mongo()

            return redirect(url_for('.note', note_id=note_id))

        else:
            return render_template('/notes/edit_note.html', note=note)

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

        Error_obj = Error_(
            error_msg=''.join(error_msg),
            error_location='edit_note saveing and getting input from html file'
        )
        Error_obj.save_to_mongo()
        return render_template('error_page.html',
                               error_msgr='Crashed during saving your note...')
Beispiel #12
0
    def get_box_notes(self):
        box_noteID = self.find_by_id(self._id)
        notes = []
        for note_id in box_noteID.notes:
            notes.append(Note.find_by_id(note_id))

        return notes
Beispiel #13
0
def search_notes():
    user = User.find_by_email(session['email'])
    user_name = user.email
    form_ = request.form['Search_note']
    notes = Note.search_with_elastic(form_, user_nickname=user.nick_name)

    return render_template('/notes/delete_multiple.html', user_notes=notes, user_name=user_name,
                           form=form_)
Beispiel #14
0
def delete_note_from_group(group_id):
    group = Group.find_by_id(group_id)
    group_notes = group.shared_notes

    if request.method == 'GET':
        group_notes_filtered = []
        for note in group_notes:
            if note['author'] == session['_id']:
                group_notes_filtered.append(note)

        # group_notes_filtered[:] = [note for note in group_notes if note['author'] is not session['_id']]
        group_notes_ = []
        for note in group_notes_filtered:
            group_notes_.append(Note.find_by_id(note['note_id']))

    if request.method == 'POST':
        delete_notes = request.form.getlist('delete')

        flash_messages = []
        for note in delete_notes:
            try:
                del group_notes[group_notes.index({
                    'note_id': note,
                    'author': session['_id']
                })]
            except Exception:
                flash_messages.append('Note: ' + Note.find_by_id(note).title +
                                      ', ')

        group.save_to_elastic()
        group.save_to_mongo()
        if flash_messages is not []:
            # flash(' '.join(flash_messages)+'can\'t be deleted. Please try again')
            flash(
                '{ "message":"'
                '.join(flash_messages)+\'can\'t be deleted. Please try again", "type":"error" , "captaion":"Message Delete Error", "icon_id": "far fa-check-circle"}'
            )

        return redirect(url_for('groups.group', group_id=group_id))

    return render_template('groups/delete_note_from_group.html',
                           group_notes=group_notes_,
                           group_id=group_id)
Beispiel #15
0
def get_out_group(group_id):
    # save to user object
    user = User.find_by_id(session['_id'])
    user.group_id = None
    user.save_to_mongo()
    # save to group object
    group_ = Group.find_by_id(group_id)
    group_.members.remove(user._id)
    group_notes = group_.shared_notes
    # group_.shared_notes.remove({'author': user._id})
    # integrating through group notes and deleting notes from group
    for note in group_notes:
        # saving changes to note
        note_ = Note.find_by_id(note['note_id'])
        note_.share_with_group = False
        note_.save_to_mongo()
        try:
            note_.save_to_elastic()
        except:
            pass

        # removing note from group
        try:
            group_notes.remove({
                'author': user._id,
                'note_id': note['note_id']
            })
        except ValueError:
            pass

    if not group_.members:
        try:
            group_.delete_on_elastic()
        except:
            pass
        group_.delete_img()
        group_.delete()
        # flash('The group you secessioned was deleted because it has no members.')
        flash(
            '{ "message":"The group you secessioned was deleted because it has no members.", "type":"warning" , "captaion":"Group Delete", "icon_id": "fas fa-info-circle"}'
        )

    else:
        group_.save_to_mongo()
        try:
            group_.update_to_elastic()
        except:
            pass
        # flash('Secession complete')
        flash(
            '{ "message":"Secession complete", "type":"success" , "captaion":"Group Secession", "icon_id": "far fa-check-circle"}'
        )

    return redirect(url_for('groups.groups'))
Beispiel #16
0
def message(message_id, is_sended=False):

    try:

        message = Message.find_by_id(message_id)

        if message is None:
            pass

        if message.readed_by_reciver is False and is_sended is False and session[
                '_id'] in message.reciver_id and message.readed_date is None:
            message.readed_by_reciver = True
            message.readed_date = datetime.datetime.now()
            message.save_to_mongo()
            message.update_to_elastic()

        sender_nickname = User.find_by_id(message.sender_id).nick_name
        if type(message.reciver_id) is list:
            reciver_nickname = []
            for reciver in message.reciver_id:
                reciver_nickname.append(User.find_by_id(reciver).nick_name)

        else:
            reciver_nickname = User.find_by_id(message.reciver_id).nick_name

        if message.is_a_noteOBJ:
            try:
                note = Note.find_by_id(message.content)
            except:
                note = None
        else:
            note = None
        resp = make_response(
            render_template('messages/message.html',
                            message=message,
                            sender_nickname=sender_nickname,
                            reciver_nickname=reciver_nickname,
                            is_a_note=message.is_a_noteOBJ,
                            note=note,
                            join_group_redirect_info=','.join(
                                [message_id, message.is_invtation])))

        return resp

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

        Error_obj = Error_(error_msg=''.join(error_msg),
                           error_location='message message reading')
        Error_obj.save_to_mongo()

        return render_template('base_htmls/error_page.html',
                               error_msgr='Crashed during reading message...')
Beispiel #17
0
    def get_user_shared_notes(self, user_email):
        """
        This method returns user's shared notes. This method requires class Note
        :param user_email: user's email comes here. in views, you can just input session['email']
        :return: user notes(objcet)
        """
        user_notes = []
        for note in self.shared_notes:
            note_object = Note.find_by_id(note['note_id'])
            if note_object is not None:
                if note_object.author_email == user_email:
                    user_notes.append(note_object)

        return user_notes
Beispiel #18
0
def notes():

    try:
        return render_template('/notes/pub_notes.html',
                               notes=Note.find_shared_notes())
    except:
        error_msg = traceback.format_exc().split('\n')

        Error_obj = Error_(error_msg=''.join(error_msg),
                           error_location='nots publick note reading')
        Error_obj.save_to_mongo()
        return render_template(
            'error_page.html',
            error_msgr='Crashed during reading users notes...')
Beispiel #19
0
def edit_note(note_id):
    try:
        note = Note.find_by_id(note_id)

        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']

            note.shared = share
            note.share_only_with_users = share_only_with_users
            note.title = title
            note.content = content
            note.label = is_shared_validator(share, share_only_with_users)
            note.save_to_mongo()
            note.update_to_elastic()
            # flash('Your note has successfully saved.')
            flash(
                '{ "message":"Your note has successfully saved.", "type":"success" , "captaion":"Note Saved", "icon_id": "far fa-save"}'
            )

            return redirect(url_for('.note', note_id=note_id))

        else:
            return render_template(
                '/notes/edit_note.html',
                note=note,
                content=note.content.strip('\n').strip('\r'))

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

        Error_obj = Error_(
            error_msg=''.join(error_msg),
            error_location='edit_note saveing and getting input from html file'
        )
        Error_obj.save_to_mongo()
        return render_template('base_htmls/error_page.html',
                               error_msgr='Crashed during saving your note...')
Beispiel #20
0
def group(group_id):
    try:
        group_ = Group.find_by_id(group_id)
        if group_ is None:
            return render_template(
                'groups/group.html',
                error_msg='The group you requested is no longer exists!')
        members = []
        shared_notes = []
        for member in group_.members:
            members.append(User.find_by_id(member))

        for note in group_.shared_notes:
            append_data = Note.find_by_id(note['note_id'])
            if append_data is not None:
                shared_notes.append({"data": append_data, "error_note": False})
            else:
                shared_notes.append({"error_note": True, "data": None})
                group_.shared_notes.remove(note)

        if session['_id'] in group_.members:
            is_in_group = True
        else:
            is_in_group = False

        group_alerts = Notification.find_by_type('to_group', group_._id,
                                                 session['_id'])
        try:
            group_.save_to_elastic()
        except:
            pass
        group_.save_to_mongo()

        return render_template('groups/group.html',
                               group=group_,
                               members=members,
                               shared_notes=shared_notes,
                               is_in_group=is_in_group,
                               session_id=session['_id'],
                               group_alerts=group_alerts)
    except:
        error_msg = traceback.format_exc().split('\n')

        Error_obj = Error_(error_msg=''.join(error_msg),
                           error_location='group getting group')
        Error_obj.save_to_mongo()
        return render_template(
            'base_htmls/error_page.html',
            error_msgr='Crashed during getting your group info...')
Beispiel #21
0
def delete_note(note_id, redirect_to='.user_notes'):
    try:
        note = Note.find_by_id(note_id)
        note.delete_on_elastic()
        note.delete_img()
        note.delete()
        flash('Your note has successfully deleted.')
        return redirect(url_for(redirect_to))

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

        Error_obj = Error_(error_msg=''.join(error_msg), error_location='notes deleting note')
        Error_obj.save_to_mongo()
        return render_template('error_page.html', error_msgr='Crashed during deleting your note...')
Beispiel #22
0
def delete_multiple():
    try:
        user = User.find_by_email(session['email'])
        user_notes = User.get_notes(user)
        user_name = user.email

        if request.method == 'POST':
            notes_id = request.form.getlist('delete')

            for note_id in notes_id:
                note = Note.find_by_id(note_id)
                note.delete_on_elastic()
                note.delete_img()

                try:
                    my_group = Group.find_by_id(user.group_id)
                    del my_group.shared_notes[note._id]
                    my_group.save_to_mongo()
                    my_group.save_to_elastic()
                except:
                    pass

                try:
                    box = Box.find_by_id(note.box_id)
                    box.notes.remove(note._id)
                except:
                    note.delete()

            # flash('Your notes has successfully deleted.')
            flash(
                '{ "message":"Your notes has successfully deleted.", "type":"success" , "captaion":"Note Deleted", "icon_id": "far fa-check-circle"}'
            )

            return redirect(url_for('.user_notes'))

        return render_template("/notes/delete_multiple.html",
                               user_notes=user_notes,
                               user_name=user_name)

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

        Error_obj = Error_(error_msg=''.join(error_msg),
                           error_location='notes public note reading')
        Error_obj.save_to_mongo()
        return render_template(
            'base_htmls/error_page.html',
            error_msgr='Crashed during reading users notes...')
Beispiel #23
0
def add_to_group():

    # # getting all user's notes
    # notes = Note.get_user_notes(session['email'])
    # getting user object
    user = User.find_by_id(session['_id'])
    # getting user group notes
    group_notes = Group.find_by_id(user.group_id).get_user_shared_notes(
        session['email'])
    # not_added_notes = list(OrderedDict.fromkeys(user.get_notes() + group_notes))
    not_added_notes = list(set(user.get_notes()) - set(group_notes))
    # not_added_notes = list(dict.fromkeys(user.get_notes() + group_notes))

    if request.method == 'POST':
        if user.group_id is None:
            # flash("You aren't in a group. Please join a group to share notes to friends.")
            flash(
                '{ "message":"You aren\'t in a group. Please join a group to share notes to friends.", "type":"error" , "captaion":"Group Share Error", "icon_id": "fas fa-exclamation-triangle"}'
            )

            return redirect(url_for('groups.groups'))

        group_ = Group.find_by_id(user.group_id)
        add_notes = request.form.getlist('selected_notes')

        for note in add_notes:
            note = Note.find_by_id(note)
            if {
                    'author': user._id,
                    'note_id': note._id
            } in group_.shared_notes:
                pass
            else:
                group_.shared_notes.extend([{
                    'author': user._id,
                    'note_id': note._id
                }])
                group_.shared_notes = group_.shared_notes
        # saving to database
        group_.save_to_elastic()
        group_.save_to_mongo()

        return redirect(url_for('groups.group', group_id=group_._id))

    return render_template('notes/add_to_group.html', notes=not_added_notes)
Beispiel #24
0
def add_to_box(note_id):
    box_id = request.form['box']

    note_for_save = Note.find_by_id(note_id)
    box_for_save = Box.find_by_id(box_id)

    if note_id not in box_for_save.notes:
        note_for_save.box_id = box_id
        box_for_save.notes.append(note_id)
        box_for_save.save_to_mongo()
        box_for_save.update_to_elastic()
        note_for_save.save_to_mongo()
        note_for_save.update_to_elastic()
    else:
        flash(
            '{ "message":"The note has been already added", "type":"error" , "captaion":"Box Add Error", "icon_id": "fas fa-exclamation-triangle"}'
        )

    return redirect(url_for('.user_notes', box_id=box_id))
Beispiel #25
0
def send_note(note_id):

    try:
        note = Note.find_by_id(note_id)
    except:

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

        Error_obj = Error_(error_msg=''.join(error_msg),
                           error_location='send_note note finding/reading')
        Error_obj.save_to_mongo()

        return render_template('error_page.html',
                               error_msgr='Crashed during preparing page...')

    if request.method == 'POST':
        pass

    return render_template('send_note.html', note=note)
Beispiel #26
0
def note(note_id):

    try:
        note = Note.find_by_id(note_id)
        user = User.find_by_email(note.author_email)

        try:
            if note.author_email == session['email']:
                author_email_is_session = True
            else:
                author_email_is_session = False

        except:
            author_email_is_session = False

        finally:

            return render_template(
                '/notes/note.html',
                note=note,
                author_email_is_session=author_email_is_session,
                msg_=False,
                user=user)

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

        try:

            Error_obj = Error_(error_msg=''.join(error_msg),
                               error_location='note reading NOTE:' + note._id)
        except:
            Error_obj = Error_(error_msg=''.join(error_msg),
                               error_location='note reading NOTE:NONE')

        Error_obj.save_to_mongo()
        return render_template(
            'error_page.html',
            error_msgr='Crashed during reading your note...')
Beispiel #27
0
def user_notes():
    try:

        user = User.find_by_email(session['email'])
        user_notes = User.get_notes(user)
        user_name = user.email

        if request.method == 'POST':
                form_ = request.form['Search_note']
                notes = Note.search_with_elastic(form_, user_nickname=user.nick_name)

                return render_template('/notes/my_notes.html', user_notes=notes, user_name=user_name,
                                       form=form_)

        else:

            return render_template('/notes/my_notes.html', user_name=user_name, user_notes=user_notes)

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

        Error_obj = Error_(error_msg=''.join(error_msg), error_location='user note reading USER:'******'email'])
        Error_obj.save_to_mongo()
        return render_template('error_page.html', error_msgr='Crashed during reading your notes...')
Beispiel #28
0
def send_note_radio(note_id):
    try:
        note = Note.find_by_id(note_id)
        all_notes = Note.get_all()
        all_users = User.get_all()

        if request.method == 'POST':

            try:
                note = Note.find_by_id(request.form['note'])
            except:

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

                Error_obj = Error_(
                    error_msg=''.join(error_msg),
                    error_location='send_note note finding/reading')
                Error_obj.save_to_mongo()

                return render_template(
                    'base_htmls/error_page.html',
                    error_msgr='Crashed during preparing page...')

            message_title = request.form['title']

            if request.form.getlist("user") in [None, [], ""]:
                return render_template(
                    'messages/send_note.html',
                    e=
                    "You hadn't selected an reciver. Please select at least ONE reciver.",
                    all_users=all_users,
                    title=message_title,
                )

            else:

                recivers = request.form.getlist("user")

            sender_id = User.find_by_email(session['email'])._id

            message = Message(title=message_title,
                              content=note._id,
                              reciver_id=recivers,
                              sender_id=sender_id,
                              is_a_noteOBJ=True)
            message.save_to_mongo()
            message.save_to_elastic()

            # flash('Your message has been successfully sended.')
            flash(
                '{ "message":"Your message has been successfully sended.", "type":"success" , "captaion":"Send Success", "icon_id": "far fa-check-circle"}'
            )

            return redirect(url_for('.my_sended_messages', user_id=sender_id))

        return render_template('messages/send_note.html',
                               all_notes=all_notes,
                               all_users=all_users,
                               note_=note)

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

        Error_obj = Error_(error_msg=''.join(error_msg),
                           error_location='message message sending')
        Error_obj.save_to_mongo()

        return render_template('base_htmls/error_page.html',
                               error_msgr='Crashed during sending message...')
Beispiel #29
0
def notes():
    try:
        all_boxs = Box.get_user_boxes(session['_id'])
        if request.method == 'POST':
            form_ = request.form['Search_note']

            el = Elasticsearch(port=port)

            if form_ is '':
                data = el.search(index='notes',
                                 doc_type='note',
                                 body={"query": {
                                     "match_all": {}
                                 }})
            else:
                data = el.search(index='notes',
                                 doc_type='note',
                                 body={
                                     "query": {
                                         "bool": {
                                             "should": [{
                                                 "prefix": {
                                                     "title": form_
                                                 },
                                             }, {
                                                 "prefix": {
                                                     "author_nickname": form_
                                                 },
                                             }, {
                                                 "term": {
                                                     "content": form_
                                                 }
                                             }]
                                         }
                                     }
                                 })

            notes = []
            try:
                for note in data['hits']['hits']:

                    if note['_source']['shared'] is True:
                        notes.append(
                            Note.find_by_id(note['_source']['note_id']))
                    else:
                        if note['_source'][
                                'share_only_with_users'] is True and session[
                                    'email'] is not None:
                            notes.append(
                                Note.find_by_id(note['_source']['note_id']))

                        else:
                            pass
            except:
                pass

            del el
            return render_template('/notes/pub_notes.html',
                                   notes=notes,
                                   form=form_)

        try:
            if session['email'] is None:
                return render_template('/notes/pub_notes.html',
                                       notes=Note.find_shared_notes())
            else:
                return render_template('/notes/pub_notes.html',
                                       notes=Note.get_only_with_users() +
                                       Note.find_shared_notes())
        except:
            return render_template('/notes/pub_notes.html',
                                   notes=Note.find_shared_notes())

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

        Error_obj = Error_(error_msg=''.join(error_msg),
                           error_location='notes public note reading')
        Error_obj.save_to_mongo()
        return render_template(
            'base_htmls/error_page.html',
            error_msgr='Crashed during reading users notes...')
Beispiel #30
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...')