def newmessage(): if not current_user.is_authenticated: return redirect(url_for('index')) form = MessageForm() if form.validate_on_submit(): s_name = current_user.display_name s_id = current_user.id receiver = User.query.filter_by( username=form.receiver_name.data).first() r_id = receiver.id content = form.content.data msg = Message(body=content, sender_id=s_id, sender_name=s_name, receiver_id=r_id) db.session.add(msg) db.session.commit() flash('Message Sent!') return redirect(url_for('inbox')) #else: #flash('Error creating message. Please try again') return render_template('newmessage.html', title='Create Message', form=form)
def message(id): _user = User.query.filter_by(id=id).first_or_404() form = MessageForm() if form.validate_on_submit(): form_subject = form.subject.data form_message = form.body.data recipient = _user.email _message = "<p>You've received a new message from <a href='" + app.config[ 'HOST'] + url_for( 'user', id=current_user.id ) + "'>" + current_user.name + "</a> on Marketplace.</p>" _message += '<p>Subject: ' + form_subject + '</p>' _message += '<p>Message: ' + form_message + '</p>' _message += '<p><a href="' + app.config['HOST'] + url_for( 'message', id=current_user.id ) + '">Reply to ' + current_user.name + ' on Marketplace</a></p>' subject = "Message from " + current_user.name + " on Marketplace" send_email(subject, '*****@*****.**', [recipient], _message, _message) flash('Message sent') return redirect(url_for('user', id=id)) return render_template('message.html', title='Send Message', form=form, user=_user, current_user=current_user)
def private_messages(recipient): recipient = User.query.filter_by(username=recipient).first_or_404() page = request.args.get('page', 1, type=int) received_messages = current_user.messages_received.filter_by( author=recipient) sent_messages = current_user.messages_sent.filter_by(recipient=recipient) messages = received_messages.union(sent_messages).order_by( Message.timestamp).paginate(page, 15, False) next_url = url_for('private_messages', recipient=recipient.username, page=messages.next_num) if messages.has_next else None prev_url = url_for('private_messages', recipient=recipient.username, page=messages.prev_num) if messages.has_prev else None form = MessageForm() if form.validate_on_submit(): message = Message(body=form.message.data, author=current_user, recipient=recipient) db.session.add(message) db.session.commit() flash('Message sent!') return redirect( url_for('private_messages', recipient=recipient.username)) return render_template('private_messages.html', title='Private Messages', recipient=recipient, form=form, messages=messages.items, next_url=next_url, prev_url=prev_url)
def user(username): user = User.query.filter_by(username=username).first_or_404() message_form = MessageForm() if message_form.validate_on_submit(): message = Message(author=current_user, recipient=user, body=message_form.message.data) db.session.add(message) db.session.commit() user.add_notification('unread_message_count', user.new_messages()) db.session.commit() flash('Your message has been sent!') return redirect(url_for('user', username=username)) page = request.args.get('page', 1, type=int) schemes = user.schemes.order_by(Scheme.timestamp.desc()).paginate( page, app.config['SCHEMES_PER_PAGE'], False) next_url = url_for('index', page=schemes.next_num) if schemes.has_next else None prev_url = url_for('index', page=schemes.prev_num) if schemes.has_prev else None return render_template('user.html', user=user, schemes=schemes.items, next_url=next_url, prev_url=prev_url, form=message_form)
def message_user(id_param): form = MessageForm() form['csrf_token'].data = request.cookies['csrf_token'] user = User.query.filter(User.id == current_user.id).first() user_to_message = User.query.filter(User.id == id_param).first() def message_info(self): return { "id": self.id, "read": self.read, "content": self.content, "recipient_id": self.recipient_id, "message_sender_id": self.message_sender_id, "created_at": self.created_at } if form.validate_on_submit(): message_data = request.form.get('content') message = Message( read=False, content=message_data, recipient_id=user_to_message.id, message_sender_id=user.id, ) db.session.add(message) db.session.commit() return message_info(message) return "Bad Data"
def messages(username): msg_to = User.query.filter_by(username=username).first_or_404() if msg_to.id == 1 or msg_to == current_user: #Prevent chatting with admin or self return render_template('admin_restricted.html') form0 = SearchProfileForm() form = MessageForm() if form.validate_on_submit(): message = Message(body=form.message.data, author=current_user, profile=msg_to) db.session.add(message) flash('Message sent!') ##add change to table prev_convo = None new_convo = None try: print('curstart') current_start = Conversation.query.filter_by(author=current_user, profile=msg_to) print('msgstart') msg_to_start = Conversation.query.filter_by(author=msg_to, profile=current_user) print('union') prev_convo = current_start.union(msg_to_start).first() print('set_tm') prev_convo.timestamp = message.timestamp print('finish try') except: print('new convo') new_convo = Conversation(author=current_user, profile=msg_to) db.session.add(new_convo) db.session.commit() return redirect(url_for('messages', username=username)) page = request.args.get('page', 1, type=int) messages = current_user.msgs_btw(username).paginate( page, app.config['MESSAGES_PER_PAGE'], False) for message in messages.items: if message.author != current_user: message.seen = 1 db.session.commit() next_url = url_for('messages', username = username, page = messages.next_num) \ if messages.has_next else None prev_url = url_for('messages', username = username, page = messages.prev_num) \ if messages.has_prev else None return render_template('messages.html', title='Messages', messages=messages.items, form0=form0, form=form, user=msg_to, badge_colour=badge_colour, prev_url=prev_url, next_url=next_url)
def rooms(): form = MessageForm() if form.validate_on_submit(): print(form.message.data) emit('message', {'msg': current_user.username + ':' + form.message.data}) print(form.message.data) return render_template("rooms.html", title='Rooms', form=form)
def mail(): if current_user.is_authenticated: return redirect(url_for('mail')) form = MessageForm() if form.validate_on_submit(): user = User.query.filter_by(email=form.email.data).first() if user: MessageForm() flash('Mail send!')
def home(): """ home page for managing sms messages """ message_form = MessageForm() if message_form.validate_on_submit(): send_message(message_form.message) flash('Message Sent!') return render_template('index.html', MessageForm=message_form)
def message(teacher_id): teacher = db.session.query(Teacher).get_or_404(teacher_id) form = MessageForm() if form.validate_on_submit(): flash('Сообщение отправлено!', category='sent') return redirect(url_for('message', teacher_id=teacher.id)) context = {'teacher': teacher} return render_template('message.html', form=form, **context)
def leave_message(): form = MessageForm() if form.validate_on_submit(): message = Message(body=form.message.data, user_id=current_user.id) db.session.add(message) db.session.commit() flash('You successfully leave a message to us!') return redirect(url_for('leave_message')) return render_template('leave_message.html', title='Leave a Message', form=form)
def send_message(recipient): user = User.query.filter_by(username=recipient).first_or_404() form = MessageForm() if form.validate_on_submit(): msg = Message(author=current_user, recipient=user, body=form.message.data) db.session.add(msg) db.session.commit() flash('Your message has been sent.') return redirect(url_for('user', username=recipient)) return render_template('/communication/send_message.html', title='Send Message', form=form, recipient=recipient)
def index(): messages = Message.query.order_by(Message.timestamp.desc()).all() form = MessageForm() if form.validate_on_submit(): name = form.name.data body = form.body.data message = Message(name=name, body=body) db.session.add(message) db.session.commit() return redirect(url_for('index')) return render_template('index.html', form=form, messages=messages)
def send_message(recipient): user = User.query.filter_by(username=recipient).first_or_404() form = MessageForm() if form.validate_on_submit(): msg = Message(author=current_user, recipient=user,\ body=form.message.data) db.session.add(msg) user.add_notification('unread_message_count', user.new_messages()) db.session.commit() flash(('Your message has been sent.')) return redirect(url_for('user', username=recipient)) return render_template('send_message.html', title=('Send Message'),\ form=form, recipient=recipient)
def send_message(recipient): user = User.query.filter_by(username=recipient).first() messageForm = MessageForm() if messageForm.validate_on_submit(): message = Message(sender=current_user, recipient=user, body=messageForm.message.data) db.session.add(message) db.session.commit() user.add_notification('unread_message_count', user.new_messages()) flash('Your message has been sent.') return render_template('send_message.html', messageForm=messageForm)
def home(): form=MessageForm() if form.validate_on_submit(): # check if user exits in database # if not create user and add to database # create row in Message table with user (created/found) add to ta database posts = [] # output all messages # create a list of dictionaries with the following structure # [{'author':'carlos', 'message':'Yo! Where you at?!'}, # {'author':'Jerry', 'message':'Home. You?'}] return render_template('home.html', posts=posts, form=form)
def upload(): form = MessageForm() if form.validate_on_submit(): if request.method == 'POST' and 'photo' in request.files: # filename = photos.save(request.files['photo']) url = Upload(request.files['photo']) punish = Punish(message=form.message.data, to_user=form.to_user.data, url=url) db.session.add(punish) db.session.commit() flash('You have submitted it!') return redirect(url_for('index')) return render_template('upload.html', title='Upload', form=form)
def showStorage(storage_id, page=1): storage = Storage.query.filter_by(id=storage_id).first() found_count = Item.query.filter_by(store_location_id=storage.id, status='found').count() success_count = Item.query.filter_by(store_location_id=storage.id, status='success').count() page = request.args.get('page', 1, type=int) pagination = Item.query.filter_by(store_location_id=storage.id).order_by( Item.timestamp.desc()).paginate(page, per_page=10, error_out=False) items = pagination.items form = MessageForm() if current_user.group_id == 1: messages = Message.query.filter_by(store_location_id=storage_id, is_recieve=0).order_by( Message.timestamp.desc()).all() return render_template('storage_location.html', current_user=current_user, storage=storage, found_count=found_count, success_count=success_count, items=items, form=form, messages=messages, pagination=pagination) if form.validate_on_submit(): message = Message(sender_id=current_user.id, title=form.title.data, content=form.content.data, store_location_id=storage_id) db.session.add(message) success_send = 'You are succeed sending the message to this storage!' return render_template('storage_location.html', current_user=current_user, storage=storage, found_count=found_count, success_count=success_count, items=items, success_send=success_send, form=form, pagination=pagination) return render_template('storage_location.html', current_user=current_user, storage=storage, pagination=pagination, found_count=found_count, success_count=success_count, items=items, form=form)
def home(): """ Create a home page for the message board. Display a form for users to post messages under their name. Post all messages for all visitors to see. Parameters ---------- GET Method to request data. POST Method to send data. Returns ------- Render the home.html template. """ form = MessageForm() if form.validate_on_submit(): # check if user exists in database # Determine who to link the message to user = User.query.filter_by(author=form.author.data).first() if user is None: # if not create user and add to database # Allow new users to post messages as well user = User(author=form.author.data) db.session.add(user) db.session.commit() # create row in Message table with user (created/found) add to the database message = Messages(message=form.message.data, user_id=user.id) db.session.add(message) db.session.commit() posts = [] # output all messages # create a list of dictionaries with the following structure # [{'author':'carlos', 'message':'Yo! Where you at?!'}, # {'author':'Jerry', 'message':'Home. You?'}] # Follow format for collecting author name and message specified in the home.html template list = Messages.query.all() for m in list: posts.append({ 'author': User.query.get(m.user_id).author, 'message': m.message }) return render_template('home.html', posts=posts, form=form)
def index(): form = MessageForm() if form.validate_on_submit(): uploaded_image = request.files['image'] image_name = secure_filename(uploaded_image.filename) uploaded_file = request.files['file'] file_name = secure_filename(uploaded_file.filename) if image_name != '' and file_name != '': file_ext = os.path.splitext(image_name)[1] if file_ext not in app.config['UPLOAD_EXTENSIONS']: return "Invalid image", 400 uploaded_image.save( os.path.join(app.config['UPLOAD_PATH'], image_name)) data_xls = pd.read_excel(uploaded_file) template = form_template(form) user_id = current_user.id attachment = os.path.join(app.config['UPLOAD_PATH'], image_name) body = form.message.data receiver_list = data_xls.dropna(subset=['numbers']).to_json() task = Task(user_id, template, attachment, body, receiver_list) flash('Hayırlı olsun!!') #return data_xls.to_html() #return redirect(url_for('index')) frozen = jsonpickle.encode(task) return frozen return '', 204 files = os.listdir(app.config['UPLOAD_PATH']) user = {'username': '******'} messages = [{ 'author': { 'username': '******' }, 'body': 'Beautiful day in Portland!' }, { 'author': { 'username': '******' }, 'body': 'The Avengers movie was so cool!' }] return render_template('index.html', title='Home', messages=messages, form=form, files=files)
def send_message(recipient): """Funkcja wyświetlająca formularz wysyłania wiadomości prywatnej.""" user = User.query.filter_by(username=recipient).first_or_404() form = MessageForm() if form.validate_on_submit(): msg = Message(author=current_user, recipient=user, body=form.message.data) db.session.add(msg) db.session.commit() flash(_('Your message has been sent.')) return redirect(url_for('user', username=recipient)) return render_template('send_message.html', title=_('Send Message'), form=form, recipient=recipient)
def private_chat(recipient): user = User.query.filter_by(username=recipient).first_or_404() check_match(current_user.id, user.id) form = MessageForm() if form.validate_on_submit(): msg = Message(author=current_user, recipient=user, body=form.message.data) db.session.add(msg) db.session.commit() flash('Your message has been sent') return redirect(url_for('private_chat', recipient=recipient)) return render_template('private_chat.html', title='Private Chat', form=form, recipient=recipient)
def meuform(): form = MessageForm() messages = Message.query.all() if form.validate_on_submit(): mensagem = Message() mensagem.name = form.name.data mensagem.text = form.text.data mensagem.user_id = 1 db.session.add(mensagem) db.session.commit() return redirect('/meuform') return render_template("meuform.html", form=form, messages=messages)
def send_message(recipient): user = User.query.filter_by(username=recipient).first_or_404() form = MessageForm() if form.validate_on_submit(): msg = Message(author=current_user, recipient=user, body=form.message.data) db.session.add(msg) user.add_notification('unread_message_count', user.new_messages()) db.session.commit() flash('Your message has been sent.', category='success') return redirect(url_for('user', username=recipient)) return render_template('send_message.html', title='Send Message', form=form, recipient=recipient, refresh=app.config['PAGE_REFRESH'])
def index(): user = {'nickname': 'Luis'} form = MessageForm() if form.validate_on_submit(): m = Message(author=form.author.data, body=form.body.data) db.session.add(m) db.session.commit() return redirect('/') messages = Message.query.all() return render_template('index.html', title='Mi primera aplicación', user=user, messages=messages, form=form)
def home(): form=MessageForm() if form.validate_on_submit(): # check if user exits in database # if not create user and add to database # create row in Message table with user (created/found) add to ta database user = User.query.filter_by(author= form.author.data).first() if user is None: new_user = User(author = form.author.data) db.session.add(new_user) new_message = Messages(message =form.message.data, user_id = User.query.filter_by(author = form.author.data).first().id) db.session.add(new_message) db.session.commit() posts = [ { 'author':'Carlos', 'message': 'Yo! Where you at?!' }, { 'author':'Jerry', 'message':'Home. You?' } ] # output all messages # create a list of dictionaries with the following structure # [{'author':'Carlos', 'message':'Yo! Where you at?!'}, # {'author':'Jerry', 'message':'Home. You?'}] every_message = Messages.query.all() if every_message is not None: for a_message in every_message: posts = posts + [ { 'author': f'{User.query.filter_by(id = a_message.user_id).first().author}', 'message': f'{a_message.message}' } ] return render_template('home.html', posts=posts, form=form)
def post_chat(chat_id: int): """ Send message in chat """ user_id = request.cookies.get('user_id') sender = User.query.get(user_id) chat = Chat.query.get(chat_id) if chat not in sender.chats: error_msg = 'Access is closed' response = make_response({"error": error_msg}, 403) return response form = MessageForm() if request.content_type != 'application/x-www-form-urlencoded': error_msg = 'Invalid content type' response = make_response({"error": error_msg}, 400) return response if not form.validate_on_submit(): error_msg = 'Invalid form data' response = make_response({"error": error_msg}, 400) return response data = form.content.data if chat is None: error_msg = 'Non exist chat' response = make_response({"error": error_msg}, 404) return response message = Message(content=data, chat_id=chat_id, sender_id=sender.id) chat.last_message = data chat.last_message_time = datetime.datetime.now() db.session.add(message, chat) db.session.commit() response = make_response({"status": "ok"}, 201) return response
def send_mail(user_id): user = User.query.get_or_404(user_id) #user.permissions.send_message.test(403) form = MessageForm() if form.validate_on_submit(): if setting.MAIL_ENABLE: body = render_template("emails/send_message.html", user=user, subject=form.subject.data, message=form.message.data) subject ="邮件来自 %s" % current_user.username message = Message(subject=subject, body=body, recipients=[user.email]) mail.send(message) flash(u"给%s的邮件已发出" % user.username, "successfully") else: flash(u"邮件服务器未开启,请联系管理员", "error") return redirect(url_for("user.cases", username=user.username)) return render_template("user/send_mail.html", user=user, form=form)
def messages_add(): """Add a message: Show form if GET. If valid, update message and redirect to user page. """ if not g.user: flash("Access unauthorized.", "danger") return redirect("/") form = MessageForm() if form.validate_on_submit(): msg = Message(text=form.text.data) g.user.messages.append(msg) db.session.commit() return redirect(f"/users/{g.user.id}") return render_template('messages/new.html', form=form)
def send_mail(user_id): user = User.query.get_or_404(user_id) #user.permissions.send_message.test(403) form = MessageForm() if form.validate_on_submit(): if setting.MAIL_ENABLE: body = render_template("emails/send_message.html", user=user, subject=form.subject.data, message=form.message.data) subject = "邮件来自 %s" % current_user.username message = Message(subject=subject, body=body, recipients=[user.email]) mail.send(message) flash(u"给%s的邮件已发出" % user.username, "successfully") else: flash(u"邮件服务器未开启,请联系管理员", "error") return redirect(url_for("user.posts", username=user.username)) return render_template("user/send_mail.html", user=user, form=form)
def home(): form = MessageForm() if form.validate_on_submit(): # check if user exits in database '''checks db by authors name to see if they exist or not''' user = User.query.filter_by(author=form.author.data).first() # if not create user and add to database if user is None: '''creates user and adds them to db''' user = User(author=form.author.data) db.session.add(user) db.session.commit() # create row in Message table with user (created/found) add to ta database '''creates row based on user id''' row = Messages(message=form.message.data, user_id=user.id) db.session.add(row) db.session.commit() posts = [{ 'author': 'Carlos', 'message': 'Yo! Where you at?!' }, { 'author': 'Jerry', 'message': 'Home. You?' }] # output all messages # create a list of dictionaries with the following structure # [{'author':'carlos', 'message':'Yo! Where you at?!'}, # {'author':'Jerry', 'message':'Home. You?'}] '''saves all messages in a variable to be used later''' messages = Messages.query.all() '''for all the messages that have been posted, adds them to dictionary and is visible to user''' for currentMessage in messages: posts = posts + [{ 'author': User.query.filter_by(id=currentMessage.id).first().author, 'message': currentMessage.message }] return render_template('home.html', posts=posts, form=form)