def clean_workgroup(self): new_workgroup = self.cleaned_data['workgroup'] old_workgroup = self.instance.workgroup # If new item return if self.instance.pk is None: return new_workgroup # If old workgroup is None return if old_workgroup is None: return new_workgroup # If workgroup didnt change return if old_workgroup == new_workgroup: return new_workgroup # Check user can remove from old wg if not user_can_remove_from_workgroup(self.user, old_workgroup): raise forms.ValidationError(self.cant_move_from_permission_error) # Check user can move to new wg if not user_can_move_to_workgroup(self.user, new_workgroup): raise forms.ValidationError(self.cant_move_to_permission_error) return new_workgroup
def make_changes(self): import reversion from aristotle_mdr.perms import user_can_remove_from_workgroup, user_can_move_to_workgroup new_workgroup = self.cleaned_data['workgroup'] changeDetails = self.cleaned_data['changeDetails'] items = self.cleaned_data['items'] if not user_can_move_to_workgroup(self.user, new_workgroup): raise PermissionDenied move_from_checks = { } # Cache workgroup permissions as we check them to speed things up failed = [] success = [] with transaction.atomic(), reversion.revisions.create_revision(): reversion.revisions.set_user(self.user) for item in items: if item.workgroup: can_move = move_from_checks.get(item.workgroup.pk, None) if can_move is None: can_move = user_can_remove_from_workgroup( self.user, item.workgroup) move_from_checks[item.workgroup.pk] = can_move else: # There is no workgroup, the user can move their own item can_move = True if not can_move: failed.append(item) else: success.append(item) item.workgroup = new_workgroup item.save() failed = list(set(failed)) success = list(set(success)) bad_items = sorted([str(i.id) for i in failed]) if not bad_items: message = _( "%(num_items)s items moved into the workgroup '%(new_wg)s'. \n" ) % { 'new_wg': new_workgroup.name, 'num_items': len(success), } else: message = _( "%(num_items)s items moved into the workgroup '%(new_wg)s'. \n" "Some items failed, they had the id's: %(bad_ids)s") % { 'new_wg': new_workgroup.name, 'num_items': len(success), 'bad_ids': ",".join(bad_items) } reversion.revisions.set_comment(changeDetails + "\n\n" + message) return message
def make_changes(self): import reversion from aristotle_mdr.perms import user_can_remove_from_workgroup, user_can_move_to_workgroup new_workgroup = self.cleaned_data['workgroup'] changeDetails = self.cleaned_data['changeDetails'] items = self.cleaned_data['items'] if not user_can_move_to_workgroup(self.user, new_workgroup): raise PermissionDenied move_from_checks = {} # Cache workgroup permissions as we check them to speed things up failed = [] success = [] with transaction.atomic(), reversion.revisions.create_revision(): reversion.revisions.set_user(self.user) for item in items: can_move = move_from_checks.get(item.workgroup.pk, None) if can_move is None: can_move = user_can_remove_from_workgroup(self.user, item.workgroup) move_from_checks[item.workgroup.pk] = can_move if not can_move: failed.append(item) else: success.append(item) item.workgroup = new_workgroup item.save() failed = list(set(failed)) success = list(set(success)) bad_items = sorted([str(i.id) for i in failed]) if not bad_items: message = _( "%(num_items)s items moved into the workgroup '%(new_wg)s'. \n" ) % { 'new_wg': new_workgroup.name, 'num_items': len(success), } else: message = _( "%(num_items)s items moved into the workgroup '%(new_wg)s'. \n" "Some items failed, they had the id's: %(bad_ids)s" ) % { 'new_wg': new_workgroup.name, 'num_items': len(success), 'bad_ids': ",".join(bad_items) } reversion.revisions.set_comment(changeDetails + "\n\n" + message) return message
def make_changes(self): import reversion from aristotle_mdr.perms import user_can_remove_from_workgroup, user_can_move_to_workgroup new_workgroup = self.cleaned_data['workgroup'] changeDetails = self.cleaned_data['changeDetails'] items = self.cleaned_data['items'] if not user_can_move_to_workgroup(self.user, new_workgroup): raise PermissionDenied move_from_checks = { } # Cache workgroup permissions as we check them to speed things up failed = [] success = [] with transaction.atomic(), reversion.revisions.create_revision(): reversion.revisions.set_user(self.user) for item in items: if item.workgroup: can_move = move_from_checks.get(item.workgroup.pk, None) if can_move is None: can_move = user_can_remove_from_workgroup( self.user, item.workgroup) move_from_checks[item.workgroup.pk] = can_move else: # There is no workgroup, the user can move their own item can_move = True if not can_move: failed.append(item) else: success.append(item) item.workgroup = new_workgroup item.save() failed = list(set(failed)) success = list(set(success)) bad_items = sorted([str(i.id) for i in failed]) return self.generate_moving_message(new_workgroup.name, len(success), failed_items=bad_items)