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
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', {})
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()
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
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()