def update(self, session: Session, title: str, project_ids: ProjectIDListT, provider_user: Any, contact_user: Any, citation: str, abstract: str, description: str, creator_users: List[Any], associate_users: List[Any], creator_orgs: List[Any], associate_orgs: List[Any]): project_ids.sort() # TODO: projects update using given list assert project_ids == self.project_ids, "Cannot update composing projects yet" # Redo sanity check & aggregation as underlying projects might have changed self._add_composing_projects(session, project_ids) coll_id = self._collection.id # Simple fields update self._collection.title = title self._collection.citation = citation self._collection.abstract = abstract self._collection.description = description # Copy provider user id if provider_user is not None: self._collection.provider_user_id = provider_user.id # Copy contact user id if contact_user is not None: self._collection.contact_user_id = contact_user.id # Dispatch members by role by_role = { COLLECTION_ROLE_DATA_CREATOR: creator_users, COLLECTION_ROLE_ASSOCIATED_PERSON: associate_users } # Remove all to avoid diff-ing session.query(CollectionUserRole). \ filter(CollectionUserRole.collection_id == coll_id).delete() # Add all for a_role, a_user_list in by_role.items(): for a_user in a_user_list: session.add( CollectionUserRole(collection_id=coll_id, user_id=a_user.id, role=a_role)) # Dispatch orgs by role by_role_org = { COLLECTION_ROLE_DATA_CREATOR: creator_orgs, COLLECTION_ROLE_ASSOCIATED_PERSON: associate_orgs } # Remove all to avoid diff-ing session.query(CollectionOrgaRole). \ filter(CollectionOrgaRole.collection_id == coll_id).delete() # Add all for a_role, an_org_list in by_role_org.items(): for an_org in an_org_list: session.add( CollectionOrgaRole(collection_id=coll_id, organisation=an_org, role=a_role)) session.commit()
def create_job(cls, session: Session, user_id: UserIDT, job_type: str, args: Dict) -> Job: job = Job() job.state = DBJobStateEnum.Pending job.progress_msg = cls.PENDING_MESSAGE job.creation_date = job.updated_on = datetime.now() job.type = job_type job.owner_id = user_id job.params = json_dumps(args) job.inside = job.reply = json_dumps({}) job.messages = json_dumps([]) session.add(job) session.commit() return job
def create(session: Session, title: str, project_ids: ProjectIDListT) -> CollectionIDT: """ Create using minimum fields. """ # Find the collection db_coll = Collection() db_coll.title = title session.add(db_coll) session.flush() # to get the collection ID bo_coll = CollectionBO(db_coll) bo_coll.set_composing_projects(session, project_ids) session.commit() return bo_coll.id
def delete(session: Session, coll_id: CollectionIDT): """ Completely remove the collection. Being just a set of project references, the pointed-at projects are not impacted. """ # Remove links first session.query(CollectionProject). \ filter(CollectionProject.collection_id == coll_id).delete() session.query(CollectionUserRole). \ filter(CollectionUserRole.collection_id == coll_id).delete() session.query(CollectionOrgaRole). \ filter(CollectionOrgaRole.collection_id == coll_id).delete() # Remove collection session.query(Collection). \ filter(Collection.id == coll_id).delete() session.commit()
def set_preferences_per_project(session: Session, user_id: int, project_id: int, key: str, value: Any): """ Set preference for a key, for given project and user. The key disappears if set to empty string. """ current_user: User = session.query(User).get(user_id) prefs_for_proj: UserPreferences = current_user.preferences_for_projects.filter_by( project_id=project_id).first() if prefs_for_proj: all_prefs_for_proj = json.loads(prefs_for_proj.json_prefs) else: prefs_for_proj = UserPreferences() prefs_for_proj.project_id = project_id prefs_for_proj.user_id = user_id session.add(prefs_for_proj) all_prefs_for_proj = dict() all_prefs_for_proj[key] = value if value == '': del all_prefs_for_proj[key] prefs_for_proj.json_prefs = json.dumps(all_prefs_for_proj) logger.info("for %s and %d: %s", current_user.name, project_id, prefs_for_proj.json_prefs) session.commit()