Пример #1
0
def dao_get_organisation_by_email_address(email_address):

    email_address = email_address.lower().replace(".gsi.gov.uk", ".gov.uk")

    for domain in Domain.query.order_by(func.char_length(Domain.domain).desc()).all():

        if email_address.endswith("@{}".format(domain.domain)) or email_address.endswith(".{}".format(domain.domain)):
            return Organisation.query.filter_by(id=domain.organisation_id).one()

    return None
Пример #2
0
def profile_product_redirect(attr_type):
    '''fetch random product'''
    tbl = getattr(attr_models, attr_type.capitalize())
    p = tbl.query.filter(func.char_length(tbl.id) == 6).order_by(
        func.random()).first_or_404()
    return redirect(
        url_for(".profile_product",
                lang=g.locale,
                attr_type=attr_type,
                attr_id=p.get_display_id()))
Пример #3
0
def profile_product(attr_type, attr_id="usa"):
    g.page_type = mod.name
    g.page_sub_type = attr_type
    p_tbl = getattr(attr_models, attr_type.capitalize())
    pname_tbl = getattr(attr_models, attr_type.capitalize()+"_name")
    pid_name = attr_type + "_id"

    is_iOS = False
    if any(x in request.headers.get('User-Agent') for x in ["iPad","iPhone","iPod"]):
        is_iOS = True
    
    this_prod = p_tbl.query.filter(getattr(p_tbl, attr_type)==attr_id).first_or_404()
    attrs=db.session.query(p_tbl, pname_tbl.name) \
            .filter(func.char_length(p_tbl.id)==6) \
            .filter(getattr(pname_tbl, pid_name)==p_tbl.id) \
            .filter(pname_tbl.lang==g.locale) \
            .order_by(pname_tbl.name).all()
    attrs = [a[0] for a in attrs]
    this_prod_index = attrs.index(this_prod)
    prev_prod = attrs[this_prod_index-1] if this_prod_index>0 else None
    next_prod = attrs[this_prod_index+1] if this_prod_index<(len(attrs)-1) else None

    tree_map = App.query.filter_by(type="tree_map").first()

    exports = Build.query.filter_by(app=tree_map,
                                            trade_flow="export",
                                            origin="show",
                                            dest="all",
                                            product="<product>").first()

    imports = Build.query.filter_by(app=tree_map,
                                            trade_flow="import",
                                            origin="show",
                                            dest="all",
                                            product="<product>").first()

    builds = [exports, imports]
    for b in builds:
        b.set_options(origin=None,
                        dest=None,
                        product=this_prod,
                        classification=attr_type,
                        year=available_years[attr_type][-1])

    return render_template("profile/product.html",
                                is_iOS=is_iOS,
                                builds=builds,
                                classification=attr_type,
                                prev=prev_prod,
                                attr=this_prod,
                                next=next_prod)
Пример #4
0
def profile_product_redirect(attr_type):
    '''fetch random product'''
    tbl = getattr(attr_models, attr_type.capitalize())
    p = tbl.query.filter(func.char_length(tbl.id)==6).order_by(func.random()).first_or_404()
    return redirect(url_for(".profile_product", lang=g.locale, attr_type=attr_type, attr_id=p.get_display_id()))
