def create(request): already_assigned_meals = None if request.method == "POST": form = ListCreateForm(request.POST) if form.is_valid(): shopping_list = None try: from_date = form.cleaned_data["from_date"] to_date = form.cleaned_data["to_date"] date = form.cleaned_data["date"] shopping_list = ShoppingList.create_for_range(from_date, to_date) shopping_list.date = date shopping_list.save() except ShoppingList.RangeNotEmptyException as e: override_hash = hashlib.sha1("%s%s%s" % (date.ctime(), from_date.ctime(), to_date.ctime())).hexdigest() if form.cleaned_data["override"] == override_hash: shopping_list = ShoppingList.create_for_range(from_date, to_date, override=True) shopping_list.date = date shopping_list.save() else: form.data = form.data.copy() form.data["override"] = override_hash already_assigned_meals = e[0].select_related("shopping_list") if shopping_list: messages.success(request, "Shopping list created updated.") return redirect(shopping_list) else: form = ListCreateForm() dict = {"form": form, "already_assigned_meals": already_assigned_meals} return render_to_response("lists/create.html", dict, context_instance=RequestContext(request))
def post(self): try: list_add_args = { 'user_auth_token': fields.Str(required=True), 'listName': fields.Str(required=True), 'description': fields.Str(required=False) } request_args = parser.parse(list_add_args, request) user_auth_token = request_args['user_auth_token'] listName = request_args['listName'] description = request_args['description'] user_auth_record = AuthTokens.query.filter_by(auth_key=user_auth_token).first() list_guid = generate_hash_key() new_list = ShoppingList(user_auth_record.user_guid, list_guid, listName, description) db.session.add(new_list) db.session.commit() return jsonify(isListAddSuccessful=True, listAddErrorMessage="") except Exception as e: logger.error(str(e)) return jsonify(isListAddSuccessful=False, listAddErrorMessage="An exception occurred while adding list to database.")
def test_empty_range_creation(self): fro = datetime.date(2010, 9, 1) to = datetime.date(2010, 9, 30) l = ShoppingList.create_for_range(fro, to) meals = l.meals.all() self.assertEqual(meals.count(), 60) for meal in meals: meal.full_clean() l.full_clean()
def test_non_empty_range_creation_with_override(self): from planning.models import Meal fro = datetime.date(2010, 9, 1) to = datetime.date(2010, 9, 3) preexistent_meal = Meal.objects.create(date=datetime.date(2010, 9, 2), time_of_day='L', persons=1) l = ShoppingList.create_for_range(fro, to, override=True) meals = l.meals.all() self.assertEqual(meals.count(), 6) preexistent_meal = Meal.objects.get(pk= preexistent_meal.pk) # Update cache self.assertEqual(preexistent_meal.shopping_list, l) for meal in meals: meal.full_clean() l.full_clean()
def test_empty_range_creation_with_no_dinner_on_last_day(self): date = datetime.date(2010, 9, 1) l = ShoppingList.create_for_range(date, date, moment_to='L') meal = l.meals.get() self.assertEqual(meal.date, date) self.assertEqual(meal.time_of_day, 'L')
def test_empty_range_creation_with_no_lunch_on_first_day(self): date = datetime.date(2010, 9, 1) l = ShoppingList.create_for_range(date, date, moment_from='D') meal = l.meals.get() self.assertEqual(meal.date, date) self.assertEqual(meal.time_of_day, 'D')
def setUp(self): self.shopping_list = ShoppingList()
class ShoppingListTest(unittest.TestCase): """Tests for the ShoppingList class""" def setUp(self): self.shopping_list = ShoppingList() # self.list_item = ListItem() def test_shoppingList_property_initialization(self): self.assertEquals(self.shopping_list.id, 0, msg="initial value of id should be zero") self.assertIsInstance(self.shopping_list.items, list, msg="items is not a list") def test_add_item_to_shopping_list(self): self.shopping_list.add_item(name="mac book", description="apple", quantity=1) self.assertIsInstance(self.shopping_list.items[0], ListItem, msg="item was not added to shopping list") def test_get_item_on_shopping_list(self): self.shopping_list.add_item(name="mac book", description="apple", quantity=1) item = self.shopping_list.get_item(1) self.assertEquals(item.id, 1, msg="failed to get item from the list") def test_update_item_on_shopping_list(self): self.shopping_list.add_item(name="mac book", description="apple", quantity=1) self.shopping_list.update_item(item_id=1, quantity=2) item = self.shopping_list.get_item(1) self.assertEquals(item.quantity, 2, msg="item was not updated on the shopping list") def test_delete_item_from_shopping_list(self): self.shopping_list.add_item(name="mac book", description="apple", quantity=1) self.shopping_list.delete_item(1) self.assertEquals(len(self.shopping_list.items), 0, msg="item was not deleted from shopping list") def test_view_items_on_shopping_list(self): self.shopping_list.add_item(name="mac book", description="apple", quantity=1) items = self.shopping_list.view_items() self.assertEquals(items[1][0], "mac book", "view all shopping list items added failed")