Example #1
0
    def post(self):

        the_RU = check_user_id_cookie(self.request)

        if the_RU:
            # data that user has entered
            a_food_description = self.request.get(
                "food_description").strip().replace('\n',
                                                    ' ').replace('\r', '')
            lower_case_food_description = a_food_description.lower(
            )  # all letters displayed in lower case
            a_note = self.request.get("note").strip()
            an_exp_date_str = self.request.get(
                "expiry_date")  # a string in format "dd-mm-yyyy"
            an_item_id = self.request.get(
                "item_ID")  # this is a string "455646501654613" format

            # create objects of class InfoEntered. NB this is not an FoodItem object!!!
            obj_food = validation.is_food_item_valid(
                a_food_description
            )  # object is created inside is_food_item_valid()
            obj_exp_date = validation.is_exp_date_valid(
                an_exp_date_str,
                an_item_id)  # object is created inside is_exp_date_valid()

            # create list for the objects and append them
            obj_list = []

            obj_list.append(obj_food)
            obj_list.append(obj_exp_date)

            # check if all 'object.validation' are True; is so, a foodItem can be added to db
            if validation.are_all_validation_true(obj_list):
                # check if there is an expiry date entered, if so convert to yyyy-mm-dd
                if an_exp_date_str:
                    date_valid = obj_exp_date.get_validation_info(
                    )  # returns a Boolean
                    if date_valid:
                        # create from string a DateProperty with format yyyy-mm-dd
                        an_exp_date = datetime.strptime(
                            an_exp_date_str + " 12:00",
                            "%d-%m-%Y %H:%M").date()
                else:
                    an_exp_date = None

                # make first letter upper case
                a_food_description = validation.upper_case(a_food_description)

                # check if there is an_item_id to see whether to 'update' or 'create new item in db'
                if an_item_id:  # update already excisting item
                    #logging.debug("item id: " + an_item_id)
                    # get the specific item
                    the_item = FoodItem.get_by_id(
                        int(an_item_id)
                    )  # get the item with the specific id (an_item_id)
                    # update
                    the_item.description = a_food_description
                    the_item.lower_case_description = lower_case_food_description
                    the_item.note = a_note
                    the_item.expiry = an_exp_date

                    the_item.put()
                    time.sleep(
                        0.1)  # to delay so db table gets displayed correct
                    self.redirect(
                        "/frontpage"
                    )  # tells the browser to go to '/frontpage' and the response is empty

                else:  # no id 'an_item_id' (a new food is being added)
                    #logging.debug("No item id" )
                    current_user_id = dataFunctions.retrieveUserId(
                        the_RU.name)  # an int
                    #logging.debug("current_user_id INT: " + str(current_user_id))

                    # create item in db
                    FI = FoodItem(
                        description=a_food_description,
                        lower_case_description=lower_case_food_description,
                        note=a_note,
                        expiry=an_exp_date,
                        _is_expired=False,
                        fk_registered_user_id=current_user_id)
                    FI.put()
                    id_for_FI = str(FI.key().id())
                    time.sleep(
                        0.5)  # to delay so db table gets displayed correct

                    a_food_description_content = ""
                    a_note_content = ""
                    a_exp_content = ""
                    a_headline = "Add food to Freezer"
                    a_change_button = "Add Item"
                    a_passive_button = "Return to Frontpage"
                    a_item_id = ""
                    a_date_created = ""

                    f_d_err = ""
                    date_err = ""
                    add_msg = "Your Food Item was successfully added"

                    self.response.headers.add_header('Set-Cookie',
                                                     'sort_code=; Path=/')

                    self.render_foodPage(a_food_description_content, f_d_err,
                                         a_note_content, date_err,
                                         a_exp_content, a_headline,
                                         a_change_button, a_passive_button,
                                         a_item_id, a_date_created, add_msg,
                                         the_RU.name)

            # else re-render 'food.html' with the error messages
            else:
                # decide which params to pass based on 'add' or 'edit'
                if an_item_id:  # edit version
                    specific_item = FoodItem.get_by_id(
                        int(an_item_id)
                    )  # get the item with the specific id (an_item_id)

                    the_headline = "Edit food item"
                    the_change_button = "Submit Changes"
                    the_passive_button = "Cancel"
                    the_item_id = an_item_id

                    # create a string in format "dd-mm-yyyy" from the DateProperty yyyy-mm-dd
                    a_date_created = validation.convert_DateProperty_to_str_dash(
                        specific_item.added_date)

                    f_d_err = obj_food.get_error_msg()
                    date_err = obj_exp_date.get_error_msg()
                    add_msg = ""

                else:  # add version
                    the_headline = "Add food to Freezer"
                    the_change_button = "Add Item"
                    the_passive_button = "Return to Frontpage"
                    the_item_id = ""  # ok with empty str. when checking if "" that is False.... But can't use None to put in here...
                    a_date_created = ""

                    f_d_err = obj_food.get_error_msg()
                    date_err = obj_exp_date.get_error_msg()
                    add_msg = ""

                self.render_foodPage(a_food_description, f_d_err, a_note,
                                     date_err, an_exp_date_str, the_headline,
                                     the_change_button, the_passive_button,
                                     the_item_id, a_date_created, add_msg,
                                     the_RU.name)

        else:  # either user_id_cookie_value, username, or the_RU is None (see check_user_id_cookie())
            self.redirect("/logout")