Пример #5
0
def silence_rule(call_datas, rule_id, map_id, warning_id, map_rule_name):
    session = db_v1.session()
    rule_result = session.query(QiInfoSilence.quiet_time).filter_by(
        id=rule_id, is_deleted=0).all()
    if rule_result:
        datas = session.query(
            QiLabelSentence.call_id, QiLabelSentence.task_id,
            QiLabelSentence.paragraph_id, QiLabelSentence.start_time,
            QiLabelSentence.end_time, QiLabelSentence.role,
            QiLabelSentence.text,
            func.char_length(QiLabelSentence.text).label('len_text')).filter(
                QiLabelSentence.call_id.in_(call_datas)).order_by(
                    "task_id", "start_time").all()

        hit_pid = []
        re_str = '(等一下|稍等)'
        for i in range(1, len(datas)):
            if datas[i].task_id != datas[i - 1].task_id:
                continue
            if not datas[i].role is None:
                if (datas[i].start_time -
                        datas[i - 1].end_time) > rule_result[0].quiet_time:
                    if datas[i].role == 1:
                        n = 1
                        sum_words = datas[i].len_text
                        while datas[i].task_id == datas[i - n].task_id:
                            if datas[i - n].role != 1 or datas[
                                    i].paragraph_id == datas[i -
                                                             n].paragraph_id:
                                n += 1
                                sum_words += datas[i - n].len_text
                            else:
                                re_result = re.search(re_str,
                                                      datas[i - n].text)
                                if (re_result is None) or (sum_words >= 8):
                                    if datas[i].paragraph_id not in hit_pid:
                                        hit_pid.append(datas[i].paragraph_id)
                                        if warning_id:
                                            warning_data = db_v1.session.query(
                                                QiInfoWarning).filter(
                                                    QiInfoWarning.id ==
                                                    warning_id).first_or_404()
                                            warning_message = warning_data[
                                                'message']
                                            warning_ding_talk = warning_data[
                                                'ding_talk']
                                            warning_apps = warning_data['apps']
                                            if warning_message:
                                                warn_message(
                                                    datetime.datetime.now(
                                                    ).strftime(
                                                        "%Y-%m-%d %H:%M:%S"),
                                                    datas[i].call_id,
                                                    datas[i].paragraph_id,
                                                    map_rule_name,
                                                    warning_message)
                                            if warning_ding_talk:
                                                warn_ding_talk(
                                                    datetime.datetime.now(
                                                    ).strftime(
                                                        "%Y-%m-%d %H:%M:%S"),
                                                    datas[i].call_id,
                                                    datas[i].paragraph_id,
                                                    map_rule_name,
                                                    warning_ding_talk)
                                            if warning_apps:
                                                warn_apps(
                                                    datetime.datetime.now(
                                                    ).strftime(
                                                        "%Y-%m-%d %H:%M:%S"),
                                                    datas[i].call_id,
                                                    datas[i].paragraph_id,
                                                    map_rule_name,
                                                    warning_apps)
                                        try:
                                            to_db(datas[i].call_id,
                                                  datas[i].paragraph_id,
                                                  map_id, 1, None)
                                        except Exception as e:
                                            logger.error(
                                                traceback.format_exc())
                                break
                    else:
                        m = 1
                        sum_words = datas[i].len_text
                        while datas[i].task_id == datas[i - m].task_id:
                            if datas[i - m].role != 1:
                                m += 1
                                sum_words += datas[i - m].len_text
                            else:
                                re_result = re.search(re_str,
                                                      datas[i - m].text)
                                if (re_result is None) or (sum_words >= 8):
                                    j = 1
                                    while datas[i].task_id == datas[i +
                                                                    j].task_id:
                                        if datas[i + j].role != 1:
                                            j += 1
                                        else:
                                            if datas[
                                                    i +
                                                    j].paragraph_id not in hit_pid:
                                                hit_pid.append(
                                                    datas[i + j].paragraph_id)
                                                if warning_id:
                                                    warning_data = db_v1.session.query(
                                                        QiInfoWarning).filter(
                                                            QiInfoWarning.id ==
                                                            warning_id
                                                        ).first_or_404()
                                                    warning_message = warning_data[
                                                        'message']
                                                    warning_ding_talk = warning_data[
                                                        'ding_talk']
                                                    warning_apps = warning_data[
                                                        'apps']
                                                    if warning_message:
                                                        warn_message(
                                                            datetime.datetime.
                                                            now().strftime(
                                                                "%Y-%m-%d %H:%M:%S"
                                                            ),
                                                            datas[i].call_id,
                                                            datas[i].
                                                            paragraph_id,
                                                            map_rule_name,
                                                            warning_message)
                                                    if warning_ding_talk:
                                                        warn_ding_talk(
                                                            datetime.datetime.
                                                            now().strftime(
                                                                "%Y-%m-%d %H:%M:%S"
                                                            ),
                                                            datas[i].call_id,
                                                            datas[i].
                                                            paragraph_id,
                                                            map_rule_name,
                                                            warning_ding_talk)
                                                    if warning_apps:
                                                        warn_apps(
                                                            datetime.datetime.
                                                            now().strftime(
                                                                "%Y-%m-%d %H:%M:%S"
                                                            ),
                                                            datas[i].call_id,
                                                            datas[i].
                                                            paragraph_id,
                                                            map_rule_name,
                                                            warning_apps)
                                                try:
                                                    to_db(
                                                        datas[i + j].call_id,
                                                        datas[i +
                                                              j].paragraph_id,
                                                        map_id, 1, None)
                                                except Exception as e:
                                                    logger.error(
                                                        traceback.format_exc())
                                            break
                                break
