Example #1
0
def import_users():
    responses = pd.read_csv(USERS_FILENAME)

    db.init_db()

    with db.db:
        for user in db.User.select():
            if user.username != responses["username"][user.id]:
                raise RuntimeError(
                    "Mismatch between database and response form")

        next_row = db.User.select().count()

        for i, row in responses[next_row:].iterrows():
            user = db.User.create(
                id=i,
                username=row["username"],
                rank=row["rank"],
                pp=row["pp"],
                play_time=row["play_time"],
                play_count=row["play_count"],
                history_completeness=row["history_completeness"])

        pending_users = [
            u for u in db.User.select().where(~fn.EXISTS(
                db.Replay.select(1).where(db.User.id == db.Replay.user)))
        ]

    return pending_users, responses
Example #2
0
def get_management_condition(model: type):
    auth_info: AuthInfo = g.auth_info
    if auth_info.role == AuthRoleType.admin:
        return True
    elif auth_info.role == AuthRoleType.manager:
        manager: Manager = auth_info.obj

        if issubclass(model, Admin):
            return False
        elif issubclass(model, Building):
            return model.id == manager.building
        elif issubclass(model, Department):
            return False
        elif issubclass(model, Dormitory):
            return model.building == manager.building
        elif issubclass(model, ElectricityMeter):
            DM = Dormitory.alias()
            return fn.EXISTS(
                DM.select().where((DM.building == manager.building)
                                  & (DM.electricity_meter == model.id)))
        elif issubclass(model, ElectricityBill):
            DM = Dormitory.alias()
            return fn.EXISTS(DM.select().where(
                (DM.building == manager.building)
                & (DM.electricity_meter == model.electricity_meter)))
        elif issubclass(model, WaterMeter):
            DM = Dormitory.alias()
            return fn.EXISTS(
                DM.select().where((DM.building == manager.building)
                                  & (DM.water_meter == model.id)))
        elif issubclass(model, WaterBill):
            DM = Dormitory.alias()
            return fn.EXISTS(
                DM.select().where((DM.building == manager.building)
                                  & (DM.water_meter == model.water_meter)))
        elif issubclass(model, Manager):
            return False
        elif issubclass(model, Student):
            DM = Dormitory.alias()
            return fn.EXISTS(
                DM.select().where((DM.building == manager.building)
                                  & (model.dormitory == DM.id)))
        else:
            assert False
    else:
        return False
Example #3
0
def api_feature(pid):
    user = get_user()
    project = Project.get(Project.id == pid)
    if user and request.method == 'POST' and project.can_validate:
        ref_and_audit = request.get_json()
        if ref_and_audit and len(ref_and_audit) == 2:
            skipped = ref_and_audit[1] is None
            feat = Feature.get(Feature.project == project, Feature.ref == ref_and_audit[0])
            user_did_it = Task.select(Task.id).where(
                Task.user == user, Task.feature == feat).count() > 0
            Task.create(user=user, feature=feat, skipped=skipped)
            if not skipped:
                if len(ref_and_audit[1]):
                    new_audit = json.dumps(ref_and_audit[1], sort_keys=True, ensure_ascii=False)
                else:
                    new_audit = None
                if feat.audit != new_audit:
                    feat.audit = new_audit
                    feat.validates_count = 1
                elif not user_did_it:
                    feat.validates_count += 1
                feat.save()
    fref = request.args.get('ref')
    if fref:
        feature = Feature.get(Feature.project == project, Feature.ref == fref)
    elif not user or request.args.get('browse') == '1':
        feature = Feature.select().where(Feature.project == project).order_by(fn_Random()).get()
    else:
        try:
            # Maybe use a join: https://stackoverflow.com/a/35927141/1297601
            task_query = Task.select(Task.id).where(Task.user == user, Task.feature == Feature.id)
            query = Feature.select().where(
                Feature.project == project, Feature.validates_count < 2).where(
                    ~fn.EXISTS(task_query)).order_by(fn_Random())
            if project.validate_modified:
                query = query.where(Feature.action == 'm')
            if user.bboxes:
                bboxes = BBoxes(user)
                feature = None
                for f in query:
                    if bboxes.contains(f.lat/1e7, f.lon/1e7):
                        feature = f
                        break
                    elif not feature:
                        feature = f
                if not feature:
                    raise Feature.DoesNotExist()
            else:
                feature = query.get()
        except Feature.DoesNotExist:
            return jsonify(feature={}, ref=None, audit=None)
    return jsonify(feature=json.loads(feature.feature), ref=feature.ref,
                   audit=json.loads(feature.audit or 'null'))
