Example #1
0
    def bucketlists():
        from app.models import BucketList
        app.logger.info("inside get bucket lists")
        if request.method == "POST":
            name = str(request.json.get('name', ''))
            if name:
                bucketlist = BucketList(name=name)
                bucketlist.save()
                response = jsonify({
                    'id': bucketlist.id,
                    'name': bucketlist.name,
                    'date_created': bucketlist.date_created,
                    'date_modified': bucketlist.date_modified
                })
                response.status_code = 201
                return response
        else:
            # GET
            bucketlists = BucketList.get_all()
            results = []

            for bucketlist in bucketlists:
                obj = {
                    'id': bucketlist.id,
                    'name': bucketlist.name,
                    'date_created': bucketlist.date_created,
                    'date_modified': bucketlist.date_modified
                }
                results.append(obj)
            response = jsonify(results)
            response.status_code = 200
            return response
Example #2
0
 def bucketlist():
     if request.method == "GET":
         bucketlists = BucketList.get_all()
         results = []
         for bucketlist in bucketlists:
             results.append({
                 "id": bucketlist.id,
                 "name": bucketlist.name,
                 "created_on": bucketlist.date_created,
                 "date_modified": bucketlist.date_modified
             })
         response = jsonify(results)
         response.status_code = 200
         return response
     elif request.method == "POST":
         name = str(request.form.get("name", ""))
         bucketlist = BucketList(name=name)
         bucketlist.save()
         response = jsonify({
             "id": bucketlist.id,
             "name": bucketlist.name,
             "created_at": bucketlist.date_created,
             "date_modified": bucketlist.date_modified
         })
         response.status_code = 201
         return response
     else:
         return jsonify({"key": "others"})
Example #3
0
def bucketlists():
    if request.method == 'POST':
        name = str(request.data.get('name'))
        if name:
            bucketlist = BucketList(name=name)
            bucketlist.save()
            response = jsonify({
                'id': bucketlist.id,
                'name': bucketlist.name,
                'date_created': bucketlist.date_created,
                'date_modified': bucketlist.date_modified
            })
            response.status_code = 201
            return response
        else:
            bucketlists = BucketList.get_all()
            results = []

            for bucketlist in bucketlists:
                obj = {
                    'id': bucketlist.id,
                    'name': bucketlist.name,
                    'date_created': bucketlist.date_created,
                    'date_modified': bucketlist.date_modified
                }
                results.append(obj)
            response = jsonify(results)
            response.status_code = 200
            return response
Example #4
0
 def bucketlists():
     if request.method == 'POST':
         name = str(request.data.get('name'))
         if name:
             bucketlist = BucketList(name=name)
             bucketlist.save()
             response = jsonify({
                 'id': bucketlist.id,
                 'name': bucketlist.name,
                 'date_created': bucketlist.date_created,
                 'date_modified': bucketlist.date_modified
             })
             response.status_code = 201
             return response
     else:
         bucketlists = User.query.all()
         results = []
         return 'Count: ' + str(bucketlists)
         for bucketlist in bucketlists:
             obj = {
                 'id': bucketlist.id,
                 'name': bucketlist.name,
                 'date_created': bucketlist.date_created,
                 'date_modified': bucketlist.date_modified
             }
             results.append(obj)
         return "success"
         response = jsonify(results)
         response.status_code = 200
         return {"message": "succeess"}
 def setUp(self):
     self.app = create_app("testing")
     self.app_context = self.app.app_context()
     self.app_context.push()
     db.drop_all()
     db.create_all()
     u = User(username=self.default_username)
     u.hash_password(self.default_password)
     u.save()
     g.user = u
     bucketlist = BucketList(name=self.default_bucketlist)
     bucketlist.create()
     bucketlist.save()
     item = BucketItem(name=self.default_bucketlistitem, bucketlist_id=bucketlist.id)
     item.create()
     item.save()
     self.client = self.app.test_client()
Example #6
0
 def setUp(self):
     self.app = create_app('testing')
     self.app_context = self.app.app_context()
     self.app_context.push()
     db.drop_all()
     db.create_all()
     u = User(username=self.default_username)
     u.hash_password(self.default_password)
     u.save()
     g.user = u
     bucketlist = BucketList(name=self.default_bucketlist)
     bucketlist.create()
     bucketlist.save()
     item = BucketItem(name=self.default_bucketlistitem,
                       bucketlist_id=bucketlist.id)
     item.create()
     item.save()
     self.client = self.app.test_client()
