Python PlaylistReportForm Example

说明

python playlistreportform示例是从最受好评的开源项目中提取的实现代码,你可以参考下面示例的使用方式。

编程语言: Python

命名空间/包名称: playlistsforms

示例#1
文件: reports.py项目: BubbleTouchSoftware/chirpradio

def playlist_export_report_worker(results, request_params):
    form = PlaylistReportForm(data=request_params)
    if not form.is_valid():
        # TODO(Kumar) make this visible to the user
        raise ValueError('Invalid PlaylistReportForm')
    from_date = form.cleaned_data['from_date']
    to_date = form.cleaned_data['to_date']

    if results is None:
        # when starting the job, init file lines with the header row...
        results = {
            'items': {},  # items keyed by datetime established
            'last_offset': 0,
            'from_date': str(from_date),
            'to_date': str(to_date),
        }

    offset = results['last_offset']
    last_offset = offset+50
    results['last_offset'] = last_offset

    query = filter_playlist_events_by_date_range(from_date, to_date)
    all_entries = query[ offset: last_offset ]

    if len(all_entries) == 0:
        finished = True
    else:
        finished = False

    for entry in all_entries:
        established = _get_entity_attr(entry, 'established_display')
        report_key = as_encoded_str(str(established))
      
        if type(entry) == PlaylistBreak:
            results['items'][report_key] = {
                'established': as_encoded_str(established.strftime('%Y-%m-%d %H:%M:%S')),
                'is_break': True
            }
            continue
       
        playlist = _get_entity_attr(entry, 'playlist') 
        track = _get_entity_attr(entry, 'track')
        results['items'][report_key] = {
            'channel': as_encoded_str(_get_entity_attr(playlist, 'channel')),
            'date': as_encoded_str(established.strftime("%m/%d/%y")),
            'duration_ms': as_encoded_str(_get_entity_attr(track, 
                                                           'duration_ms', 0)),
            'established': as_encoded_str(established.strftime('%Y-%m-%d %H:%M:%S')),
            'artist_name': as_encoded_str(_get_entity_attr(entry,
                                                           'artist_name')),
            'track_title': as_encoded_str(_get_entity_attr(entry,
                                                            'track_title')),
            'album_title': as_encoded_str(_get_entity_attr(entry, 
                                                           'album_title_display')),
            'label': as_encoded_str(_get_entity_attr(entry, 'label_display')),
            'is_break': False
        }

    return finished, results

示例#2
文件: reports.py项目: BubbleTouchSoftware/chirpradio

def playlist_report_worker(results, request_params):
    form = PlaylistReportForm(data=request_params)
    if not form.is_valid():
        # TODO(Kumar) make this visible to the user
        raise ValueError('Invalid PlaylistReportForm')
    from_date = form.cleaned_data['from_date']
    to_date = form.cleaned_data['to_date']

    if results is None:
        # when starting the job, init file lines with the header row...
        results = {
            'items': {},  # items keyed by play key
            'last_offset': 0,
            'play_counts': {},  # play keys to number of plays
            'from_date': str(from_date),
            'to_date': str(to_date),
        }

    offset = results['last_offset']
    last_offset = offset+50
    results['last_offset'] = last_offset

    query = filter_tracks_by_date_range(from_date, to_date)
    all_entries = query[ offset: last_offset ]

    if len(all_entries) == 0:
        finished = True
    else:
        finished = False

    for entry in all_entries:
        play_key = play_count_key(entry)
        if play_key in results['play_counts']:
            results['play_counts'][play_key] += 1
            continue
        else:
            results['play_counts'][play_key] = 1
        results['items'][play_key] = {
            'album_title': as_encoded_str(_get_entity_attr(entry,
                                                           'album_title')),
            'artist_name': as_encoded_str(_get_entity_attr(entry,
                                                           'artist_name')),
            'label': as_encoded_str(_get_entity_attr(entry, 'label')),
            'heavy_rotation': str(int(bool(HEAVY_ROTATION_TAG in
                                           entry.categories))),
            'light_rotation': str(int(bool(LIGHT_ROTATION_TAG in
                                           entry.categories)))
        }

    return finished, results

示例#3
文件: reports.py项目: BubbleTouchSoftware/chirpradio

def report_playlist(request, template='playlists/reports.html'):
    vars = {}

    # report vars
    items = None
    fields = REPORT_FIELDS

    # default report
    if request.method == 'GET':
        to_date = datetime.now().date()
        from_date = to_date - timedelta(days=1)
        items = query_group_by_track_key(from_date, to_date)

        # default form
        form = PlaylistReportForm({'from_date':from_date, 'to_date':to_date})

    # check form data post
    elif request.method == 'POST':

        # generic search form
        form = PlaylistReportForm(data=request.POST)
        if form.is_valid():
            from_date = form.cleaned_data['from_date']
            to_date = form.cleaned_data['to_date']


            # special case to download report
            if request.POST.get('download') == 'Download':
                fname = "chirp-play-count_%s_%s" % (from_date, to_date)
                return http_send_csv_file(fname, fields, query_group_by_track_key(from_date, to_date))

            # generate report from date range
            if request.POST.get('search') == 'Search':
                items = query_group_by_track_key(from_date, to_date)

    # template vars
    vars['form'] = form

    return render_to_response(template, vars,
            context_instance=RequestContext(request))

展开阅读全文