Beispiel #1
0
 def post(self):
     sig, payload = self.request.get('signed_request').split('.',1)
     sr = decode_signed_req(payload)
     
     if 'oauth_token' not in sr:
         self.response.out.write(template.render('index.html',{
             'signed_request': self.request.get('signed_request'),
             'not_authorised': True,
             }))
         return
     
     logging.warning('oauth_token provided')
     
     graph = get_facebook_data('graph',sr['oauth_token'])
     friends = get_facebook_data('friends',sr['oauth_token'])
     
     player = Player.get_or_create('facebook',graph['id'],graph['name'])
     
     action = self.request.get('action')
     
     if action:
         try:
             target = Player.get_or_create(self.request.get('target_network'),
                                           self.request.get('target_id'),
                                           self.request.get('target_username'))
             act(player,target,action,self.request.get('narration'))
             player = Player.get_by_key_name('facebook|%s' % graph['id']) # TODO: figure out why I have this step and comment on it
         except Alert, a:
             logging.warning(a)           
Beispiel #2
0
 def get(self,network,id):
    
     target = Player.get_by_key_name(Player.make_key(network,id))
     refdate = self.request.get('refdate',None)
     self.response.out.write(template.render('status.html',{
         'status' : get_current_info(target,refdate),
         'person' : target,
         'action' : None,        
         }))
Beispiel #3
0
    def post(self, base_url, extra):
        """The user is saving their availability for the year, so save it to the datastore and
        invalidate the affected pages' HTML"""
        try:
            delete_years = set()
            if self.request.POST.has_key('updated'):
                player = Player.get_by_key_name(self.request.get('updated'))
                for year_round_key in self.request.POST.keys():
                    try:
                        split_key = year_round_key.split('-')
                        year = split_key[0]
                        delete_years.add(year)
                        round = split_key[1]
                        if len(round) == 0:
                            # Might have been a negative round, so an extra split!
                            round = '-' + split_key[2]
                        round_key = Round.get_key(round, year)
                        round = Round.get_by_key_name(round_key)
                        # Writes the current round's date each time, just in case it has changed. Would have preferred to have
                        # multiple records for the player, linked by date and not time, but django made it difficult (unless I
                        # wanted to have a Model that just contains a date, and everything links back to that...)
                        if round:
                            args = {
                                'key_name':
                                Availability.get_key(player.db_key, round.num,
                                                     round.year),
                                'year':
                                round.year,
                                'round':
                                round,
                                'player':
                                player,
                                'playing':
                                self.request.POST.get(year_round_key) == "0",
                                'given_date':
                                round.date,
                            }
                            Availability(**args).put()
                    except IndexError:
                        pass

                # Pages that have been affected by this update:
                for y in delete_years:
                    # - Draw page, via the number available
                    memcache.delete(PlayerPage.get_draw_mem_key(y))
                    # - Player summary, because their checkboxes have changed
                    memcache.delete(
                        PlayerPage.get_player_mem_key(player.db_key, y))
                memcache.delete(PlayerPage.get_draw_mem_key(ALL_YEARS))
                memcache.delete(
                    PlayerPage.get_player_mem_key(player.db_key, ALL_YEARS))
            self.get(base_url, extra)
        except Exception, e:
            self.get(base_url, extra, error=e)
Beispiel #4
0
    def post(self, base_url, extra):
        """Save the data posted about the all the items from the HTML form"""
        try:
            delete_years = set()  # Only refresh the cache for affected years
            # All the rounds are saved at once
            for year_round_key in self.request.POST.keys():
                try:
                    split_key = year_round_key.split('-')
                    year = split_key[0]
                    delete_years.add(year)
                    round = split_key[1]
                    if len(round) == 0:
                        # Might have been a negative round, so an extra split!
                        round = '-' + split_key[2]
                    round_key = Round.get_key(round, year)
                    round = Round.get_by_key_name(round_key)
                    if round:
                        args = {
                            'year':
                            round.year,
                            'key_name':
                            self.data_class.get_key(round.num, round.year),
                            'round':
                            round
                        }
                        player_key = self.request.POST.get(year_round_key)
                        args[self.name_property] = player_key
                        if not player_key in self.data_class.OTHERS:
                            args['player_ref'] = Player.get_by_key_name(
                                player_key)

                        new_entry = self.data_class(**args)
                        new_entry.put()
                except IndexError:
                    pass

        # Flush the cache for the affected years
            for y in delete_years:
                memcache.delete(CommonItemPage.get_mem_key(self.type, y))
            memcache.delete(CommonItemPage.get_mem_key(self.type, ALL_YEARS))
            self.get(base_url, extra)
        except Exception, e:
            self.get(base_url, extra, error=e)
Beispiel #5
0
  def DoGet(self, player_key_name):
    player_to_show = Player.get_by_key_name(player_key_name)

    logging.info('Getting player %s' % player_to_show)
    if not self.player:
      self.error(404)
      self.response.out.write("""<strong>No player with key %s.
      Try looking through the <a href="/players">list of players</a>.</strong>""" % player_key_name)

    additional_values = {
      'player_to_show'  : player_to_show,
      'available_games' : player_to_show.available_games(),
      'completed_games' : player_to_show.completed_games(),
      'active_games'    : player_to_show.active_games(),
      'stats'           : stats(player_to_show)
    }

    self.template_values.update(additional_values)
    self.render_to_response("player.html")
