def restore_object(self, attrs, instance=None): #watchers is not a field from the model but can be attached in the get_queryset of the viewset. #If that's the case we need to remove it before calling the super method watcher_field = self.fields.pop("watchers", None) self.validate_watchers(attrs, "watchers") new_watcher_ids = set(attrs.pop("watchers", [])) obj = super(WatchedResourceModelSerializer, self).restore_object(attrs, instance) #A partial update can exclude the watchers field or if the new instance can still not be saved if instance is None or len(new_watcher_ids) == 0: return obj old_watcher_ids = set(obj.get_watchers().values_list("id", flat=True)) adding_watcher_ids = list(new_watcher_ids.difference(old_watcher_ids)) removing_watcher_ids = list(old_watcher_ids.difference(new_watcher_ids)) User = apps.get_model("users", "User") adding_users = User.objects.filter(id__in=adding_watcher_ids) removing_users = User.objects.filter(id__in=removing_watcher_ids) for user in adding_users: services.add_watcher(obj, user) for user in removing_users: services.remove_watcher(obj, user) obj.watchers = obj.get_watchers() return obj
def restore_object(self, attrs, instance=None): # watchers is not a field from the model but can be attached in the get_queryset of the viewset. # If that's the case we need to remove it before calling the super method self.fields.pop("watchers", None) self.validate_watchers(attrs, "watchers") new_watcher_ids = attrs.pop("watchers", None) obj = super(EditableWatchedResourceSerializer, self).restore_object(attrs, instance) # A partial update can exclude the watchers field or if the new instance can still not be saved if instance is None or new_watcher_ids is None: return obj new_watcher_ids = set(new_watcher_ids) old_watcher_ids = set(obj.get_watchers().values_list("id", flat=True)) adding_watcher_ids = list(new_watcher_ids.difference(old_watcher_ids)) removing_watcher_ids = list(old_watcher_ids.difference(new_watcher_ids)) adding_users = get_user_model().objects.filter(id__in=adding_watcher_ids) removing_users = get_user_model().objects.filter(id__in=removing_watcher_ids) for user in adding_users: services.add_watcher(obj, user) for user in removing_users: services.remove_watcher(obj, user) obj.watchers = obj.get_watchers() return obj
def restore_object(self, attrs, instance=None): #watchers is not a field from the model but can be attached in the get_queryset of the viewset. #If that's the case we need to remove it before calling the super method watcher_field = self.fields.pop("watchers", None) instance = super(WatchedResourceModelSerializer, self).restore_object(attrs, instance) if instance is not None and self.validate_watchers(attrs, "watchers"): new_watcher_ids = set(attrs.get("watchers", [])) old_watcher_ids = set( services.get_watchers(instance).values_list("id", flat=True)) adding_watcher_ids = list( new_watcher_ids.difference(old_watcher_ids)) removing_watcher_ids = list( old_watcher_ids.difference(new_watcher_ids)) User = apps.get_model("users", "User") adding_users = User.objects.filter(id__in=adding_watcher_ids) removing_users = User.objects.filter(id__in=removing_watcher_ids) for user in adding_users: services.add_watcher(instance, user) for user in removing_users: services.remove_watcher(instance, user) instance.watchers = services.get_watchers(instance) return instance
def save_watchers(self): new_watcher_emails = set(self._watchers) old_watcher_emails = set(self.object.get_watchers().values_list("email", flat=True)) adding_watcher_emails = list(new_watcher_emails.difference(old_watcher_emails)) removing_watcher_emails = list(old_watcher_emails.difference(new_watcher_emails)) User = apps.get_model("users", "User") adding_users = User.objects.filter(email__in=adding_watcher_emails) removing_users = User.objects.filter(email__in=removing_watcher_emails) for user in adding_users: notifications_services.add_watcher(self.object, user) for user in removing_users: notifications_services.remove_watcher(self.object, user) self.object.watchers = [user.email for user in self.object.get_watchers()]
def save_watchers(self): new_watcher_emails = set(self._watchers) old_watcher_emails = set(self.object.get_watchers().values_list("email", flat=True)) adding_watcher_emails = list(new_watcher_emails.difference(old_watcher_emails)) removing_watcher_emails = list(old_watcher_emails.difference(new_watcher_emails)) User = apps.get_model("users", "User") adding_users = User.objects.filter(email__in=adding_watcher_emails) removing_users = User.objects.filter(email__in=removing_watcher_emails) for user in adding_users: notifications_services.add_watcher(self.object, user) for user in removing_users: notifications_services.remove_watcher(self.object, user) self.object.watchers = [user.email for user in self.object.get_watchers()]
def restore_object(self, attrs, instance=None): #watchers is not a field from the model but can be attached in the get_queryset of the viewset. #If that's the case we need to remove it before calling the super method watcher_field = self.fields.pop("watchers", None) instance = super(WatchedResourceModelSerializer, self).restore_object(attrs, instance) if instance is not None and self.validate_watchers(attrs, "watchers"): new_watcher_ids = set(attrs.get("watchers", [])) old_watcher_ids = set(services.get_watchers(instance).values_list("id", flat=True)) adding_watcher_ids = list(new_watcher_ids.difference(old_watcher_ids)) removing_watcher_ids = list(old_watcher_ids.difference(new_watcher_ids)) User = apps.get_model("users", "User") adding_users = User.objects.filter(id__in=adding_watcher_ids) removing_users = User.objects.filter(id__in=removing_watcher_ids) for user in adding_users: services.add_watcher(instance, user) for user in removing_users: services.remove_watcher(instance, user) instance.watchers = services.get_watchers(instance) return instance
def unwatch(self, request, pk=None): obj = self.get_object() self.check_permissions(request, "unwatch", obj) services.remove_watcher(obj, request.user) return response.Ok()
def remove_watcher(self, user): services.remove_watcher(self, user)
def unwatch(self, request, pk=None): obj = self.get_object() self.check_permissions(request, "unwatch", obj) self.pre_conditions_on_save(obj) services.remove_watcher(obj, request.user) return response.Ok()
def remove_watcher(self, user): services.remove_watcher(self, user)