Example #1
0
    def put(self):
        set_id = self._request.matchdict['id']
        try:
            dset = DBSession.query(DigitalSet).filter(
                DigitalSet.id_set == set_id).one()
            dset.set_from_dict(self._request.validated)

            old_groups = DBSession.query(GroupSet).filter(
                GroupSet.id_set == set_id).all()
            old_ids = [gs.id_group for gs in old_groups]
            new_ids = [
                g['id_group'] for g in self._request.validated['groups']
            ]
            new_groups = [
                GroupSet(set_id, gid) for gid in new_ids if not gid in old_ids
            ]
            for new_group in new_groups:
                DBSession.add(new_group)

            for old_group in old_groups:
                if old_group.id_group not in new_ids:
                    DBSession.delete(old_group)

            # metadata = [x for x in DBSession.query(Metadata).filter(Metadata.id == dset.id_set).all()]
            update_metadata(self._request.validated['metadata'], dset.id_set,
                            'set')
            DBSession.flush()
            return APIResponse(
                add_metadata_record(dset.get_dict(), dset.id_set, 'set'))

        except Exception as e:
            print(e)
            raise HTTPNotFound()
Example #2
0
 def delete(self):
     container_id = self._request.matchdict['container_id']
     cnt = DBSession.query(Container).filter(Container.id_view == self._view_id).filter(Container.id_container == container_id).one()
     cntitems = DBSession.query(ContainerItem).filter(ContainerItem.id_container == container_id).all()
     for itm in cntitems:
         DBSession.delete(cnt)
     DBSession.delete(cnt)
     DBSession.flush()
     return APIResponse(True)
Example #3
0
 def put(self):
     v = self._request.validated
     container_id = self._request.matchdict['container_id']
     cnt = DBSession.query(Container).filter(sqlalchemy.and_(Container.id_container == container_id, Container.id_view == self._view_id)).one()
     cnt.set_from_dict(v)
     sr = Request.blank('/api/v1/view/' + self._view_id + '/container/' + cnt.id_container)
     return self._request.invoke_subrequest(sr)
Example #4
0
 def get(self):
     key = self._request.matchdict['key']
     try:
         metakey = DBSession.query(MetaKey).filter(MetaKey.key == key).one()
         return APIResponse(metakey.get_dict())
     except:
         raise HTTPNotFound()
Example #5
0
    def collection_get(self):
        if not self._root:
            raise HTTPServerError()

        prefix = 'storage:'
        used_paths = [x[0][len(prefix):] for x in DBSession.query(Link.uri).filter(Link.type == 'data').all() if
                      x[0].startswith(prefix)]
        if self._request.GET.get('dirs', '0') == '1':
            directories = {}
            for root, dirs, files in os.walk(self._root):
                d = directories
                for dd in self._strip_path(root).split('/')[1:]:
                    d = d['dirs'][dd]
                d['dirs'] = {dd: {} for dd in dirs}
                d['file_count'] = len(files)
                d['new_files'] = len([x for x in files if not self._strip_path(os.path.join(root, x)) in used_paths])

            return APIResponse(directories)

        print(self._request.matchdict['path'])
        req_path = os.path.join(self._root, *self._request.matchdict['path'])
        print(req_path)

        r, d, f = next(os.walk(req_path))
        res = {'files': [self._process_file(file, os.path.join(r, file), used_paths) for file in f if not file.endswith('.mods.xml')],
               'path': self._strip_path(r),
               'dirs': d}
        print(res)

        return APIResponse(res)
Example #6
0
    def collection_get(self):
        return APIResponse([x.get_dict() for x in DBSession.query(MetaKey).all()])



    # @view(permission=Everyone)
    # def collection_get(self):
    #     return [x.get_dict() for x in DBSession.query(DigitalItem).all()]
    #
    # @view(permission=Everyone, schema = DigitalItemSchema, validators=(colander_body_validator,))
    # def collection_post(self):
    #     self._request.validated['id_item'] = str(uuid4())
    #     self._request.validated['created'] = datetime.datetime.now()
    #     #di = DigitalItem(**self._request.validated)
    #     v = self._request.validated
    #     di = DigitalItem(v['id_item'], v['mime'], v['created'], '', v['author'], v['rights'])
    #     print(di)
    #
    #     required_keys = DBSession.query(MetaKey).filter(MetaKey.mandatory.contains('i'))
    #     print('Required keys: %s' % (','.join((x.key for x in required_keys))))
    #
    #     DBSession.add(di)
    #     DBSession.flush()
    #     print(di)
    #     return di.get_dict()
