<commit_before>from flask import jsonify
from flask_restful import Resource
from app.models import db, User


class BucketList(Resource):

    def get(self):

        user = User("Bonny", 1234)

    def post(self):
        pass


class BucketListEntry(Resource):

    def get(self, bucketlist_id):
        return jsonify("something is here!")

    def put(self, bucketlist_id):
        pass

    def delete(self, bucketlist_id):
        pass<commit_msg>Created endpoints for bucketlist resources<commit_after>from datetime import datetime

from flask import request, jsonify
from flask_restful import Resource

from app.auth import authenticate_token
from app.models import (db, BucketList, BucketlistItemSchema,
                        BucketlistSchema)

schema = BucketlistItemSchema()
bucketlist_schema = BucketlistSchema()


class BucketLists(Resource):
    """This class creates the endpoints
    for creating and getting bucketlists.
    """

    def get(self):
        """
        Gets all bucketlists
        """
        user = authenticate_token(request)
        limit = request.args.get("limit")
        # Paginate items using limit
        if not limit or int(limit) < 0:
            limit = 20
        elif int(limit) > 100:
            limit = 100
        else:
            limit = int(limit)
        bucketlists = BucketList.query.filter_by(
            user_id=user.id).paginate(1, limit, 30)
        bucket_list = []
        for lst in bucketlists.items:
            items = lst.items.all()
            items_list = []
            for item in items:
                item_json = schema.dump(item)
                items_list.append(item_json.data)
            bucket_list.append({"id": lst.id,
                                "user_id": lst.user_id,
                                "name": lst.name,
                                "items": items_list,
                                "date_created": lst.date_created,
                                "date_modified": lst.date_modified,
                                "created_by": lst.created_by}
                               )
        return jsonify(bucket_list)

    def post(self):
        """Creates new bucketlist"""
        user = authenticate_token(request)
        data = request.get_json(silent=True)
        try:
            name = data["name"]
        except Exception:
            return {"error": "Bucketlist has no data"}, 400
        exists = db.session.query(BucketList).filter_by(
            name=name).scalar() is not None
        if not exists:
            new_bucketlist = BucketList(name=name,
                                        user_id=user.id,
                                        created_by=user.username)
            db.session.add(new_bucketlist)
            db.session.commit()
            return {"message": "created new bucketlist",
                    "name": "{}".format(name)}, 201
        else:
            return {"message": "Bucketlist already exists"}


class BucketListSingle(Resource):
    """
    This class defines the endpoints
    for updating and deleting
    bucketlists
    """

    def get(self, bucketlist_id):
        """
        Gets a bucketlist with id <bucketlist_id>

        Returns the bucketlist as json
        """
        user = authenticate_token(request)
        # bucketlist = self.get_bucketlist(bucketlist_id)
        bucketlist = BucketList.query.filter_by(
            id=bucketlist_id, user_id=user.id).first()
        if bucketlist:
            bucketlist_items = bucketlist.items.all()
            items_list = []
            for item in bucketlist_items:
                item_json = schema.dump(item)
                items_list.append(item_json.data)
            return jsonify({
                "name": bucketlist.name,
                "id": bucketlist.id,
                "items": items_list,
                "date_created": bucketlist.date_created,
                "date_modified": bucketlist.date_modified,
                "created_by": bucketlist.created_by
            })
        else:
            return {"error": "Bucketlist not found"}, 404

    def put(self, bucketlist_id):
        """Edits bucketlist with id <bucketlist_id>"""
        authenticate_token(request)
        bucketlist = self.get_bucketlist(bucketlist_id)
        if bucketlist:
            data = request.get_json(silent=True)
            new_name = data["name"]
            bucketlist.name = new_name
            bucketlist.date_modified = datetime.utcnow()
            db.session.add(bucketlist)
            db.session.commit()
            return {"message": "Modified bucketlist {0}".format(bucketlist.id)}
        else:
            return {"error": "Bucketlist not found"}, 404

    def delete(self, bucketlist_id):
        """Deletes bucketlist with id <bucketlist_id>"""
        authenticate_token(request)
        bucketlist = self.get_bucketlist(bucketlist_id)
        if bucketlist:
            db.session.delete(bucketlist)
            db.session.commit()
            return {"message": "Deleted bucketlist {0}"
                    .format(bucketlist_id)}, 204
        else:
            return {"error": "Bucketlist not found"}, 404

    def get_bucketlist(self, bucketlist_id):
        """Gets buckelist with the id <bucketlist_id>"""
        bucketlist = BucketList.query.get(bucketlist_id)
        if bucketlist:
            return bucketlist
        else:
            return None

    def not_found(self):
        """Error message for item not found"""
        return {"error": "Bucketlist not found"}, 404
{'fraction_non_alphanumeric': 0.0930903842684467, 'fraction_numerical': 0.007035901136568645, 'mean_word_length': 1.8271290158082611, 'pattern_counts': {'":': 23, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7280557', 'n_tokens_mistral': 1537, 'n_tokens_neox': 1448, 'n_words': 361}