def deleteGroup(self, group_id): """ Delete a group (group must be empty or error will be thrown). :param group_id: The numeric ID of group (cannot use group name here). :type group_id: int :return: The deleted group object. :rtype: dict """ g = groups.delete(group_id) auditlog.log(auditlog.CODE_CONTENT_DEL, target=g) return g.to_dict()
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})