Example #4
0
def get_self_condition(model: type):
    auth_info: AuthInfo = g.auth_info
    if auth_info.role == AuthRoleType.admin:
        admin: Admin = auth_info.obj
        if issubclass(model, Admin):
            return model.id == admin.id
        else:
            return False
    if auth_info.role == AuthRoleType.manager:
        manager: Manager = auth_info.obj
        if issubclass(model, Manager):
            return model.id == manager.id
        else:
            return False
    elif auth_info.role == AuthRoleType.student:
        student: Student = auth_info.obj

        if issubclass(model, Building):
            DM = Dormitory.alias()
            return fn.EXISTS(DM.select().where((DM.building == model.id)
                                               & (DM.id == student.dormitory)))
        elif issubclass(model, Department):
            return False
        elif issubclass(model, Dormitory):
            return model.id == student.dormitory
        elif issubclass(model, ElectricityMeter):
            DM = Dormitory.alias()
            return fn.EXISTS(
                DM.select().where((DM.electricity_meter == model.id)
                                  & (DM.id == student.dormitory)))
        elif issubclass(model, ElectricityBill):
            DM = Dormitory.alias()
            return fn.EXISTS(DM.select().where(
                (DM.electricity_meter == model.electricity_meter)
                & (DM.id == student.dormitory)))
        elif issubclass(model, WaterMeter):
            DM = Dormitory.alias()
            return fn.EXISTS(DM.select().where((DM.water_meter == model.id)
                                               & (DM.id == student.dormitory)))
        elif issubclass(model, ElectricityBill):
            DM = Dormitory.alias()
            return fn.EXISTS(
                DM.select().where((DM.electricity_meter == model.water_meter)
                                  & (DM.id == student.dormitory)))
        elif issubclass(model, Manager):
            DM = Dormitory.alias()
            return fn.EXISTS(DM.select().where((DM.building == model.building)
                                               & (DM.id == student.dormitory)))
        elif issubclass(model, Student):
            return model.id == student.id
        else:
            assert False
    elif auth_info.role == AuthRoleType.anonymous:
        return False
def get_similar_not_in_table(table, max=20, only_dim=None, only_sep=False, no_particle=False, no_divsum=False,
                             no_mixed=True):
    for cls, field_name in [(Network, 'network'),
                (ComboNetwork, 'combo_network'),
                (MultiNetwork, 'multi_network')
                ]:
        non_sliced = (cls
                      .select()
                      .where(~fn.EXISTS(table.select().where(getattr(table, field_name) == cls.id)))
                     )
        if only_dim is not None:
            non_sliced &= cls.select().where(SQL("array_length(feature_names, 1)=" + str(only_dim)))

        if no_mixed:
            non_sliced &= cls.select().where(~(SQL("(array_to_string(target_names, ',') like %s)", ['%pf%']) &
                                              (SQL("(array_to_string(target_names, ',') like %s)", ['%ef%'])))
                                             )
        tags = []
        if no_divsum is True:
            tags.extend(["div", "plus"])
        if no_particle is True:
            tags.append('pf')
        if len(tags) != 0:
            non_sliced &= no_elements_in_list(cls, 'target_names', tags)
        if only_sep is True:
            non_sliced &= any_element_in_list(cls, 'target_names', ['TEM', 'ITG', 'ETG'])
        if non_sliced.count() > 0:
            network = non_sliced.get()
            break

    non_sliced &= (cls.select()
                  .where(cls.target_names == AsIs(network.target_names))
                  .where(cls.feature_names == AsIs(network.feature_names))
                  )
    non_sliced = non_sliced.limit(max)
    return non_sliced
