# Path: profiles/modules/codes/models.py
# class Code(models.Model):
#     """
#     Registration codes
#     """
#     code = models.CharField(max_length=255, unique=True)
#     is_active = models.BooleanField(default=True)
# 
#     class Meta:
#         app_label = 'profiles'
#         ordering = ('code',)
# 
#     def __unicode__(self):
#         return mark_safe(self.code)


import string
import random
from django import forms
from django.contrib import admin
from django.contrib.admin.options import csrf_protect_m
from django.core.urlresolvers import reverse
from django.core.exceptions import PermissionDenied
from django.http import HttpResponseRedirect
from django.utils.translation import ugettext_lazy as _
from .models import Code
        from django.conf.urls import patterns, include, url

    @csrf_protect_m
    def auto_generate(self, request):
        if request.method == 'POST':
            if not self.has_add_permission(request):
                raise PermissionDenied
            form = AutoGenerateForm(request.POST)
            if form.is_valid():
                number = form.cleaned_data.get('number')
                length = form.cleaned_data.get('length')
                for i in range(number):
                    code = CodeAdmin.generate_code(length)
                    while True:
                        code = CodeAdmin.generate_code(length)
                        if not Code.objects.filter(code=code).count():
                            break

                    Code.objects.create(code=code, is_active=True)

            else:
                return self.changelist_view(request, extra_context={'auto_generate_form': form})

        return HttpResponseRedirect(reverse('admin:profiles_code_changelist'))

    @staticmethod
    def generate_code(length):
        """Generate a random string of ascii_uppercase case chars and digits."""
        return ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(length))



# Path: profiles/modules/codes/models.py
# class Code(models.Model):
#     """
#     Registration codes
#     """
#     code = models.CharField(max_length=255, unique=True)
#     is_active = models.BooleanField(default=True)
# 
#     class Meta:
#         app_label = 'profiles'
#         ordering = ('code',)
# 
#     def __unicode__(self):
#         return mark_safe(self.code)


# Path: profiles/modules/codes/admin.py
import string
import random
from django import forms
from django.contrib import admin
from django.contrib.admin.options import csrf_protect_m
from django.core.urlresolvers import reverse
from django.core.exceptions import PermissionDenied
from django.http import HttpResponseRedirect
from django.utils.translation import ugettext_lazy as _
from .models import Code
        from django.conf.urls import patterns, include, url


    @csrf_protect_m
    def auto_generate(self, request):
        if request.method == 'POST':
            if not self.has_add_permission(request):
                raise PermissionDenied
            form = AutoGenerateForm(request.POST)
            if form.is_valid():
                number = form.cleaned_data.get('number')
                length = form.cleaned_data.get('length')
                for i in range(number):
                    code = CodeAdmin.generate_code(length)
                    while True:
                        code = CodeAdmin.generate_code(length)
                        if not Code.objects.filter(code=code).count():
                            break

                    Code.objects.create(code=code, is_active=True)

            else:
                return self.changelist_view(request, extra_context={'auto_generate_form': form})

        return HttpResponseRedirect(reverse('admin:profiles_code_changelist'))

    @staticmethod
    def generate_code(length):
        """Generate a random string of ascii_uppercase case chars and digits."""
        return ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(length))



admin.site.register(Code, CodeAdmin)