<commit_before>from datetime import datetime, timedelta
from flask import jsonify, request, abort, g
import jwt
import json
import re
from app import app
from app.v1.models import Users, BucketList, Items
from sqlalchemy.exc import IntegrityError
from app import databases
databases.create_all()


@app.route('/bucketlist/api/v1/auth/register', methods=['POST'])
def register():
    request.get_json(force=True)
    try:

        uname = request.json['username']
        passwd = request.json['password']
        if not uname:
            response = jsonify({'error': 'Username cannot be blank'})
            return response
        elif not re.match("^[a-zA-Z0-9_]*$", uname):
            response = jsonify({'error': 'Username cannot contain special characters'})
            return response
        elif len(passwd) < 5:
            response = jsonify({'error': 'Password should be more than 5 characters'})
            return response
        else:
            res = Users.query.all()
            uname_check = [r.username for r in res]
            if uname in uname_check:
                response = jsonify({'error': 'This username is already in use'})
                return response
            else:
                userInfo = Users(username=uname)
                userInfo.hash_password(passwd)
                userInfo.save()
                response = jsonify(
                    {'Registration status': 'Successfully registered ' + userInfo.username})
                response.status_code = 201
                return response
    except KeyError:
        response = jsonify({
            'error': 'Please use username and password for dict keys.'
        })
        response.status_code = 500
        return response


@app.route('/bucketlist/api/v1/auth/login', methods=['POST'])
def login():
    request.get_json(force=True)
    try:
        user_name = request.json['username']
        passwd = request.json['password']
        res = Users.query.all()
        user_name_check = [user.username for user in res if user.verify_password(passwd) == True]
        if not user_name:
            response = jsonify({'error': 'Username field cannot be blank'})
            response.status_code = 400
            return response
        elif not passwd:
            response = jsonify({'error': 'Password field cannot be blank'})
            response.status_code = 400
            return response

        elif not re.match("^[a-zA-Z0-9_]*$", user_name):
            response = jsonify({'error': 'Username cannot contain special characters'})
            response.status_code = 400
            return response
        elif user_name in user_name_check:
            payload = {
                "username": user_name,
                "exp": datetime.utcnow() + timedelta(seconds=1500)}
            token = jwt.encode(payload, app.config['SECRET_KEY'], algorithm='HS256')

            response = jsonify(
                {'Login status': 'Successfully Logged in ',
                 'Token': token.decode('utf-8')})
            g.user = user_name
            response.status_code = 200
            return response
        else:
            response = jsonify(
                {'Login status': 'Invalid credentials'})
            response.status_code = 200
            return response
    except KeyError:
        response = jsonify({
            'error': 'Please use username and password for dict keys.'
        })
        response.status_code = 500
        return response


@app.route('/bucketlist/api/v1/bucketlist', methods=['POST'])
def create_bucketlist():
    request.get_json(force=True)
    try:
        verify_token(request)
        bucketlist_name = request.json['name']
        res = BucketList.query.all()
        if not bucketlist_name:
            response = jsonify({'error': 'Your Bucketlist needs a name/title to proceed.'})
            response.status_code = 403
            return response
        else:
            bucket = BucketList(name=bucketlist_name)
            bucket.save()
            response = jsonify(
                {'Status': 'Success'})
            response.status_code = 201
            return response
    except KeyError:
        response = jsonify({
            'error': 'Please use name for dict key.'
        })
        response.status_code = 500
        return response


@app.route('/bucketlist/api/v1/bucketlist', methods=['GET'])
def get_bucketlist():
    verify_token(request)
    res = BucketList.query.all()
    if not res:
        response = jsonify({'error': 'Ooops! You have not created any bucketlist yet!'})
        response.status_code = 200
        return response
    else:
        bucketlist_data = []
        for data in res:
            final = {
                'id': data.id,
                'name': data.name,
                'date-created': data.date_created,
                'date_modified': data.date_modified,
            }
            bucketlist_data.append(final)
        response = jsonify(bucketlist_data)
        response.status_code = 200
        return response


