# Path: july/models.py
# class User(AbstractUser):
#     location = models.ForeignKey(Location, blank=True, null=True,
#                                  related_name='location_members')
#     team = models.ForeignKey(Team, blank=True, null=True,
#                              related_name='team_members')
#     projects = models.ManyToManyField(Project, blank=True, null=True)
#     description = models.TextField(blank=True)
#     url = models.URLField(blank=True, null=True)
#     picture_url = models.URLField(blank=True, null=True)
# 
#     def __unicode__(self):
#         return self.get_full_name() or self.username
# 
#     def add_auth_id(self, auth_str):
#         """
#         Add a social auth identifier for this user.
# 
#         The `auth_str` should be in the format '{provider}:{uid}'
#         this is useful for adding multiple unique email addresses.
# 
#         Example::
# 
#             user = User.objects.get(username='foo')
#             user.add_auth_id('email:foo@example.com')
#         """
#         provider, uid = auth_str.split(':')
#         return UserSocialAuth.create_social_auth(self, uid, provider)
# 
#     def add_auth_email(self, email, request=None):
#         """
#         Adds a new, non verified email address, and sends a verification email.
#         """
#         auth_email = self.add_auth_id('email:%s' % email)
#         auth_email.extra_data = {'verified': False}
#         auth_email.save()
#         return auth_email
# 
#     def get_provider(self, provider):
#         """Return the uid of the provider or None if not set."""
#         try:
#             return self.social_auth.filter(provider=provider).get()
#         except UserSocialAuth.DoesNotExist:
#             return None
# 
#     def find_valid_email(self, token):
#         result = None
#         for email_auth in self.social_auth.filter(provider="email"):
#             email = email_auth.uid
#             expected = salted_hmac(settings.SECRET_KEY, email).hexdigest()
#             if expected == token:
#                 result = email_auth
#         return result
# 
#     @property
#     def gittip(self):
#         return self.get_provider('gittip')
# 
#     @property
#     def twitter(self):
#         return self.get_provider('twitter')
# 
#     @property
#     def github(self):
#         return self.get_provider('github')
# 
#     @classmethod
#     def get_by_auth_id(cls, auth_str):
#         """
#         Return the user identified by the auth id.
# 
#         Example::
# 
#             user = User.get_by_auth_id('twitter:julython')
#         """
#         provider, uid = auth_str.split(':')
#         sa = UserSocialAuth.get_social_auth(provider, uid)
#         if sa is None:
#             return None
#         return sa.user
# 
#     @property
#     def auth_ids(self):
#         auths = self.social_auth.all()
#         return [':'.join([a.provider, a.uid]) for a in auths]
# 
#     @property
#     def points(self):
#         try:
#             player = self.player_set.latest()
#         except:
#             return 0
#         return player.points
# 
#     @property
#     def total(self):
#         return self.points
# 
# Path: july/people/models.py
# class Location(Group):
#     """Simple model for holding point totals and projects for a location"""
#     template = 'registration/location.html'
#     rel_lookup = 'user__location'
#     lookup = 'location'
#     auto_verify = True
# 
#     def get_absolute_url(self):
#         from django.core.urlresolvers import reverse
#         return reverse('location-detail', kwargs={'slug': self.slug})
# 
# Path: july/utils.py
# def check_location(location):
#     resp = requests.get(
#         'http://maps.googleapis.com/maps/api/geocode/json',
#         params={'address': location, 'sensor': 'false'})
#     resp.raise_for_status()
# 
#     data = resp.json()
#     if data['status'] == 'ZERO_RESULTS':
#         return None
#     try:
#         return data['results'][0]['formatted_address']
#     except (KeyError, IndexError):
#         return None


import logging
from django.core.management.base import BaseCommand
from django.template.defaultfilters import slugify
from july.models import User
from july.people.models import Location
from july.utils import check_location
from optparse import make_option





class Command(BaseCommand):
    help = 'fix locations'
    option_list = BaseCommand.option_list + (
        make_option(
            '--commit',
            action='store_true',
            dest='commit',
            default=False,
            help='Actually move the items.'),
    )

    def handle(self, *args, **options):
        commit = options['commit']
        empty = 0
        fine = 0
        fixable = 0
        bad = []
        for location in Location.objects.all():

