# Path: rolodex/slug.py
# def unique_slugify(instance, value, slug_field_name='slug', queryset=None,
#                    slug_separator='-'):
#     """
#     Calculates and stores a unique slug of ``value`` for an instance.
# 
#     ``slug_field_name`` should be a string matching the name of the field to
#     store the slug in (and the field to check against for uniqueness).
# 
#     ``queryset`` usually doesn't need to be explicitly provided - it'll default
#     to using the ``.all()`` queryset from the model's default manager.
#     """
#     slug_field = instance._meta.get_field(slug_field_name)
# 
#     slug = getattr(instance, slug_field.attname)
#     slug_len = slug_field.max_length
# 
#     # Sort out the initial slug, limiting its length if necessary.
#     slug = slugify(value)
#     if slug_len:
#         slug = slug[:slug_len]
#     slug = _slug_strip(slug, slug_separator)
#     original_slug = slug
# 
#     # Create the queryset if one wasn't explicitly provided and exclude the
#     # current instance from the queryset.
#     if queryset is None:
#         queryset = instance.__class__._default_manager.all()
#     if instance.pk:
#         queryset = queryset.exclude(pk=instance.pk)
# 
#     # Find a unique slug. If one matches, add '-2' to the end and try again
#     # (then '-3', etc).
#     next = 2
#     while not slug or queryset.filter(**{slug_field_name: slug}):
#         slug = original_slug
#         end = '%s%s' % (slug_separator, next)
#         if slug_len and len(slug) + len(end) > slug_len:
#             slug = slug[:slug_len - len(end)]
#             slug = _slug_strip(slug, slug_separator)
#         slug = '%s%s' % (slug, end)
#         next += 1
# 
#     setattr(instance, slug_field.attname, slug)


from django.db import models
from itertools import chain
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext as _
from django.core.validators import URLValidator, validate_email
from rolodex.slug import unique_slugify
from taggit.managers import TaggableManager
import networkx as nx

class GetOrNoneManager(models.Manager):
    def get_or_none(self, **kwargs):
        try:
            return self.get(**kwargs)
        except self.model.DoesNotExist:
            return None


#################
# Choice Models #
#################

'''
Choice models are registered in the admin and are designed to be set by user prior to entering entities into Rolodex.
'''


class OpenRecordsLaw(models.Model):
	'''
	Which open records law applies to the org.
	'''
	slug = models.SlugField(unique=True, editable=False)
	name = models.CharField(max_length=250)
	link = models.URLField(blank=True, null=True)

	def __unicode__(self):
		return self.name

	def save(self, *args, **kwargs):

# Path: rolodex/slug.py
# def unique_slugify(instance, value, slug_field_name='slug', queryset=None,
#                    slug_separator='-'):
#     """
#     Calculates and stores a unique slug of ``value`` for an instance.
# 
#     ``slug_field_name`` should be a string matching the name of the field to
#     store the slug in (and the field to check against for uniqueness).
# 
#     ``queryset`` usually doesn't need to be explicitly provided - it'll default
#     to using the ``.all()`` queryset from the model's default manager.
#     """
#     slug_field = instance._meta.get_field(slug_field_name)
# 
#     slug = getattr(instance, slug_field.attname)
#     slug_len = slug_field.max_length
# 
#     # Sort out the initial slug, limiting its length if necessary.
#     slug = slugify(value)
#     if slug_len:
#         slug = slug[:slug_len]
#     slug = _slug_strip(slug, slug_separator)
#     original_slug = slug
# 
#     # Create the queryset if one wasn't explicitly provided and exclude the
#     # current instance from the queryset.
#     if queryset is None:
#         queryset = instance.__class__._default_manager.all()
#     if instance.pk:
#         queryset = queryset.exclude(pk=instance.pk)
# 
#     # Find a unique slug. If one matches, add '-2' to the end and try again
#     # (then '-3', etc).
#     next = 2
#     while not slug or queryset.filter(**{slug_field_name: slug}):
#         slug = original_slug
#         end = '%s%s' % (slug_separator, next)
#         if slug_len and len(slug) + len(end) > slug_len:
#             slug = slug[:slug_len - len(end)]
#             slug = _slug_strip(slug, slug_separator)
#         slug = '%s%s' % (slug, end)
#         next += 1
# 
#     setattr(instance, slug_field.attname, slug)


# Path: rolodex/models.py
from django.db import models
from itertools import chain
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext as _
from django.core.validators import URLValidator, validate_email
from rolodex.slug import unique_slugify
from taggit.managers import TaggableManager
import networkx as nx


class GetOrNoneManager(models.Manager):
    def get_or_none(self, **kwargs):
        try:
            return self.get(**kwargs)
        except self.model.DoesNotExist:
            return None


#################
# Choice Models #
#################

'''
Choice models are registered in the admin and are designed to be set by user prior to entering entities into Rolodex.
'''


class OpenRecordsLaw(models.Model):
	'''
	Which open records law applies to the org.
	'''
	slug = models.SlugField(unique=True, editable=False)
	name = models.CharField(max_length=250)
	link = models.URLField(blank=True, null=True)

	def __unicode__(self):
		return self.name

	def save(self, *args, **kwargs):

		unique_slugify(self, self.name)