Example #1
0
    def handle(self, *args, **options):
        all_courses_objs = Course.objects.only('id').all()
        all_courses_ids = [c.id for c in all_courses_objs]

        if options['all_courses']:
            courses = all_courses_objs
        elif not options['courses']:
            # FIXME this crashes, there are problems with the dates
            courses = Course.objects.only('id').filter(status='p').filter(
                # exclude courses that haven't started
                Q(start_date__is_null=True) |
                Q(start_date__is_null=False, start_date__lte=datetime.now)
            )
            if not options['finished_courses']:
                # exclude finished courses
                courses = courses.filter(
                    Q(end_date__is_null=True) |
                    Q(end_date__is_null=False, end_date__gte=datetime.now)
                )
        else:
            user_courses = options['courses'].split(',')
            courses = []
            for id_or_slug in user_courses:
                try:
                    if id_or_slug.isdigit():
                        c = Course.objects.only('id').get(id=id_or_slug)
                    else:
                        c = Course.objects.only('id').get(slug=id_or_slug)
                    courses.append(c)
                except Course.DoesNotExist:
                    print '"%s" does not exist' % id_or_slug
                    pass

        courses = [c.id for c in courses]
        blacklist = [cid for cid in all_courses_ids if cid not in courses]

        print 'Calculating stats for these courses (ids): %s' % ', '.join([str(cid) for cid in courses])

        # Drop existing stats for selected courses
        db = get_db()
        stats_course = db.get_collection('stats_course')
        stats_unit = db.get_collection('stats_unit')
        stats_kq = db.get_collection('stats_kq')
        for cid in courses:
            stats_course.remove({'course_id': cid}, safe=True)
            stats_unit.remove({'course_id': cid}, safe=True)
            stats_kq.remove({'course_id': cid}, safe=True)

        # Callback to show some progress information
        def callback(step='', counter=0, total=0):
            step = max(1, int(total / 100))
            if counter % step == 0:
                if step == 'calculating':
                    print 'Processed %d of %d users' % (counter, total)
                elif step == 'storing':
                    print 'Saved %d of %d statistics entries' % (counter, total)

        calculate_all_stats(callback=callback, course_blacklist=blacklist)
Example #2
0
    def handle(self, *args, **options):
        all_courses_objs = Course.objects.only('id').all()
        all_courses_ids = [c.id for c in all_courses_objs]

        if options['all_courses']:
            courses = all_courses_objs
        elif not options['courses']:
            # FIXME this crashes, there are problems with the dates
            courses = Course.objects.only('id').filter(status='p').filter(
                # exclude courses that haven't started
                Q(start_date__is_null=True)
                | Q(start_date__is_null=False, start_date__lte=datetime.now))
            if not options['finished_courses']:
                # exclude finished courses
                courses = courses.filter(
                    Q(end_date__is_null=True)
                    | Q(end_date__is_null=False, end_date__gte=datetime.now))
        else:
            user_courses = options['courses'].split(',')
            courses = []
            for id_or_slug in user_courses:
                try:
                    if id_or_slug.isdigit():
                        c = Course.objects.only('id').get(id=id_or_slug)
                    else:
                        c = Course.objects.only('id').get(slug=id_or_slug)
                    courses.append(c)
                except Course.DoesNotExist:
                    print '"%s" does not exist' % id_or_slug
                    pass

        courses = [c.id for c in courses]
        blacklist = [cid for cid in all_courses_ids if cid not in courses]

        print 'Calculating stats for these courses (ids): %s' % ', '.join(
            [str(cid) for cid in courses])

        # Drop existing stats for selected courses
        db = get_db()
        stats_course = db.get_collection('stats_course')
        stats_unit = db.get_collection('stats_unit')
        stats_kq = db.get_collection('stats_kq')
        for cid in courses:
            stats_course.remove({'course_id': cid}, safe=True)
            stats_unit.remove({'course_id': cid}, safe=True)
            stats_kq.remove({'course_id': cid}, safe=True)

        # Callback to show some progress information
        def callback(step='', counter=0, total=0):
            step = max(1, int(total / 100))
            if counter % step == 0:
                if step == 'calculating':
                    print 'Processed %d of %d users' % (counter, total)
                elif step == 'storing':
                    print 'Saved %d of %d statistics entries' % (counter,
                                                                 total)

        calculate_all_stats(callback=callback, course_blacklist=blacklist)
    def forwards(self, orm):
        def callback(step="", counter=0, total=0):
            step = max(1, int(total / 100))
            if counter % step == 0:
                if step == "calculating":
                    print "Processed %d of %d users" % (counter, total)
                elif step == "storing":
                    print "Saved %d of %d statistics entries" % (counter, total)

        calculate_all_stats(
            user_objects=orm["auth.user"].objects, kq_objects=orm["courses.knowledgequantum"].objects, callback=callback
        )
Example #4
0
    def forwards(self, orm):
        def callback(step='', counter=0, total=0):
            step = max(1, int(total / 100))
            if counter % step == 0:
                if step == 'calculating':
                    print 'Processed %d of %d users' % (counter, total)
                elif step == 'storing':
                    print 'Saved %d of %d statistics entries' % (counter, total)

        calculate_all_stats(
            user_objects=orm['auth.user'].objects,
            kq_objects=orm['courses.knowledgequantum'].objects,
            callback=callback)