# Path: july/models.py
# class User(AbstractUser):
#     location = models.ForeignKey(Location, blank=True, null=True,
#                                  related_name='location_members')
#     team = models.ForeignKey(Team, blank=True, null=True,
#                              related_name='team_members')
#     projects = models.ManyToManyField(Project, blank=True, null=True)
#     description = models.TextField(blank=True)
#     url = models.URLField(blank=True, null=True)
#     picture_url = models.URLField(blank=True, null=True)
# 
#     def __unicode__(self):
#         return self.get_full_name() or self.username
# 
#     def add_auth_id(self, auth_str):
#         """
#         Add a social auth identifier for this user.
# 
#         The `auth_str` should be in the format '{provider}:{uid}'
#         this is useful for adding multiple unique email addresses.
# 
#         Example::
# 
#             user = User.objects.get(username='foo')
#             user.add_auth_id('email:foo@example.com')
#         """
#         provider, uid = auth_str.split(':')
#         return UserSocialAuth.create_social_auth(self, uid, provider)
# 
#     def add_auth_email(self, email, request=None):
#         """
#         Adds a new, non verified email address, and sends a verification email.
#         """
#         auth_email = self.add_auth_id('email:%s' % email)
#         auth_email.extra_data = {'verified': False}
#         auth_email.save()
#         return auth_email
# 
#     def get_provider(self, provider):
#         """Return the uid of the provider or None if not set."""
#         try:
#             return self.social_auth.filter(provider=provider).get()
#         except UserSocialAuth.DoesNotExist:
#             return None
# 
#     def find_valid_email(self, token):
#         result = None
#         for email_auth in self.social_auth.filter(provider="email"):
#             email = email_auth.uid
#             expected = salted_hmac(settings.SECRET_KEY, email).hexdigest()
#             if expected == token:
#                 result = email_auth
#         return result
# 
#     @property
#     def gittip(self):
#         return self.get_provider('gittip')
# 
#     @property
#     def twitter(self):
#         return self.get_provider('twitter')
# 
#     @property
#     def github(self):
#         return self.get_provider('github')
# 
#     @classmethod
#     def get_by_auth_id(cls, auth_str):
#         """
#         Return the user identified by the auth id.
# 
#         Example::
# 
#             user = User.get_by_auth_id('twitter:julython')
#         """
#         provider, uid = auth_str.split(':')
#         sa = UserSocialAuth.get_social_auth(provider, uid)
#         if sa is None:
#             return None
#         return sa.user
# 
#     @property
#     def auth_ids(self):
#         auths = self.social_auth.all()
#         return [':'.join([a.provider, a.uid]) for a in auths]
# 
#     @property
#     def points(self):
#         try:
#             player = self.player_set.latest()
#         except:
#             return 0
#         return player.points
# 
#     @property
#     def total(self):
#         return self.points
# 
# Path: july/people/models.py
# class Location(Group):
#     """Simple model for holding point totals and projects for a location"""
#     template = 'registration/location.html'
#     rel_lookup = 'user__location'
#     lookup = 'location'
#     auto_verify = True
# 
#     def get_absolute_url(self):
#         from django.core.urlresolvers import reverse
#         return reverse('location-detail', kwargs={'slug': self.slug})
# 
# Path: july/utils.py
# def check_location(location):
#     resp = requests.get(
#         'http://maps.googleapis.com/maps/api/geocode/json',
#         params={'address': location, 'sensor': 'false'})
#     resp.raise_for_status()
# 
#     data = resp.json()
#     if data['status'] == 'ZERO_RESULTS':
#         return None
#     try:
#         return data['results'][0]['formatted_address']
#     except (KeyError, IndexError):
#         return None


# Path: july/management/commands/fix_locations.py
import logging
from django.core.management.base import BaseCommand
from django.template.defaultfilters import slugify
from july.models import User
from july.people.models import Location
from july.utils import check_location
from optparse import make_option






class Command(BaseCommand):
    help = 'fix locations'
    option_list = BaseCommand.option_list + (
        make_option(
            '--commit',
            action='store_true',
            dest='commit',
            default=False,
            help='Actually move the items.'),
    )

    def handle(self, *args, **options):
        commit = options['commit']
        empty = 0
        fine = 0
        fixable = 0
        bad = []
        for location in Location.objects.all():

            user_count = User.objects.filter(location=location).count()