def check_watch(entity): if not c.user: return None watch = Watch.find_by_entity(c.user, entity) if request.params.get('watch') and not watch: Watch.create(c.user, entity) elif watch: watch.delete() meta.Session.commit()
def set_watch(entity, set_watch=True): """ Make sure that the current user watches entity, if set_watch is True. Make sure that the current user doesn't watch entity, if set_watch is False. """ has_watch = Watch.find_by_entity(c.user, entity) if set_watch and has_watch is None: Watch.create(c.user, entity) meta.Session.commit() elif not set_watch and has_watch is not None: has_watch.delete() meta.Session.commit()
def traverse_watchlist(entity): """ Traverse the watchlist for all affected topics. Returns only the most closely matching watchlist entries. """ def merge(outer, inner): return inner + [w for w in outer if not w.user in [w.user for w in inner]] watches = Watch.all_by_entity(entity) if isinstance(entity, Comment): if entity.reply is not None: watches = merge(watches, traverse_watchlist(entity.reply)) else: watches = merge(watches, traverse_watchlist(entity.topic)) elif isinstance(entity, Delegateable): if entity.milestone is not None and not entity.milestone.is_deleted(): watches = merge(watches, traverse_watchlist(entity.milestone)) if len(entity.parents): for parent in entity.parents: watches = merge(watches, traverse_watchlist(parent)) else: watches = merge(watches, traverse_watchlist(entity.instance)) return watches
def traverse_watchlist(entity): """ Traverse the watchlist for all affected topics. Returns only the most closely matching watchlist entries. """ def merge(outer, inner): return inner + [w for w in outer if \ not w.user in [w.user for w in inner]] watches = Watch.all_by_entity(entity) if isinstance(entity, Comment): if entity.reply is not None: watches = merge(watches, traverse_watchlist(entity.reply)) else: watches = merge(watches, traverse_watchlist(entity.topic)) elif isinstance(entity, Delegateable): if entity.milestone is not None and not entity.milestone.is_deleted(): watches = merge(watches, traverse_watchlist(entity.milestone)) if len(entity.parents): for parent in entity.parents: watches = merge(watches, traverse_watchlist(parent)) else: watches = merge(watches, traverse_watchlist(entity.instance)) return watches
def _to_python(self, value, state): from adhocracy.model import Watch watch = Watch.by_id(value) if not watch: raise formencode.Invalid( _("No watchlist entry with ID '%s' exists") % value, value, state) return watch
def test_delete_user_deletes_watches(self): from adhocracy.model import Watch voter_group = Group.by_code(Group.CODE_VOTER) user = tt_make_user(instance_group=voter_group) instance = tt_get_instance() watch = Watch.create(user, instance) self.assertFalse(watch.is_deleted()) user.delete() self.assertTrue(watch.is_deleted())
def clean_stale_watches(): log.debug("Beginning to clean up watchlist entries...") count = 0 for watch in Watch.all(): if hasattr(watch.entity, "is_deleted") and watch.entity.is_deleted(): count += 1 watch.delete() meta.Session.commit() if count > 0: log.debug("Removed %d stale watchlist entries." % count)
def clean_stale_watches(): log.debug("Beginning to clean up watchlist entries...") count = 0 for watch in Watch.all(): if hasattr(watch.entity, 'is_deleted') and \ watch.entity.is_deleted(): count += 1 watch.delete() meta.Session.commit() if count > 0: log.debug("Removed %d stale watchlist entries." % count)
def find_watch(entity): return Watch.find_by_entity(c.user, entity)