def all_q(cls, instance_filter=True, include_deleted=False): q = meta.Session.query(Membership) if ifilter.has_instance() and instance_filter: q = q.filter(Membership.instance_id == ifilter.get_instance().id) if not include_deleted: q = q.filter(or_(Membership.expire_time == None, Membership.expire_time > datetime.utcnow())) # noqa return q
def find(cls, id, instance_filter=True, include_deleted=False): if instance_filter and ifilter.has_instance(): instance = ifilter.get_instance() else: instance = None query = cls.all_q(instance=instance, include_deleted=include_deleted) query = query.filter(Message.id == id) return query.first()
def all_q(cls, instance_filter=True, include_deleted=False): q = meta.Session.query(Membership) if ifilter.has_instance() and instance_filter: q = q.filter(Membership.instance_id == ifilter.get_instance().id) if not include_deleted: q = q.filter(or_(Membership.expire_time == None, Membership.expire_time > datetime.utcnow())) return q
def find_by_creator(cls, user, instance_filter=True): q = meta.Session.query(Proposal) q = q.filter(Proposal.creator == user) q = q.filter(or_(Proposal.delete_time == None, # noqa Proposal.delete_time > datetime.utcnow())) if ifilter.has_instance() and instance_filter: q = q.filter(Proposal.instance_id == ifilter.get_instance().id) return q.all()
def find_all(cls, ids, instance_filter=True, include_deleted=False): q = meta.Session.query(Proposal) q = q.filter(Proposal.id.in_(ids)) if ifilter.has_instance() and instance_filter: q = q.filter(Proposal.instance_id == ifilter.get_instance().id) if not include_deleted: q = q.filter(or_(Proposal.delete_time == None, # noqa Proposal.delete_time > datetime.utcnow())) return q.all()
def find(cls, id, instance_filter=True, include_deleted=False): try: q = meta.Session.query(Event) q = q.filter(Event.id == id) if ifilter.has_instance() and instance_filter: q = q.filter(Event.instance_id == ifilter.get_instance().id) return q.one() except Exception, e: log.warn("find(%s): %s" % (id, e)) return None
def find_by_topics(cls, topics): from delegateable import Delegateable topics = map(lambda d: d.id, topics) q = meta.Session.query(Event) q = q.join(Event.topics) q = q.filter(Delegateable.id.in_(topics)) q = q.order_by(Event.time.desc()) if ifilter.has_instance(): q = q.filter(Event.instance_id == ifilter.get_instance().id) return q.all()
def all_q(cls, instance_filter=True, include_deleted=False): from delegateable import Delegateable q = meta.Session.query(Comment) q = q.join(Delegateable) if ifilter.has_instance() and instance_filter: q = q.filter(Delegateable.instance_id == ifilter.get_instance().id) if not include_deleted: q = q.filter(or_(Comment.delete_time == None, Comment.delete_time > datetime.utcnow())) return q
def all_q(cls, instance_filter=True, include_deleted=False): from delegateable import Delegateable q = meta.Session.query(Comment) q = q.join(Delegateable) if ifilter.has_instance() and instance_filter: q = q.filter(Delegateable.instance_id == ifilter.get_instance().id) if not include_deleted: q = q.filter(or_(Comment.delete_time == None, # noqa Comment.delete_time > datetime.utcnow())) return q
def complete(cls, prefix, limit=5, instance_filter=True): q = meta.Session.query(User) prefix = prefix.lower() q = q.filter( or_(func.lower(User.user_name).like(prefix + u"%"), func.lower(User.display_name).like(prefix + u"%")) ) q = q.limit(limit) completions = q.all() if ifilter.has_instance() and instance_filter: inst = ifilter.get_instance() completions = filter(lambda u: u.is_member(inst), completions) return completions
def find_by_topics(cls, topics, limit=None, include_hidden=True): from delegateable import Delegateable topics = map(lambda d: d.id, topics) if ifilter.has_instance(): instance = ifilter.get_instance() else: instance = None q = cls.all_q(instance=instance, include_hidden=include_hidden) q = q.join(Event.topics) q = q.filter(Delegateable.id.in_(topics)) q = q.order_by(Event.time.desc()) return q.all()
def __len__(self): if self._count is None: from tagging import Tagging from delegateable import Delegateable q = meta.Session.query(Tagging) q = q.filter(Tagging.tag == self) if ifilter.has_instance(): q = q.join(Delegateable) q = q.filter( Delegateable.instance_id == ifilter.get_instance().id) self._count = q.count() return self._count
def __len__(self): if self._count is None: from tagging import Tagging from delegateable import Delegateable q = meta.Session.query(Tagging) q = q.filter(Tagging.tag == self) if ifilter.has_instance(): q = q.join(Delegateable) q = q.filter(Delegateable.instance_id == ifilter.get_instance().id) self._count = q.count() return self._count
def find(cls, id, instance_filter=True, include_deleted=False, include_hidden=True): try: if instance_filter and ifilter.has_instance(): instance = ifilter.get_instance() else: instance = None q = cls.all_q(instance=instance, include_hidden=include_hidden) q = q.filter(Event.id == id) return q.one() except Exception, e: log.warn("find(%s): %s" % (id, e)) return None
def complete(cls, prefix, limit=5, instance_filter=True): q = meta.Session.query(User) prefix = prefix.lower() q = q.filter( or_( func.lower(User.user_name).like(prefix + u"%"), func.lower(User.display_name).like(prefix + u"%"))) q = q.limit(limit) completions = q.all() if ifilter.has_instance() and instance_filter: inst = ifilter.get_instance() completions = filter(lambda u: u.is_member(inst), completions) return completions
def find(cls, id, instance_filter=True, include_deleted=False): try: q = meta.Session.query(Vote) q = q.filter(Vote.id == int(id)) vote = q.first() if ifilter.has_instance() and instance_filter: vote = vote.poll.scope.instance == ifilter.get_instance() \ and vote or None return vote except Exception, e: log.exception(e) #log.warn("find(%s): %s" % (id, e)) return None
def find(cls, id, instance_filter=True, include_deleted=False): try: q = meta.Session.query(Delegateable) q = q.filter(Delegateable.id == int(id)) if not include_deleted: q = q.filter(or_(Delegateable.delete_time == None, # noqa Delegateable.delete_time > datetime.utcnow())) if ifilter.has_instance() and instance_filter: q = q.filter((Delegateable.instance_id == ifilter.get_instance().id)) return q.limit(1).first() except Exception, e: log.warn("find(%s): %s" % (id, e)) return None
def find(cls, id, instance_filter=True, include_deleted=False): try: q = meta.Session.query(Delegation) q = q.filter(Delegation.id == id) if not include_deleted: q = q.filter(or_(Delegation.revoke_time == None, Delegation.revoke_time > datetime.utcnow())) d = q.one() if ifilter.has_instance() and instance_filter: if d.scope.instance != ifilter.get_instance(): return None return d except Exception, e: log.warn("find(%s): %s" % (id, e)) return None
def find_all(cls, unames, instance_filter=True, include_deleted=False): from membership import Membership q = meta.Session.query(User) q = q.filter(User.user_name.in_(unames)) if not include_deleted: q = q.filter(or_(User.delete_time == None, User.delete_time > datetime.utcnow())) if ifilter.has_instance() and instance_filter: q = q.join(Membership) q = q.filter(or_(Membership.expire_time == None, Membership.expire_time > datetime.utcnow())) q = q.filter(Membership.instance == ifilter.get_instance()) # log.debug("QueryAll: %s" % q) # log.debug("LEN: %s" % len(q.all())) return q.all()
def find(cls, id, instance_filter=True, include_deleted=False): try: q = meta.Session.query(Delegation) q = q.filter(Delegation.id == id) if not include_deleted: q = q.filter(or_(Delegation.revoke_time == None, # noqa Delegation.revoke_time > datetime.utcnow())) d = q.one() if ifilter.has_instance() and instance_filter: if d.scope.instance != ifilter.get_instance(): return None return d except Exception, e: log.warn("find(%s): %s" % (id, e)) return None
def find(cls, id, instance_filter=True, include_deleted=True): from delegateable import Delegateable try: q = meta.Session.query(Poll) q = q.filter(Poll.id == int(id)) if not include_deleted: q = q.filter(or_(Poll.end_time == None, Poll.end_time > datetime.utcnow())) if ifilter.has_instance() and instance_filter: q = q.join(Delegateable) q = q.filter(Delegateable.instance == ifilter.get_instance()) return q.limit(1).first() except Exception, e: log.warn("find(%s): %s" % (id, e)) return None
def find(cls, id, instance_filter=True, include_deleted=False): from proposal import Proposal try: q = meta.Session.query(Selection) q = q.filter(Selection.id == id) if not include_deleted: q = q.filter(or_(Selection.delete_time == None, Selection.delete_time > datetime.utcnow())) if ifilter.has_instance() and instance_filter: q = q.join(Proposal) q = q.filter(Proposal.instance == ifilter.get_instance()) return q.limit(1).first() except Exception, e: log.warn("find(%s): %s" % (id, e)) return None
def find(cls, id, instance_filter=True, include_deleted=True): from delegateable import Delegateable try: q = meta.Session.query(Poll) q = q.filter(Poll.id == int(id)) if not include_deleted: q = q.filter(or_(Poll.end_time == None, # noqa Poll.end_time > datetime.utcnow())) if ifilter.has_instance() and instance_filter: q = q.join(Delegateable) q = q.filter(Delegateable.instance == ifilter.get_instance()) return q.limit(1).first() except Exception, e: log.warn("find(%s): %s" % (id, e)) return None
def find(cls, id, instance_filter=True, include_deleted=False): from proposal import Proposal try: q = meta.Session.query(Selection) q = q.filter(Selection.id == id) if not include_deleted: q = q.filter(or_(Selection.delete_time == None, Selection.delete_time > datetime.utcnow())) # noqa if ifilter.has_instance() and instance_filter: q = q.join(Proposal) q = q.filter(Proposal.instance == ifilter.get_instance()) return q.limit(1).first() except Exception, e: log.warn("find(%s): %s" % (id, e)) return None
def find_fuzzy(cls, id, instance_filter=True, include_deleted=False): page = cls.find(id, instance_filter=instance_filter, include_deleted=include_deleted) if page is None: from text import Text q = meta.Session.query(Page) q = q.join(Text) q = q.filter(Text.title.like(id)) if not include_deleted: q = q.filter(or_(Page.delete_time == None, # noqa Page.delete_time > datetime.utcnow())) if ifilter.has_instance() and instance_filter: q = q.filter(Page.instance == ifilter.get_instance()) q = q.order_by(Text.create_time.asc()) page = q.limit(1).first() return page
def unused_label(cls, label, instance_filter=True, functions=None, include_deleted=False): q = meta.Session.query(Page)\ .filter(Page.label == label) if not include_deleted: q = q.filter( or_( Page.delete_time == None, # noqa Page.delete_time > datetime.utcnow())) if ifilter.has_instance() and instance_filter: q = q.filter(Page.instance == ifilter.get_instance()) return not q.count()
def find_fuzzy(cls, id, instance_filter=True, include_deleted=False): page = cls.find(id, instance_filter=instance_filter, include_deleted=include_deleted) if page is None: from text import Text q = meta.Session.query(Page) q = q.join(Text) q = q.filter(Text.title.like(id)) if not include_deleted: q = q.filter(or_(Page.delete_time == None, Page.delete_time > datetime.utcnow())) if ifilter.has_instance() and instance_filter: q = q.filter(Page.instance == ifilter.get_instance()) q = q.order_by(Text.create_time.asc()) page = q.limit(1).first() return page
def find_by_agent_principal_scope(cls, agent, principal, scope, instance_filter=True, include_deleted=False): try: q = meta.Session.query(Delegation) q = q.filter(Delegation.agent == agent) q = q.filter(Delegation.principal == principal) q = q.filter(Delegation.scope == scope) if not include_deleted: q = q.filter(or_(Delegation.revoke_time == None, Delegation.revoke_time > datetime.utcnow())) d = q.one() if ifilter.has_instance() and instance_filter: if d.scope.instance != ifilter.get_instance(): return None return d except Exception, e: log.warn("find(%s): %s" % (id, e)) return None
def complete(cls, prefix, limit=5, instance_filter=True): from tagging import Tagging from delegateable import Delegateable q = meta.Session.query(Tag) q = q.add_column(func.count(Tagging.id)) q = q.join(Tagging) q = q.filter(func.lower(Tag.name).like(prefix.lower() + "%")) if ifilter.has_instance() and instance_filter: q = q.join(Delegateable) q = q.filter(Delegateable.instance_id == ifilter.get_instance().id) q = q.group_by(Tag.id, Tag.create_time, Tag.name) q = q.order_by(func.count(Tagging.id).desc()) # SQLAlchemy turns this into a f*****g subquery: #if limit is not None: # q = q.limit(limit) #print "QUERY", q return q.all()[:limit]
def popular_tags(cls, limit=None): from tagging import Tagging from delegateable import Delegateable q = meta.Session.query(Tag) q = q.add_column(func.count(Tagging.id)) q = q.join(Tagging) if ifilter.has_instance(): q = q.join(Delegateable) q = q.filter(Delegateable.instance_id == ifilter.get_instance().id) q = q.group_by(Tag.id, Tag.create_time, Tag.name) q = q.order_by(func.count(Tagging.id).desc()) # SQLAlchemy turns this into a f*****g subquery: #if limit is not None: # q = q.limit(limit) #print "QUERY", q tags = [(k, v) for k, v in q.all() if v > 0] return tags[:limit]
def find_all(cls, unames, instance_filter=True, include_deleted=False): from membership import Membership q = meta.Session.query(User) q = q.filter(User.user_name.in_(unames)) if not include_deleted: q = q.filter( or_(User.delete_time == None, User.delete_time > datetime.utcnow())) if ifilter.has_instance() and instance_filter: q = q.join(Membership) q = q.filter( or_(Membership.expire_time == None, Membership.expire_time > datetime.utcnow())) q = q.filter(Membership.instance == ifilter.get_instance()) #log.debug("QueryAll: %s" % q) #log.debug("LEN: %s" % len(q.all())) return q.all()
def find(cls, id, instance_filter=True, include_deleted=False): try: q = meta.Session.query(Page) try: id = int(id) q = q.filter(Page.id == id) except ValueError: #from adhocracy.lib.text import title2alias q = q.filter(Page.label == id) if not include_deleted: q = q.filter(or_(Page.delete_time == None, Page.delete_time > datetime.utcnow())) if ifilter.has_instance() and instance_filter: q = q.filter(Page.instance == ifilter.get_instance()) return q.limit(1).first() except Exception, e: log.warn("find(%s): %s" % (id, e)) return None
def find(cls, id, instance_filter=True, include_deleted=False): try: q = meta.Session.query(Page) try: id = int(id) q = q.filter(Page.id == id) except ValueError: #from adhocracy.lib.text import title2alias q = q.filter(Page.label == id) if not include_deleted: q = q.filter(or_(Page.delete_time == None, # noqa Page.delete_time > datetime.utcnow())) if ifilter.has_instance() and instance_filter: q = q.filter(Page.instance == ifilter.get_instance()) return q.limit(1).first() except Exception, e: log.warn("find(%s): %s" % (id, e)) return None
def find_by_agent_principal_scope(cls, agent, principal, scope, instance_filter=True, include_deleted=False): try: q = meta.Session.query(Delegation) q = q.filter(Delegation.agent == agent) q = q.filter(Delegation.principal == principal) q = q.filter(Delegation.scope == scope) if not include_deleted: q = q.filter(or_(Delegation.revoke_time == None, # noqa Delegation.revoke_time > datetime.utcnow())) d = q.one() if ifilter.has_instance() and instance_filter: if d.scope.instance != ifilter.get_instance(): return None return d except Exception, e: log.warn("find(%s): %s" % (id, e)) return None
def find(cls, user_name, instance_filter=True, include_deleted=False): from membership import Membership try: q = meta.Session.query(User) try: q = q.filter(User.id == int(user_name)) except ValueError: q = q.filter(User.user_name == unicode(user_name)) if not include_deleted: q = q.filter(or_(User.delete_time == None, User.delete_time > datetime.utcnow())) if ifilter.has_instance() and instance_filter: q = q.join(Membership) q = q.filter(or_(Membership.expire_time == None, Membership.expire_time > datetime.utcnow())) q = q.filter(Membership.instance == ifilter.get_instance()) return q.limit(1).first() except Exception, e: log.warn("find(%s): %s" % (user_name, e)) return None
def find(cls, id, instance_filter=True, include_deleted=False, full=False): try: q = meta.Session.query(Proposal) id = int(unicode(id).split('-', 1)[0]) q = q.filter(Proposal.id == id) if full: q = q.options(eagerload(Proposal.comments)) q = q.options(eagerload(Proposal.adopt_poll)) q = q.options(eagerload(Proposal.rate_poll)) q = q.options(eagerload(Proposal.taggings)) q = q.options(eagerload(Proposal.parents)) if ifilter.has_instance() and instance_filter: q = q.filter(Proposal.instance_id == ifilter.get_instance().id) if not include_deleted: q = q.filter(or_(Proposal.delete_time == None, # noqa Proposal.delete_time > datetime.utcnow())) return q.limit(1).first() except Exception, e: log.warn("find(%s): %s" % (id, e)) return None
def similar_tags(cls, tag, limit=None): from tagging import Tagging from delegateable import Delegateable q = meta.Session.query(Tag) q = q.add_column(func.count(Tagging.id)) fst_tagging = aliased(Tagging) q = q.join(fst_tagging, Tag.taggings) q = q.join((Delegateable, fst_tagging.delegateable)) snd_tagging = aliased(Tagging) q = q.join((snd_tagging, Delegateable.taggings)) q = q.filter(snd_tagging.tag_id == tag.id) q = q.filter(Tag.id != tag.id) #q = q.filter(func.count(Tagging.id)>0) if ifilter.has_instance(): q = q.filter(Delegateable.instance_id == ifilter.get_instance().id) q = q.group_by(Tag.id, Tag.create_time, Tag.name) q = q.order_by(func.count(Tagging.id).desc()) # SQLAlchemy turns this into a f*****g subquery: #if limit is not None: # q = q.limit(limit) tags = [(k, v) for k, v in q.all() if v > 0] return tags[:limit]
def find(cls, user_name, instance_filter=True, include_deleted=False): from membership import Membership try: q = meta.Session.query(User) try: q = q.filter(User.id == int(user_name)) except ValueError: q = q.filter(User.user_name == unicode(user_name)) if not include_deleted: q = q.filter( or_(User.delete_time == None, User.delete_time > datetime.utcnow())) if ifilter.has_instance() and instance_filter: q = q.join(Membership) q = q.filter( or_(Membership.expire_time == None, Membership.expire_time > datetime.utcnow())) q = q.filter(Membership.instance == ifilter.get_instance()) return q.limit(1).first() except Exception, e: log.warn("find(%s): %s" % (user_name, e)) return None
def unusedTitle(cls, title, instance_filter=True, functions=None, include_deleted=False, selection=None): q = meta.Session.query(Page)\ .filter(Page.label == title) if not include_deleted: q = q.filter(or_(Page.delete_time == None, # noqa Page.delete_time > datetime.utcnow())) if ifilter.has_instance() and instance_filter: q = q.filter(Page.instance == ifilter.get_instance()) matches = q.all() if selection is not None: def same_selection(page): try: return (page.function == Page.DESCRIPTION and page.proposal.is_amendment and page.proposal.selection.page == selection) except Exception as e: log.warn(e) return False matches = filter(same_selection, matches) return not bool(matches)
def current_delegated(self, instance_filter=True): ds = filter(lambda d: not d.is_revoked(), self.delegated) if ifilter.has_instance() and instance_filter: ds = filter(lambda d: d.scope.instance == ifilter.get_instance(), ds) return ds