def deleteResource(self, resource_id): """ Delete a resource (resource must be empty or error will be thrown). :param resource_id: The numeric ID of resource (cannot use resource name here). :type resource_id: int :return: The deleted resource object. :rtype: dict """ r = resources.delete(resource_id) auditlog.log(auditlog.CODE_CONTENT_DEL, target=r) return r.to_dict(decrypt=False)
def delete(self, group_id): """ Deletes a group. """ group = groups.get(group_id) all_resources = group.resources.all() # This is very lazy (could be done in SQL), but simple/easy-to-debug. resources_only_in_this_group = [] resources_in_other_groups_too = [] for r in all_resources: if r.groups.all() == [group]: resources_only_in_this_group.append(r) else: resources_in_other_groups_too.append(r) if cherrypy.request.method == 'POST': # First remove any resources that are only owned by this group. for r in resources_only_in_this_group: # Remove any passwords in this resource for pw in r.passwords: del_pw = passwords.delete(pw.id) auditlog.log(auditlog.CODE_CONTENT_DEL, target=del_pw) del_r = resources.delete(r.id) auditlog.log(auditlog.CODE_CONTENT_DEL, target=del_r) # Next we manually remove the group from any other resources that were associated # with this group. for r in resources_in_other_groups_too: group_ids = set([g.id for g in r.groups.all()]) group_ids.remove(group.id) (mod_r, modified) = resources.modify(r.id, group_ids=group_ids) if modified: auditlog.log(auditlog.CODE_CONTENT_MOD, target=mod_r, attributes_modified=modified) # And finally we can delete the group itself. group = groups.delete(group.id) auditlog.log(auditlog.CODE_CONTENT_DEL, target=group) notify_entity_activity(group, 'deleted') raise cherrypy.HTTPRedirect('/group/list') else: return render('group/delete.html', {'group_id': group_id, 'del_resources': resources_only_in_this_group, 'mod_resources': resources_in_other_groups_too})
def delete(self, resource_id, redirect_to=None): resource = resources.get(resource_id) if cherrypy.request.method == 'POST': # First remove any passwords for this resource. for pw in resource.passwords: del_pw = passwords.delete(pw.id) auditlog.log(auditlog.CODE_CONTENT_DEL, target=del_pw) # Then remove the actual resource resource = resources.delete(resource_id) auditlog.log(auditlog.CODE_CONTENT_DEL, target=resource) notify_entity_activity(resource, 'deleted') if redirect_to: raise cherrypy.HTTPRedirect(redirect_to) else: raise cherrypy.HTTPRedirect('/resource/list') else: return render('resource/delete.html', {'resource': resource, 'redirect_to': redirect_to})