示例#1
0
    def post(self):
        args = self.parser.parse_args()
        email = args['email']
        password = args['password']
        username = email[0:email.find('@')]
        user = User.query.filter_by(email=email).first()
        if user is not None:
            return {
                'status': 400,
                'message': f'User with email {email} already exists'
            }
        user = User(email=email, username=username)
        user.set_password(password)
        with Image.open('bookspace/static/images/avatar.png') as img:
            output = io.BytesIO()
            img.save(output, format='png')
            image_data = output.getvalue()
            user.image = image_data
        session.add(user)
        try:
            session.commit()
        except SQLAlchemyError:
            session.rollback()
        status = Stats(user_id=user.id)
        session.add(user)
        session.add(status)
        try:
            session.commit()
        except SQLAlchemyError:
            session.rollback()

        msg = Message(f"BookSpace register", recipients=[email])
        msg.body = f"You've been registered! To login, use this password (you have to change it later): {password}"
        mail.send(msg)
        return {'message': 'Successfully created', 'status': 201}
示例#2
0
    def post(self):
        args = self.parser.parse_args()
        email = args['email']
        password = args['password']

        user = User.query.filter_by(email=email).first()
        if user is not None:
            return {
                'status': 400,
                'message': f'User with email {email} already exists'
            }
        elif email is not None and password is not None:
            username = email[0:email.find('@')]
            user = User(
                email=email,
                username=username,
            )
            user.set_password(password)
            with Image.open('bookspace/static/images/avatar.png') as img:
                output = io.BytesIO()
                img.save(output, format='png')
                image_data = output.getvalue()
                user.image = image_data
            session.add(user)
            try:
                session.commit()
            except SQLAlchemyError:
                session.rollback()
            status = Stats(user_id=user.id)
            session.add(status)
            try:
                session.commit()
            except SQLAlchemyError:
                session.rollback()
            return {'message': 'Successfully created', 'status': 201}
        else:
            return _BAD_REQUEST