Ejemplo n.º 1
0
Archivo: views.py Proyecto: ox-it/OPMS
def itu_item(request, item_id):
    """Display an absolute record of an item."""
    message = ''
    error = ''
    tags = Tag.objects.all()
    item = ItuItem.objects.get(id=int(item_id))
    chartrecords = ItuItemChartScan.objects.filter(ituitem=item)

    metrics_to_plot = []
    traffic_to_plot = []
    categories_to_plot = []
    if chartrecords:
        #Get or create a suitable Metric
        metrics = Metric.objects.filter(description=item.latest.name)
        if not metrics:
            random_colour = '#' + str(random.randint(222222, 999999))
            top_items_position = Metric(description=item.latest.name, linecolor=random_colour, fillcolor='#FFFFFF',
                mouseover=True, defaultvisibility=True, source='itu-item-chart', ituitem=item)
            top_items_position.save()
            metrics_to_plot.append(top_items_position)
        else:
            metrics_to_plot.append(metrics[0])

        #Add the first chartrecord of the day to traffic_to_plot
        dates = []
        for chartrecord in chartrecords:
            if chartrecord.date.date() not in dates:
                dates.append(chartrecord.date.date())
        for date in dates:
            chartrecords_day = []
            for chartrecord in chartrecords:
                if chartrecord.date.date() == date:
                    chartrecords_day.append(chartrecord)
            traffic_to_plot.append(
                Traffic(date=date, count=(-1 * chartrecords_day[0].position), metric=metrics_to_plot[0]))

    return render_to_response('monitors/itu_item.html',
            {'error': error, 'message': message, 'item': item, 'chartrecords': chartrecords,
             'comments_to_plot': [],
             'metrics_to_plot': metrics_to_plot,
             'metric_textfiles': create_metric_textfiles(traffic_to_plot, metrics_to_plot),
             'categories_to_plot': categories_to_plot,
             'events': [],
             'chart': True, 'tags': tags}, context_instance=RequestContext(request))
