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()
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()
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()
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()
def collection_post(self): self._request.validated['id_group'] = str(uuid4()) self._request.validated['created'] = datetime.datetime.now() v = self._request.validated print(v) metakeys = [m['key'] for m in v['metadata']] print(metakeys) missing_metakeys = check_missing_metakeys(metakeys, 'g') if len(missing_metakeys) > 0: raise HTTPBadRequest('missing metadata keys: %s' % ', '.join(missing_metakeys)) dg = DIGroup(v['id_group'], v['created'], '', v['id_user'], v['type']) update_metadata(v['metadata'], dg.id_group, 'group') DBSession.add(dg) DBSession.flush() print(dg) return APIResponse( add_metadata_record(dg.get_dict(), dg.id_group, 'group'))
def collection_post(self): self._request.validated['id_set'] = 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, 's') if len(missing_metakeys) > 0: raise HTTPBadRequest('missing metadata keys: %s' % ', '.join(missing_metakeys)) dset = DigitalSet(v['id_set'], v['created'], '', v['id_user']) update_metadata(v['metadata'], dset.id_set, 'set') DBSession.add(dset) DBSession.flush() return APIResponse( add_metadata_record(dset.get_dict(), dset.id_set, 'set'))
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)
def _add_metadata(self, item): ret = add_metadata_record(item.get_dict(), item.id_item, 'item') ret['links'] = [x.get_dict() for x in DBSession.query(Link) \ .filter(Link.id_item == item.id_item) \ .all()] return ret
def collection_post(self): self._request.validated['id_view'] = str(uuid4()) self._request.validated['created'] = datetime.datetime.now() copy_view_id = self._request.GET.get('view_id', '') 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)) view = View(v['id_view'], v['created'], v['description'], v['id_user'], v['public']) update_metadata(v['metadata'], view.id_view, 'view') DBSession.add(view) DBSession.flush() # # for meta in v['metadata']: # m = Metadata(view.id_view, 'view', meta['key'], meta['value']) # DBSession.add(m) # DBSession.flush() # If user specified a view_id parameter, let's make a copy if copy_view_id: old_view = DBSession.query(View).filter( View.id_view == copy_view_id).one() old_items = DBSession.query(ViewItem).filter( ViewItem.id_view == copy_view_id).all() for item in old_items: print('Adding item' + item.id_item) vi = ViewItem(view.id_view, item.id_item, item.path) DBSession.add(vi) old_containers = DBSession\ .query(Container, ContainerItem)\ .join(ContainerItem, ContainerItem.id_container == Container.id_container)\ .filter(Container.id_view == copy_view_id)\ .all() process_conts = {} for ci in old_containers: cid = ci[0].id_container if not cid in process_conts: c = ci[0] new_cid = str(uuid4()) print('Adding container %s as %s' % (cid, new_cid)) process_conts[cid] = new_cid cont = Container(new_cid, view.id_view, c.type, c.description, c.x, c.y, c.width, c.height, c.z, c.data) DBSession.add(cont) else: new_cid = process_conts[cid] print('Adding item %s to container %s' % (ci[1].id_item, new_cid)) citem = ContainerItem(new_cid, ci[1].id_item, ci[1].data) DBSession.add(citem) print(view) return APIResponse( add_metadata_record(view.get_dict(), view.id_view, 'view'))