Exemplo n.º 1
0
def message(recipient):
    recipient = recipient.lower()
    user = User.query.filter_by(username=recipient).first_or_404()
    current_user.last_message_read_time = datetime.utcnow()
    db.session.commit()
    #if user == current_user:
    #return redirect(url_for('messages'))
    form = MessageForm()
    if form.validate_on_submit():
        msg = m(author=current_user, recipient=user, body=form.message.data)
        db.session.add(msg)
        db.session.commit()
        flash('Your message has been sent.', 'success')
        return redirect(url_for('message', recipient=recipient))
    sent = current_user.messages_sent.filter_by(recipient_id=user.id)
    received = current_user.messages_received.filter_by(sender_id=user.id)
    messages = sent.union(received).order_by(m.timestamp.asc())

    #adding all names of people who texted me to recent chats
    received_all = current_user.messages_received.order_by(m.timestamp.desc())
    recent_chats = list()
    for message in received_all:
        recent_chats.append(message.author)
    recent_chats = list(dict.fromkeys(recent_chats))

    return render_template('send_message.html',
                           recipient=recipient,
                           title="Chat with " + recipient.title(),
                           user=user,
                           form=form,
                           messages=messages,
                           received_all=received_all,
                           recent_chats=recent_chats)
Exemplo n.º 2
0
def send_message(recipient):
    recipient = recipient.lower()
    user = User.query.filter_by(username=recipient).first_or_404()
    if user == current_user:
        return redirect(url_for('messages'))
    form = MessageForm()
    if form.validate_on_submit():
        msg = m(author=current_user, recipient=user, body=form.message.data)
        db.session.add(msg)
        db.session.commit()
        flash('Your message has been sent.', 'success')
        return redirect(url_for('user_posts', username=recipient))
    return render_template('send_message.html',
                           recipient=recipient,
                           title="Send Message",
                           form=form)