Ejemplo n.º 2
0
Archivo: views.py Proyecto: ox-it/OPMS
def itu_institution(request, institution_id):
    """Display an institution."""
    message = ''
    error = ''
    tags = Tag.objects.all()
    institution = ItuInstitution.objects.get(id=int(institution_id))
    try:
        latest_tc_scanlog = ItuScanLog.objects.filter(mode=2).order_by('-time')[0]
        latest_ti_scanlog = ItuScanLog.objects.filter(mode=3).order_by('-time')[0]
    except:
        error += 'Couldn\'t find the latest top collections/items scanlogs. Perhaps you need to run scan_itunes first?'
        return render_to_response('monitors/itu_home.html',
                {'error': error, 'message': message, 'institution': institution}, context_instance=RequestContext(request))
    comments = ItuComment.objects.filter(ituinstitution=institution).order_by('-date')
    collections = ItuCollection.objects.filter(institution=institution).order_by('-latest__last_modified')
    current_collection_chartscans = ItuCollectionChartScan.objects.filter(itucollection__institution=institution,scanlog=latest_tc_scanlog).order_by('position')
    current_item_chartscans = ItuItemChartScan.objects.filter(ituitem__institution=institution,scanlog=latest_ti_scanlog)
    if current_collection_chartscans.count() > 5:
        top_five_tc_scanlogs = current_collection_chartscans[0:5]
    else:
        top_five_tc_scanlogs = current_collection_chartscans
    if collections.filter(latest__last_modified__isnull=False).count() > 5:
        recently_updated_collections = collections.filter(latest__last_modified__isnull=False)[0:5]
    else:
        recently_updated_collections = collections.filter(latest__last_modified__isnull=False)
    try:
        audio_items = ItuItem.objects.filter(Q(institution=institution) & Q(latest__missing=None) & (
        Q(latest__file_extension='mp3') | Q(latest__file_extension='m4a') | Q(latest__file_extension='aac') | Q(
            latest__file_extension='aif') | Q(latest__file_extension='aiff') | Q(latest__file_extension='aifc') | Q(
            latest__file_extension='wav')))
        audio_duration = timedelta(
            microseconds=int(audio_items.aggregate(Sum('latest__duration'))['latest__duration__sum']) * 1000)
        audio_number = audio_items.count()
    except:
        audio_duration = timedelta(microseconds=0)
        audio_number = 0
        message += 'WARNING: Couldn\'t calculate the total audio duration/number properly. '
    try:
        video_items = ItuItem.objects.filter(Q(institution=institution) & Q(latest__missing=None) & (
        Q(latest__file_extension='mp4') | Q(latest__file_extension='m4v') | Q(latest__file_extension='mov')))
        video_duration = timedelta(
            microseconds=int(video_items.aggregate(Sum('latest__duration'))['latest__duration__sum']) * 1000)
        video_number = video_items.count()
    except:
        video_duration = timedelta(microseconds=0)
        video_number = 0
        message += 'WARNING: Couldn\'t calculate the total video duration/number properly. '
    try:
        total_duration = timedelta(microseconds=int(ItuItem.objects.filter(Q(institution=institution) & Q(latest__missing=None)).aggregate(Sum('latest__duration'))['latest__duration__sum']) * 1000)
    except:
        total_duration = timedelta(microseconds=0)
        message += 'WARNING: Couldn\'t calculate total duration properly. '
    unknown_duration = total_duration - (audio_duration + video_duration)
    try:
        total_number = ItuItem.objects.filter(latest__missing=None, latest__institution=institution).count()
    except:
        total_number = 0
        message += 'WARNING: Couldn\'t calculate the total number of items properly. '
    other_number = total_number - (audio_number + video_number)
    collections_number = collections.count()
    collections_containing_movies_number = collections.filter(latest__contains_movies=True).count()
    collections_not_containing_movies_number = collections_number - collections_containing_movies_number

    metrics_to_plot = []
    traffic_to_plot = []
    categories_to_plot = []
    comments_to_plot = []

    try:
        top_collections_count = Metric.objects.get(description='# of collections in the top 200')
        metrics_to_plot.append(top_collections_count)
    except:
        random_colour = '#' + str(random.randint(222222, 999999))
        top_collections_count = Metric(description='# of collections in the top 200', linecolor=random_colour,
            fillcolor='#FFFFFF', mouseover=True, defaultvisibility=True, source='itu-#tc', ituinstitution=institution)
        top_collections_count.save()
        metrics_to_plot.append(top_collections_count)
    try:
        top_items_count = Metric.objects.get(description='# of items in the top 200')
        metrics_to_plot.append(top_items_count)
    except:
        random_colour = '#' + str(random.randint(222222, 999999))
        top_items_count = Metric(description='# of items in the top 200', linecolor=random_colour,
            fillcolor='#FFFFFF', mouseover=True, defaultvisibility=True, source='itu-#ti', ituinstitution=institution)
        top_items_count.save()
        metrics_to_plot.append(top_items_count)

    dates_processed = []
    for tc_scan in ItuScanLog.objects.filter(mode=2).order_by('time'):
        date = tc_scan.time.date()
        if date not in dates_processed:
            dates_processed.append(date)
            tc_count = ItuCollectionChartScan.objects.filter(scanlog=tc_scan,
                itucollection__institution=institution).count()
            traffic_to_plot.append(Traffic(date=date, count=tc_count, metric=top_collections_count))
    dates_processed = []
    for ti_scan in ItuScanLog.objects.filter(mode=3).order_by('time'):
        date = ti_scan.time.date()
        if date not in dates_processed:
            dates_processed.append(date)
            ti_count = ItuItemChartScan.objects.filter(scanlog=ti_scan, ituitem__institution=institution).count()
            traffic_to_plot.append(Traffic(date=date, count=ti_count, metric=top_items_count))

    if comments:
        from_itunes_u = Category.objects.get(description='From iTunes U')
        from_itunes_u.defaultvisibility = False
        categories_to_plot.append(from_itunes_u)
        for comment in comments:
            comment_to_plot = Comment(date=comment.date,
                source=(comment.itucollectionhistorical.name + ' - comment by ' + comment.source), detail=comment.detail
                , user_email='*****@*****.**', category=from_itunes_u)
            comments_to_plot.append(comment_to_plot)

    return render_to_response('monitors/itu_institution.html',
            {'error': error, 'message': message, 'institution': institution, 'comments': comments,
             'total_duration': total_duration, 'audio_duration': audio_duration, 'video_duration': video_duration, 'unknown_duration': unknown_duration,
             'audio_number': audio_number, 'video_number': video_number, 'total_number': total_number, 'other_number': other_number,
             'collections_number': collections_number,
             'collections_containing_movies_number': collections_containing_movies_number,
             'collections_not_containing_movies_number': collections_not_containing_movies_number,
             'collections': collections, 'current_collection_chartscans': current_collection_chartscans, 'current_item_chartscans': current_item_chartscans, 'top_five_tc_scanlogs': top_five_tc_scanlogs, 'recently_updated_collections': recently_updated_collections,
             'comments_to_plot': comments_to_plot,
             'metrics_to_plot': metrics_to_plot,
             'metric_textfiles': create_metric_textfiles(traffic_to_plot, metrics_to_plot),
             'categories_to_plot': categories_to_plot,
             'events': [],
             'chart': False,
             'tags': tags}, context_instance=RequestContext(request))
