Exemplo n.º 1
0
    def send_email(self, com_id, blog, recipients):
        tool = ToolManager()
        sg = tool.get_sendgrid()
        message = sendgrid.Mail()
        url_root = tool.getProperty("url_root")["value"]

        urlBlog = "https://{}/#/blog_in_community/{}".format(url_root, com_id);
        url_to_bet = "https://{}/#/bet_in_community/{}".format(url_root, com_id);
        for r in recipients:
            message.add_to(r)
        message.add_to("*****@*****.**")
        message.set_from("*****@*****.**")
        message.set_subject("eurommxvi : {}".format(blog.title))
        logger.info("email title={}".format(blog.title))
        logger.info("email body-to_mail={}".format(blog.body_to_mail()))
        body = u"""<html><head></head><body><pre style='font-size: 16px;font-family:Verdana;'>{}</pre>
        <br/><h2>Laissez vos commentaires ici : {}</h2>
        <br/><h1>Et, surtout n'oubiez pas de parier: {}</h1></body></html>""".format(blog.body_to_mail(), urlBlog, url_to_bet)
        message.set_html(body)

        res = sg.send(message)

        logger.debug("email result={}/{}".format(str(res[0]), str(res[1])))

        return res
Exemplo n.º 2
0
def confirmationSubscription(user_id):
    u"""
    url called from email to confirm subscription
    :return: redirect to user detail page (normal not return json data as angular style because user
    is in its email client and not in our site)
    """
    logger.info("confirmationSubscription")
    logger.info(u"confirmationSubscription::user_id:{} ".format(user_id))
    tool = ToolManager()
    #sg = tool.get_sendgrid()

    mgr = UserManager()
    user = mgr.getUserByUserId(user_id)
    logger.info(u'confirmationSubscription::user={}'.format(user))

    mgr.saveUser(user.email, user.nickName, user.description, user.user_id, True, "")

    
    tool_mgr = ToolManager()
    url_root = tool_mgr.getProperty("url_root")["value"]
    
    # from_email = Email("*****@*****.**")
    # to_email = Email(user.email)
    # subject = "phipha2018 - confirmation"
    # content = Content("text/html", "<html><head></head><body><h1><a href='{}'>Congratulations on your registration ! </a></h1></hr></body></html>")
    # mail = Mail(from_email, subject, to_email, content)
    # response = sg.client.mail.send.post(request_body=mail.get())
    # print(response.status_code)
    # print(response.body)
    # print(response.headers)


    return redirect("{}/#!/user_detail/{}/?firstConnection=true".format(url_root,user_id))
Exemplo n.º 3
0
def subscriptionPost():
    u"""
    first step of subscription : store user in db  and email send (before user validation)
    :return: forward to a page (not angular style : TODO change it if necessary)
    """
    logger.info("subscriptionPost")
    email = request.form['email']

    mgr = UserManager()
    user = mgr.getUserByEmail(email)
    if user is None:
        tool = ToolManager()
        sg = tool.get_sendgrid()
        message = sendgrid.Mail()

        message.add_to(email)

        message.add_to("*****@*****.**")
        message.set_from("*****@*****.**")
        message.set_subject("euroxxxvi - subscription")

        uuid = str(uuid4())
        logger.info(u"subscriptionPost::user_id:{}".format(uuid))
        mgr.saveUser(email, "", "", uuid, False, "")
        logger.info(u"\tsubscriptionPost::save done")
        tool_mgr = ToolManager()
        url_root = tool_mgr.getProperty("url_root")["value"]
        urlcallback=u"http://{}/users/{}/confirmation".format(url_root, uuid)
        message.set_html("<html><head></head><body><h1>MERCI DE</h1><h1><a href='{}'>Confirmer votre inscription</a></h1></hr></body></html>".format(urlcallback))

        sg.send(message)
        return redirect(u"/#logon_successfull")
    else:
        return redirect(u"/")
