class LogicLoginByCode(object):
    def __init__(self):
        self.user = User()
        self.cellphone = Cellphone()
        self.imei = IMEI()

    def authenticate(self, country_code, username, password, cellphone_dict,
                     base_url, code):
        country_code_object = self.user.get_country_code(country_code)
        country_code_id = country_code_object.id if country_code_object else 0

        OHHOLog.print_log("check user")
        user_check_result = self.user.check_user_only_by_user(
            username, code, country_code_id)
        OHHOLog.print_log(user_check_result)
        user = self.user.get_by_cellphone(username)
        cellphone_key = cellphone_dict.get("key", None)
        self.cellphone.set_key(cellphone_key)
        cellphone = self.cellphone.get()

        OHHOLog.print_log("check user and cellphone relation")
        if user and cellphone:
            is_bind = self.cellphone.is_bound_by_user(cellphone.id, user.id)
            if is_bind:
                cellphone_relation_result = Result.result_success()
            else:
                OHHOLog.print_log("unsafe cellphone!")
                cellphone_relation_result = Result.result_failed()
        else:
            OHHOLog.print_log("user or cellphone not exist!")
            cellphone_relation_result = Result.result_failed()

            if not cellphone:
                OHHOLog.print_log("add cellphone!")
                success = self.cellphone.add_cellphone(cellphone_dict)
                if success:
                    add_cellphone_result = Result.result_success()
                else:
                    add_cellphone_result = Result.result_failed()
                OHHOLog.print_log(add_cellphone_result)
        OHHOLog.print_log(cellphone_relation_result)

        if Result.is_success(user_check_result) and Result.is_success(
                cellphone_relation_result):
            result = Result.result_success()
        elif Result.is_password_incorrect(
                user_check_result) or Result.is_update_beyond_three_month(
                    user_check_result):
            result = user_check_result
        elif Result.is_success(user_check_result):
            result = Result.result_unsafe()
        elif Result.is_not_exist(user_check_result):
            result = Result.result_not_exist()
        else:
            result = Result.result_failed()
        if user and Result.is_success(result):
            result["data"] = self.user.get_user_information(user.id, base_url)

        return result
    def post(self):
        the_post = Post()
        user_instance = User()
        relation_instance = UserAndMatchCondition()

        username = the_post.get_username(self)
        name = the_post.get_name(self)
        match_condition_id = the_post.get_match_condition_id(self)
        user_and_match_condition_id = the_post.get_id(self)

        relation = relation_instance.get_by_id(user_and_match_condition_id)
        match_condition = relation_instance.get_match_condition(
            match_condition_id)

        submit = the_post.get_submit(self)
        delete = the_post.get_delete(self)
        detail_url = USER_AND_MATCH_CONDITION_DETAIL_URL + "?id=" + user_and_match_condition_id

        user = user_instance.get_by_cellphone(username)
        OHHOLog.print_log(submit)
        OHHOLog.print_log(relation)
        OHHOLog.print_log(user)
        OHHOLog.print_log(match_condition)

        if submit and relation and user and match_condition:
            OHHOLog.print_log("start logic")
            data = dict()
            data["user_id"] = user.id
            data["name"] = name
            data["match_condition_id"] = match_condition_id
            success = relation_instance.update(relation, data)
            if success:
                return self.redirect(USER_AND_MATCH_CONDITION_LIST_URL)
        if delete and relation:
            success = relation_instance.delete(relation)
            if success:
                return self.redirect(USER_AND_MATCH_CONDITION_LIST_URL)
        return self.redirect(detail_url)