3

\                 @   s<   d dl mZ d dlmZ d dlmZ dddZdd
dZdS )    )zip_longest)
connection)get_random_string ABCDEFGHJKLMNPQRSTUVWXYZ23456789   -c             C   s4   dd t | |dD }|jdd t|g|  D S )z/Create a string of 16 chars grouped by 4 chars.c             s   s   | ]
}|V  qd S )N ).0ir   r   3/var/www/html/oscar2019/oscar/apps/voucher/utils.py	<genexpr>
   s    z generate_code.<locals>.<genexpr>)lengthallowed_charsc             s   s   | ]}d j td|V  qdS ) N)joinfilter)r	   ar   r   r   r      s   )r   r   r   )r   charsgroup_length	separatorZrandom_stringr   r   r   generate_code   s    r      c             C   s:   t j }x,t| ||d}|jd|g |j s
|S q
W dS )a  Generate a code, check in the db if it already exists and return it.

    i.e. ASDA-QWEE-DFDF-KFGG

    :param int length: the number of characters in the code
    :param int group_length: length of character groups separated by dash '-'
    :return: voucher code
    :rtype: str

    )r   r   z+SELECT 1 FROM voucher_voucher WHERE code=%sN)r   cursorr   executefetchall)r   r   r   r   coder   r   r   get_unused_code   s    
r   N)r   r   r   )r   r   r   )	itertoolsr   	django.dbr   django.utils.cryptor   r   r   r   r   r   r   <module>   s
    
	