def shows(request): page_number = request.GET.get('page', 1) today = datetime.now().date() initial = { 'show_range': 'on', 'show_date': today } data = None if request.GET.get('search'): data = request.GET show_form = AdminShowFilterForm(data = data, initial = initial) if data and not show_form.is_valid(): raise Exception('Invalid admin filters') if data: filters = show_form.cleaned_data else: filters = initial shows = Show.objects() if filters.get('query'): f = SearchFilter(filters.get('query'), 'title', 'venue__name', 'parse_meta__parser_id') shows = f.apply(shows) if filters.get('show_date'): range_map = { 'on': None, 'before': 'lt', 'after': 'gt' } range_method = range_map[filters.get('show_range')] f = DateFilter(filters.get('show_date'), 'date', range_method) shows = f.apply(shows) if filters.get('is_hidden'): shows = shows.filter(visible = False) if filters.get('no_image'): shows = shows.filter(image_url = None) if filters.get('restrict') == 'handling_failed': shows = transform_exec_js(Show, shows, show_has_resource_state(RESOURCE_HANDLING_FAILED)) elif filters.get('restrict') == 'not_handled': shows = transform_exec_js(Show, shows, show_has_resource_state(RESOURCE_NOT_HANDLED)) elif filters.get('restrict') == 'no_headliner': shows = transform_exec_js(Show, shows, show_lacks_headliner) if filters.get('resource') not in ('', None): shows = transform_exec_js(Show, shows, show_has_resource(filters.get('resource'))) shows = shows.order_by('date', '-rank') paginator = Paginator(shows, 100) page = paginator.page(page_number) shows = list(page.object_list) today = datetime.today().replace(hour = 0, minute = 0, second = 0) three_days_ago = today - timedelta(days = 3) for show in shows: artist_map = show.related_artists() for a in show.artists: a.full = artist_map.get(a.artist_id) context = { 'show_form': show_form, 'shows': shows, 'page': page, 'paginator': paginator } return render_to_response('fancy_admin/shows.html', context)
def artists(request): page_number = request.GET.get('page', 1) artist_form = AdminArtistFilterForm(data = request.GET) if not artist_form.is_valid(): raise Exception('Invalid admin filters') filters = artist_form.cleaned_data artists = Artist.objects() artists = artists.order_by('name') if filters.get('query'): f = SearchFilter(filters.get('query'), 'name') artists = f.apply(artists) if filters.get('suspect'): artists = transform_exec_js(Artist, artists, artist_is_suspect()) paginator = Paginator(artists, 10000) page = paginator.page(page_number) artists = [] today = datetime.now().replace(hour = 0, minute = 0, second = 0) yesterday = today - timedelta(days = 1) three_days_ago = today - timedelta(days = 3) thirty_days_ago = today - timedelta(days = 30) periods = ( ('last_30_days', thirty_days_ago, yesterday), # ('last_three_days', three_days_ago, yesterday), # ('yesterday', yesterday, today) ) for artist in list(page.object_list): stats = { } profiles = { } artists.append({'artist': artist, 'stats': stats, 'profiles': profiles}) last_sum = None for profile in artist.profiles: profiles[profile.system_id] = True for period, start, end in periods: media_info = [a.stats.stats_over(start, end).number_of_plays or 0 for a in artist.audio] media_info.sort(reverse = True) if len(media_info[0:3]) > 0: stats[period] = sum(media_info[0:3])/len(media_info[0:3]) else: stats[period] = '' if last_sum and info[period]: stats['%s_perc' % period] = int((1.0 * stats[period] - last_sum) / last_sum * 100) last_sum = stats[period] context = { 'artist_form': artist_form, 'artists': artists, 'page': page, 'paginator': paginator } return render_to_response('fancy_admin/artists.html', context)