Beispiel #6
0
    def do_body(self, args):
        """Render the HTML for all the players, and provide enough information to edit an existing player or create a new one"""
        self.do_player_list(args, True)
        data = {
            'year': self.get_year(args),
            'url_args': args,
            'new_player': True,
            'past_years': list(range(datetime.now().year, FIRST_YEAR - 1, -1))
        }
        if args.has_key('player'):
            player_key = urllib.unquote(
                urllib.unquote(args['player'])
            )  # Dealing with spaces - why don't they disappear??
            curr_player = Player.get_by_key_name(player_key)

            if curr_player:
                data['current'] = curr_player
                data['new_player'] = False
        tpath = os.path.join(DeeWhyPage.TEMPLATE_DIR, 'manage_players.html')
        self.response.out.write(template.render(tpath, data))
Beispiel #7
0
    def DoGet(self, player_key_name):
        player_to_show = Player.get_by_key_name(player_key_name)

        logging.info('Getting player %s' % player_to_show)
        if not self.player:
            self.error(404)
            self.response.out.write("""<strong>No player with key %s.
      Try looking through the <a href="/players">list of players</a>.</strong>"""
                                    % player_key_name)

        additional_values = {
            'player_to_show': player_to_show,
            'available_games': player_to_show.available_games(),
            'completed_games': player_to_show.completed_games(),
            'active_games': player_to_show.active_games(),
            'stats': stats(player_to_show)
        }

        self.template_values.update(additional_values)
        self.render_to_response("player.html")
Beispiel #8
0
    def do_body(self, args):
        """Render the HTML for the front page"""
        self.do_player_list(args)
        year = self.get_year(args)
        # Different key for the different modes
        if args.has_key('player'):
            mem_key = PlayerPage.get_player_mem_key(args['player'], year)
        else:
            mem_key = PlayerPage.get_draw_mem_key(year)

        player_page = memcache.get(mem_key)
        if player_page is None:
            data = {
                'url_args': args,
            }

            # Switches mode depending on the input:
            if args.has_key('player'):
                # Present the player with the dialogues to set the availability
                page = 'individual.html'
                data = {
                    'rounds': Round.gql('WHERE year = :1 ORDER BY date ASC',
                                        year),
                    'player': Player.get_by_key_name(args['player']),
                }
            else:
                # Present the full season summary of upcoming games
                page = 'summary.html'
                if year == ALL_YEARS:
                    data['rounds'] = Round.gql('ORDER BY date ASC')
                else:
                    data['rounds'] = Round.gql(
                        'WHERE year = :1 ORDER BY date ASC', year)
            tpath = os.path.join(DeeWhyPage.TEMPLATE_DIR, page)
            player_page = template.render(tpath, data)
            memcache.set(mem_key, player_page)
        self.response.out.write(player_page)
Beispiel #9
0
    def post(self, base_url, extra):
        """Save the data posted about the result from the HTML form"""
        try:
            curr_round = None
            round_num = int(self.request.get('round_num'))
            year = int(self.request.get('year'))
            round_key = Round.get_key(round_num, year)
            if round_key:
                curr_round = Round.get_by_key_name(round_key)

            if curr_round:
                # Quite a few cached pages depend upon this output, need to delete them all
                # - Removing this page from the summary
                memcache.delete(PlayerPage.get_draw_mem_key(year))
                memcache.delete(PlayerPage.get_draw_mem_key(ALL_YEARS))
                # - Disabling the options for this page for each player
                for t in TeamList.gql('WHERE year = :1', year):
                    memcache.delete(
                        PlayerPage.get_player_mem_key(t.player.db_key, year))
                # - Changing the result and who scored
                memcache.delete(ResultsPage.get_mem_key(year))
                memcache.delete(ResultsPage.get_mem_key(ALL_YEARS))
                # - Changing the number of goals
                memcache.delete(ScorersPage.get_mem_key(year))
                memcache.delete(ScorersPage.get_mem_key(ALL_YEARS))
                # - Triggering previously suppressed rounds for the beer and shirts
                memcache.delete(BeerPage.get_mem_key('beer', year))
                memcache.delete(BeerPage.get_mem_key('beer', ALL_YEARS))
                memcache.delete(ShirtsPage.get_mem_key('shirts', year))
                memcache.delete(ShirtsPage.get_mem_key('shirts', ALL_YEARS))

                args = {
                    'year': year,
                    'key_name': Result.get_key(round_num, year),
                    'round': curr_round,
                }

                deewhy_forfeit = self.request.get('deewhy_forfeit')
                opponent_forfeit = self.request.get('opponent_forfeit')
                if not (deewhy_forfeit or opponent_forfeit):
                    # Regular operation - read the goal counts
                    args['deewhy_goals'] = self.get_goals('deewhy_goals')
                    args['opponent_goals'] = self.get_goals('opponent_goals')
                    args['other_goals'] = self.get_goals('other_goals')
                    args['own_goals'] = self.get_goals('own_goals')
                elif deewhy_forfeit is not None and self.request.get(
                        'deewhy_forfeit') == 'on':
                    # No goals in a forfeit
                    args['deewhy_forfeit'] = True
                elif opponent_forfeit is not None and self.request.get(
                        'opponent_forfeit') == 'on':
                    # No goals in a forfeit
                    args['opponent_forfeit'] = True
                curr_result = Result(**args)
                curr_result.put()

                # Need to delete any existing scorers for this round - otherwise orphaned goals will stuff up the tallies
                for scorer in curr_result.scorer_set:
                    scorer.delete()

                if not (deewhy_forfeit or opponent_forfeit):
                    for post_key in self.request.POST.keys():
                        # Each goal scorer in the game needs a separate record
                        if post_key.startswith('goals_'):
                            player = Player.get_by_key_name(
                                post_key[len('goals_'):])
                            goals = self.get_goals(post_key)
                            if player is not None and goals > 0:
                                GoalsScored(
                                    key_name=GoalsScored.get_key(
                                        player.db_key, round_num, year),
                                    year=int(year),
                                    player=player,
                                    result=curr_result,
                                    count=goals,
                                ).put()
            self.get(base_url, extra)
        except Exception, e:
            self.get(base_url, extra, error=e)
