Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add route('/send_message/<recipient>', methods=['GET', 'POST']) #45

Merged
merged 1 commit into from
Feb 15, 2024

Conversation

JustRomanVolkov
Copy link
Owner

No description provided.

Copy link

Here are some suggestions for improving the code in app/main/routes.py:

  1. Add docstrings to the functions to explain their purpose and any parameters they accept. This will make the code more readable and help other developers understand its behavior.

Example:

@bp.route('/send_message/<recipient>', methods=['GET', 'POST'])
@login_required
def send_message(recipient):
    """
    Send a message to a specified recipient.

    Args:
        recipient (str): The username of the recipient user.

    Returns:
        The rendered template for sending a message.

    """
    ...
  1. Use an SQLAlchemy query to get the user instead of using db.first_or_404(). You can pass the recipient parameter directly in the query instead of using an extra condition.

Instead of:

user = db.first_or_404(sa.select(User).where(User.username == recipient))

You can use:

user = User.query.filter_by(username=recipient).first_or_404()
  1. Consider moving the message sending logic to a separate function. This can improve code organization and make the send_message function more focused on handling the request and rendering the template.

Example:

def send_message(recipient, form):
    user = User.query.filter_by(username=recipient).first_or_404()
    msg = Message(author=current_user, recipient=user, body=form.message.data)
    db.session.add(msg)
    user.add_notification('unread_message_count', user.unread_message_count())
    db.session.commit()
    flash(_('Your message has been sent.'))
    return redirect(url_for('main.user', username=recipient))


@bp.route('/send_message/<recipient>', methods=['GET', 'POST'])
@login_required
def send_message_route(recipient):
    form = MessageForm()
    if form.validate_on_submit():
        return send_message(recipient, form)
    return render_template('send_message.html', title=_('Send Message'), form=form, recipient=recipient)
  1. Consider using consistent indentation. The line next_url=next_url, prev_url=prev_url) in the search() function has a different indentation level than the rest of the code in the function.

  2. It's a good practice to validate the existence of the recipient user before rendering the send_message template. If the user does not exist, you can display an appropriate error message.

Example:

def send_message_route(recipient):
    user = User.query.filter_by(username=recipient).first()
    if not user:
        flash(_('Recipient user not found'))
        return redirect(url_for('main.search'))
    form = MessageForm()
    if form.validate_on_submit():
        return send_message(recipient, form)
    return render_template('send_message.html', title=_('Send Message'), form=form, recipient=recipient)

By implementing these suggestions, the code will be more readable, maintainable, and follow best practices.

@JustRomanVolkov JustRomanVolkov merged commit 535defd into master Feb 15, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant