def locations(request, template_name='people/locations.html'): locations = Location.query().order(-Location.total).fetch(1000) return render_to_response(template_name, {'locations': locations}, context_instance=RequestContext(request))
def get_user_details(self, response): """Return user details from Github account""" data = { USERNAME: response.get('login'), 'email': response.get('email') or '', 'fullname': response['name'], 'last_name': '', 'url': response.get('blog', ''), 'description': response.get('bio', ''), 'picture_url': response.get('avatar_url', '') } try: data['first_name'], data['last_name'] = response['name'].split(' ', 1) except: data['first_name'] = response.get('name') or 'Annon' try: location = response.get('location', '') if location: data['location'], _ = Location.create(location) except: logging.exception('Problem finding location') return data
def get_user_details(self, response): """Return user details from Github account""" data = { 'username': response.get('login'), 'email': response.get('email') or '', 'fullname': response.get('name', 'Secret Agent'), 'last_name': '', 'url': response.get('blog', ''), 'description': response.get('bio', ''), 'picture_url': response.get('avatar_url', '') } try: names = data['fullname'].split(' ') data['first_name'], data['last_name'] = names[0], names[-1] except: data['first_name'] = data['fullname'] try: location = response.get('location', '') if location: data['location'], _ = Location.create(location) except: logging.exception('Problem finding location') return data
def test_post_adds_points_to_location(self): self.policy.SetProbability(1) user = self.make_user('chris', location='Austin TX') user.add_auth_id('email:[email protected]') self.app.post('/api/v1/github', self.POST) location = Location.get_by_id('austin-tx') # TODO: figure out how to test this! if location is not None: self.assertEqual(location.total, 2)
def test_post_adds_points_to_location(self): self.policy.SetProbability(1) user = self.make_user('marcus', location='Austin TX') user.add_auth_id('email:[email protected]') self.app.post('/api/v1/bitbucket', self.POST) location = Location.get_by_id('austin-tx') # TODO: figure out how to test this! if location is not None: self.assertEqual(location.total, 1)
def fix_location(slug, cursor=None, total=0): # Don't try to lookup slugs that are the empty string. # hint they don't exist! if not slug: return location_slug = slug location = Location.get_or_insert(location_slug) projects = set([]) if cursor: # we are looping Grab the existing project list so we don't # wipe out the earlier runs work location_p = getattr(location, 'projects', []) projects = set(location_p) cursor = Cursor(urlsafe=cursor) people = User.query().filter(User.location_slug == location_slug) # Go through the users in chucks models, next_cursor, more = people.fetch_page(100, start_cursor=cursor) for model in models: user_projects = getattr(model, 'projects', []) user_total = getattr(model, 'total', 0) # Do a little math to figure out how many commits they have commits = user_total - (len(user_projects) * 10) if commits > 0: logging.info('Adding %s to %s', commits, location_slug) total += commits # Add the users projects to the project set (this filters duplicates) projects.update(user_projects) # Run update in a transaction projects = list(projects) total = total + (len(projects) * 10) @ndb.transactional def txn(): location = Location.get_or_insert(location_slug) location.total = total location.projects = projects location.put() txn() if more: # We have more people to loop through!! return deferred.defer(fix_location, location_slug, cursor=next_cursor.urlsafe(), total=total)
def users_by_location(request, location_slug, template_name='people/people_list.html'): users = User.query(User.location_slug == location_slug) users.order(-ndb.GenericProperty('total')).fetch(1000) location = Location.get_by_id(location_slug) return render_to_response(template_name, {'users':users, 'location': location, 'slug': location_slug}, context_instance=RequestContext(request))
def test_post_adds_points_to_location(self): self.policy.SetProbability(1) user = self.make_user('chris', location='Austin TX') user.add_auth_id('email:[email protected]') self.app.post('/api/v1/github', self.POST) tasks = self.taskqueue_stub.GetTasks('default') self.assertEqual(5, len(tasks)) # Run the task task = tasks[0] deferred.run(base64.b64decode(task['body'])) location = Location.get_by_id('austin-tx') self.assertEqual(location.total, 12)
def fix_locations(cursor=None): """Look up all the locations and re-count totals.""" if cursor: cursor = Cursor(urlsafe=cursor) query = Location.query() models, next_cursor, more = query.fetch_page(15, start_cursor=cursor) for location in models: deferred.defer(fix_location, location.key.id()) if more: deferred.defer(fix_locations, cursor=next_cursor.urlsafe())
def test_post_adds_points_to_location(self): self.policy.SetProbability(1) user = self.make_user('marcus', location='Austin TX') user.add_auth_id('email:[email protected]') self.app.post('/api/v1/bitbucket', self.POST) tasks = self.taskqueue_stub.GetTasks('default') self.assertEqual(3, len(tasks)) # Run the task task = tasks[0] deferred.run(base64.b64decode(task['body'])) location = Location.get_by_id('austin-tx') self.assertEqual(location.total, 11)
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() if not user_count: logging.info("Empty location: %s", location) if commit: location.delete() logging.info('Deleted') empty += 1 continue l = check_location(location.name) if l == location.name: logging.info('Location fine: %s', location) fine += 1 continue if not commit: if l: fixable += 1 else: bad.append((location, user_count)) continue elif l is not None: new_loc = Location.create(l) User.objects.filter(location=location).update(location=new_loc) user_count = User.objects.filter(location=location).count() if not user_count: logging.error("missed users!") else: location.delete() elif l is None: logging.info('Bad location: %s', location) location.approved = False location.save() if not commit: [logging.error('Bad Loc: %s, count: %s', l, c) for l, c in bad] logging.info('Empty: %s, Fine: %s, fixable: %s', empty, fine, fixable) logging.info('Add --commit to fix locations')
def locations(request, template_name='people/locations.html'): limit = 100 cursor = request.GET.get('cursor') if cursor: cursor = Cursor(urlsafe=cursor) query = Location.query().order(-Location.total) models, next_cursor, more = query.fetch_page(limit, start_cursor=cursor) if next_cursor is not None: next_cursor = next_cursor.urlsafe() return render_to_response(template_name, {'locations': models, 'next': next_cursor, 'more': more}, context_instance=RequestContext(request))
def users_by_location(request, location_slug, template_name='people/people_list.html'): limit = 100 cursor = request.GET.get('cursor') if cursor: cursor = Cursor(urlsafe=cursor) query = User.query(User.location_slug == location_slug) query = query.order(-ndb.GenericProperty('total')) models, next_cursor, more = query.fetch_page(limit, start_cursor=cursor) location = Location.get_by_id(location_slug) return render_to_response(template_name, {'next':next_cursor, 'more':more, 'users':models, 'location': location, 'slug': location_slug}, context_instance=RequestContext(request))
def get_user_details(self, response): """Return user details from Twitter account""" data = { USERNAME: response['screen_name'], 'email': '', # not supplied 'fullname': response['name'], 'last_name': '', 'url': response.get('url', ''), 'description': response.get('description', ''), 'picture_url': response.get('profile_image_url', '') } try: data['first_name'], data['last_name'] = response['name'].split(' ', 1) except: data['first_name'] = response['name'] try: location = response.get('location', '') if location: data['location'], _ = Location.create(location) except: logging.exception('Problem finding location') return data
def txn(): location = Location.get_or_insert(location_slug) location.total = total location.projects = projects location.put()
def index(request): """Render the home page""" # For now we are just using hard coded sections #sections = cache.get('front_page') #if sections is None: # sections = Section.all().order('order').fetch(10) # cache.set('front_page', sections, 120) stats = [] total = 0 people = [] locations = [] projects = [] teams = [] messages = [] token = '' # this is only shown on authenticated page loads # to save on the overhead. if True: stats = Accumulator.get_histogram('global') total = sum(stats) location_future = Location.query().order(-Location.total).fetch_async(15) people_future = User.query().order(-ndb.GenericProperty('total')).fetch_async(10) project_future = Project.query().order(-Project.total).fetch_async(10) team_future = Team.query().order(-Team.total).fetch_async(15) message_future = Message.query().order(-Message.timestamp).fetch_async(30) # Julython live stuffs #token_key = 'live_token:%s' % request.user.username #token = memcache.get(token_key) #if token is None: #token = channel.create_channel(request.user.username) #memcache.set(token_key, token, time=7000) locations = location_future.get_result() people = people_future.get_result() projects = project_future.get_result() teams = team_future.get_result() message_models = message_future.get_result() m_list = [to_dict(m) for m in message_models] m_list.reverse() messages = json.dumps(m_list) ctx = Context({ 'sections': [], 'people': people, 'projects': projects, 'locations': locations, 'teams': teams, 'stats': json.dumps(stats), 'total': total, 'token': token, 'messages': messages, 'user': request.user, 'MEDIA_URL': settings.MEDIA_URL, 'STATIC_URL': settings.STATIC_URL}) return render_to_response('index.html', context_instance=ctx)
def index(request): """Render the home page""" # For now we are just using hard coded sections #sections = cache.get('front_page') #if sections is None: # sections = Section.all().order('order').fetch(10) # cache.set('front_page', sections, 120) stats = [] total = 0 people = [] locations = [] projects = [] teams = [] messages = [] token = '' # this is only shown on authenticated page loads # to save on the overhead. if True: stats = Accumulator.get_histogram('global') total = sum(stats) location_future = Location.query().order(-Location.total).fetch_async( 15) people_future = User.query().order( -ndb.GenericProperty('total')).fetch_async(10) project_future = Project.query().order(-Project.total).fetch_async(10) team_future = Team.query().order(-Team.total).fetch_async(15) message_future = Message.query().order(-Message.timestamp).fetch_async( 30) # Julython live stuffs #token_key = 'live_token:%s' % request.user.username #token = memcache.get(token_key) #if token is None: #token = channel.create_channel(request.user.username) #memcache.set(token_key, token, time=7000) locations = location_future.get_result() people = people_future.get_result() projects = project_future.get_result() teams = team_future.get_result() message_models = message_future.get_result() m_list = [to_dict(m) for m in message_models] m_list.reverse() messages = json.dumps(m_list) ctx = Context({ 'sections': [], 'people': people, 'projects': projects, 'locations': locations, 'teams': teams, 'stats': json.dumps(stats), 'total': total, 'token': token, 'messages': messages, 'user': request.user, 'MEDIA_URL': settings.MEDIA_URL, 'STATIC_URL': settings.STATIC_URL }) return render_to_response('index.html', context_instance=ctx)