Example #7
0
def get_bucketlist():
    #get access tocken from header
    auth_header = request.headers.get('Authorization')

    access_token = auth_header.split(" ")[1]
    if access_token:
        user_id = User.decode_token(access_token)
        if not isinstance(user_id, str):
            if request.method == "POST":
                topic = str(request.data.get('topic', ''))
                topic_by = str(request.data.get('topic_by', ''))
                if topic:
                    bucketlist = BucketList(topic=topic,
                                            topic_by=topic_by,
                                            created_by=user_id)
                    bucketlist.save()
                    response = jsonify({
                        'id': bucketlist.id,
                        'topic': bucketlist.topic,
                        'topic_by': bucketlist.topic_by,
                        'created_date': bucketlist.created_date,
                        'date_modified': bucketlist.date_modified,
                        'created_by': user_id
                    })
                    return make_response(response), 201

            else:
                bucketlists = BucketList.get_all()
                results = []
                for bucketlist in bucketlists:
                    obj = {
                        'id': bucketlist.id,
                        'topic': bucketlist.topic,
                        'topic_by': bucketlist.topic_by,
                        'created_date': bucketlist.created_date,
                        'date_modified': bucketlist.date_modified,
                        'created_by': bucketlist.created_by
                    }
                    results.append(obj)
                return make_response(jsonify(results)), 200
        else:
            message = user_id
            response = {'message': message}
            return make_response(jsonify(response)), 401
Example #8
0
def create_bucketlist(name, created_by):
    bucketlist = BucketList.query.filter_by(
        name=name, created_by=created_by).first()
    if bucketlist:
        return jsonify({"message": "Bucketlist with the name %s already exist" % (name)})
    else:
        bucketlist = BucketList(name, created_by)
        return jsonify({"message": "Saved",
                        "name": name,
                        "created_by": created_by
                        }) if bucketlist.save() \
            else jsonify({"message": "Cannot save bucketlist"})
Example #9
0
    def setUp(self):
        """
        Setup for Bucketlist API testing
        """
        self.app = create_app('testing')
        self.client = self.app.test_client()
        db.create_all()

        # user creation
        user = User(username=self.username)
        user.hash_password(password=self.password)
        user.save()
        g.user = user

        # bucketlist creation
        bucketlist = BucketList(name=self.bucketlist_name)
        bucketlist.save()

        # bucketlistitem creation
        bucketlistitem = BucketListItem(name=self.bucketlistitem_name, bucketlist_id=bucketlist.id)
        bucketlistitem.save()
    def post(self, user_id):
        name = request.data.get("name", "").strip()
        if name:
            existing = BucketList.query.filter_by(name=name,
                                                  created_by=user_id).first()
            if existing:
                response = {"message": "Bucketlist exists"}
                return make_response(jsonify(response)), 409

            bucketlist = BucketList(name=name, created_by=user_id)
            bucketlist.save()
            response = jsonify({
                "id": bucketlist.id,
                "name": bucketlist.name,
                "date_created": bucketlist.date_created,
                "date_modified": bucketlist.date_modified,
                "created_by": user_id
            })
            return make_response(response), 201

        response = {"message": "Bucketlist can not be blank name"}
        return make_response(jsonify(response)), 400
Example #11
0
 def bucketlists():
     if request.method == 'POST':
         name = str(request.data.get('name', ''))
         if name:
             bucketlist = BucketList(name=name)
             bucketlist.save()
             response = jsonify({
                 'id': bucketlist.id,
                 'name': bucketlist.name,
                 'date_created': bucketlist.date_created,
                 'date_modified': bucketlist.date_modified
             })
             response.status_code = 201
             return response
     else:
         # GET
         bucketlists = BucketList.get_all()
         results = list(map(lambda bucket: dict(id=bucket.id, name=bucket.name, date_created=bucket.date_created,
                                                date_modified=bucket.date_modified), bucketlists))
         response = jsonify(results)
         response.status_code = 200
         return response
Example #12
0
def create_bucketlist(name, created_by):
    bucketlist = BucketList.query.filter_by(name=name,
                                            created_by=created_by).first()
    if bucketlist:
        return jsonify(
            {"message": "Bucketlist with the name %s already exist" % (name)})
    else:
        bucketlist = BucketList(name, created_by)
        return jsonify({"message": "Saved",
                        "name": name,
                        "created_by": created_by
                        }) if bucketlist.save() \
            else jsonify({"message": "Cannot save bucketlist"})