Example #6
0
    def _build_filter_expression(key, values):
        def in_(lhs, rhs):
            return lhs << ([i.strip() for i in rhs.split(',')])

        operations = {
            'eq': operator.eq,
            'ne': operator.ne,
            'ge': operator.ge,
            'gt': operator.gt,
            'le': operator.le,
            'lt': operator.lt,
            'in': in_,
            'contains': lambda l, r: operator.pow(l, '%%%s%%' % r),
            'startswith': lambda l, r: operator.pow(l, '%s%%' % r),
            'endswith': lambda l, r: operator.pow(l, '%%%s' % r),
            'regex': lambda l, r: l.regexp(r),
        }
        if key.find('__') != -1:
            key, op = key.rsplit('__', 1)
            if op not in operations:
                error('Unrecognized operation: %s. Supported operations are:'
                      '\n%s' % (op, '\n'.join(sorted(operations.keys()))))
        else:
            op = 'eq'

        op = operations[op]
        if isinstance(values, (list, tuple)):
            expr = reduce(operator.or_,
                          [((Metadata.key == key) & op(Metadata.value, value))
                           for value in values])
        else:
            expr = ((Metadata.key == key) & op(Metadata.value, values))

        return fn.EXISTS(
            Metadata.select().where(expr
                                    & (Metadata.document == Document.docid)))
Example #7
0
def emptybusinesscheck():
    """获取没有联系人的业务,并同时发给系统管理员和对应的业务管理员"""

    # 获取空联系人业务信息,如果结果为空,则退出后续检查
    subq = BusinessNotifier.select().where(BusinessNotifier.business_id == Business.id)
    query1 = Business.select().where((~fn.EXISTS(subq)) & (Business.status == True))
    if not query1:
        return

    # 获取管理员列表
    query2 = User.select().where((User.admin == True) & (User.status == True))

    # 列表生成(系统管理员)
    # stridlist用作和perm_list进行交叉比对
    stridlist = []
    strlist = []
    infolist = []

    subject = 'Empty Business - SystemAdmin'

    for i in query1:
        biz = str(i.id) + ' : ' + i.business_name
        strlist.append(biz)
        bizid = str(i.id)
        stridlist.append(bizid)
    mailstring = "\n".join(strlist)

    for item in query2:
        subinfolist = []
        notifier = item.email
        subinfolist.append(notifier)
        subinfolist.append(mailstring)
        subinfolist.append(subject)
        infolist.append(subinfolist)

    # 发送告警信息给系统管理员
    send_email(infolist)

    # 列表生成(业务管理员)
    strlist = []
    infolist = []

    subject = 'Empty Business - BizAdmin'

    user_perm_list = Permission.select().join(User)\
        .where((Permission.perm_list != '0') & (Permission.perm_list != '') & (User.status == 1))
    for item in user_perm_list:
        user_perm_list_item = item.perm_list.split()
        # perm_list和无联系人业务id列表进行交叉比对
        business_intersection = [x for x in user_perm_list_item if x in set(stridlist)]
        if business_intersection:
            subinfolist = []
            notifier = item.perm_user.email
            subinfolist.append(notifier)
            # 根据id获取业务名称作为邮件正文
            for perm_item in business_intersection:
                biz = Business.select().where(Business.id == perm_item).get().business_name
                strlist.append(biz)
            mailstring = "\n".join(strlist)
            subinfolist.append(mailstring)
            subinfolist.append(subject)
            infolist.append(subinfolist)

    # 发送告警信息给业务管理员
    send_email(infolist)