Пример #1
0
def authz_lists(action):
    if action == 'read' and request.authz_lists.get('read') is None:
        request.authz_lists['read'] = List.user_list_ids(current_user)
    if action == 'write' and request.authz_lists.get('write') is None:
        request.authz_lists['write'] = List.user_list_ids(
            current_user, include_public=False)  # noqa
    return request.authz_lists[action] or []
Пример #2
0
def authz_lists(action):
    if action == 'read' and request.authz_lists.get('read') is None:
        request.authz_lists['read'] = List.user_list_ids(current_user)
    if action == 'write' and request.authz_lists.get('write') is None:
        request.authz_lists['write'] = List.user_list_ids(current_user,
            include_public=False) # noqa
    return request.authz_lists[action] or []
Пример #3
0
def load_fixture(name):
    dir_name = os.path.join(fixtures_path, name)
    if not os.path.isdir(dir_name):
        raise ValueError("No such directory: %r" % dir_name)

    with open(os.path.join(dir_name, 'mapping.yaml'), 'rb') as fh:
        data = yaml.load(fh)

    lst = List.by_label(data.get('list'))
    selectors = set()
    if lst is not None:
        selectors = lst.terms
        lst.delete()
        db.session.commit()

    lst = List.create(
        {
            'label': data.get('list'),
            'public': data.get('public'),
            'users': []
        }, None)
    log.info("Loading %r", lst)

    mapping = data.get('mapping')
    default_category = data.get('default_category')
    assert default_category in CATEGORIES, default_category

    entities = defaultdict(set)
    with open(os.path.join(dir_name, 'data.csv'), 'rb') as fh:
        for row in unicodecsv.DictReader(fh):
            label = row.get(mapping.get('label', 'label'))
            if label is None:
                continue

            category = row.get(mapping.get('category', 'category'))
            category = category or default_category

            selectors = [row.get(mapping.get('selector', 'selector'))]
            selectors = [s for s in selectors if s]
            entities[(label, category)].update(selectors)

    for (label, category), selectors in entities.items():
        data = {
            'label': label,
            'category': category,
            'selectors': selectors,
            'list': lst
        }
        try:
            Entity.create(data, None)
        except Invalid, inv:
            log.warn("Failed: %s", inv)
Пример #4
0
def update(id):
    authz.require(authz.list_write(id))
    lst = obj_or_404(List.by_id(id))
    lst.update(request_data(), current_user)
    db.session.add(lst)
    db.session.commit()
    return view(id)
Пример #5
0
def load_fixture(name):
    dir_name = os.path.join(fixtures_path, name)
    if not os.path.isdir(dir_name):
        raise ValueError("No such directory: %r" % dir_name)

    with open(os.path.join(dir_name, 'mapping.yaml'), 'rb') as fh:
        data = yaml.load(fh)

    lst = List.by_label(data.get('list'))
    selectors = set()
    if lst is not None:
        selectors = lst.terms
        lst.delete()
        db.session.commit()

    lst = List.create({
        'label': data.get('list'),
        'public': data.get('public'),
        'users': []
    }, None)
    log.info("Loading %r", lst)

    mapping = data.get('mapping')
    default_category = data.get('default_category')
    assert default_category in CATEGORIES, default_category

    entities = defaultdict(set)
    with open(os.path.join(dir_name, 'data.csv'), 'rb') as fh:
        for row in unicodecsv.DictReader(fh):
            label = row.get(mapping.get('label', 'label'))
            if label is None:
                continue

            category = row.get(mapping.get('category', 'category'))
            category = category or default_category

            selectors = [row.get(mapping.get('selector', 'selector'))]
            selectors = [s for s in selectors if s]
            entities[(label, category)].update(selectors)

    for (label, category), selectors in entities.items():
        data = {'label': label, 'category': category,
                'selectors': selectors, 'list': lst}
        try:
            Entity.create(data, None)
        except Invalid, inv:
            log.warn("Failed: %s", inv)
Пример #6
0
def delete(id):
    authz.require(authz.list_write(id))
    lst = obj_or_404(List.by_id(id))
    selectors = lst.terms
    lst.delete()
    db.session.commit()
    refresh_selectors.delay(list(selectors))
    return jsonify({'status': 'ok'})
Пример #7
0
def view(id):
    authz.require(authz.list_read(id))
    lst = obj_or_404(List.by_id(id))
    etag_cache_keygen(lst)
    data = lst.to_dict()
    data['can_write'] = authz.list_write(id)
    if data['can_write']:
        data['users'] = [u.id for u in lst.users]
    return jsonify(data)
Пример #8
0
def create():
    authz.require(authz.logged_in())
    data = request_data()
    data['creator'] = current_user
    if 'users' not in data:
        data['users'] = []
    lst = List.create(data, current_user)
    db.session.commit()
    return view(lst.id)
Пример #9
0
def index():
    q = List.all_by_user(current_user)
    data = Pager(q).to_dict()
    results = []
    for lst in data.pop('results'):
        ldata = lst.to_dict()
        ldata['can_write'] = authz.list_write(lst.id)
        results.append(ldata)
    data['results'] = results
    return jsonify(data)
Пример #10
0
def index():
    list_ids = List.user_list_ids(current_user)
    filter_lists = request.args.getlist("list")
    if len(filter_lists):
        try:
            filter_lists = [int(f) for f in filter_lists]
            list_ids = [l for l in list_ids if l in filter_lists]
        except ValueError:
            raise BadRequest()

    prefix = request.args.get("prefix")
    q = Entity.by_lists(list_ids, prefix=prefix)
    return jsonify(Pager(q))
Пример #11
0
def index():
    list_ids = List.user_list_ids(current_user)
    filter_lists = request.args.getlist('list')
    if len(filter_lists):
        try:
            filter_lists = [int(f) for f in filter_lists]
            list_ids = [l for l in list_ids if l in filter_lists]
        except ValueError:
            raise BadRequest()

    prefix = request.args.get('prefix')
    q = Entity.by_lists(list_ids, prefix=prefix)
    return jsonify(Pager(q))