From 2ffaee2d5b9722a50ed33e5d6a338df77af5232c Mon Sep 17 00:00:00 2001 From: PCoder Date: Sun, 29 Oct 2017 13:14:29 +0100 Subject: [PATCH] Add clear_items_from_list utility function --- utils/hosting_utils.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/utils/hosting_utils.py b/utils/hosting_utils.py index 3c193ad7..9541588d 100644 --- a/utils/hosting_utils.py +++ b/utils/hosting_utils.py @@ -60,3 +60,23 @@ def get_vm_price(cpu, memory, disk_size): :return: The price of the VM """ return (cpu * 5) + (memory * 2) + (disk_size * 0.6) + + +class HostingUtils: + @staticmethod + def clear_items_from_list(from_list, items_list): + """ + A utility function to clear items from a given list. + Useful when deleting items in bulk from session. + e.g.: + HostingUtils.clear_items_from_list( + request.session, + ['token', 'billing_address_data', 'card_id',] + ) + :param from_list: + :param items_list: + :return: + """ + for var in items_list: + if var in from_list: + del from_list[var]