Example #7
0
def view_item_service_func2(request):
    view_id = request.matchdict['view_id']
    item_id = request.matchdict['item_id']
    vi = DBSession.query(ViewItem).filter(
        sqlalchemy.and_(ViewItem.id_view == view_id,
                        ViewItem.id_item == item_id)).one()
    usages = DBSession.query(Container, ContainerItem)\
        .join(ContainerItem, ContainerItem.id_container == Container.id_container)\
        .filter(ContainerItem.id_item == item_id)\
        .filter(Container.id_view == view_id)\
        .count()
    if usages > 0:
        raise HTTPBadRequest()

    DBSession.delete(vi)
    req = Request.blank('/api/v1/view/' + view_id)
    return request.invoke_subrequest(req)
Example #8
0
 def _get_items(self, view):
     bundles = [x for x in
                     DBSession.query(DigitalItem, ViewItem) \
                         .join(ViewItem, ViewItem.id_item == DigitalItem.id_item) \
                         .filter(ViewItem.id_view == view.id_view) \
                         .all()]
     items = []
     for bundle in bundles:
         item = bundle[0].get_dict()
         item['metadata'] = [meta_record(x[1], x[0]) for x in
                            DBSession.query(Metadata, MetaKey) \
                                .join(MetaKey, Metadata.key == MetaKey.key) \
                                .filter(sqlalchemy.and_(Metadata.id == item['id_item'], Metadata.target == 'item')) \
                                .all()]
         item['path'] = bundle[1].path
         items.append(item)
     return items
Example #9
0
 def get(self):
     # item_id = self._request.matchdict['item_id']
     view_id = self._request.matchdict['view_id']
     try:
         view_info = DBSession.query(View).filter(
             View.id_view == view_id).one()
         res = add_metadata_record(view_info.get_dict(), view_info.id_view,
                                   'view')
         res['items'] = self._get_items(view_info)
         res['containers'] = [
             x.get_dict() for x in DBSession.query(Container).filter(
                 Container.id_view == view_id).all()
         ]
         return APIResponse(res)
     except Exception as e:
         print(e)
         raise HTTPNotFound()
Example #10
0
    def collection_post(self):
        key = self._request.validated['key']
        item_id = self._request.matchdict['id']
        try:
            # Let's check if the id/key pair is already present. If it is, then bail out
            res = DBSession.query(Metadata).filter(Metadata.id == item_id).filter(Metadata.key == key).count()
            if res:
                print('key already present...')
                raise HTTPBadRequest()
            # We're adding new metadata, so let's check whether the key is already present in metaKey table
            meta_key = [x for x in DBSession.query(MetaKey).filter(MetaKey.key == key).all()]
            if len(meta_key) < 1:
                # nope, so let's create it
                meta_key = MetaKey(key, 'string', '', '')
                DBSession.add(meta_key)

            metadata = Metadata(item_id, 'item', key, self._request.validated['value'])
            DBSession.add(metadata)
            DBSession.flush()
            return metadata.get_dict()
        except Exception as e:
            print(str(e))
            raise HTTPBadRequest()



                    # @view(permission=Everyone)
    # def collection_get(self):
    #     return [x.get_dict() for x in DBSession.query(DigitalItem).all()]
    #
    # @view(permission=Everyone, schema = DigitalItemSchema, validators=(colander_body_validator,))
    # def collection_post(self):
    #     self._request.validated['id_item'] = str(uuid4())
    #     self._request.validated['created'] = datetime.datetime.now()
    #     #di = DigitalItem(**self._request.validated)
    #     v = self._request.validated
    #     di = DigitalItem(v['id_item'], v['mime'], v['created'], '', v['author'], v['rights'])
    #     print(di)
    #
    #     required_keys = DBSession.query(MetaKey).filter(MetaKey.mandatory.contains('i'))
    #     print('Required keys: %s' % (','.join((x.key for x in required_keys))))
    #
    #     DBSession.add(di)
    #     DBSession.flush()
    #     print(di)
    #     return di.get_dict()
