예제 #1
0
    def handle(self, filepath='', **options):
        # Some basic checking
        if filepath == '':
            raise CommandError("Please specify a file to import.")
        try:
            file = open(filepath)
        except:
            raise CommandError("Could not open file.")
        try:
            file_text = file.read()
        except:
            raise CommandError("Could not read file.")
        file.close()
        lines = deque(file_text.split(
            '\n/')) #New rows begin with this; we can't just use either \n or / because they both appear in comments...
        comments = []
        lines.popleft() #Remove header row
        for line in lines:
            comment = {}
            comma_separated_chunks = line.split(',')
            unix_timestamp = int(comma_separated_chunks.pop()) #Throw away a long number
            try:
                comment['date'] = datetime.datetime.fromtimestamp(unix_timestamp).date()
                comment['time'] = datetime.datetime.fromtimestamp(unix_timestamp).time()
            except:
                raise CommandError('Failed to convert timestamp ' + str(unix_timestamp) + ' to a date and a time.')
            comma_separated_chunks.pop() #Throw away 'Let us know'
            comma_separated_chunks = deque(comma_separated_chunks)
            comment['feed'] = comma_separated_chunks.popleft()
            throw_away = ''
            while throw_away != 'feedback':
                try:
                    throw_away = comma_separated_chunks.popleft() #Throw away everything else up to and including 'feedback'
                except KeyError:
                    raise CommandError("One line lacks the string 'feedback'.")
            comment['detail'] = ','.join(comma_separated_chunks)
            if comment['detail']:
                if not Comment.objects.filter(detail=comment['detail'],
                    source=comment['feed']): #Have we seen this comment before? If so, don't add it to comments.
                    comments.append(comment)
            else:
                print('Ignoring blank comment about feed ' + comment['feed'] + '.')

        for comment in comments:
            try:
                category = Category.objects.get(description='podcasts.ox.ac.uk')
            except:
                raise CommandError("Could not find podcasts.ox.ac.uk category.")
            try:
                comment_to_save = Comment(date=comment['date'], time=comment['time'], source=comment['feed'], detail=comment['detail'],
                    user_email='*****@*****.**', category=category)
                comment_to_save.save()
            except:
                raise CommandError("Could not save comment \"" + str(comment) + '\".')
예제 #2
0
def comment(request, slug):
    article = get_object_or_404(Article, slug=slug)
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = Comment(author=request.user,
                              text=request.POST.get("text", ""),
                              article_article=article)
            if request.user.is_staff:
                comment.status = "APP"
            comment.save()
    return redirect(article, slug)
예제 #3
0
파일: views.py 프로젝트: ox-it/OPMS
def comment_add(request,comment=None, error='', message=''):
    """Adds a new comment to the database. Optionally, it may replace the comment instead."""
    categories = Category.objects.all()
    error_fields=[]
    default_comment = Comment(date=datetime.date.today(), time=datetime.datetime.now().time, source='', detail='', category=Category.objects.filter(description='Events')[0], user_email='')

    try:
        added = bool(request.POST['add'])
    except:
        added = False
    try:
        action = request.POST['action']
    except:
        action = 'add'

    if added == True:
        try:
            new_date = parse(request.POST['date'], dayfirst=True)
            new_time = parse(request.POST['time'])
        except:
            error += ' Datetime invalid or not specified.'

        try:
            new_detail = request.POST['detail']
            if new_detail == '':
                error += ' Comment text is blank.'
        except:
            error += ' No comment text provided.'

        try:
            new_source = request.POST['source']
            if new_source == '':
                error += ' Source is blank.'
        except:
            error += ' No comment source provided.'

        try:
            new_category = Category.objects.filter(pk=int(request.POST['category_id']))[0] #The [0] is OK since the fact that category_id is a primary key ensures that the array has only length 1.
        except:
            error += ' Category invalid or nonexistent.'

        try:
            new_user_email = request.POST['user_email']
            if new_user_email == '':
                error += ' You haven\'t provided your e-mail address.'
        except:
            error += ' No user e-mail address provided.'

        if error == '':
            try:
                new_comment = Comment(date=new_date, time=new_time, source=new_source, detail=new_detail, category=new_category, user_email=new_user_email)
                new_comment.full_clean()
                try:
                    new_comment.save()
                    message += 'Your comment was added to the database.'
                except:
                    error += 'Failed to access the database.'
            except ValidationError as ve:
                for k in ve.message_dict.keys():
                    error_fields.append(k)
                    for m in ve.message_dict[k]:
                        error += m + ' '
                default_comment = new_comment

    if action == 'saveandaddanother' or action == 'add' or error != '':
        return render_to_response('feedback/comment_add.html',
            {'categories': categories,
            'error': error,
            'error_fields': error_fields,
            'message': message,
            'added': added,
            'comment': default_comment},
            context_instance=RequestContext(request))
    elif action == 'save':
        return index(request, error=error, message=message)
    else:
        error += 'Invalid submit action requested.'
        return render_to_response('feedback/comment_add.html',
                {'categories': categories,
                 'error': error,
                 'error_fields': error_fields,
                 'added': added,
                 'message': message,
                 'comment': default_comment},
            context_instance=RequestContext(request))