Пример #6
0
def interruption_rule(call_datas, rule_id, map_id, warning_id, map_rule_name):
    session = db_v1.session()
    rule_result = session.query(
        QiInfoInterruption.cross_time,
        QiInfoInterruption.out_rule).filter_by(id=rule_id).all()
    if rule_result:
        datas = session.query(
            QiLabelParagraph.call_id, QiLabelParagraph.task_id,
            QiLabelParagraph.paragraph_id, QiLabelParagraph.start_time,
            QiLabelParagraph.end_time,
            func.char_length(QiLabelParagraph.text).label('len_text'),
            QiLabelParagraph.role).filter(
                QiLabelParagraph.call_id.in_(call_datas)).order_by(
                    "task_id", "start_time").all()
        hit_pid = []
        for i in range(1, len(datas)):
            if datas[i].task_id != datas[i - 1].task_id:
                continue
            if not datas[i].role is None:
                if (datas[i - 1].end_time -
                        datas[i].start_time) > rule_result[0].cross_time:
                    if datas[i].role == 1:
                        if (datas[i].len_text >= rule_result[0].out_rule) and (
                                datas[i].paragraph_id not in hit_pid):
                            hit_pid.append(datas[i].paragraph_id)
                            if warning_id:
                                warning_data = db_v1.session.query(
                                    QiInfoWarning).filter(
                                        QiInfoWarning.id ==
                                        warning_id).first_or_404()
                                warning_message = warning_data['message']
                                warning_ding_talk = warning_data['ding_talk']
                                warning_apps = warning_data['apps']
                                if warning_message:
                                    warn_message(
                                        datetime.datetime.now().strftime(
                                            "%Y-%m-%d %H:%M:%S"),
                                        datas[i].call_id,
                                        datas[i].paragraph_id, map_rule_name,
                                        warning_message)
                                if warning_ding_talk:
                                    warn_ding_talk(
                                        datetime.datetime.now().strftime(
                                            "%Y-%m-%d %H:%M:%S"),
                                        datas[i].call_id,
                                        datas[i].paragraph_id, map_rule_name,
                                        warning_ding_talk)
                                if warning_apps:
                                    warn_apps(
                                        datetime.datetime.now().strftime(
                                            "%Y-%m-%d %H:%M:%S"),
                                        datas[i].call_id,
                                        datas[i].paragraph_id, map_rule_name,
                                        warning_apps)
                            try:
                                to_db(datas[i].call_id, datas[i].paragraph_id,
                                      map_id, 1, None)
                            except Exception as e:
                                logger.error(traceback.format_exc())

                    else:
                        if (datas[i - 1].len_text >= rule_result[0].out_rule
                            ) and (datas[i - 1].paragraph_id not in hit_pid):
                            hit_pid.append(datas[i - 1].paragraph_id)
                            if warning_id:
                                warning_data = db_v1.session.query(
                                    QiInfoWarning).filter(
                                        QiInfoWarning.id ==
                                        warning_id).first_or_404()
                                warning_message = warning_data['message']
                                warning_ding_talk = warning_data['ding_talk']
                                warning_apps = warning_data['apps']
                                if warning_message:
                                    warn_message(
                                        datetime.datetime.now().strftime(
                                            "%Y-%m-%d %H:%M:%S"),
                                        datas[i].call_id,
                                        datas[i].paragraph_id, map_rule_name,
                                        warning_message)
                                if warning_ding_talk:
                                    warn_ding_talk(
                                        datetime.datetime.now().strftime(
                                            "%Y-%m-%d %H:%M:%S"),
                                        datas[i].call_id,
                                        datas[i].paragraph_id, map_rule_name,
                                        warning_ding_talk)
                                if warning_apps:
                                    warn_apps(
                                        datetime.datetime.now().strftime(
                                            "%Y-%m-%d %H:%M:%S"),
                                        datas[i].call_id,
                                        datas[i].paragraph_id, map_rule_name,
                                        warning_apps)
                            try:
                                to_db(datas[i - 1].call_id,
                                      datas[i - 1].paragraph_id, map_id, 1,
                                      None)
                            except Exception as e:
                                logger.error(traceback.format_exc())