Example #13
0
    def setUp(self):
        db.create_all(app=self.app_t)

        self.today = datetime.now().strftime('%Y-%m-%d')
        self.http = self.create_app().test_client()

        # Create and add a user to the db
        user = User(username='******',
                    password=generate_password_hash('migwi123'))
        user.save()

        # Add a BucketList to the db
        bucketlist = BucketList(name='December Vacation',
                                date_created=datetime.now(),
                                date_modified=datetime.now(),
                                created_by=1)
        bucketlist.save()

        # Add Items to the db
        # Task 1
        tasks1 = Item(name='Visit Nigeria',
                      bucketlist_id=1,
                      date_created=datetime.now(),
                      date_modified=datetime.now(),
                      done=False)
        tasks1.save()

        # Task 2
        tasks2 = Item(name='Visit New York',
                      bucketlist_id=1,
                      date_created=datetime.now(),
                      date_modified=datetime.now(),
                      done=False)
        tasks2.save()

        # Task 3
        tasks3 = Item(name='Visit Las Vegas',
                      bucketlist_id=1,
                      date_created=datetime.now(),
                      date_modified=datetime.now(),
                      done=False)
        tasks3.save()

        query_user = User.query.filter_by(username='******').first()
        if query_user:
            user = {}
            user['username'] = '******'
            user['id'] = query_user.id
            token = generate_a_token(user)

        # retrieve a token ::: Token should not a contain password
        if token:
            self.auth_head = {'Authorization': 'Bearer %s' % token}
        else:
            log.error('No Token Was Generated')
            self.fail()  # stop if a token generation failed

        # Add Other Users
        user = User(username='******',
                    password=generate_password_hash('njirap123'))
        user.save()

        user = User(username='******',
                    password=generate_password_hash('kimani123'))
        user.save()

        # Add a BucketList to the db
        bucketlist = BucketList(name='Visit Kenya',
                                date_created=datetime.now(),
                                date_modified=datetime.now(),
                                created_by=1)
        bucketlist.save()
Example #14
0
    def bucketlists(user_id, *args, **kwargs):
        if request.method == "POST":
            all_bucketlists = [
                bucketlist.name for bucketlist in BucketList.query.filter_by(
                    created_by=user_id)
            ]
            name = str(request.data.get("name", ""))
            if name and name not in all_bucketlists:
                bucketlist = BucketList(name=name, created_by=user_id)
                bucketlist.save()
                response = jsonify({
                    "id": bucketlist.id,
                    "name": bucketlist.name,
                    "date_created": bucketlist.date_created,
                    "date_modified": bucketlist.date_modified,
                    "created_by": user_id
                })

                return make_response(response), 201

            elif name in all_bucketlists:
                response = jsonify({"message": "Bucket List already exists"})
                return make_response(response), 409

        elif request.method == "GET":
            search_query = str(request.args.get("q", ""))

            if not search_query:
                # Paginate results for all bucketlists by default
                limit = request.args.get("limit")
                if request.args.get("page"):
                    # Get the page requested
                    page = int(request.args.get("page"))
                else:
                    # If no page number request, start at the first one
                    page = 1

                if limit and int(limit) < 100:
                    # Use the limit value from user if it exists
                    limit = int(request.args.get("limit"))
                else:
                    # Set the default limit value if none was received
                    limit = 10

                paginated_results = BucketList.query.filter_by(
                    created_by=user_id).paginate(page, limit, False)

                if paginated_results.has_next:
                    next_page = request.endpoint + '?page=' + str(
                        page + 1) + '&limit=' + str(limit)
                else:
                    next_page = ""

                if paginated_results.has_prev:
                    previous_page = request.endpoint + '?page=' + str(
                        page - 1) + '&limit=' + str(limit)
                else:
                    previous_page = ""

                paginated_bucketlists = paginated_results.items
                # Return the bucket lists
                results = []

                for bucketlist in paginated_bucketlists:
                    # Get the items in the bucketlists searched
                    items = Item.query.filter_by(bucketlist_id=bucketlist.id)
                    items_list = []

                    for item in items:
                        obj = {
                            "id": item.id,
                            "name": item.name,
                            "date_created": item.date_created,
                            "date_modified": item.date_modified,
                            "done": item.done
                        }
                        items_list.append(obj)

                    bucketlist_object = {
                        "id": bucketlist.id,
                        "name": bucketlist.name,
                        "date_created": bucketlist.date_created,
                        "date_modified": bucketlist.date_modified,
                        "items": items_list,
                        "created_by": bucketlist.created_by
                    }
                    results.append(bucketlist_object)

                response = {
                    "next_page": next_page,
                    "previous_page": previous_page,
                    "bucketlists": results
                }

                return make_response(jsonify(response)), 200

            elif search_query:
                # Paginate search results for bucket lists
                limit = request.args.get("limit")
                if request.args.get("page"):
                    # Get the page requested
                    page = int(request.args.get("page"))
                else:
                    # If no page number request, start at the first one
                    page = 1

                if limit and int(limit) < 100:
                    # Use the limit value from user if it exists
                    limit = int(request.args.get("limit"))
                else:
                    # Set the default limit value if none was received
                    limit = 10

                paginated_results = BucketList.query.filter(
                    BucketList.name.ilike('%' + search_query + '%')).filter_by(
                        created_by=user_id).paginate(page, limit, False)

                if paginated_results.items:

                    if paginated_results.has_next:
                        next_page = request.endpoint + '?q=' + search_query + \
                         '&page=' + str(page + 1) + '&limit=' + str(limit)
                    else:
                        next_page = ""

                    if paginated_results.has_prev:
                        previous_page = request.endpoint + '?q=' + search_query\
                         + '&page=' + str(page - 1) + '&limit=' + str(limit)
                    else:
                        previous_page = ""

                    paginated_bucketlists = paginated_results.items
                    # Return the bucket lists
                    results = []

                    for bucketlist in paginated_bucketlists:
                        # Get the items in the bucketlists searched
                        items = Item.query.filter_by(
                            bucketlist_id=bucketlist.id)
                        items_list = []

                        for item in items:
                            obj = {
                                "id": item.id,
                                "name": item.name,
                                "date_created": item.date_created,
                                "date_modified": item.date_modified,
                                "done": item.done
                            }
                            items_list.append(obj)

                        bucketlist_object = {
                            "id": bucketlist.id,
                            "name": bucketlist.name,
                            "date_created": bucketlist.date_created,
                            "date_modified": bucketlist.date_modified,
                            "items": items_list,
                            "created_by": bucketlist.created_by
                        }
                        results.append(bucketlist_object)

                    response = {
                        "next_page": next_page,
                        "previous_page": previous_page,
                        "bucketlists": results
                    }

                    return make_response(jsonify(response)), 200

                # If there are no results after the search
                else:
                    response = {
                        "message": "No results found",
                        "bucketlists": []
                    }
                    return make_response(jsonify(response)), 404

            else:
                # If the request was not a search
                results = []

                for bucketlist in bucketlists:
                    # Get the items in the bucketlists searched
                    items = Item.query.filter_by(bucketlist_id=bucketlist.id)
                    items_list = []

                    for item in items:
                        obj = {
                            "id": item.id,
                            "name": item.name,
                            "date_created": item.date_created,
                            "date_modified": item.date_modified,
                            "done": item.done
                        }
                        items_list.append(obj)

                    bucketlist_object = {
                        "id": bucketlist.id,
                        "name": bucketlist.name,
                        "date_created": bucketlist.date_created,
                        "date_modified": bucketlist.date_modified,
                        "items": items_list,
                        "created_by": bucketlist.created_by
                    }
                    results.append(bucketlist_object)

                return make_response(jsonify(results)), 200