Beispiel #10
0
    def post(self, base_url, extra):
        """Save the data posted about the player from the HTML form"""
        #try:
        affected_years = set()
        old_key = self.request.get('key_name')
        if self.request.POST.has_key('do_delete') and self.request.POST.get(
                'do_delete') == 'true':
            # Delete the selected player
            # Also need to delete anything else they were involved with e.g. teamlists, goals scored, etc...
            key = old_key
            player = Player.get_by_key_name(key)
            for x in player.year_list:
                x.delete()
            for x in player.beer_set:
                x.delete()
            for x in player.shirt_set:
                x.delete()
            for x in player.goal_set:
                x.delete()
            for x in player.availability_set:
                x.delete()
            self.delete_player(key)
        else:
            # Create/update the player
            first = self.request.get('first_name').strip()
            last = self.request.get('last_name').strip()
            key = Player.get_key(first, last)
            nick = self.request.get('nick_name').strip()
            email = self.request.get('email').strip()
            phone = self.request.get('phone').strip()
            registration = self.request.get('registration').strip()
            shirt = self.request.get('shirt').strip()
            if key != '':
                new_player = Player(
                    key_name=key,
                    db_key=key,
                    first_name=first,
                    last_name=last,
                    nick_name=nick,
                    email=email,
                    phone=phone,
                    registration=registration,
                    shirt=shirt,
                )
                new_player.put()
                for y in xrange(datetime.now().year, FIRST_YEAR - 1, -1):
                    year_key = TeamList.get_key(key, y)
                    if self.request.POST.has_key('played_' + str(y)):
                        TeamList(
                            key_name=year_key,
                            player=new_player,
                            year=y,
                        ).put()
                    else:
                        self.delete_player_year(year_key)
            if old_key != key:
                # If changing the info changes the key, be sure to scrap the old entry too
                self.delete_player(old_key)

        # Quite a few pages are potentially affected when you change a player's details. But only do it if the
        # form says to do so, because it's so expensive to regenerate everything!
        nickname_update = (self.request.POST.has_key('do_flush')
                           and self.request.get('do_flush') == 'on')

        # Update the side listings
        memcache.delete(PlayerPage.get_list_mem_key(ALL_YEARS))
        memcache.delete(PlayerManagePage.get_list_mem_key(ALL_YEARS))
        memcache.delete(PlayerPage.get_player_mem_key(
            key, ALL_YEARS))  # Their availability
        if nickname_update:
            memcache.delete(PlayerPage.get_draw_mem_key(ALL_YEARS))
            memcache.delete(ResultsPage.get_mem_key(ALL_YEARS))
            memcache.delete(ScorersPage.get_mem_key(ALL_YEARS))
            memcache.delete(BeerPage.get_mem_key('beer', ALL_YEARS))
            memcache.delete(ShirtsPage.get_mem_key('shirts', ALL_YEARS))
            memcache.delete(ProtectedPage.get_mem_key())

        for y in xrange(datetime.now().year, FIRST_YEAR - 1, -1):
            memcache.delete(PlayerPage.get_list_mem_key(y))
            memcache.delete(PlayerManagePage.get_list_mem_key(y))
            memcache.delete(PlayerPage.get_player_mem_key(
                key, y))  # Their availability

            if nickname_update:
                memcache.delete(PlayerPage.get_draw_mem_key(y))
                memcache.delete(ResultsPage.get_mem_key(y))
                memcache.delete(ScorersPage.get_mem_key(y))
                memcache.delete(BeerPage.get_mem_key('beer', y))
                memcache.delete(ShirtsPage.get_mem_key('shirts', y))

        self.get(base_url, extra)
Beispiel #11
0
 def delete_player(self, key):
     """Scrap the entire player"""
     if key:
         p = Player.get_by_key_name(key)
         if p:
             p.delete()