Пример #7
0
def speed_rule(call_datas, rule_id, map_id, warning_id, map_rule_name):
    session = db_v1.session()
    rule_result = session.query(
        QiInfoSpeed.rule_type, QiInfoSpeed.speed_counts,
        QiInfoSpeed.out_rule).filter_by(id=rule_id, is_deleted=0).all()
    if rule_result:
        datas = session.query(
            QiLabelParagraph.paragraph_id, QiLabelParagraph.call_id,
            QiLabelParagraph.task_id, QiLabelParagraph.duration,
            func.char_length(QiLabelParagraph.text).label('len_text')).filter(
                and_(
                    QiLabelParagraph.role == 1,
                    func.char_length(QiLabelParagraph.text) >=
                    rule_result[0].out_rule,
                    QiLabelParagraph.call_id.in_(call_datas))).all()
        hit_pid = []
        # hit_tid = []
        if rule_result[0].rule_type == 1:  # 小于
            for data in datas:
                if (data.len_text /
                        data.duration) * 60000 < rule_result[0].speed_counts:
                    hit_pid.append(data.paragraph_id)
                    if warning_id:
                        warning_data = db_v1.session.query(
                            QiInfoWarning).filter(
                                QiInfoWarning.id == warning_id).first_or_404()
                        warning_message = warning_data['message']
                        warning_ding_talk = warning_data['ding_talk']
                        warning_apps = warning_data['apps']
                        if warning_message:
                            warn_message(
                                datetime.datetime.now().strftime(
                                    "%Y-%m-%d %H:%M:%S"), data.call_id,
                                data.paragraph_id, map_rule_name,
                                warning_message)
                        if warning_ding_talk:
                            warn_ding_talk(
                                datetime.datetime.now().strftime(
                                    "%Y-%m-%d %H:%M:%S"), data.call_id,
                                data.paragraph_id, map_rule_name,
                                warning_ding_talk)
                        if warning_apps:
                            warn_apps(
                                datetime.datetime.now().strftime(
                                    "%Y-%m-%d %H:%M:%S"), data.call_id,
                                data.paragraph_id, map_rule_name, warning_apps)
                    try:
                        to_db(data.call_id, data.paragraph_id, map_id, 1, None)
                    except Exception as e:
                        logger.error(traceback.format_exc())
                    # hit_tid.append(data.task_id)
        else:  # 超过
            for data in datas:
                if (data.len_text /
                        data.duration) * 60000 > rule_result[0].speed_counts:
                    hit_pid.append(data.paragraph_id)
                    if warning_id:
                        warning_data = db_v1.session.query(
                            QiInfoWarning).filter(
                                QiInfoWarning.id == warning_id).first_or_404()
                        warning_message = warning_data['message']
                        warning_ding_talk = warning_data['ding_talk']
                        warning_apps = warning_data['apps']
                        if warning_message:
                            warn_message(
                                datetime.datetime.now().strftime(
                                    "%Y-%m-%d %H:%M:%S"), data.call_id,
                                data.paragraph_id, map_rule_name,
                                warning_message)
                        if warning_ding_talk:
                            warn_ding_talk(
                                datetime.datetime.now().strftime(
                                    "%Y-%m-%d %H:%M:%S"), data.call_id,
                                data.paragraph_id, map_rule_name,
                                warning_ding_talk)
                        if warning_apps:
                            warn_apps(
                                datetime.datetime.now().strftime(
                                    "%Y-%m-%d %H:%M:%S"), data.call_id,
                                data.paragraph_id, map_rule_name, warning_apps)
                    try:
                        to_db(data.call_id, data.paragraph_id, map_id, 1, None)
                    except Exception as e:
                        logger.error(traceback.format_exc())