示例#1
0
    def send_notice_email(self):
        """
        为当前邮箱状态为0的用户发送提醒邮件
        :return:
        """
        db = TinyDB("data.json")
        People = Query()
        target_users = db.search(People.status == 0)
        email_config = get_email_config()
        org_config = get_org_config()
        for user in target_users[:30]:  # 这里最多需要150 * 30s来发送完毕
            token = str(uuid.uuid1())
            content = '感谢您的辛苦付出,请点击链接 <a href="%s?token=' % org_config["frontend_url"]
            content += token + '">%s?token=' % org_config["frontend_url"]
            content += token + '</a>领取您的《志愿者证书》\n'
            content += org_config["name"]
            content += '\n网址:<a href="{0}">{0}</a>'.format(org_config["website"])

            is_successful = send_email(
                to_email=user['email'],
                subject='快来领取您的《 %s 志愿者证书》' % org_config["name"],
                content=content,
            )
            if is_successful:
                update_status_and_token(email=user['email'], status=1, token=token)
            slope = email_config["max_second"] - email_config["min_second"]
            time.sleep(email_config["min_second"] + random.random() * slope)

        db.close()
示例#2
0
def confirm_admin_token(token):
    orgconfig = utils.get_org_config()
    admintoken = orgconfig["admin_token"]
    if token == admintoken:
        return True
    else:
        return False
 def test_updateOrgConfig(self):
     client = app.test_client()
     json_data = {"token": "1234",
                  "website":"https://community.wuhan2020.org.cn/",
                  "username": "******",
                  "password": "******"}
     response = client.post('/api/updateOrgConfig',
                             json=json_data,
                             headers={'Referer': 'http://example.org/admin.html'})
     res_json = json.loads(response.data.decode('ascii'))
     self.assertEqual(res_json['code'], 0)
     email_config = get_email_config()
     self.assertEqual(email_config["server_address"], "smtp.example.org")
     org_config = get_org_config()
     self.assertEqual(org_config["frontend_url"], 'http://example.org/index.html')
示例#4
0
def update_config():
    if request.method == 'POST':
        message = json.loads(request.get_data(as_text=True))
        token = message["token"]
        result = confirm_admin_token(token)  # 没有每个人唯一的Key
    response = Response()
    response.headers['Content-Type'] = 'application/json'
    response.headers['Access-Control-Allow-Origin'] = '*'
    response.headers['Access-Control-Allow-Headers'] = '*'
    if request.method == 'OPTIONS':
        return response
    return_json = {'code': 1, 'message': '网络异常', 'data': None}
    response.data = return_msg(return_json)
    if result == False:
        return response
    try:
        referer = request.headers.get('Referer', None)
        if referer is not None:
            referer = referer.replace('admin', 'index')
            message['frontend_url'] = referer
        if message.get('username'):  # email admin address
            message['server_address'] = get_smtp_url(
                message.get('username'))  # email smtp address
        orgconfig = utils.get_org_config()
        emailconfig = utils.get_email_config()
        for domain in message:
            if domain == "token" or len(str(message[domain])) == 0:
                continue
            if domain in orgconfig:
                orgconfig[domain] = message[domain]
            elif domain in emailconfig:
                emailconfig[domain] = message[domain]
        utils.update_org_config(orgconfig)
        utils.update_email_config(emailconfig)
    except Exception as e:
        logger.error(e)
    return_json = {
        'code': 0,
        'message': 'update config successfully',
        'data': None
    }
    response.data = return_msg(return_json)
    return response
示例#5
0
def write_to_pic(name, email, token):  # 执行完这个方法后生成一个 result.png 图片 可加入email参数
    number = get_number(email)  # 用这个方法获取到编号
    date_str = datetime.datetime.strftime(datetime.datetime.now(),
                                          '%Y年%m月%d日')  #获取日期
    update_status(email)  # use 参数 变为1  生成了证书
    org_config = get_org_config()
    im = Image.open("pic.jpg")
    draw = ImageDraw.Draw(im)
    font_name = ImageFont.truetype('font/1.ttf', size=55)  # 名字的字体和字号
    font_number = ImageFont.truetype('font/2.ttf', size=35)  # 编号的字体和字号
    setFontdate = ImageFont.truetype('font/DENG.TTF', size=28)  # date字体路径
    imwidth, imheight = im.size
    font_width, font_height = draw.textsize(name, font_name)  # 获取名字的大小
    horizontal_offset = (font_width - font_name.getoffset(name)[0]) / 2
    draw.text((org_config["name_horizontal_pos"] - horizontal_offset,
               org_config['name_vertical_pos']),
              text=name,
              font=font_name,
              fill=(0, 0, 0))  # 写上名字 x使用了居中
    if org_config["serial_number_horizontal_pos"] > 0:
        draw.text(xy=(org_config["serial_number_horizontal_pos"],
                      org_config["serial_number_vertical_pos"]),
                  text=number,
                  font=font_number)  # 写上编号
    if org_config["date_horizontal_pos"] > 0:
        draw.text((org_config["date_horizontal_pos"],
                   org_config["date_vertical_pos"]),
                  date_str,
                  font=setFontdate,
                  fill=(0, 0, 0))  # 写上日期
    image_file = 'images/%s.png' % token
    im.save(image_file)
    update_status(email, 3)
    content = '您好,附件中有您的证书\n\n\n'
    content += org_config["name"]
    content += '\n\n\n网址:<a href="{0}">{0}</a>'.format(org_config["website"])
    send_email(to_email=email,
               subject='请领取您的志愿者证书',
               content=content,
               attachment=[image_file])
    update_status(email, 4)