Example #11
0
File: auth.py Project: iimcz/emod
 def delete(self):
     if not self._request.token:
         # this should never happen
         raise HTTPUnauthorized
     tok = DBSession.query(Token).filter(
         Token.id_token == self._request.token).one()
     DBSession.delete(tok)
     return APIResponse(None)
Example #12
0
 def collection_get(self):
     item_id = self._request.matchdict['id']
     try:
         res = DBSession.query(Metadata).filter(Metadata.id == item_id).all()
         return [r.get_dict() for r in res]
     except Exception as e:
         print(str(e))
         raise HTTPNotFound()
Example #13
0
 def get(self):
     item_id = self._request.matchdict['id']
     key = self._request.matchdict['key']
     try:
         res = DBSession.query(Metadata).filter(Metadata.key == key).filter(Metadata.id == item_id).one()
         return res.get_dict()
     except:
         raise HTTPNotFound()
Example #14
0
File: link.py Project: iimcz/emod
 def get(self):
     # item_id = self._request.matchdict['item_id']
     link_id = self._request.matchdict['link_id']
     try:
         res = DBSession.query(Link).filter(Link.id_link == link_id).one()
         return res.get_dict()
     except:
         raise HTTPNotFound()
Example #15
0
 def delete(self):
     annotation_id = self._request.matchdict['annot_id']
     q = DBSession.query(Link).filter(Link.id_link == annotation_id)
     if self._request.user.auth_level < RIGHTLevels.Editor:
         q = q.filter(Link.id_user == self._request.user.id_user)
     link = q.one()
     DBSession.delete(link)
     DBSession.flush()
     return APIResponse(True)
Example #16
0
    def get(self):
        set_id = self._request.matchdict['id']
        try:
            dset = DBSession.query(DigitalSet).filter(
                DigitalSet.id_set == set_id).one()
            out_dict = dset.get_dict()

            groups = DBSession.query(GroupSet, DIGroup)\
                .join(DIGroup, GroupSet.id_group == DIGroup.id_group)\
                .filter(GroupSet.id_set == set_id)\
                .all()

            out_dict['groups'] = [g[1].get_dict() for g in groups]
            return APIResponse(
                add_metadata_record(out_dict, dset.id_set, 'set'))
        except Exception as e:
            print(e)
            raise HTTPNotFound()
Example #17
0
File: user.py Project: iimcz/emod
 def collection_get(self):
     params = get_list_params(self._request)
     users = DBSession.query(User)
     if len(params.query_keys) > 0:
         users = users.filter(sqlalchemy.and_(
             *[sqlalchemy.or_(User.id_user.value.contains(key), User.username.contains(key), User.fullname.contains(key)) for key in params.query_keys]))
     if params.dry:
         return APIResponse(users.count())
     users = users.offset(params.offset).limit(params.limit)
     return APIResponse([x.get_dict() for x in users.all()])
Example #18
0
File: user.py Project: iimcz/emod
 def _verify_access(self):
     '''
     Verifies that logged user has rights to edit/view specified user
     :return:
     '''
     id_user = self._request.matchdict['id_user']
     if self._request.user.id_user != id_user and self._request.user.auth_level < 3:
         # User is requesting data about different user and is not an admin. That is no-no
         raise HTTPUnauthorized()
     return DBSession.query(User).filter(User.id_user == id_user).one()
Example #19
0
def group_item_service_func_del(request):
    group_id = request.matchdict['group_id']
    item_id = request.matchdict['item_id']
    gi = DBSession.query(GroupItem).filter(
        sqlalchemy.and_(GroupItem.id_group == group_id,
                        GroupItem.id_item == item_id)).one()
    DBSession.delete(gi)
    DBSession.flush()
    req = Request.blank('/api/v1/dig/' + group_id)
    return request.invoke_subrequest(req)
Example #20
0
 def get(self):
     print(DigitalItemSchema)
     di_id = self._request.matchdict['id']
     try:
         item = DBSession.query(DigitalItem).filter(
             DigitalItem.id_item == di_id).one()
         return APIResponse(self._add_metadata(item))
     except Exception as e:
         print(e)
         raise HTTPNotFound()