Exemplo n.º 4
0
def confirmationSubscription(user_id):
    u"""
    url called from email to confirm subscription
    :return: redirect to user detail page (normal not return json data as angular style because user
    is in its email client and not in our site)
    """
    logger.info("confirmationSubscription")
    logger.info(u"confirmationSubscription::user_id:{} ".format(user_id))
    tool = ToolManager()
    sg = tool.get_sendgrid()

    message = sendgrid.Mail()
    mgr = UserManager()
    user = mgr.getUserByUserId(user_id)
    logger.info(u'confirmationSubscription::user={}'.format(user))

    mgr.saveUser(user.email, user.nickName, user.description, user.user_id, True, "")

    message.add_to(user.email)

    message.add_to("*****@*****.**")
    message.set_from("*****@*****.**")
    message.set_subject("euroxxxvi - confirmation")
    tool_mgr = ToolManager()
    url_root = tool_mgr.getProperty("url_root")["value"]
    urlcallback = u"http://{}/".format(url_root)

    message.set_html("<html><head></head><body><h1><a href='{}'>Félicitations pour votre inscription ! </a></h1></hr></body></html>".format(urlcallback))

    sg.send(message)

    return redirect("/#user_detail/{}/?firstConnection=true".format(user_id))
Exemplo n.º 5
0
def subscriptionPost():
    u"""
    first step of subscription : store user in db  and email send (before user validation)
    :return: forward to a page (not angular style : TODO change it if necessary)
    """
    logger.info("subscriptionPost")
    email = request.form['email']

    mgr = UserManager()
    user = mgr.getUserByEmail(email)
    if user is None:
        tool = ToolManager()
        #sg = tool.get_sendgrid()
        #message = sendgrid.Mail()

        #message.add_to(email)

        #message.add_to("*****@*****.**")
        #message.set_from("*****@*****.**")
        #message.set_subject("euroxxxvi - subscription")

        uuid = str(uuid4())
        logger.info(u"subscriptionPost::user_id:{}".format(uuid))
        mgr.saveUser(email, "", "", uuid, False, "")
        logger.info(u"\tsubscriptionPost::save done")
        tool_mgr = ToolManager()
        url_root = tool_mgr.getProperty("url_root")["value"]
        urlcallback=u"{}/users/{}/confirmation".format(url_root, uuid)
        logger.info("urlcallback={}".format(urlcallback))
        #message.set_html("<html><head></head><body><h1>MERCI DE</h1><h1><a href='{}'>Confirmer votre #inscription</a></h1></hr></body></html>".format(urlcallback))

        #sg.send(message)
        
        #from_email = Email("*****@*****.**")
        #to_email = Email(email)
        #subject = "phipha2018 - subscription"
        #content = Content("text/html", "<html><head></head><body><h1>Please</h1><h1><a href='{}'> confirm your subscription</a></h1></hr></body></html>".format(urlcallback))
        #mail = Mail(from_email, subject, to_email, content)
        #response = sg.client.mail.send.post(request_body=mail.get())
        #print(response.status_code)
        #print(response.body)
        #print(response.headers)
        #return redirect("{}/#logon_successfull".format(url_root))
        return redirect("{}".format(urlcallback))
    else:
        if user.validated:
            tool_mgr = ToolManager()
            url_root = tool_mgr.getProperty("url_root")["value"]
            return redirect("{}".format(url_root))
        else:
            tool_mgr = ToolManager()
            url_root = tool_mgr.getProperty("url_root")["value"]
            urlcallback = u"{}/users/{}/confirmation".format(url_root, user.user_id)
            return redirect("{}".format(urlcallback))
Exemplo n.º 6
0
def testmail():
    tool = ToolManager()
    sg = tool.get_sendgrid()

    message = sendgrid.Mail()

    message.add_to("*****@*****.**")
    message.set_from("*****@*****.**")
    message.set_subject("test from openshift")
    message.set_html("<html><head></head><body><h1>Il faut bien tapper : rhc env set SENDGRID_HOSTNAME=smtp.sendgrid.net -a euroxxxvi</h1></hr></body></html>")

    res = sg.send(message)

    return u"look at your email box : result="+str(res[0])+" - "+str(res[1]), 200
Exemplo n.º 7
0
def testmail():
    tool = ToolManager()
    sg = tool.get_sendgrid()

    api_key = tool.getProperty('SENDGRID_API_KEY')["value"]
    logger.info("sendgrid={}".format(api_key))
    sg = sendgrid.SendGridAPIClient(apikey=api_key)
    from_email = Email("*****@*****.**")
    to_email = Email("*****@*****.**")
    subject = "Sending with SendGrid is Fun"
    content = Content(
        "text/html",
        "and <b>easy</h> to do anywhere, even with <h1>Python</h1>")
    mail = Mail(from_email, subject, to_email, content)
    response = sg.client.mail.send.post(request_body=mail.get())
    print(response.status_code)
    print(response.body)
    print(response.headers)

    return u"look at your email box : result=" + str(
        response.status_code) + " - " + str(response.body), 200