예제 #4
0
파일: views.py 프로젝트: ox-it/OPMS
def index(request, error='', message='', tag=None, tag_id=None, comment_id=None, event_id=None, metric_id=None):
    """Display a report on either a given tag, or a default set of comments, events and metrics if a tag is not specified."""
    if tag:
        metrics_to_plot = Metric.objects.filter(tags=tag)
    else:
        metrics_to_plot = Metric.objects.filter(source='appleweekly')

    traffic_to_plot = []
    for metric in metrics_to_plot:
        metric_traffic = list(Traffic.objects.filter(metric=metric))
        if metric_traffic:
            traffic_to_plot.append(metric_traffic)

    chart = False

    for m in metrics_to_plot:
        if m.source == 'appleweekly':
            try:
                #Import Apple weekly summary metrics, but just for one-time use - don't save in db.
                append = traffic_to_plot.append #Avoid re-calling the .append function in the middle of all those loops.
                for w in AppleWeeklySummary.merged.all():
                    for field in AppleWeeklySummary._meta._fields():             #This grabs a list of field objects from the model specified as part of the stats app
                        if field.verbose_name == m.appleweeklyfield:             #Verbose name is specified as ("verbose_name") in stats/models/apple_summary.py
                            append(Traffic(date=w.week_beginning, count=w.__dict__[field.name], metric=m))
            except:
                debug.onscreen('WARNING: Can\'t find any Apple summary data. Have you imported it?')
        elif m.source == 'itu-collection-chart':
            try:
                #Add the first chartrecord of the day to traffic_to_plot
                dates = []
                chartrecords = ItuCollectionChartScan.objects.filter(itucollection=m.itucollection).order_by('date')
                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=m))
                chart = True
            except:
                error += 'Failed to process traffic for an itu-collection-chart.'
        elif m.source == 'itu-item-chart':
            try:
                #Add the first chartrecord of the day to traffic_to_plot
                dates = []
                chartrecords = ItuItemChartScan.objects.filter(ituitem=m.ituitem).order_by('date')
                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=m))
                chart = True
            except:
                error += 'Failed to process traffic for an itu-item-chart.'
        elif m.source =='itu-#tc':
            try:
                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=m.ituinstitution).count()
                        traffic_to_plot.append(Traffic(date=date, count=tc_count, metric=m))
            except:
                error += 'Failed to process traffic for the # of collections in the top 200.'
        elif m.source =='itu-#ti':
            try:
                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=m.ituinstitution).count()
                        traffic_to_plot.append(Traffic(date=date, count=ti_count, metric=m))
            except:
                error += 'Failed to process traffic for the # of collections in the top 200.'

    #NOTE: We do not need to handle the temporal range of comments and events since this is done automatically by Timeplot.

    from_itunes_u = Category.objects.get(description='From iTunes U')
    #Create comments in the feedback database if they don't already exist.
    for itu_comment in ItuComment.objects.filter(ituinstitution__name = 'Oxford University'):
        comment = Comment(
            date=itu_comment.date,
            time=datetime.time(0,0,0),
            source=itu_comment.itucollectionhistorical.name + ' - comment by ' + itu_comment.source,
            detail=itu_comment.detail,
            user_email='*****@*****.**',
            moderated=True,
            category=from_itunes_u,
            itu_source=itu_comment
        )
        if Comment.objects.filter(detail=itu_comment.detail).count() > 0:
            pass
        else:
            comment.save()
    if tag:
        comments_to_plot = Comment.objects.filter(moderated=True,tags=tag)
        events_to_plot = Event.objects.filter(moderated=True,tags=tag)
    else:
        comments_to_plot = Comment.objects.filter(moderated=True)
        events_to_plot = Event.objects.filter(moderated=True)

    categories_to_plot = []
    for category in comments_to_plot.values_list('category').distinct():
        categories_to_plot.append(Category.objects.get(id=category[0]))
    for category in events_to_plot.values_list('category').distinct():
        if Category.objects.get(id=category[0]) not in categories_to_plot:
            categories_to_plot.append(Category.objects.get(id=category[0]))

    return render_to_response('feedback/index.html', {
        'metrics_to_plot': metrics_to_plot,
        'metric_textfiles': create_metric_textfiles(traffic_to_plot,metrics_to_plot),
        'categories_to_plot': categories_to_plot,
        'comments_to_plot': comments_to_plot,
        'events': events_to_plot,
        'chart': chart,
        'error': error,
        'message': message,
        'tag': tag, 'tag_id': tag_id, 'tags': Tag.objects.all(), 'comment_id': comment_id, 'event_id': event_id, 'metric_id': metric_id,
    }, context_instance=RequestContext(request))