Example #21
0
 def put(self):
     item_id = self._request.matchdict['id']
     key = self._request.matchdict['key']
     try:
         res = DBSession.query(Metadata).filter(Metadata.key == key).filter(Metadata.id == item_id).one()
         res.value = self._request.validated['value']
         DBSession.flush()
         return res.get_dict()
     except Exception as e:
         print(str(e))
         raise HTTPBadRequest()
Example #22
0
def download_resource(request):
    '''
    Expects requests for DI from storage. Optionally accepts params
    :param request:
    :return:
    '''
    print(request.matchdict['id'], request.matchdict['opts'])
    item_id = request.matchdict['id']
    opts = request.matchdict['opts']
    item_info = DBSession.query(DigitalItem, Link) \
        .filter(DigitalItem.id_item == request.matchdict['id']) \
        .join(Link, Link.id_item == DigitalItem.id_item).all()

    if len(item_info) == 0:
        raise HTTPNotFound()

    # TODO: Check access (somehow...)

    cfg = get_cfg(request.registry.settings, 'naki.storage.')
    root = cfg.get('root', None)
    prefix = 'storage:'
    path = None
    for item, link in item_info:
        print(link.uri)
        if link.uri.startswith(prefix):
            path = link.uri[len(prefix):]
            break

    if path is None:
        raise HTTPNotFound()
    if path.startswith('/'):
        path = path[1:]
    full_path = os.path.join(root, path)

    if 'thumbnail' in opts:
        item = item_info[0][0]
        thumb_dir = cfg.get('thumbnails', os.path.join(root, '.thumbnails'))
        os.makedirs(thumb_dir, exist_ok=True)
        thumb_path = os.path.join(thumb_dir, item_id + '.jpg')
        return prepare_image(request, item, (128, 128), full_path, thumb_path)
    elif 'preview' in opts:
        item = item_info[0][0]
        preview_dir = cfg.get('preview', os.path.join(root, '.preview'))
        os.makedirs(preview_dir, exist_ok=True)
        preview_path = os.path.join(preview_dir, item_id + '.jpg')
        return prepare_image(request, item, (1280, 720), full_path,
                             preview_path)

    resp = FileResponse(full_path, request=request)
    d, fname = os.path.split(full_path)
    resp.content_disposition = '%s; filename="%s"' % (
        'attachment' if 'download' in opts else ' inline', fname)
    return resp
Example #23
0
File: auth.py Project: iimcz/emod
 def post(self):
     data = self._request.validated
     user = DBSession.query(User).filter(
         User.username == data['username']).filter(
             User.passwd == hash_pw(data['password'])).one_or_none()
     if not user:
         raise HTTPUnauthorized()
     token = Token(str(uuid.uuid4()), user.id_user)
     DBSession.add(token)
     d = user.get_dict()
     d['token'] = token.id_token
     return APIResponse(d)
Example #24
0
    def collection_get(self):
        limit = self._request.GET.get('limit', 10)
        offset = self._request.GET.get('offset', 0)
        q = self._request.GET.get('q', '')
        dry = self._request.GET.get('dry', '0') == '1'
        query_keys = [x for x in q.split(' ') if len(x) > 0]
        subq = DBSession.query(DIGroup.id_group) \
            .outerjoin(Metadata, sqlalchemy.and_(Metadata.id == DIGroup.id_group, Metadata.target == 'group'))
        if len(query_keys) > 0:
            subq = subq.filter(
                sqlalchemy.and_(
                    *[Metadata.value.contains(key) for key in query_keys]))
        subq = subq.group_by(DIGroup.id_group)

        if dry:
            return APIResponse(subq.count())

        subq = subq.offset(offset).limit(limit).subquery('subq')

        groups_raw = DBSession.query(DIGroup, Metadata) \
            .outerjoin(Metadata, Metadata.id == DIGroup.id_group) \
            .join(subq, subq.c.sID_Group == DIGroup.id_group) \
            .all()

        groups = {}
        for bundle in groups_raw:
            gid = bundle[0].id_group
            if not gid in groups:
                groups[gid] = bundle[0].get_dict()
                groups[gid]['metadata'] = []
            group = groups[gid]
            if bundle[1]:
                if not next(
                    (x
                     for x in group['metadata'] if x['key'] == bundle[1].key),
                        None):
                    group['metadata'].append(bundle[1].get_dict())

        return APIResponse([groups[x] for x in groups])
