Exemple #1
0
def group_item_service_func(request):
    group_id = request.matchdict['group_id']
    item_id = request.matchdict['item_id']
    gi = GroupItem(group_id, item_id, 0, 0)
    DBSession.add(gi)
    req = Request.blank('/api/v1/di/' + item_id)
    return request.invoke_subrequest(req)
Exemple #2
0
def view_item_service_func(request):
    view_id = request.matchdict['view_id']
    container_id = request.matchdict['container_id']
    item_id = request.matchdict['item_id']
    ci = ContainerItem(container_id, item_id, '')
    DBSession.add(ci)
    req = Request.blank('/api/v1/view/' + view_id + '/container/' + container_id)
    return request.invoke_subrequest(req)
Exemple #3
0
 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)
Exemple #4
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)
Exemple #5
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)
Exemple #6
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()
Exemple #7
0
 def collection_post(self):
     v = self._request.validated
     v['id_container']=str(uuid4())
     v['id_view'] = self._view_id
     cnt = Container(v['id_container'], v['id_view'], v['type'], '', v['x'], v['y'], v['width'], v['height'], v['z'], v['data'])
     DBSession.add(cnt)
     for item in v['item_ids']:
         ci = ContainerItem(v['id_container'], item, '{}')
         DBSession.add(ci)
     sr = Request.blank('/api/v1/view/' + self._view_id + '/container/' + cnt.id_container)
     return self._request.invoke_subrequest(sr)
Exemple #8
0
 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)
Exemple #9
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)
Exemple #10
0
 def collection_post(self):
     item_id = self._request.matchdict['item_id']
     try:
         v = self._request.validated
         link = Link(str(uuid4()), item_id, v['id_user'], v['type'],
                     v['description'], v['uri'])
         link = Link(str(uuid4()), item_id, v['id_user'], v['type'],
                     v['description'], v['uri'])
         DBSession.add(link)
         DBSession.flush()
         return link.get_dict()
     except Exception as e:
         print(str(e))
         return HTTPBadRequest
Exemple #11
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()
Exemple #12
0
    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
        print(v)
        metakeys = [m['key'] for m in v['metadata']]
        print(metakeys)

        missing_metakeys = check_missing_metakeys(metakeys, 'i')
        if len(missing_metakeys) > 0:
            raise HTTPBadRequest('missing metadata keys: %s' %
                                 ', '.join(missing_metakeys))

        di = DigitalItem(v['id_item'], v['mime'], v['created'], '',
                         v['id_user'], v['rights'])

        update_metadata(v['metadata'], di.id_item, 'item')
        DBSession.add(di)
        DBSession.flush()

        for link in v['links']:
            l = Link(str(uuid4()), di.id_item, link['id_user'], link['type'],
                     link['description'], link['uri'])
            DBSession.add(l)

        for group_id in v['group_ids']:
            print('Adding to group %s' % group_id)
            group_item = GroupItem(group_id, di.id_item, 0, 0)
            DBSession.add(group_item)

        print(di)
        return APIResponse(self._add_metadata(di))
Exemple #13
0
def set_di_mods(request):
    di_id = request.matchdict['id']
    try:
        mods = parse_mods(request.body.decode('utf8'))
        print(mods)
        new_meta = [{'key': key, 'value': mods[key]} for key in mods]
        update_metadata(new_meta, di_id, 'item', False)
        DBSession.flush()
        subreq = Request.blank('/api/v1/di/' + di_id)
        response = request.invoke_subrequest(subreq)
        return response
    except Exception as e:
        print('Exception %s' % str(e))
        raise HTTPBadRequest()
Exemple #14
0
 def collection_post(self):
     v = self._request.validated
     v['id_user'] = self._request.user.id_user
     type = 'annotation'
     if v['time'] > 0 or v['duration'] > 0:
         if v['duration'] > 0:
             type = '%s;%d;%d' % (type, v['time'], v['duration'])
         else:
             type = '%s;%d' % (type, v['time'])
     link = Link(str(uuid4()), v['id_item'], v['id_user'], type, '',
                 v['uri'])
     DBSession.add(link)
     DBSession.flush()
     return APIResponse(link.get_dict())
Exemple #15
0
 def put(self):
     dg_id = self._request.matchdict['id']
     try:
         group = DBSession.query(DIGroup).filter(
             DIGroup.id_group == dg_id).one()
         group.type = self._request.validated['type']
         update_metadata(self._request.validated['metadata'],
                         group.id_group, 'group')
         DBSession.flush()
         # return APIResponse(self._add_metadata(group))
         return APIResponse(
             add_metadata_record(group.get_dict(), group.id_group, 'group'))
     except Exception as e:
         print(e)
         raise HTTPNotFound()
Exemple #16
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()
Exemple #17
0
    def put(self):
        user = self._verify_access()
        v = self._request.validated
        if self._request.user.auth_level < 3:
            v['auth_level'] = user.auth_level
        print(v)
        user.set_from_dict(v)

        # set_dict() doesn't set password, so let's hash and update it manually
        new_pw =  v.get('passwd', '')
        if new_pw:
            user.passwd = hash_pw(new_pw)

        DBSession.flush()
        return APIResponse(user.get_dict())
Exemple #18
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()
Exemple #19
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)
Exemple #20
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)
Exemple #21
0
def view_item_service_func(request):
    view_id = request.matchdict['view_id']
    item_id = request.matchdict['item_id']
    path = request.GET.get('path', None)
    if path == 'null':
        path = None
    vi = DBSession.query(ViewItem).filter(
        sqlalchemy.and_(ViewItem.id_item == item_id,
                        ViewItem.id_view == view_id)).one_or_none()
    if vi:
        vi.path = path
    else:
        vi = ViewItem(view_id, item_id, path)
        DBSession.add(vi)
    req = Request.blank('/api/v1/di/' + item_id)
    return request.invoke_subrequest(req)
Exemple #22
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()
Exemple #23
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()
Exemple #24
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)
Exemple #25
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
Exemple #26
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()
Exemple #27
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()
Exemple #28
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)
Exemple #29
0
 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()
Exemple #30
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()