Example #1
0
def create_entry(category_data):
    from api.models import Category, Favorite
    
    try:
        category = Category(name=category_data['name'])
        category.save()

        for each in category_data.get('favorites', []):
            favorite = Favorite(**dict(**each, category_id=category.id))
            favorite.save()
    except Exception as e:
        print(e)
        pass
Example #2
0
def favorite(app, category3):
    params = {
        'title' : fake.alphanumeric(15),
        'description' : fake.alphanumeric(200),
        'ranking' : 1,
        'meta_data' : {
            'color' : 'red',
            'quantity' : 2,
            'date_purchased' : '2019-02-05',
            'condition' : 'bad'
        },
        'category_id' : category3.id
    }
    favorite = Favorite(**params)
    return favorite.save()
Example #3
0
def add_favorite():

    # Agregar validaciĆ³n para que no hayan repetidos en el mismo usuario.

    user_id = get_jwt_identity() #THIS CAN BE THE EMAIL 
    print("Hola", user_id)
    user = User.query.filter_by(id=user_id).first()

    if request.method == 'POST':
        req = request.get_json()
        if req is None:
            raise APIException("Resquest is need as a json object", status_code=400)
        if 'cocktail_id' not in req:
            raise APIException("Cocktail ID must be typed ", status_code=400)
        if 'cocktail_name' not in req:
            raise APIException("Cocktail name must be typed", status_code=400)
        
        fav = Favorite(cocktail_id=req["cocktail_id"],cocktail_name= req["cocktail_name"], user_id=user.id)
        db.session.add(fav)
        db.session.commit()
        
        
        getFavUser = Favorite.query.filter_by(user_id = user.id)
        mapping = list(map(lambda f: f.serialize(), getFavUser))
        
        return jsonify(mapping), 200  #It is OK

    if request.method == 'GET':
        myfavs = Favorite.query.all()
        myfavs = list(map(lambda f: f.serialize(), favs))
        return  jsonify(myfavs), 200
    return "Error, invalid method", 404
Example #4
0
 def test_get(self, init_db, favorite):
     """Test for get method
     
         Args:
             init_db(SQLAlchemy): fixture to initialize the test database
             favorite (Favorite): Fixture to create a new favorite
     """
     assert Favorite.get(favorite.id) == favorite
Example #5
0
def category_with_favorites(app, category2):
    favorites = []
    for each in range(3):
        params = {
            'title' : fake.alphanumeric(15),
            'description' : fake.alphanumeric(200),
            'ranking' : 1,
            'meta_data' : {
                'color' : 'red',
                'quantity' : 2,
                'date_purchased' : '2019-02-05',
                'condition' : 'bad'
            },
            'category_id' : category2.id
        }
        favorite = Favorite(**params)
        favorites.append(favorite.save())
    return category2
Example #6
0
def create_favorite():

    body = request.get_json()
    new_favorite = Favorite(title=body["title"],
                            description=body["description"],
                            link=body["link"],
                            user_id=body["user_id"])
    db.session.add(new_favorite)
    db.session.commit()
    return jsonify(body), 200
Example #7
0
    def handle(self, *args, **options):
        users = UserFactory.create_batch(USERS)

        for user in users:
            for _ in range(randint(0, MAX_RECIPES)):
                RecipeFactory(author=user, tags=(0, 1, 2))

        for user in User.objects.all():
            recipes = list(Recipe.objects.all())
            to_favorite = sample(recipes, k=randint(1, MAX_FAVORITES))
            Favorite.objects.bulk_create(
                [Favorite(user=user, recipe=recipe) for recipe in to_favorite])
Example #8
0
def user_favorite_foods(request):
    token = request.headers.get('token')
    token = Token.objects.filter(token=token)
    if token.exists():
        try:
            user = token[0].user
            if request.method == 'GET':
                favs = Favorite.objects.filter(user=user)
                favs_list = []
                for f in favs:
                    favs_list.append(f.to_json())

                return my_response(True, 'success', favs_list)

            elif request.method == 'POST':
                is_food = request.GET.get('isFood')
                _id = request.GET.get('id')
                fav = request.GET.get('fav')
                fav = int(fav)
                is_food = int(is_food)
                if fav == 1:
                    if is_food == 1:
                        f = Favorite(user=user, food_id=_id)
                        f.save()
                    else:
                        f = Favorite(user=user, option_id=_id)
                        f.save()
                    return my_response(True, 'success', f.to_json())
                else:
                    if is_food == 1:
                        Favorite.objects.filter(user=user,
                                                food_id=_id).delete()
                    else:
                        Favorite.objects.filter(user=user,
                                                option_id=_id).delete()
                    return my_response(True, 'success', {})

            else:
                return my_response(False, 'invalid method', {})
        except Exception as e:
            return my_response(False, 'error in favorite, ' + str(e), {})

    else:
        return my_response(False, 'token not exist', {})
Example #9
0
def add_favorite():
    user_id = current_user.get_id()
    outfit_id = request.json['outfit_id']
    name = request.json['name']
    description = request.json['description']
    rating = request.json['rating']

    new_favorite = Favorite(outfit_id, user_id, name, description, rating)

    db.session.add(new_favorite)
    db.session.commit()

    return favorite_schema.jsonify(new_favorite)
Example #10
0
    def test_save(self, init_db, category1):
        """Test creating new favorite

        Args:
            init_db (SQLAlchemy): fixture to initialize the test database
            category (Category): Fixture to create a new category
        """
        params = {
            'title' : fake.alphanumeric(15),
            'description' : fake.alphanumeric(200),
            'ranking' : 1,
            'meta_data' : {
                'color' : 'red',
                'quantity' : 2,
                'date_purchased' : '2019-02-05',
                'condition' : 'bad'
            },
            'category_id' : category1.id
        }

        favorite = Favorite(**params)
        assert favorite == favorite.save()
Example #11
0
    def test_child_relationships(self, init_db, favorite1):
        """ Test unimplemented child relationships of favorite

            Args:
                init_db(SQLAlchemy): fixture to initialize the test database
                favorite (Favorite): Fixture to create a new favorite
        """

        favorite = Favorite.get(id=favorite1.id)

        with raises(NotImplementedError) as error:
            favorite.get_child_relationships()
        
        assert  str(error.value) == "The get_relationships method must be overridden in all child model classes"
Example #12
0
reviews.append(r)

for review in reviews:
    #add to db
    db.session.add(review)
    #save
    db.session.commit()

#Seeding Favorite Data
#clear data in db
db.session.query(Favorite).delete()
db.session.commit()

favorites = []

f = Favorite(user_id='1', shop_id='1')
favorites.append(f)
f = Favorite(user_id='2', shop_id='4')
favorites.append(f)
f = Favorite(user_id='3', shop_id='1')
favorites.append(f)
f = Favorite(user_id='3', shop_id='3')
favorites.append(f)
f = Favorite(user_id='3', shop_id='4')
favorites.append(f)

for favorite in favorites:
    #add to db
    db.session.add(favorite)
    #save
    db.session.commit()