Example #2
0
    def post(self):

        the_RU = check_user_id_cookie(self.request)

        if the_RU:
            # data that user has entered
            a_food_description = self.request.get("food_description").strip().replace("\n", " ").replace("\r", "")
            lower_case_food_description = a_food_description.lower()  # all letters displayed in lower case
            a_note = self.request.get("note").strip()
            an_exp_date_str = self.request.get("expiry_date")  # a string in format "dd-mm-yyyy"
            an_item_id = self.request.get("item_ID")  # this is a string "455646501654613" format

            # create objects of class InfoEntered. NB this is not an FoodItem object!!!
            obj_food = validation.is_food_item_valid(
                a_food_description
            )  # object is created inside is_food_item_valid()
            obj_exp_date = validation.is_exp_date_valid(
                an_exp_date_str, an_item_id
            )  # object is created inside is_exp_date_valid()

            # create list for the objects and append them
            obj_list = []

            obj_list.append(obj_food)
            obj_list.append(obj_exp_date)

            # check if all 'object.validation' are True; is so, a foodItem can be added to db
            if validation.are_all_validation_true(obj_list):
                # check if there is an expiry date entered, if so convert to yyyy-mm-dd
                if an_exp_date_str:
                    date_valid = obj_exp_date.get_validation_info()  # returns a Boolean
                    if date_valid:
                        # create from string a DateProperty with format yyyy-mm-dd
                        an_exp_date = datetime.strptime(an_exp_date_str + " 12:00", "%d-%m-%Y %H:%M").date()
                else:
                    an_exp_date = None

                # make first letter upper case
                a_food_description = validation.upper_case(a_food_description)

                # check if there is an_item_id to see whether to 'update' or 'create new item in db'
                if an_item_id:  # update already excisting item
                    # logging.debug("item id: " + an_item_id)
                    # get the specific item
                    the_item = FoodItem.get_by_id(int(an_item_id))  # get the item with the specific id (an_item_id)
                    # update
                    the_item.description = a_food_description
                    the_item.lower_case_description = lower_case_food_description
                    the_item.note = a_note
                    the_item.expiry = an_exp_date

                    the_item.put()
                    time.sleep(0.1)  # to delay so db table gets displayed correct
                    self.redirect("/frontpage")  # tells the browser to go to '/frontpage' and the response is empty

                else:  # no id 'an_item_id' (a new food is being added)
                    # logging.debug("No item id" )
                    current_user_id = dataFunctions.retrieveUserId(the_RU.name)  # an int
                    # logging.debug("current_user_id INT: " + str(current_user_id))

                    # create item in db
                    FI = FoodItem(
                        description=a_food_description,
                        lower_case_description=lower_case_food_description,
                        note=a_note,
                        expiry=an_exp_date,
                        _is_expired=False,
                        fk_registered_user_id=current_user_id,
                    )
                    FI.put()
                    id_for_FI = str(FI.key().id())
                    time.sleep(0.5)  # to delay so db table gets displayed correct

                    a_food_description_content = ""
                    a_note_content = ""
                    a_exp_content = ""
                    a_headline = "Add food to Freezer"
                    a_change_button = "Add Item"
                    a_passive_button = "Return to Frontpage"
                    a_item_id = ""
                    a_date_created = ""

                    f_d_err = ""
                    date_err = ""
                    add_msg = "Your Food Item was successfully added"

                    self.response.headers.add_header("Set-Cookie", "sort_code=; Path=/")

                    self.render_foodPage(
                        a_food_description_content,
                        f_d_err,
                        a_note_content,
                        date_err,
                        a_exp_content,
                        a_headline,
                        a_change_button,
                        a_passive_button,
                        a_item_id,
                        a_date_created,
                        add_msg,
                        the_RU.name,
                    )

            # else re-render 'food.html' with the error messages
            else:
                # decide which params to pass based on 'add' or 'edit'
                if an_item_id:  # edit version
                    specific_item = FoodItem.get_by_id(
                        int(an_item_id)
                    )  # get the item with the specific id (an_item_id)

                    the_headline = "Edit food item"
                    the_change_button = "Submit Changes"
                    the_passive_button = "Cancel"
                    the_item_id = an_item_id

                    # create a string in format "dd-mm-yyyy" from the DateProperty yyyy-mm-dd
                    a_date_created = validation.convert_DateProperty_to_str_dash(specific_item.added_date)

                    f_d_err = obj_food.get_error_msg()
                    date_err = obj_exp_date.get_error_msg()
                    add_msg = ""

                else:  # add version
                    the_headline = "Add food to Freezer"
                    the_change_button = "Add Item"
                    the_passive_button = "Return to Frontpage"
                    the_item_id = (
                        ""
                    )  # ok with empty str. when checking if "" that is False.... But can't use None to put in here...
                    a_date_created = ""

                    f_d_err = obj_food.get_error_msg()
                    date_err = obj_exp_date.get_error_msg()
                    add_msg = ""

                self.render_foodPage(
                    a_food_description,
                    f_d_err,
                    a_note,
                    date_err,
                    an_exp_date_str,
                    the_headline,
                    the_change_button,
                    the_passive_button,
                    the_item_id,
                    a_date_created,
                    add_msg,
                    the_RU.name,
                )

        else:  # either user_id_cookie_value, username, or the_RU is None (see check_user_id_cookie())
            self.redirect("/logout")