Example #15
0
def bucketlists():
    '''Route for bucketlist(POST) and (GET)'''
    time_now = datetime.now()
    if request.method == 'POST':
        bucklist_name = None
        if request.json:
            bucklist_name = request.json.get('name', None)

        if not bucklist_name:
            return return_response(dict(Error='Create Failed: %s'
                                              '' % ACCEPTED_INPUT_FORMAT), 400)

        all_current_bucketlist = BucketList.query.filter_by(
            created_by=g.user_id).all()
        bucketlist_names = [bucket.name for bucket in all_current_bucketlist]

        # check if the name was passed or exists
        new_items = None
        if bucklist_name not in bucketlist_names:
            b_list = BucketList(name=bucklist_name,
                                date_created=time_now,
                                date_modified=time_now,
                                created_by=g.user_id)
            new_items = b_list.save()

        if new_items:
            return return_response(new_items.get(), 201)
        return return_response(dict(Message="Resource Created Successfully"),
                               201)

    if request.method == 'GET':
        return_bucketlists = {}

        limit = '20' if not request.args else request.args.get('limit', '20')
        limit = 20 if not limit.isdigit() else int(limit)
        if limit > 100:
            return return_response(
                dict(Error=('Get Failed: Only a maximum of 100 '
                            'items can be retrieved at ago')), 400)

        page = '1' if not request.args else request.args.get('page', '1')
        page = 1 if not page.isdigit() else int(page)  # equal to 1 by default

        # Pagination
        count = limit * (page - 1)
        base_query = (BucketList.query.filter_by(created_by=g.user_id).
                      order_by(BucketList.id))
        all_bucks = base_query.paginate(page, limit, False).items

        # searching a word in bucketlist names
        q = '' if not request.args else request.args.get('q', None)
        if q:
            base_query = BucketList.query.filter(BucketList.name.contains(q))
            all_bucks = base_query.filter_by(created_by=g.user_id).all()

        if not all_bucks:
            return return_response(
                dict(Error=('Get Failed: No Bucketlists Found.')), 400)

        for bucket_l in all_bucks:
            items = Item.query.filter_by(bucketlist_id=bucket_l.id).all()
            bucketlists = bucket_l.get()
            bucketlists['item'] = Item().get_all(items)
            count += 1
            return_bucketlists[count] = bucketlists
        return return_response(return_bucketlists, 200)