def save_message(methods=['GET', 'POST']): chatbox_ = ChatBox.find_by_id(session['chatbox_id']) title = None content = request.form['content'] if content is '': return redirect( url_for('chatboxs.chatbox', chatbox_id=session['chatbox_id'])) sender = User.find_by_email(session['email']) sender_name = sender.nick_name sender_id = sender._id message = Message(title=title, content=content, reciver_id=session['chatbox_id'], sender_id=sender_id, is_a_noteOBJ=False, sender_name=sender_name) message.save_to_mongo() message.save_to_elastic() chatbox_.messages.extend([message._id]) chatbox_.save_to_mongo() return redirect( url_for('chatboxs.chatbox', chatbox_id=session['chatbox_id']))
def send_message(): try: all_users = User.get_all() recivers = [] if request.method == 'POST': title = request.form['title'] content = request.form['content'] if request.form['reciver_email'] in [None, [], ""]: return render_template( 'messages/send_message.html', e= 'Your receiver field is empty. Please fill in at least ONE receiver.', all_users=all_users, title=title, content=content) try: # reciver_id = User.find_by_email(request.form['reciver_email'])._id recivers_string = request.form['reciver_email'].split() for email in recivers_string: recivers.append(User.find_by_email(email)._id) except Exception: return render_template( 'messages/send_message.html', e= "Please Check That you have coped EXACTLY the target user's email! And separated the emails with spaces!!", all_users=all_users, title=title, content=content) sender_id = User.find_by_email(session['email'])._id message = Message(title=title, content=content, reciver_id=recivers, sender_id=sender_id) message.save_to_mongo() return redirect(url_for('.my_sended_messages', user_id=sender_id)) return render_template('messages/send_message.html', all_users=all_users) except: error_msg = traceback.format_exc().split('\n') Error_obj = Error_(error_msg=''.join(error_msg), error_location='send_message message sending') Error_obj.save_to_mongo() return render_template( 'error_page.html', error_msgr='Crashed during sending your message...')
def send_message(user_to_send=None): try: all_users = User.get_all() if request.method == 'POST': title = request.form['title'] content = request.form['content'] if request.form.getlist("user") in [None, [], ""]: return render_template( 'messages/send_message.html', e= "You hadn't selected an reciver. Please select at least ONE reciver.", all_users=all_users, title=title, content=content) else: recivers = request.form.getlist("user") sender = User.find_by_email(session['email']) sender_name = sender.nick_name sender_id = sender._id message = Message(title=title, content=content, reciver_id=recivers, sender_id=sender_id, is_a_noteOBJ=False, sender_name=sender_name, is_chat_message=False) message.save_to_mongo() message.save_to_elastic() return redirect(url_for('.my_sended_messages', user_id=sender_id)) if user_to_send is not None: return render_template('messages/send_message.html', all_users=all_users, user_to_send=user_to_send) else: return render_template('messages/send_message.html', all_users=all_users) except: error_msg = traceback.format_exc().split('\n') Error_obj = Error_(error_msg=''.join(error_msg), error_location='send_message message sending') Error_obj.save_to_mongo() return render_template( 'base_htmls/error_page.html', error_msgr='Crashed during sending your message...')
def secession_chatbox(secession_chatbox_id): user_chatboxs = ChatBox.get_user_chatboxs(session['_id']) if secession_chatbox_id is not None: chatbox_obj = ChatBox.find_by_id(secession_chatbox_id) chatbox_obj.user_ids.remove(session['_id']) chatbox_obj.save_to_mongo() current_user_name = User.find_by_id(session['_id']).nick_name # flash('Successfully secessioned chatbox ' + chatbox_obj.name) flash( '{ "message":"Successfully secessioned chatbox " + chatbox_obj.name, "type":"success" , "captaion":"Chatbox Secession", "icon_id": "fas fa-sign-out-alt"}' ) message_bar = Message(title="User %s has secessioned." % (current_user_name), content=None, reciver_id=None, sender_id=None, sender_name="System") message_bar.save_to_mongo() return redirect(url_for('chatboxs.chatboxs')) if request.method == 'POST': if secession_chatbox_id is not None: chatbox_obj = ChatBox.find_by_id(secession_chatbox_id) chatbox_obj.user_ids.remove(session['_id']) chatbox_obj.save_to_mongo() # flash('Successfully secessioned chatbox '+ chatbox_obj.name) flash( '{ "message":"Successfully secessioned chatbox " + chatbox_obj.name, "type":"success" , "captaion":"Chatbox Secession", "icon_id": "fas fa-sign-out-alt"}' ) return redirect(url_for('chatboxs.chatboxs')) secession_chatboxes = request.form.getlist('secession_chatboxes') chatbox_objs = [] for _id in secession_chatboxes: if ChatBox.find_by_id(_id) is not None: chatbox_objs.append(ChatBox.find_by_id(_id)) for chatbox in chatbox_objs: chatbox.user_ids.remove(session['_id']) chatbox.save_to_mongo() return redirect(url_for('chatboxs.chatboxs')) # socketio.emit('secession') return render_template('chatboxs/secession_chatbox.html', user_chatboxs=user_chatboxs)
def send(json, methods=['POST', 'GET']): chatbox_ = ChatBox.find_by_id(session['chatbox_id']) title = None content = json['content'] if content is '': return else: sender = User.find_by_email(session['email']) sender_name = sender.nick_name sender_id = sender._id message = Message(title=title, content=content, reciver_id=session['chatbox_id'], sender_id=sender_id, is_a_noteOBJ=False, sender_name=sender_name, is_chat_message=True) message.save_to_mongo() try: message.save_to_elastic() except: pass chatbox_.messages.extend([message._id]) chatbox_.save_to_mongo() # make response data for emit response_data = { "created_date": message.sended_date.strftime('%m/%d/%Y'), "content": message.content, "sender_name": message.sender_name, "sender_id": message.sender_id, "chatbox_members": chatbox_.user_ids } socketio.emit('chat response', response_data, broadcast=True, room=json['room'])
def invite_friend(): user_ = User.find_by_id(session['_id']) group_ = Group.find_by_id(user_.group_id) group_name = group_.name all_friends_ = get_friends_whitout_added() if request.method == 'POST': users = request.form.getlist('users') if users == []: return render_template('groups/invite_friend.html', friends=all_friends_, group_name=group_name, error_msg='You havn\'t submitted anything!', group_id=group_._id) else: for member in users: message = Message(title='Do you want to join my group?', content=''' Join me on group {}! If you want to join, please click the link below. '''.format(group_.name), is_invtation=group_._id, reciver_id=member, sender_id=user_._id) message.save_to_elastic() message.save_to_mongo() # flash('Successfully sended invitations to friends!') flash( '{ "message":"Successfully sended invitations to friends!", "type":"success" , "captaion":"Invitation Sended", "icon_id": "far fa-check-circle"}' ) return redirect(url_for('groups.group', group_id=group_._id)) return render_template('groups/invite_friend.html', friends=all_friends_, group_name=group_name, group_id=group_._id)
def register_user(): try: if request.method == 'POST': email = request.form['email'] password = request.form['password'] nick_name = request.form['nickname'] try: if User.register_user(email, password, nick_name): user_id = User.find_by_email(email)._id session['email'] = email session['_id'] = user_id message = Message( title="Welcome to Note-it™!", content="""Welcome to Note-it! You can make a note, and share it with other users! Or you can just keep the note to your selves. You can send messages to other users too! Check out this website!!""", reciver_id=list(user_id), sender_id=User.find_by_email('*****@*****.**')._id) message.save_to_mongo() return redirect(url_for("home")) except UserErrors.UserError as e: return e.message return render_template("users/register.html") except: error_msg = traceback.format_exc().split('\n') Error_obj = Error_( error_msg=''.join(error_msg), error_location='register_user while registering user') Error_obj.save_to_mongo() return render_template('error_page.html', error_msgr='Crashed during registering you...')
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...')
def create_group(): try: if request.method == 'GET': all_firends_diclist = gen_all_friends_diclist() return render_template('groups/create_group.html', all_firends=all_firends_diclist) if request.method == 'POST': user = User.find_by_id(session['_id']) name = request.form['name'] members = request.form.getlist('members') members.append(user._id) group_img = request.files['file'] description = request.form['description'] share = request.form['inputGroupSelect01'] try: if group_img is not None: file_name, file_extenstion = os.path.splitext( group_img.filename) if file_extenstion not in ALLOWED_GROUP_IMG_FORMATS: all_firends_diclist = gen_all_friends_diclist() return render_template( 'groups/create_group.html', all_firends=all_firends_diclist, error_msg= 'Too much images!! Please upload just one image.', name=name, members=members, share=share, description=description) # saving file # create name for file sid = shortid.ShortId() # create path for file file_path, file_extenstion = os.path.splitext( group_img.filename) filename = secure_filename( sid.generate()) + file_extenstion # os.chdir("static/img/file/") # save file and add file to filenames list group_img.save(os.path.join(filename)) else: filename = None except: group_img = None filename = None # saving group group_id = uuid.uuid4().hex group_for_save = Group(_id=group_id, name=name, members=[user._id], group_img_name=filename, shared=share, shared_notes=[], description=description) group_for_save.save_to_mongo() try: group_for_save.save_to_elastic() except: pass # # saving to user user.group_id = group_id user.save_to_mongo() # sending messages to users for member in members: message = Message(title='Do you want to join my group?', content=''' Join me on group {}! If you want to join, please click the link below. '''.format(group_for_save.name), is_invtation=group_id, reciver_id=member, sender_id=user._id, sender_name=user.nick_name) try: message.save_to_elastic() except: pass message.save_to_mongo() # redirecting # flash('Successfully created group! | Sended invitations to users') flash( '{ "message":"Successfully created group! | Sended invitations to users", "type":"info" , "captaion":"Success", "icon_id": "fas fa-info-circle"}' ) return redirect(url_for('groups.groups')) except: error_msg = traceback.format_exc().split('\n') Error_obj = Error_(error_msg=''.join(error_msg), error_location='create_group creating group') Error_obj.save_to_mongo() return render_template( 'base_htmls/error_page.html', error_msgr='Crashed during creating your group...')