Ejemplo n.º 3
0
Archivo: views.py Proyecto: ox-it/OPMS
def itu_collection(request, collection_id):
    """Display an absolute record of a collection."""
    message = ''
    error = ''
    tags = Tag.objects.all()
    collection = ItuCollection.objects.get(id=int(collection_id))
    chartrecords = ItuCollectionChartScan.objects.filter(itucollection=collection).order_by('date')
    items = ItuItem.objects.filter(latest__series__itucollection=collection, latest__missing=None)
    try:
        total_duration = timedelta(
            microseconds=int(items.aggregate(Sum('latest__duration'))['latest__duration__sum']) * 1000)
    except:
        total_duration = timedelta(microseconds=0)
        message += 'WARNING: Couldn\'t calculate the total duration properly.'
    comments = ItuComment.objects.filter(itucollectionhistorical__itucollection=collection)
    ratings = ItuRating.objects.filter(itucollectionhistorical=collection.latest)
    average_rating = collection.latest.average_rating()
    metrics_to_plot = []
    traffic_to_plot = []
    categories_to_plot = []
    comments_to_plot = []
    if chartrecords:
        #Get or create a suitable Metric
        try:
            top_collections_position = Metric.objects.get(description=collection.latest.name)
            metrics_to_plot.append(top_collections_position)
        except:
            random_colour = '#' + str(random.randint(222222, 999999))
            top_collections_position = Metric(description=collection.latest.name, linecolor=random_colour,
                fillcolor='#FFFFFF', mouseover=True, defaultvisibility=True, source='itu-collection-chart', itucollection=collection)
            top_collections_position.save()
            metrics_to_plot.append(top_collections_position)

        #Add the first chartrecord of the day to traffic_to_plot
        dates = []
        for chartrecord in chartrecords:
            if chartrecord.date.date() not in dates:
                dates.append(chartrecord.date.date())
        for date in dates:
            chartrecords_day = []
            for chartrecord in chartrecords:
                if chartrecord.date.date() == date:
                    chartrecords_day.append(chartrecord)
            traffic_to_plot.append(
                Traffic(date=date, count=(-1 * chartrecords_day[0].position), metric=top_collections_position))

        if comments:
            from_itunes_u = Category.objects.get(description='From iTunes U')
            from_itunes_u.defaultvisibility = False
            categories_to_plot.append(from_itunes_u)
            for comment in comments:
                comment_to_plot = Comment(date=comment.date,
                    source=(comment.itucollectionhistorical.name + ' - comment by ' + comment.source),
                    detail=comment.detail, user_email='*****@*****.**', category=from_itunes_u)
                comments_to_plot.append(comment_to_plot)

    return render_to_response('monitors/itu_collection.html',
            {'error': error, 'message': message, 'collection': collection, 'chartrecords': chartrecords,
             'comments': comments, 'items': items, 'total_duration': total_duration, 'ratings': ratings,
             'average_rating': average_rating,
             'comments_to_plot': comments_to_plot,
             'metrics_to_plot': metrics_to_plot,
             'metric_textfiles': create_metric_textfiles(traffic_to_plot, metrics_to_plot),
             'categories_to_plot': categories_to_plot,
             'events': [],
             'chart': True,
             'tags': tags}, context_instance=RequestContext(request), )