Exemplo n.º 8
0
def testmongo():
    tool = ToolManager()
    db = tool.getDb()
    logger.info(u'test mongo : db={}'.format(db))
    tmp = tool.getProperty(u"test")
    logger.info(u'test mongo : tmp={}'.format(tmp))
    tool.saveProperty(u"test", "firstProperties")
    props = tool.getProperties()
    logger.info(u'test mongo : {}'.format(props))

    return u"Test Mongo", 200
Exemplo n.º 9
0
    def send_email(self, com_id, blog, recipients):
        tool = ToolManager()
        sg = tool.get_sendgrid()
        url_root = tool.getProperty("url_root")["value"]

        urlBlog = "{}/#/blog_in_community/{}".format(url_root, com_id)
        url_to_bet = "{}/#/bet_in_community/{}".format(url_root, com_id)

        logger.info("email title={}".format(blog.title))
        logger.info("email body-to_mail={}".format(blog.body_to_mail()))
        body = u"""<html><head></head><body><pre style='font-size: 16px;font-family:Verdana;'>{}</pre>
        <br/><h2>Leave your comments here: {}</h2>
        <br/><h1>And don't forget to bet: {}</h1></body></html>""".format(
            blog.body_to_mail(), urlBlog, url_to_bet)
        try:
            from_email = Email("*****@*****.**",
                               "phipha2018-noreply-please")
            content = Content("text/html", body)

            mail = Mail(from_email, blog.title, from_email, content)

            for r in recipients:
                logger.debug(r)
                mail.personalizations[0].add_bcc(Email(r))
            logger.debug(mail.personalizations)

            response = sg.client.mail.send.post(request_body=mail.get())
            logger.debug(response.status_code)
            logger.debug(response.body)
            logger.debug(response.headers)

            # Bloc de test
        except RuntimeError as err:
            print("Unexpected error:", err)
            raise

        return response
Exemplo n.º 10
0
def testmongo():
    client = MongoClient(
        "mongodb+srv://phipha:[email protected]/phipha?retryWrites=true",
        ssl=True)
    db = client.first
    logger.info(u'test mongo : db={}'.format(db))
    tool = ToolManager()
    db = tool.getDb()
    logger.info(u'test mongo : db={}'.format(db))
    tmp = tool.getProperty(u"test")
    logger.info(u'test mongo : tmp={}'.format(tmp))
    tool.saveProperty(u"test", "firstProperties")
    props = tool.getProperties()
    logger.info(u'test mongo : {}'.format(props))

    return u"Test Mongo", 200
Exemplo n.º 11
0
    def update_all_matchs(self, matchs_to_update, no_save):
        #load all match from db (because we just want to update result
        logger.info(u"update_all_matchs::start-before getAllMatchs")
        matchs = self.getAllMatchs()
        logger.info(u"update_all_matchs::end getAllMatchs")
        nb_hits=0
        bets_for_mail = list()
        bet_mgr = BetsManager()
        betList = bet_mgr.get_all_bets()
        logger.info(u"update_all_matchs::end get_all_bets")
        for m in matchs:
            match = Match()
            match.convertFromBson(m)
            match_key=match.key
            #quick filter !! i love python
            unique_match_list = [x for x in matchs_to_update if x["key"] == match_key]
            match_dict=unique_match_list[0]
            if match_dict is not None:
                match.resultA = match_dict["resultA"]
                match.resultB = match_dict["resultB"]
                if not no_save:
                    # mettre à jour juste les resultats
                    logger.info(u'\tupdate_all_matchs::try update match["key" : {}] with match={}'.format(match_key, match_dict))
                    result = self.getDb().matchs.update_one({"key": match_key},
                                         {"$set": {"resultA": match_dict["resultA"],
                                                   "resultB": match_dict["resultB"]}}, upsert=True)
                    nb_hits = nb_hits + result.matched_count
                else:
                    logger.info("no match updated")
            else:
                logger.warn(u'\tmatch notfound in matchs_to_update["key" : {}]'.format(match_key))

            # pour chaque match demander à betmanager de calculer le nb de points de chq bet
            # le principe sera de calculer le nbde pts d'un user = somme de ses paris
            shortList = [b for b in betList if b.key == m["key"]]
            for bet in shortList:
                match.computeResult(bet)
                logger.info(
                    u'\t\tupdate_all_matchs::bet={}/{} - nbpts={}'.format(bet.key, bet.user_id, bet.nbpoints))
                bets_for_mail.append(self.format_bet(bet, match))

        if not no_save:
            for bet in betList:
                logger.info("bet update")
                bet_mgr.saveScore(bet)


        message = sendgrid.Mail()
        message.add_to("*****@*****.**")
        message.set_from("*****@*****.**")
        message.set_subject("euroxxxvi - bets")
        head = u"<html><head></head><body><table border='1'><tr><td>m.key</td><td>teamA</td><td>teamB</td><td>resultA</td>"
        head=head+u"<td>resultB</td><td>&nbsp;</td><td>bet.key</td><td>com_id</td><td>user_id</td>"
        head=head+u"<td>bet.resultA</td><td>bet.resultB</td><td>bet.nbpoints</td></tr>"
        content = head+"\n".join(bets_for_mail)+"</table></body></html>"
        logger.info(content)
        message.set_html(content)
        tool = ToolManager()
        sg = tool.get_sendgrid()
        sg.send(message)

        return None
