def notify_position_changes(request): predictions = Prediction.objects.filter(year=getThisYear(), user__facebookuser__isnull=False) fb = request.facebook news_added = [] streams_updated = [] for prediction in predictions: change = prediction.change_on_last_position if change == 0: continue message = "just moved %s %d places in WhatEverTrevor's league!" direction = change > 0 and "up" or "down" amount = abs(change) uid = prediction.user.facebookuser_set.get().uid message = message % (direction, amount) attachment = {'name':"WhatEverTrevor's league", 'href':APP_URL, 'caption':'{*actor*} ' + message, 'description':('WhatEverTrevor is a free game ' 'about football, with a £1million jackpot prize')} news = [{'message':"@:%s %s" % (uid, message)}] news_added.append(fb.dashboard.addNews(news, uid=uid)) streams_updated.append(fb.stream.publish(attachment=attachment, uid=uid)) return locals()
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()
def notify_position_changes(request): predictions = Prediction.objects.filter(year=getThisYear(), user__facebookuser__isnull=False) fb = request.facebook news_added = [] streams_updated = [] for prediction in predictions: change = prediction.change_on_last_position if change == 0: continue message = "just moved %s %d places in WhatEverTrevor's league!" direction = change > 0 and "up" or "down" amount = abs(change) uid = prediction.user.facebookuser_set.get().uid message = message % (direction, amount) attachment = { 'name': "WhatEverTrevor's league", 'href': APP_URL, 'caption': '{*actor*} ' + message, 'description': ('WhatEverTrevor is a free game ' 'about football, with a £1million jackpot prize') } news = [{'message': "@:%s %s" % (uid, message)}] news_added.append(fb.dashboard.addNews(news, uid=uid)) streams_updated.append( fb.stream.publish(attachment=attachment, uid=uid)) return locals()
def facebooktab(request): uid = request.POST['fb_sig_profile_user'] user = FacebookUser.objects.get(uid=uid) show_condensed_table = True current_page = request.GET.get('current_page', 1) full_table_url = "%s?%s" % (APP_URL, "full=1") predictions = Prediction.objects.filter(year=getThisYear()) predictions = predictions.order_by('-score', '-goaldiff', 'created_date') predictions = predictions.all() can_show_full_table = False app_url = APP_URL return locals()
def home2(request): if request.method == 'POST': waiting = WaitingListForm(request.POST) if waiting.is_valid(): item = waiting.save() item.save() message = "Thanks. We'll let you know when the new game starts!" else: message = "That's not a valid email address" blog_date, blog_title, blog_content = _parseFeed() this_year = getThisYear() return locals()
def home(request): if request.method == 'POST': waiting = WaitingListForm(request.POST) if waiting.is_valid(): item = waiting.save() item.save() message = "Thanks. We'll let you know when the new game starts!" else: message = "That's not a valid email address" blog_date, blog_title, blog_content = _parseFeed() this_year = getThisYear() predictions = Prediction.objects.filter(year=this_year) predictions = predictions.order_by('-score', '-goaldiff', 'created_date') predictions = predictions.all()[:10] return locals()
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]) predictions = Prediction.objects.filter( year=getThisYear(), user__facebookuser__isnull=False) fb = request.facebook for prediction in predictions: change = prediction.change_on_last_position if change == 0: continue message = "@:%s just moved %s %d places in WhatEverTrevor's league" direction = change > 0 and "up" or "down" amount = abs(change) uid = prediction.user.facebookuser_set.get().uid message = message % (uid, direction, amount) news = [{'message': message}] print fb.dashboard.addNews(news, uid=uid) print fb.stream.publish(message, uid=uid)
def canvas(request): fb = request.facebook uid = fb.uid button = ('<fb:req-choice url="%s" label="Authorize My ' 'Application" />') % APP_URL has_authorized = _check_authorized(request, uid) if has_authorized: user = _get_or_create_user(request, uid) show_condensed_table = True current_page = request.GET.get('current_page', 1) full_table_url = "%s?%s" % (APP_URL, "full=1") predictions = Prediction.objects.filter(year=getThisYear()) predictions = predictions.order_by('-score', '-goaldiff', 'created_date') predictions = predictions.all() can_show_full_table = True is_canvas = True app_url = APP_URL if request.GET.get('full', ''): show_full_table = True show_condensed_table = False return locals()
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]) predictions = Prediction.objects.filter(year=getThisYear(), user__facebookuser__isnull=False) fb = request.facebook for prediction in predictions: change = prediction.change_on_last_position if change == 0: continue message = "@:%s just moved %s %d places in WhatEverTrevor's league" direction = change > 0 and "up" or "down" amount = abs(change) uid = prediction.user.facebookuser_set.get().uid message = message % (uid, direction, amount) news = [{'message':message}] print fb.dashboard.addNews(news, uid=uid) print fb.stream.publish(message, uid=uid)
def handle(self, *args, **options): if not args: raise CommandError("USAGE: ./manage.py %s <csvfile>" % \ os.path.basename(__file__).split('.')[0]) this_year = utils.getThisYear() csvfile = open(args[0]) dialect = csv.Sniffer().sniff(csvfile.read(1024)) csvfile.seek(0) users = csv.DictReader(csvfile, dialect=dialect) seen = [] for user in users: email = user["userEmail_str"] if email in seen: continue order = user["userOrder_str"] order = order.split(",") positions = [Team.objects.get(name=INITIAL_TABLE[int(o)-1])\ for o in order] user = CustomUser.objects.create(username=email, email=email, postcode="", can_email=False, password="******", first_name="", last_name="", is_active=True) user.save() prediction_obj = Prediction(name=email, year=this_year, user=user) prediction_obj.save() for p in positions: prediction_obj.teams.add(p) prediction_obj.save() seen.append(email) print prediction_obj
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))
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))