コード例 #1
0
ファイル: league.py プロジェクト: sebbacon/trevor
    def handle(self, *args, **options):
        if not args or (args and args[0] not in ('load')):
            raise CommandError("USAGE: ./manage.py %s load" % \
                    os.path.basename(__file__).split('.')[0])

        transaction.enter_transaction_management()
        transaction.managed(True)
        this_year = utils.getThisYear()
        last_year = utils.getLastYear()
        table = scrape.parseTable(this_year)
        teams_table = []
        prev_current = models.getCurrentTable()
        for name in table:
            teams_table.append(getOrMakeTeam(name))
        current = models.LeagueTable(name=settings.CURRENT_LEAGUE_ID,
                                     year=this_year)
        current.save()
        for t in teams_table:            
            current.teams.add(t)
        current.save()
        if prev_current and list(current.ordered_teams) \
               == list(prev_current.ordered_teams):
            current.delete()
        else:
            print "table changed"
        if not models.getPreviousTable():
            previous = models.LeagueTable(name=settings.PREVIOUS_LEAGUE_ID,
                                          year=last_year)
            previous.save()
            for name in scrape.parseTable(last_year):
                previous.teams.add(getOrMakeTeam(name))
            previous.save()
            print previous
        transaction.commit()
コード例 #2
0
    def handle(self, *args, **options):
        if not args or (args and args[0] not in ('load')):
            raise CommandError("USAGE: ./manage.py %s load" % \
                    os.path.basename(__file__).split('.')[0])

        transaction.enter_transaction_management()
        transaction.managed(True)
        this_year = utils.getThisYear()
        last_year = utils.getLastYear()
        table = scrape.parseTable(this_year)
        teams_table = []
        prev_current = models.getCurrentTable()
        for name in table:
            teams_table.append(getOrMakeTeam(name))
        current = models.LeagueTable(name=settings.CURRENT_LEAGUE_ID,
                                     year=this_year)
        current.save()
        for t in teams_table:
            current.teams.add(t)
        current.save()
        if prev_current and list(current.ordered_teams) \
               == list(prev_current.ordered_teams):
            current.delete()
        else:
            print "table changed"
        if not models.getPreviousTable():
            previous = models.LeagueTable(name=settings.PREVIOUS_LEAGUE_ID,
                                          year=last_year)
            previous.save()
            for name in scrape.parseTable(last_year):
                previous.teams.add(getOrMakeTeam(name))
            previous.save()
            print previous
        transaction.commit()
コード例 #3
0
    def handle_noargs(self, **options):
        start = time.time()
        this_year = getThisYear()
        predictions = models.Prediction.objects
        now = datetime.datetime.now()
        yesterday = now - datetime.timedelta(1)
        # we are going to run calculation for all competitions that
        # have started, and the date of the competition is after
        # yesterday
        predictions = predictions.filter(
            competition__start_date__lt=now,
            competition__competition_date__gt=yesterday).all()
        count = 0
        done = False
        meta_competition = models.Competition.objects.get(
            pk=settings.CURRENT_META_COMPETITION_ID)
        for p in predictions:
            score = p.calculateScore()
            p.calculateGoalDiff()
            p.needs_update = False
            p.save()
            if p.included_in_meta_competition == False\
                   and p.competition.competition_date < now:
                running_score, _ = models.RunningScore.objects\
                                   .get_or_create(user=p.user,
                                                  competition=meta_competition)
                running_score.calculateScore(new_score=p.score)
                running_score.calculateGoalDiff(new_goaldiff=p.goaldiff)
                p.included_in_meta_competition = True
                p.save()
            count += 1
        date = datetime.date.today()
        current = models.getCurrentTable()
        for comp in models.Competition.objects\
                .filter(competition_date__gt=yesterday):
            if comp.prediction_set\
                   .filter(needs_ordering=True).count():
                position = 1
                for p in comp.prediction_set.all():
                    pos, created = models.Position.objects\
                                   .get_or_create(position=position,
                                                  date=date)
                    p.positions.add(pos)
                    if p.abs_change_on_last_position > 0:
                        position_changed.send(sender=None, prediction=p)
                    p.needs_ordering = False
                    p.save()
                    position += 1
                position = 1
                running_scores = comp.runningscore_set.all()
                for score in running_scores:
                    pos, created = models.Position.objects\
                                   .get_or_create(position=position,
                                                  date=date)
                    score.positions.add(pos)
                    if score.abs_change_on_last_position > 0:
                        position_changed.send(sender=None, prediction=score)
                    score.needs_ordering = False
                    score.save()
                    position += 1

        if count and False:
            print "Calculated %d scores in %f seconds" % (count,
                                                          time.time() - start)
            print "(%f per second)" % (count / (time.time() - start))
コード例 #4
0
ファイル: calculate.py プロジェクト: sebbacon/trevor
    def handle_noargs(self, **options):
        start = time.time()
        this_year = getThisYear()
        predictions = models.Prediction.objects
        now = datetime.datetime.now()
        yesterday = now - datetime.timedelta(1)
        # we are going to run calculation for all competitions that
        # have started, and the date of the competition is after
        # yesterday
        predictions = predictions.filter(
            competition__start_date__lt=now,
            competition__competition_date__gt=yesterday).all()
        count = 0
        done = False
        meta_competition = models.Competition.objects.get(
            pk=settings.CURRENT_META_COMPETITION_ID)
        for p in predictions:
            score = p.calculateScore()            
            p.calculateGoalDiff()
            p.needs_update = False
            p.save()
            if p.included_in_meta_competition == False\
                   and p.competition.competition_date < now:
                running_score, _ = models.RunningScore.objects\
                                   .get_or_create(user=p.user,
                                                  competition=meta_competition)
                running_score.calculateScore(
                    new_score=p.score)
                running_score.calculateGoalDiff(
                    new_goaldiff=p.goaldiff)
                p.included_in_meta_competition = True
                p.save()                
            count += 1
        date = datetime.date.today()
        current = models.getCurrentTable()
        for comp in models.Competition.objects\
                .filter(competition_date__gt=yesterday):
            if comp.prediction_set\
                   .filter(needs_ordering=True).count():
                position = 1
                for p in comp.prediction_set.all():
                    pos, created = models.Position.objects\
                                   .get_or_create(position=position,
                                                  date=date)
                    p.positions.add(pos)
                    if p.abs_change_on_last_position > 0:
                        position_changed.send(sender=None,prediction=p)
                    p.needs_ordering = False
                    p.save()
                    position += 1
                position = 1
                running_scores = comp.runningscore_set.all()
                for score in running_scores:
                    pos, created = models.Position.objects\
                                   .get_or_create(position=position,
                                                  date=date)
                    score.positions.add(pos)
                    if score.abs_change_on_last_position > 0:
                        position_changed.send(sender=None,
                                              prediction=score)
                    score.needs_ordering = False
                    score.save()
                    position += 1
            

        if count and False:
            print "Calculated %d scores in %f seconds" % (count, time.time() - start)
            print "(%f per second)" % (count/(time.time()-start))