Exemplo n.º 12
0
    def update_all_matchs(self, matchs_to_update, no_save):
        #load all match from db (because we just want to update result
        logger.info(u"update_all_matchs::start-before getAllMatchs")
        matchs = self.getAllMatchs()
        logger.info(u"update_all_matchs::end getAllMatchs")
        nb_hits = 0
        bets_for_mail = list()
        bet_mgr = BetsManager()
        betList = bet_mgr.get_all_bets()
        logger.info(u"update_all_matchs::end get_all_bets")
        for m in matchs:
            match = Match()
            match.convertFromBson(m)
            match_key = match.key
            #quick filter !! i love python
            unique_match_list = [
                x for x in matchs_to_update if x["key"] == match_key
            ]
            match_dict = unique_match_list[0]
            if match_dict is not None:
                match.resultA = match_dict["resultA"]
                match.resultB = match_dict["resultB"]
                if not no_save:
                    # mettre à jour juste les resultats
                    logger.info(
                        u'\tupdate_all_matchs::try update match["key" : {}] with match={}'
                        .format(match_key, match_dict))
                    result = self.getDb().matchs.update_one(
                        {"key": match_key}, {
                            "$set": {
                                "resultA": match_dict["resultA"],
                                "resultB": match_dict["resultB"]
                            }
                        },
                        upsert=True)
                    nb_hits = nb_hits + result.matched_count
                else:
                    logger.info("no match updated")
            else:
                logger.warn(
                    u'\tmatch notfound in matchs_to_update["key" : {}]'.format(
                        match_key))

            # pour chaque match demander à betmanager de calculer le nb de points de chq bet
            # le principe sera de calculer le nbde pts d'un user = somme de ses paris
            shortList = [b for b in betList if b.key == m["key"]]
            for bet in shortList:
                match.computeResult(bet)
                logger.info(
                    u'\t\tupdate_all_matchs::bet={}/{} - nbpts={}'.format(
                        bet.key, bet.user_id, bet.nbpoints))
                bets_for_mail.append(self.format_bet(bet, match))

        if not no_save:
            for bet in betList:
                logger.info("bet update")
                bet_mgr.saveScore(bet)

        from_email = Email("*****@*****.**")
        to_email = Email("*****@*****.**")
        subject = "phipha2018 - bets saved"
        head = u"<html><head></head><body><table border='1'><tr><td>m.key</td><td>teamA</td><td>teamB</td><td>resultA</td>"
        head = head + u"<td>resultB</td><td>&nbsp;</td><td>bet.key</td><td>com_id</td><td>user_id</td>"
        head = head + u"<td>bet.resultA</td><td>bet.resultB</td><td>bet.nbpoints</td></tr>"
        content = Content(
            "text/html",
            head + "\n".join(bets_for_mail) + "</table></body></html>")
        mail = Mail(from_email, subject, to_email, content)
        tool = ToolManager()
        sg = tool.get_sendgrid()
        response = sg.client.mail.send.post(request_body=mail.get())
        print(response.status_code)
        print(response.body)
        print(response.headers)

        return None