<commit_before># -*- coding: utf-8 -*-
"""User views."""
from flask import Blueprint, render_template
from flask_login import login_required
from anerp.restful import Api, Resource, parse
from anerp.models.user import User

blueprint = Blueprint('user', __name__, static_folder='static')

api = Api(blueprint)


@blueprint.route('/list')
@login_required
def members():
    """List members."""
    return render_template('users/members.html')


from flask_restful import fields, marshal_with
from flask_restful.reqparse import Argument

user_arguments = [
    Argument('username', required=True, help='The user\'s username'),
    Argument('email', required=True, help='The user\'s email'),
    Argument('password', required=True, help='<PASSWORD>'),
    Argument('first_name', help='The user\'s first name'),
    Argument('last_name', help='The user\'s last name')
]


# user_arguments2 = dict(
#     username={'required': True, 'help': 'The user\'s username'},
#     email={'required': True, 'help': 'The user\'s email'},
#     password={'<PASSWORD>': True, 'help': '<PASSWORD>\'<PASSWORD>'},
#     first_name={'help': 'The user\'s first name'},
#     last_name={'help': 'The user\'s last name'})

user_fields = {
    'id': fields.Integer,
    'username': fields.String,
    'email': fields.String}


@api.route('/<int:id>')
class UserApi(Resource):

    @marshal_with(user_fields)
    def get(self, id):
        return User.query.get_or_404(id)

    def patch(self, id):
        user = User.query.get_or_404(id)
        args = parse('*', user_arguments)
        print(type(args))
        print(dir(args))
        for key, value in args.items():
            setattr(user, key, value)
        user.update()
        return self.get(id)

    def delete(self, id):
        user = User.query.get_or_404(id)
        user.delete()
        return '', 204


@api.route('/')
class UserList(Resource):

    @marshal_with(user_fields)
    def get(self):
        return User.query.all()

    @marshal_with(user_fields)
    def post(self):
        args = parse('username email password', user_arguments)
        user = User.create(args.username, args.email, args.password)
        return user<commit_msg>Creating a rest API for users.<commit_after># -*- coding: utf-8 -*-
"""User views."""
from flask import Blueprint, render_template
from flask_login import login_required
from anerp.restful import Api, marshal_with, request_parser, Resource
from anerp.models.user import User

blueprint = Blueprint('user', __name__, static_folder='static')

api = Api(blueprint)


@blueprint.route('/list')
@login_required
def members():
    """List members."""
    return render_template('users/members.html')

arguments = {
    'id': {'type': int, 'help': 'The user\'s id'},
    'username': {'help': 'The user\'s username'},
    'email': {'help': 'The user\'s email'},
    'password': {'help': '<PASSWORD>'},
    'first_name': {'help': 'The user\'s first name'},
    'last_name': {'help': 'The user\'s last name'}}

patch_parser = request_parser(arguments, remove=['id'])  # create new parser

post_parser = request_parser(  # copy patch_parser
    patch_parser,
    update={  # update some arguments
        'username': {'required': True},
        'email': {'required': True},
        'password': {'required': True}},
    remove=['first_name', 'last_name'])

public_fields = User.marshal_fields(
    'id',
    'username',
    'email',
    'first_name',
    'last_name',
    'created_at')


@api.route('/<int:id>')
class UserApi(Resource):

    @marshal_with(public_fields)
    def get(self, id):
        return User.query.get_or_404(id)

    def patch(self, id):
        user = User.query.get_or_404(id)
        args = patch_parser.parse_args()
        for key, value in args.items():
            setattr(user, key, value)
        user.update()
        return self.get(id)

    def delete(self, id):
        user = User.query.get_or_404(id)
        user.delete()
        return '', 204


@api.route('/')
class UserList(Resource):

    @marshal_with(public_fields)
    def get(self):
        return User.query.all()

    @marshal_with(public_fields)
    def post(self):
        args = post_parser.parse_args()
        user = User.create(
            username=args.username,
            email=args.email,
            password=<PASSWORD>)
        return user
{'fraction_non_alphanumeric': 0.1563149388132071, 'fraction_numerical': 0.006234126067882706, 'mean_word_length': 3.3363363363363363, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '307900', 'n_tokens_mistral': 1390, 'n_tokens_neox': 1344, 'n_words': 331}