def cronlogger_data(request): context = {} values = ( 'job', 'created', 'stdout', 'stderr', 'exc_type', 'exc_value', 'exc_traceback', 'duration', ) qs = CronLog.objects.all() jobs = [] for each in qs.values('job').annotate(Count('job')): jobs.append({ 'text': '%s (%d)' % (each['job'], each['job__count']), 'value': each['job'] }) jobs.sort(key=lambda x: x['value']) context['jobs'] = jobs if request.GET.get('job'): qs = qs.filter(job__exact=request.GET['job']) context['count'] = qs.count() logs = [] for log_dict in qs.order_by('-created').values(*values)[:100]: log = dot_dict(log_dict) log['created'] = log['created'].isoformat() logs.append(log) context['logs'] = logs return context
def _get_all_pictures(event=None): values = ( 'id', 'title', 'placeholder_img', 'picture_id' ) event_map = collections.defaultdict(list) cant_delete = collections.defaultdict(bool) for each in Event.objects.filter(picture__isnull=False).values(*values): event_map[each['picture_id']].append({ 'id': each['id'], 'title': each['title'] }) if not each['placeholder_img']: # then you can definitely not delete this picture cant_delete[each['picture_id']] = True pictures = [] values = ( 'id', 'size', 'width', 'height', 'notes', 'created', 'modified', 'modified_user', 'event_id' ) qs = Picture.objects.all() if event: qs = qs.filter( Q(event__isnull=True) | Q(event=event) ) else: qs = qs.filter(event__isnull=True) for picture_dict in qs.order_by('event', '-created').values(*values): picture = dot_dict(picture_dict) item = { 'id': picture.id, 'width': picture.width, 'height': picture.height, 'size': picture.size, 'created': picture.created.isoformat(), 'events': event_map[picture.id], 'event': picture.event_id } if cant_delete.get(picture.id): item['cant_delete'] = True if picture.notes: item['notes'] = picture.notes # if picture.id in event_map: # item['events'] = event_map[picture.id] pictures.append(item) return pictures
def test_basic_use(self): data = { 'type': 'Info', 'meta': { 'name': 'Peter' } } dotted = utils.dot_dict(data) eq_(dotted.type, 'Info') eq_(dotted.meta.name, 'Peter')
def _get_all_users(): groups = {} for group in Group.objects.all().values('id', 'name'): groups[group['id']] = group['name'] groups_map = collections.defaultdict(list) for x in User.groups.through.objects.all().values('user_id', 'group_id'): groups_map[x['user_id']].append(groups[x['group_id']]) users = [] qs = User.objects.all() values = ( 'email', 'id', 'last_login', 'is_staff', 'is_active', 'is_superuser' ) # make a big fat list of the user IDs of people who are contributors contributor_ids = ( UserProfile.objects .filter(contributor=True) .values_list('user_id', flat=True) ) for user_dict in qs.values(*values): user = dot_dict(user_dict) item = { 'id': user.id, 'email': user.email, } if user.last_login: item['last_login'] = user.last_login.isoformat() # The reason we only add these if they're true is because we want # to minimize the amount of JSON we return. It works because in # javascript, doing `if (thing.something)` works equally if it # exists and is false or if it does not exist. if user.is_staff: item['is_staff'] = True if user.is_superuser: item['is_superuser'] = True if user.id in contributor_ids: item['is_contributor'] = True if not user.is_active: item['is_inactive'] = True if groups_map[user.id]: item['groups'] = groups_map[user.id] users.append(item) return users
def _get_all_users(): groups = {} for group in Group.objects.all().values('id', 'name'): groups[group['id']] = group['name'] groups_map = collections.defaultdict(list) for x in User.groups.through.objects.all().values('user_id', 'group_id'): groups_map[x['user_id']].append(groups[x['group_id']]) users = [] qs = User.objects.all() values = ( 'email', 'id', 'last_login', 'is_staff', 'is_active', 'is_superuser' ) # make a big fat list of the user IDs of people who are contributors contributor_ids = ( UserProfile.objects .filter(contributor=True) .values_list('user_id', flat=True) ) for user_dict in qs.values(*values): user = dot_dict(user_dict) item = { 'id': user.id, 'email': user.email, 'last_login': user.last_login.isoformat(), } # The reason we only add these if they're true is because we want # to minimize the amount of JSON we return. It works because in # javascript, doing `if (thing.something)` works equally if it # exists and is false or if it does not exist. if user.is_staff: item['is_staff'] = True if user.is_superuser: item['is_superuser'] = True if user.id in contributor_ids: item['is_contributor'] = True if not user.is_active: item['is_inactive'] = True if groups_map[user.id]: item['groups'] = groups_map[user.id] users.append(item) return users
def _get_all_pictures(event=None): values = ('id', 'title', 'placeholder_img', 'picture_id') event_map = collections.defaultdict(list) cant_delete = collections.defaultdict(bool) for each in Event.objects.filter(picture__isnull=False).values(*values): event_map[each['picture_id']].append({ 'id': each['id'], 'title': each['title'] }) if not each['placeholder_img']: # then you can definitely not delete this picture cant_delete[each['picture_id']] = True pictures = [] values = ('id', 'size', 'width', 'height', 'notes', 'created', 'modified', 'modified_user', 'event_id') qs = Picture.objects.all() if event: qs = qs.filter(Q(event__isnull=True) | Q(event=event)) else: qs = qs.filter(event__isnull=True) for picture_dict in qs.order_by('event', '-created').values(*values): picture = dot_dict(picture_dict) item = { 'id': picture.id, 'width': picture.width, 'height': picture.height, 'size': picture.size, 'created': picture.created.isoformat(), 'events': event_map[picture.id], 'event': picture.event_id } if cant_delete.get(picture.id): item['cant_delete'] = True if picture.notes: item['notes'] = picture.notes # if picture.id in event_map: # item['events'] = event_map[picture.id] pictures.append(item) return pictures