Example #25
0
def view_item_service_func2(request):
    view_id = request.matchdict['view_id']
    container_id = request.matchdict['container_id']
    item_id = request.matchdict['item_id']
    ci = DBSession.query(ContainerItem, Container)\
        .join(Container, Container.id_container == ContainerItem.id_container)\
        .filter(Container.id_view == view_id)\
        .filter(sqlalchemy.and_(ContainerItem.id_container == container_id, ContainerItem.id_item == item_id))\
        .one()

    DBSession.delete(ci[0])
    req = Request.blank('/api/v1/view/' + view_id + '/container/' + container_id)
    return request.invoke_subrequest(req)
Example #26
0
def get_di_mods(request):
    di_id = request.matchdict['id']
    try:
        item = DBSession.query(DigitalItem).filter(
            DigitalItem.id_item == di_id).one()
        resp = request.response
        resp.status_int = 200
        resp.body = generate_mods(item, None).encode('utf8')
        resp.content_type = 'text/xml'
        return resp
    except Exception as e:
        print('Exception %s' % str(e))
        raise HTTPBadRequest()
Example #27
0
    def get(self):
        container_id = self._request.matchdict['container_id']
        items = DBSession.query(Container, ContainerItem)\
            .outerjoin(ContainerItem, ContainerItem.id_container == Container.id_container)\
            .filter(Container.id_view == self._view_id) \
            .filter(Container.id_container == container_id)\
            .all()

        if len(items) == 0:
            raise HTTPNotFound()

        ret = items[0][0].get_dict();
        ret['item_ids'] = [x[1].id_item for x in items] if items[0][1] else []
        return APIResponse(ret)
Example #28
0
    def get(self):
        dg_id = self._request.matchdict['id']
        dgs = [x for x in DBSession.query(DIGroup, DigitalItem) \
            .outerjoin(GroupItem, GroupItem.id_group == DIGroup.id_group) \
            .outerjoin(DigitalItem, DigitalItem.id_item == GroupItem.id_item) \
            .filter(DIGroup.id_group == dg_id) \
            .all()]
        if len(dgs) == 0:
            raise HTTPNotFound()

        dg = add_metadata_record(dgs[0][0].get_dict(), dgs[0][0].id_group,
                                 'group')
        meta = DBSession.query(Metadata, MetaKey) \
            .join(MetaKey, MetaKey.key == Metadata.key) \
            .filter(Metadata.target == 'item') \
            .filter(Metadata.id.in_([x[1].id_item for x in dgs if x[1]])) \
            .all()
        dg['items'] = []
        for bundle in dgs:
            if not bundle[1]:
                continue
            item = bundle[1].get_dict()
            item['metadata'] = [
                meta_record(x[1], x[0]) for x in meta
                if x[0].id == bundle[1].id_item
            ]
            dg['items'].append(item)
        if not self._request.user or self._request.user.auth_level < RIGHTLevels.Researcher:
            # Guests should see only public items!
            old_items = dg['items']
            dg['items'] = [
                x for x in old_items
                if self._get_key_value(x['metadata'], 'public') == '1'
            ]

        # dg['items'] = [x[1].get_dict() for x in dgs if x[1]]
        return APIResponse(dg)
Example #29
0
 def put(self):
     view_id = self._request.matchdict['view_id']
     try:
         q = DBSession.query(View).filter(View.id_view == view_id)
         if self._request.user.auth_level < RIGHTLevels.Editor:
             q = q.filter(View.id_user == self._request.user.id_user)
         view = q.one()
         view.set_from_dict(self._request.validated)
         update_metadata(self._request.validated['metadata'], view.id_view,
                         'view')
         DBSession.flush()
         return APIResponse(view.get_dict())
     except Exception as e:
         print(e)
         raise HTTPNotFound()
Example #30
0
 def put(self):
     di_id = self._request.matchdict['id']
     try:
         item = DBSession.query(DigitalItem).filter(
             DigitalItem.id_item == di_id).one()
         item.set_from_dict(self._request.validated)
         update_metadata(self._request.validated['metadata'], item.id_item,
                         'item')
         update_links(self._request.validated['links'], item.id_item)
         DBSession.flush()
         return APIResponse(self._add_metadata(item))
     except Exception as e:
         print(e)
         traceback.print_exc()
         raise HTTPNotFound()