<commit_before>"""Controller(s) for dealing with users."""


from werkzeug.exceptions import BadRequest, Conflict

from peanuts.controllers.base import BaseRestController
from peanuts.models.user import User, UserData, PeanutsAuth


__all__ = ['UserController']


class UserController(BaseRestController):
    """A controller for dealing with users."""
    Model = User

    def post(self, post_data):
        """Overwrites the default post method to add information to the user
            submodels including data and authentication.
        """
        password = post_data.get('password')
        password_confirm = post_data.get('password')

        if not password or password != password_confirm:
            raise BadRequest('Password and confirmation don\'t match')

        email = post_data.get('email')
        if not email:
            raise BadRequest('Email is required.')
        elif (
                self.db_session.query(User).join(User.data).filter(
                    UserData.email == email
                ).first()
            ):
            raise Conflict('User already exists')

        user_data = UserData(
            email=email,
            first_name=post_data.get('first_name'),
            last_name=post_data.get('last_name'),
            user_name=post_data.get('user_name')
            )
        peanuts_auth = PeanutsAuth(
            email=email,
            password=password
            )
        user = User(
            is_admin=post_data.get('is_admin'),
            data=user_data,
            peanuts_auth=peanuts_auth
            )

        self.db_session.add(user)
        self.commit()

        self.session.clear()
        self.session['user_id'] = user.id_
        self.session.permanent = post_data.get('stay_logged_in')

        return user<commit_msg>admins stay logged in when creating users<commit_after>"""Controller(s) for dealing with users."""


from werkzeug.exceptions import BadRequest, Conflict

from peanuts.lib.auth import admin_need
from peanuts.controllers.base import BaseRestController
from peanuts.models.user import User, UserData, PeanutsAuth


__all__ = ['UserController']


class UserController(BaseRestController):
    """A controller for dealing with users."""
    Model = User

    def post(self, post_data):
        """Overwrites the default post method to add information to the user
            submodels including data and authentication.
        """
        password = post_data.get('password')
        password_confirm = post_data.get('password')

        if not password or password != password_confirm:
            raise BadRequest('Password and confirmation don\'t match')

        email = post_data.get('email')
        if not email:
            raise BadRequest('Email is required.')
        elif (
                self.db_session.query(User).join(User.data).filter(
                    UserData.email == email
                ).first()
            ):
            raise Conflict('User already exists')

        user_data = UserData(
            email=email,
            first_name=post_data.get('first_name'),
            last_name=post_data.get('last_name'),
            user_name=post_data.get('user_name')
            )
        peanuts_auth = PeanutsAuth(
            email=email,
            password=password
            )
        user = User(
            is_admin=post_data.get('is_admin'),
            data=user_data,
            peanuts_auth=peanuts_auth
            )

        self.db_session.add(user)
        self.commit()

        # Log the new user in, unless the current user is an admin.
        if not admin_need():
            self.session.clear()
            self.session['user_id'] = user.id_
            self.session.permanent = post_data.get('stay_logged_in')

        return user
{'fraction_non_alphanumeric': 0.09385710519377802, 'fraction_numerical': 0.0, 'mean_word_length': 2.125205930807249, 'pattern_counts': {'":': 0, '<': 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': '330918', 'n_tokens_mistral': 1033, 'n_tokens_neox': 986, 'n_words': 242}