Example #3
0
    def get(self):

        the_RU = check_user_id_cookie(self.request)

        if the_RU:
            an_id = self.request.get(
                "id")  # if any foodItem description is clicked, there is an_id

            if an_id:  # means there is an item to edit
                specific_item = FoodItem.get_by_id(
                    int(an_id))  # get the item with the specific id (an_id)

                # check if there is a DateProperty (expiry) yyyy-mm-dd. It is NOT a string
                if specific_item.expiry:
                    # create a string in format "dd-mm-yyyy" of the DateProperty yyyy-mm-dd
                    date_html_format = validation.convert_DateProperty_to_str_dash(
                        specific_item.expiry)

                else:  # no expiry date for this item
                    date_html_format = ""

                # set values for specific item
                a_food_description_content = specific_item.description
                a_note_content = specific_item.note
                a_exp_content = date_html_format
                a_headline = "Edit food item"
                a_change_button = "Submit Changes"
                a_passive_button = "Cancel"
                a_item_id = an_id
                # create a string in format "dd-mm-yyyy" of the DateProperty yyyy-mm-dd
                a_date_created = validation.convert_DateProperty_to_str_dash(
                    specific_item.added_date)

                f_d_err = ""
                date_err = ""
                add_msg = ""

            else:  # no id, set values to a blank "food.html"
                a_food_description_content = ""
                a_note_content = ""
                a_exp_content = ""
                a_headline = "Add food to Freezer"
                a_change_button = "Add Item"
                a_passive_button = "Return to Frontpage"
                a_item_id = ""
                a_date_created = ""

                f_d_err = ""
                date_err = ""
                add_msg = ""

            logging.debug("description = " + a_food_description_content)

            # render "food.html" with correct params!
            self.render_foodPage(a_food_description_content, f_d_err,
                                 a_note_content, date_err, a_exp_content,
                                 a_headline, a_change_button, a_passive_button,
                                 a_item_id, a_date_created, add_msg,
                                 the_RU.name)
        else:  # either user_id_cookie_value, username, or the_RU is None (see check_user_id_cookie())
            self.redirect("/logout")
Example #4
0
    def get(self):

        the_RU = check_user_id_cookie(self.request)

        if the_RU:
            an_id = self.request.get("id")  # if any foodItem description is clicked, there is an_id

            if an_id:  # means there is an item to edit
                specific_item = FoodItem.get_by_id(int(an_id))  # get the item with the specific id (an_id)

                # check if there is a DateProperty (expiry) yyyy-mm-dd. It is NOT a string
                if specific_item.expiry:
                    # create a string in format "dd-mm-yyyy" of the DateProperty yyyy-mm-dd
                    date_html_format = validation.convert_DateProperty_to_str_dash(specific_item.expiry)

                else:  # no expiry date for this item
                    date_html_format = ""

                # set values for specific item
                a_food_description_content = specific_item.description
                a_note_content = specific_item.note
                a_exp_content = date_html_format
                a_headline = "Edit food item"
                a_change_button = "Submit Changes"
                a_passive_button = "Cancel"
                a_item_id = an_id
                # create a string in format "dd-mm-yyyy" of the DateProperty yyyy-mm-dd
                a_date_created = validation.convert_DateProperty_to_str_dash(specific_item.added_date)

                f_d_err = ""
                date_err = ""
                add_msg = ""

            else:  # no id, set values to a blank "food.html"
                a_food_description_content = ""
                a_note_content = ""
                a_exp_content = ""
                a_headline = "Add food to Freezer"
                a_change_button = "Add Item"
                a_passive_button = "Return to Frontpage"
                a_item_id = ""
                a_date_created = ""

                f_d_err = ""
                date_err = ""
                add_msg = ""

            logging.debug("description = " + a_food_description_content)

            # render "food.html" with correct params!
            self.render_foodPage(
                a_food_description_content,
                f_d_err,
                a_note_content,
                date_err,
                a_exp_content,
                a_headline,
                a_change_button,
                a_passive_button,
                a_item_id,
                a_date_created,
                add_msg,
                the_RU.name,
            )
        else:  # either user_id_cookie_value, username, or the_RU is None (see check_user_id_cookie())
            self.redirect("/logout")