Exemplo n.º 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")
Exemplo n.º 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")
Exemplo n.º 3
0
    def render_front(
            self,
            a_username,
            parameter=""):  # 'youngest' created date shown first by default
        current_user_id = dataFunctions.retrieveUserId(a_username)  # an int
        all_food_items = db.GqlQuery(
            "SELECT * FROM FoodItem WHERE fk_registered_user_id=%s ORDER BY %s"
            % (current_user_id, parameter)).fetch(1000)

        counter = 0  # keep track of amount of iteration in for loop

        logging.debug("before loopie")
        # loop through all items and set is_expired
        for item in all_food_items:
            logging.debug("item._days_before_exp: in loop start" +
                          str(item._days_before_exp))
            counter = counter + 1
            if item.expiry:
                if date.today() >= item.expiry:
                    item._is_expired = True
                # check if expiry soon happens and update days_before_exp
                item._is_soon_to_expire, item._days_before_exp = validation.expires_soon(
                    item.expiry)
                item._exp_with_month_letters = validation.convert_to_letter_month(
                    item.expiry)
            else:  # no exp date
                item._is_expired = False
                item._is_soon_to_expire = False
                item._days_before_exp = None

            logging.debug("item._days_before_exp: in loop end" +
                          str(item._days_before_exp))
            item._days_in_freezer = validation.days_in_freezer(item.added_date)

        if counter == 0:  # checks if there is any items in database
            all_food_items = None

        logging.debug("all_food_items = " + str(type(all_food_items)))

        # toggle function

        # toggle variables
        descrip_a_d = "ASC"
        days_left_a_d = "ASC"
        #exp_a_d="ASC"
        days_frozen_a_d = "DESC"

        # decide which sorted code (1-7) you pass into html and also update variables.
        code = validation.get_number_code(
            parameter)  #return an int (1-7) based on which parameter passed in

        # check if any toggle variables must be updated
        if code == 2:  # parameter=="lower_case_description ASC"
            descrip_a_d = "DESC"
        elif code == 5 or code == 1:  # parameter=="created DESC"
            days_frozen_a_d = "ASC"
        elif code == 6:  # parameter=="expiry ASC"
            days_left_a_d = "DESC"

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

        self.render("frontpage.html",
                    username=a_username,
                    food_items=all_food_items,
                    descr_asc_desc=descrip_a_d,
                    days_left_asc_desc=days_left_a_d,
                    days_frozen_asc_desc=days_frozen_a_d,
                    look_number=code)  # passing contents into the html file
Exemplo n.º 4
0
    def render_front(self, a_username, parameter=""):  # 'youngest' created date shown first by default
        current_user_id = dataFunctions.retrieveUserId(a_username)  # an int
        all_food_items = db.GqlQuery(
            "SELECT * FROM FoodItem WHERE fk_registered_user_id=%s ORDER BY %s" % (current_user_id, parameter)
        ).fetch(1000)

        counter = 0  # keep track of amount of iteration in for loop

        logging.debug("before loopie")
        # loop through all items and set is_expired
        for item in all_food_items:
            logging.debug("item._days_before_exp: in loop start" + str(item._days_before_exp))
            counter = counter + 1
            if item.expiry:
                if date.today() >= item.expiry:
                    item._is_expired = True
                # check if expiry soon happens and update days_before_exp
                item._is_soon_to_expire, item._days_before_exp = validation.expires_soon(item.expiry)
                item._exp_with_month_letters = validation.convert_to_letter_month(item.expiry)
            else:  # no exp date
                item._is_expired = False
                item._is_soon_to_expire = False
                item._days_before_exp = None

            logging.debug("item._days_before_exp: in loop end" + str(item._days_before_exp))
            item._days_in_freezer = validation.days_in_freezer(item.added_date)

        if counter == 0:  # checks if there is any items in database
            all_food_items = None

        logging.debug("all_food_items = " + str(type(all_food_items)))

        # toggle function

        # toggle variables
        descrip_a_d = "ASC"
        days_left_a_d = "ASC"
        # exp_a_d="ASC"
        days_frozen_a_d = "DESC"

        # decide which sorted code (1-7) you pass into html and also update variables.
        code = validation.get_number_code(parameter)  # return an int (1-7) based on which parameter passed in

        # check if any toggle variables must be updated
        if code == 2:  # parameter=="lower_case_description ASC"
            descrip_a_d = "DESC"
        elif code == 5 or code == 1:  # parameter=="created DESC"
            days_frozen_a_d = "ASC"
        elif code == 6:  # parameter=="expiry ASC"
            days_left_a_d = "DESC"

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

        self.render(
            "frontpage.html",
            username=a_username,
            food_items=all_food_items,
            descr_asc_desc=descrip_a_d,
            days_left_asc_desc=days_left_a_d,
            days_frozen_asc_desc=days_frozen_a_d,
            look_number=code,
        )  # passing contents into the html file