Ejemplo n.º 1
0
def create_manual_temple_noti():
    #外部触发模板消息
    project = get_url_params('project')
    temple_id = get_url_params('temple_id')
    send_at = get_url_params('send_at') if get_url_params('send_at') else int(
        time.time())
    password = get_url_params('password')
    owner = get_url_params('owner')
    data = get_url_params('data')
    if password == admin.admin_password and request.method == 'POST' and owner and owner != '' and project:
        try:
            data_jsons = json.loads(data)
            data_list = []
            for item in data_jsons:
                print(item)
                if 'distinct_id' in item and 'data_json' in item:
                    result_temple = select_noti_temple(project=project,
                                                       temple_id=temple_id)
                    result = apply_temple(
                        project=project,
                        temple_args=json.loads(result_temple[0][0][2]),
                        temple_content=json.loads(result_temple[0][0][3]),
                        data_json=item['data_json'],
                        data_key=item['distinct_id'],
                        send_at=send_at,
                        group_id=None,
                        owner=owner)
                    data_list.append(result)
            result_insert = create_non_usergroup_noti(
                args={
                    'owner': owner,
                    'temple_id': temple_id,
                    'project': project,
                    'data': data_list
                })
            return jsonify(result_insert)
        except Exception:
            error = traceback.format_exc()
            write_to_log(filename='api_noti',
                         defname='create_manual_temple_noti',
                         result=error)
            returnjson = {'result': 'fail', 'error': error}
            return jsonify(returnjson)
Ejemplo n.º 2
0
def create_noti_group(project,
                      temple_id,
                      user_group_id=None,
                      data_id=None,
                      owner='noti',
                      send_at=None):
    #对分群应用模板,有data_id时,优先使用data_id
    result_temple = select_noti_temple(project=project, temple_id=temple_id)
    if not user_group_id and not data_id:
        return 'no_group'
    else:
        if user_group_id and not data_id:
            result_data = select_usergroup_data(project=project,
                                                group_list_id=user_group_id)
        elif data_id:
            result_data = select_usergroupdata_data(project=project,
                                                    data_id=data_id)
        timenow = int(time.time())
        result_group = insert_noti_group(
            project=project,
            plan_id=result_data[0][0][0],
            list_id=result_data[0][0][1],
            data_id=result_data[0][0][2],
            temple_id=result_temple[0][0][0],
            owner=owner,
            send_at=send_at if send_at else timenow,
            sent=0,
            total=result_data[1],
            priority=13,
            status=result_data[0][0][6])
        for noti in result_data[0]:
            timenow = int(time.time())
            temple_content = apply_temple(
                project=project,
                temple_args=json.loads(result_temple[0][0][2]),
                temple_content=json.loads(result_temple[0][0][3]),
                data_json=json.loads(noti[5]),
                data_key=noti[4],
                group_id=user_group_id,
                owner=owner,
                send_at=send_at if send_at else timenow)
            insert_result = insert_noti(
                project=project,
                type_1=json.loads(result_temple[0][0][2])['meta']['medium_id'],
                created_at=timenow,
                updated_at=timenow,
                distinct_id=noti[4],
                content=temple_content,
                send_at=send_at if send_at else timenow,
                plan_id=noti[0],
                list_id=noti[1],
                data_id=noti[2],
                temple_id=result_temple[0][0][0],
                noti_group_id=result_group[2],
                priority=13,
                status=noti[6],
                owner=owner,
                recall_result=None,
                key=temple_content['key'] if 'key' in temple_content else None,
                level=temple_content['level']
                if 'level' in temple_content else None)
        update_noti_temple(
            project=project,
            temple_id=temple_id,
            apply_times=1,
            lastest_apply_time=timenow,
            lastest_apply_list=user_group_id if user_group_id else 0)
        update_noti_group(project=project, noti_group_id=result_group[2])
        update_usergroup_list(project=project,
                              list_id=result_data[0][0][1],
                              apply_temple_times=1)
        update_usergroup_plan(project=project,
                              plan_id=result_data[0][0][0],
                              latest_apply_temple_id=temple_id,
                              latest_apply_temple_time=timenow)
        return 'success'