def add_to_collection(self, user, rel_path): """Adds the given user directory to collection repository with all subdirectories and files. Also sets the user permission and subscription.""" # read description file desc_parser = info.parse_description(user, rel_path) # Create root directory and save it with all subdirectories and files item = os.path.basename(rel_path) root_dir = Directory(revision=1, name=item, user_modified=user, size=WebFolder.get_dir_size(user, rel_path)) root_dir.save() info.create_traits(user, rel_path, root_dir.identifier, 1) root_dir.save_recursive(user, rel_path) root_dir.save() # Set collection attributes self.directory = root_dir self.revision = 1 self.save() self.abstract = desc_parser.get_abstract() self.details = desc_parser.get_details() self.comment = "Add the collection to the repository." self.save() for (key,value) in desc_parser.get_tags(): try: tag = Tag.objects.get(key=key, value=value) except ObjectDoesNotExist: tag = Tag(key=key, value=value) tag.save() self.tags.add(tag) # Set user access permission = UserPermission(collection=self, user=user, permission=enum.Permission.WRITE) permission.save() # Set user subscription subscription = Subscription(collection=self, user=user) subscription.save() # Update news log content = "A new collection <b>" + self.name + "</b> was added to the repository.\n" + \ "<i><b>Abstract:</b>\n" + self.abstract + "</i>" news = News(user=User.get_current_user(), content=content, collection=self) news.save()
def update_group_permission(self, group, permission): """Grant the given group the given permission of this collection.""" if permission: if GroupPermission.objects.filter(group=group, collection=self): p = GroupPermission.objects.get(group=group, collection=self) p.permission = permission else: p = GroupPermission(group=group, collection=self, permission=permission) p.save_all_revisions() else: permission = GroupPermission.objects.get(group=group, collection=self) permission.delete_all_revisions() # Update news log content = "The group permissions of collection <b>" + self.name + "</b> were changed.\n" + \ "The group <b>" + group.group_name + "</b> has " + utils.enum.Permission.get_readable_permission(permission) + " access." news = News(user=User.get_current_user(), content=content, collection=self) news.save()
def update_user_permission(self, user, permission): """Grant the given user the given permission of this collection.""" if permission: if UserPermission.objects.filter(user=user, collection=self): p = UserPermission.objects.get(user=user, collection=self) p.permission = permission else: p = UserPermission(user=user, collection=self, permission=permission) p.save_all_revisions() else: p = UserPermission.objects.get(user=user, collection=self) p.delete_all_revisions() # Update news log content = "The user permissions of collection <b>" + self.name + "</b> were changed.\n" + \ "The user <b>" + user.user_name + "</b> has " + utils.enum.Permission.get_readable_permission(permission) + " access." news = News(user=User.get_current_user(), content=content, collection=self) news.save()
def update_public_access(self, access): """Set public access to all revisions of the collection. Parameter access is a boolean value.""" if(self.public_access!=access): self.public_access = access self.save() for c in self.get_all_revisions(): c.public_access = access c.save() # Update news log if access: readable_access="public" else: readable_access="not public" content = "The permissions of collection <b>" + self.name + "</b> were changed.\n" + \ "The collection is " + readable_access + "." news = News(user=User.get_current_user(), content=content, collection=self) news.save()
def push_local_revision(self, user, rel_path, prev_col, comment): """Push the local revision to the repository.""" prev_max_revision = Collection.objects.filter(identifier=prev_col.identifier).aggregate(Max('revision'))['revision__max'] # collection is at newest revision if prev_col.revision == prev_max_revision: # read description file desc_parser = info.parse_description(user, rel_path) # Create root directory and update it with the local revision item = os.path.basename(rel_path) root_dir = Directory(identifier=prev_col.directory.identifier, revision=prev_col.revision+1, name=item, user_modified=user, size=WebFolder.get_dir_size(user, rel_path)) root_dir.save() info.create_traits(user, rel_path, root_dir.identifier, root_dir.revision) root_dir.push_recursive(user, rel_path, prev_col.directory) if root_dir.hash == prev_col.directory.hash: root_dir.delete() raise NoLocalChangesException() else: # Set collection attributes self.identifier = prev_col.identifier self.directory = root_dir self.revision = prev_col.revision+1 self.save() self.abstract = desc_parser.get_abstract() self.details = desc_parser.get_details() self.comment = comment self.save() for (key,value) in desc_parser.get_tags(): try: tag = Tag.objects.get(key=key, value=value) except ObjectDoesNotExist: tag = Tag(key=key, value=value) tag.save() self.tags.add(tag) # Set user access UserPermission.update(self) GroupPermission.update(self) # Set user subscription subscription = Subscription(collection=self, user=user) subscription.save() # Update news log content = "The collection <b>" + self.name + "</b> was updated to revision " + \ str(self.revision) + ".\n" + "<i><b>Comment:</b>\n" + self.comment + "</i>" news = News(user=User.get_current_user(), content=content, collection=self) news.save() # collection is not at newest revision else: raise NotNewestRevisionException()