@app.route('/bucketlist/api/v1/bucketlist/<int:bucket_id>', methods=['GET', 'PUT', 'DELETE'])
def bucketlist_by_id(bucket_id):
    verify_token(request)
    res = BucketList.query.all()
    bucket_data = [bucket for bucket in res if bucket.id == bucket_id]
    if request.method == 'GET':
        data = {}
        for data in bucket_data:
            final_data = []
            for item_data in data.items:
                item_data = {
                    'id': item_data.id,
                    'name': item_data.name,
                    'date-created': item_data.datecreated,
                    'date_modified': item_data.date_modified,
                }
                final_data.append(item_data)
            data = {
                'id': data.id,
                'name': data.name,
                'date-created': data.date_created,<commit_msg>Implement Pagination and serach by name<commit_after>from datetime import datetime, timedelta
from flask import jsonify, request, abort, g
import jwt
import json
import re
from app import app
from app.v1.models import Users, BucketList, Items
from sqlalchemy.exc import IntegrityError
from app import databases
databases.create_all()


@app.route('/bucketlist/api/v1/auth/register', methods=['POST'])
def register():
    request.get_json(force=True)
    try:

        uname = request.json['username']
        passwd = request.json['password']
        if not uname:
            response = jsonify({'error': 'Username cannot be blank'})
            return response
        elif not re.match("^[a-zA-Z0-9_]*$", uname):
            response = jsonify({'error': 'Username cannot contain special characters'})
            return response
        elif len(passwd) < 5:
            response = jsonify({'error': 'Password should be more than 5 characters'})
            return response
        else:
            res = Users.query.all()
            uname_check = [r.username for r in res]
            if uname in uname_check:
                response = jsonify({'error': 'This username is already in use'})
                return response
            else:
                userInfo = Users(username=uname)
                userInfo.hash_password(passwd)
                userInfo.save()
                response = jsonify(
                    {'Registration status': 'Successfully registered ' + userInfo.username})
                response.status_code = 201
                return response
    except KeyError:
        response = jsonify({
            'error': 'Please use username and password for dict keys.'
        })
        response.status_code = 500
        return response


@app.route('/bucketlist/api/v1/auth/login', methods=['POST'])
def login():
    request.get_json(force=True)
    try:
        user_name = request.json['username']
        passwd = request.json['password']
        res = Users.query.all()
        user_name_check = [user.username for user in res if user.verify_password(passwd) == True]
        if not user_name:
            response = jsonify({'error': 'Username field cannot be blank'})
            response.status_code = 400
            return response
        elif not passwd:
            response = jsonify({'error': 'Password field cannot be blank'})
            response.status_code = 400
            return response

        elif not re.match("^[a-zA-Z0-9_]*$", user_name):
            response = jsonify({'error': 'Username cannot contain special characters'})
            response.status_code = 400
            return response
        elif user_name in user_name_check:
            payload = {
                "username": user_name,
                "exp": datetime.utcnow() + timedelta(seconds=1500)}
            token = jwt.encode(payload, app.config['SECRET_KEY'], algorithm='HS256')

            response = jsonify(
                {'Login status': 'Successfully Logged in ',
                 'Token': token.decode('utf-8')})
            g.user = user_name
            response.status_code = 200
            return response
        else:
            response = jsonify(
                {'Login status': 'Invalid credentials'})
            response.status_code = 200
            return response
    except KeyError:
        response = jsonify({
            'error': 'Please use username and password for dict keys.'
        })
        response.status_code = 500
        return response


@app.route('/bucketlist/api/v1/bucketlist', methods=['POST'])
def create_bucketlist():
    request.get_json(force=True)
    try:
        verify_token(request)
        bucketlist_name = request.json['name']
        res = BucketList.query.all()
        if not bucketlist_name:
            response = jsonify({'error': 'Your Bucketlist needs a name/title to proceed.'})
            response.status_code = 403
            return response
        else:
            bucket = BucketList(name=bucketlist_name)
            bucket.save()
            response = jsonify(
                {'Status': 'Success'})
            response.status_code = 201
            return response
    except KeyError:
        response = jsonify({
            'error': 'Please use name for dict key.'
        })
        response.status_code = 500
        return response


@app.route('/bucketlist/api/v1/bucketlist', methods=['GET'])
def get_bucketlist():
    verify_token(request)
    res = BucketList.query.all()
    if not res:
        response = jsonify({'error': 'Ooops! You have not created any bucketlist yet!'})
        response.status_code = 200
        return response
    else:
        limit = int(request.args.get("limit", 20))
        page = int(request.args.get("page", 1))
        if limit > 100:
            limit = 100
        search = request.args.get("q", "")
        if search:
            res = [bucket for bucket in res if bucket.name in search]
            if not res:
                return jsonify({
                    'message': 'Ooops! No data matching your search query'
                })
            else:
                bucketlist_data = []
                for data in res:
                    final = {
                        'id': data.id,
                        'name': data.name,
                        'date-created': data.date_created,
                        'date_modified': data.date_modified,
                    }
                    bucketlist_data.clear()
                    bucketlist_data.append(final)
                response = jsonify(bucketlist_data)
                response.status_code = 200
                return response
        else:
            bucketlist_data = []
            for data in res:
                final = {
                    'id': data.id,
                    'name': data.name,
                    'date-created': data.date_created,
                    'date_modified': data.date_modified,
                }
                bucketlist_data.append(final)
            response = jsonify(bucketlist_data)
            response.status_code = 200
            return response


@app.route('/bucketlist/api/v1/bucketlist/<int:bucket_id>', methods=['GET', 'PUT', 'DELETE'])
def bucketlist_by_id(bucket_id):
    verify_token(request)
    res = BucketList.query.all()
    bucket_data = [bucket for bucket in res if bucket.id == bucket_id]
    if request.method == 'GET':
        data = {}
        for data in bucket_data:
            final_data = []
            for item_data in data.items:
                item_data = {
                    'id': item_data.id,
                    'name': item_data.name,
                    'date-created': item_data.datecreated,
                    'date_modified': item_data.date_modified,
                }
                final_data.append(item_data)
            data = {
                'id': data.id,
                'name': data.name,
                'date-created': data.date_created,
{'fraction_non_alphanumeric': 0.10246060700147482, 'fraction_numerical': 0.010090817356205853, 'mean_word_length': 1.791161178509532, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 6, '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': '3668248', 'n_tokens_mistral': 3547, 'n_tokens_neox': 3333, 'n_words': 896}