<commit_before>"""Contain helper functions."""

from sqlalchemy.exc import IntegrityError
from bucketlist.app import db, app
from flask import g, request, jsonify
from bucketlist.models import User


@app.before_request
def before_request():
    """Set global attributes."""
    if request.endpoint not in ["userlogin", "userregister", "home"]:
        if request.endpoint in ["createitem",
                                "updateitem",
                                "deleteitem",
                                "createbucketlist",
                                "getallbucketlists",
                                "getsinglebucketlist",
                                "updatebucketlist",
                                "deletebucketlist"]:
            token = request.headers.get("token")
            if token is not None:
                user = User.verify_auth_token(token)
                if user:
                    g.user = user
                else:
                    return jsonify({"message": "Error: Invalid Token"}), 401
            else:
                return jsonify({"message": "Error: Please enter a token"}), 401
        else:
            return jsonify({"message": "Error: Wrong URL or Incorrect request"
                            " METHOD. Please check and try again"}), 400


def add_user(user_object):
    """Add a user, bucket list, or bucket list item to the database."""
    try:
        db.session.add(user_object)
        db.session.commit()

        message = {"message": "You have successfully registered."
                   "Please login to get an access token"}
        return message, 201

    except IntegrityError:
        """Show when the username already exists"""
        db.session.rollback()
        return {"message": "Error: " + user_object.email +
                " already exists."}, 400


def add_bucketlist(bucketlist_object):
    """Add a bucketlist item to the database."""
    try:
        db.session.add(bucketlist_object)
        db.session.commit()
        message = {"message": "You have successfully added a new bucketlist."}
        return message, 201

    except IntegrityError:
        """Show when the bucketlist already exists"""
        db.session.rollback()
        return {"message": "Error: " + bucketlist_object.title +
                " already exists."}, 400

<commit_msg>[ft #148219329] Include user details in response<commit_after>"""Contain helper functions."""

from sqlalchemy.exc import IntegrityError
from bucketlist.app import db, app
from flask import g, request, jsonify
from bucketlist.models import User
from flask_restful import marshal, fields


@app.before_request
def before_request():
    """Set global attributes."""
    if request.endpoint not in ["userlogin", "userregister", "home"]:
        if request.endpoint in ["createitem",
                                "updateitem",
                                "deleteitem",
                                "createbucketlist",
                                "getallbucketlists",
                                "getsinglebucketlist",
                                "updatebucketlist",
                                "deletebucketlist"]:
            token = request.headers.get("token")
            if token is not None:
                user = User.verify_auth_token(token)
                if user:
                    g.user = user
                else:
                    return jsonify({"message": "Error: Invalid Token"}), 401
            else:
                return jsonify({"message": "Error: Please enter a token"}), 401
        else:
            return jsonify({"message": "Error: Wrong URL or Incorrect request"
                            " METHOD. Please check and try again"}), 400


def add_user(user_object):
    """Add a user, bucket list, or bucket list item to the database."""
    try:
        db.session.add(user_object)
        db.session.commit()
        user_serializer = {"id": fields.Integer,
                           "username": fields.String,
                           "email": fields.String}

        message = {"message": "You have successfully registered."
                   "Please login to get an access token"}
        message.update(marshal(user_object, user_serializer))
        return message, 201

    except IntegrityError:
        """Show when the username already exists"""
        db.session.rollback()
        return {"message": "Error: " + user_object.email +
                " already exists."}, 400


def add_bucketlist(bucketlist_object):
    """Add a bucketlist item to the database."""
    try:
        db.session.add(bucketlist_object)
        db.session.commit()
        message = {"message": "You have successfully added a new bucketlist."}
        return message, 201

    except IntegrityError:
        """Show when the bucketlist already exists"""
        db.session.rollback()
        return {"message": "Error: " + bucketlist_object.title +
                " already exists."}, 400


{'fraction_non_alphanumeric': 0.09953935509713599, 'fraction_numerical': 0.010214300020028039, 'mean_word_length': 1.7713651498335183, 'pattern_counts': {'":': 17, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4415074', 'n_tokens_mistral': 1285, 'n_tokens_neox': 1174, 'n_words': 380}