예제 #1
0
파일: admin.py 프로젝트: podgib/brownlow
  def post(self):
    game_id = self.request.get("game")
    voter_id = self.request.get("player")

    voter = Player.get_by_id(int(voter_id))
    game = Game.get_by_id(int(game_id))

    value = base64.urlsafe_b64encode(os.urandom(16))
    token = Token(value=value, voter=voter.key, game=game.key, used=False)
    token.put()
    url = "http://vote.ouarfc.co.uk/vote/" + value
    self.response.out.write(url)
예제 #2
0
파일: worker.py 프로젝트: podgib/brownlow
    def post(self):
        player_id = self.request.get("player")
        game_id = self.request.get("game")

        player = Player.get_by_id(int(player_id))
        game = Game.get_by_id(int(game_id))

        token = Token.query(ndb.AND(Token.game == game.key, Token.voter == player.key)).get()

        if not token:
            logging.info("No token found for %s. Creating new token.", player.name)
            token_string = base64.urlsafe_b64encode(os.urandom(16))
            token = Token(value=token_string, voter=player.key, game=game.key, used=False)
            token.put()
        else:
            logging.info("Token found for %s.", player.name)
            if token.used:
                logging.info("%s has already voted, not sending email.", player.name)
                return

        url = "http://vote.ouarfc.co.uk/vote/" + token.value

        if player.phone:
            logging.info("Sending SMS to %s", player.name)
            template = jinja_environment.get_template("templates/sms.txt")
            sms_data = {
                "ORIGINATOR": "OUARFC",
                "NUMBERS": player.phone,
                "BODY": template.render({"url": url, "opponent": game.opponent, "team": Team.getString(game.team)}),
            }

            response = urlfetch.fetch(
                "https://api.zensend.io/v3/sendsms",
                payload=urllib.urlencode(sms_data),
                method=urlfetch.POST,
                headers={"X-API-KEY": config.zensend_key},
            )
            content_type = response.headers["content-type"]
            if content_type is not None and "application/json" in content_type:
                result = json.loads(response.content)
                if "success" in result:
                    logging.info("Successfully sent SMS to " + player.phone)
                    if config.phone_only:
                        # SMS was a success, don't send email
                        return
                elif "failure" in result:
                    failure = result["failure"]
                    logging.error(
                        "SMS send failed. Player: %s, phone: %s, parameter: %s, error code: %s, status code: %s",
                        player.name,
                        player.phone,
                        failure["parameter"],
                        failure["failcode"],
                        response.status_code,
                    )
                else:
                    logging.error("SMS send failed. Status code: %s", response.status_code)
            else:
                logging.error("SMS send failed. Status code: %s", response.status_code)

        template = jinja_environment.get_template("templates/email.txt")
        subject = "OUARFC: Please Vote For Best & Fairest"
        message = mail.EmailMessage(sender="OUARFC <*****@*****.**>", subject=subject)
        message.to = player.email
        message.body = template.render(
            {
                "name": player.name,
                "opponent": game.opponent,
                "date": game.date,
                "team": Team.getString(game.team),
                "url": url,
            }
        )
        logging.info("Sending email to " + player.email)
        message.send()
        logging.info(message.body)