import re import random import string def generate_encryption_key(length = 32): """Generate a new encryption key of `length` chars. This is done by using the :func:`get_random_string` function with a default of 32 chars. Args: length (int, optional): The length in chars of the encryption key. Defaults to 32. Returns: str: A string of `length` chars. """ return get_random_string(length) def get_ip_address(request): """Get the IP address of the requesting viewer. This is done by looking into the following variables in the headers. 1. HTTP_X_FORWARDED_FOR 2. REMOTE_ADDR Args: request (BaseRequest): The Django request. Returns: str: IP address of the request """ x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR', None) if x_forwarded_for: ip = x_forwarded_for.split(',')[0] else: ip = request.META.get('REMOTE_ADDR') ### Real IP address of client Machine return ip def get_random_int_value(length = 6): """Generate a random number of `length` length numbers. Args: length (int, optional): The length of the random number in amount of numbers. Defaults to 6. Returns: int: Returns a random number of 'length' numbers. """ return int(''.join(list(map(lambda x: str(random.randint(1,9)), list(range(length)))))) def get_random_string(length = 8): """Generate a random string of `length` length characters. Args: length (int, optional): The length of the random string in amount of characters. Defaults to 8. Returns: str: Returns a random string of 'length' characters. """ return ''.join(random.choices(string.ascii_uppercase + string.digits + string.ascii_lowercase, k=length)) def remove_html_tags(text): """Remove HTML tags and code from the input text Args: text (str): Input text to be cleaned from HTML Returns: str: Cleaned HTML. """ clean = re.compile('<.*?>') return re.sub(clean, '', text)