Exemple #1
0
 def get_by_id(cls, id):
     id = int(id)
     if not Tag.cache_by_id.has_key(id):
         tag = Session.query(Tag).filter(Tag.id == id).one()
         Tag.cache_by_text[int(tag)] = tag
         Tag.cache_by_id[int(tag)] = tag
     return Tag.cache_by_id[id]
Exemple #2
0
 def get_by_id(cls, id):
     id = int(id)
     if not Tag.cache_by_id.has_key(id):
         tag = Session.query(Tag).filter(Tag.id == id).one()
         Tag.cache_by_text[int(tag)] = tag
         Tag.cache_by_id[int(tag)] = tag
     return Tag.cache_by_id[id]
Exemple #3
0
 def get_relationship(self, other_user, relationship):
     """Returns the requested relationship object, if it exists."""
     return Session.query(UserRelationship) \
            .filter_by(from_user_id=self.id,
                       to_user_id=other_user.id,
                       relationship=relationship) \
            .first()
Exemple #4
0
 def is_online(self):
     ip_log_q = Session.query(IPLogEntry).with_parent(self)
     last_log_entry = ip_log_q.order_by(IPLogEntry.end_time.desc()).first()
     if last_log_entry:
         return datetime.now() - last_log_entry.end_time < timedelta(0, 60 * 15)
     else:
         return False
Exemple #5
0
    def recent_notes(self):
        """
        Finds the most recent note in each of this user's conversations, in
        order from newest to oldest.  Returns a query object that can be paged
        however desired.
        """

        # Avoid import recursion
        from ferrox.model.db.messages import Note

        # Group-wise maximum, as inspired by the MySQL manual.  The only
        # difference between this and the example in the manual is that I add
        # the receipient's user id to the ON clause, ensuring that rows
        # belonging to each user are clustered together, and then filter out the
        # desired user with a WHERE.
        # I say "I" instead of "we" because this took me two days to figure out
        # and I think it's pretty f*****g cool -- enough that I am going to put
        # my name on it.                                             -- Eevee

        older_note_a = Note.__table__.alias()

        note_q = Session.query(Note).select_from(Note.__table__
            .outerjoin(older_note_a,
                sql.and_(
                    Note.original_note_id == older_note_a.c.original_note_id,
                    Note.to_user_id == older_note_a.c.to_user_id,
                    Note.time < older_note_a.c.time
                    )
                )
            ) \
            .filter(older_note_a.c.id == None) \
            .filter(Note.to_user_id == self.id) \
            .order_by(Note.time.desc())

        return note_q
Exemple #6
0
 def get_by_name(cls, username, eagerloads=[]):
     """Fetch a user, given eir username."""
     try:
         q = Session.query(cls).filter_by(username=username)
         for el in eagerloads:
             q = q.options(eagerload(el))
         return q.one()
     except InvalidRequestError:
         return None
Exemple #7
0
 def get_parent(self):
     """Returns this comment's parent."""
     if not hasattr(self, '_parent'):
         self._parent = Session.query(Comment) \
             .filter(Comment.discussion_id == self.discussion_id) \
             .filter(Comment.left < self.left) \
             .filter(Comment.right > self.left) \
             .order_by(Comment.left.desc()) \
             .first()
     return self._parent
Exemple #8
0
    def unread_note_count(self):
        """
        Returns the number of unread notes this user has.
        """

        # Eh.  Import recursion.  Note needs User, and we need Note here.
        from ferrox.model.db.messages import Note

        return Session.query(Note) \
            .filter(Note.to_user_id == self.id) \
            .filter(Note.status == 'unread') \
            .count()
Exemple #9
0
 def get_by_text(cls, text, create=False):
     text = Tag.sanitize(text)
     if not Tag.cache_by_text.has_key(text):
         try:
             tag = Session.query(Tag).filter(Tag.text == text).one()
             Tag.cache_by_id[tag.id] = tag
         except InvalidRequestError:
             if create:
                 # Need to create tag
                 tag = Tag(text=text)
                 Session.add(tag)
             else:
                 return None
         Tag.cache_by_text[text] = tag
     return Tag.cache_by_text[text]
Exemple #10
0
 def get_by_text(cls, text, create = False):
     text = Tag.sanitize(text)
     if not Tag.cache_by_text.has_key(text):
         try:
             tag = Session.query(Tag).filter(Tag.text == text).one()
             Tag.cache_by_id[tag.id] = tag
         except InvalidRequestError:
             if create:
                 # Need to create tag
                 tag = Tag(text=text)
                 Session.add(tag)
             else:
                 return None
         Tag.cache_by_text[text] = tag
     return Tag.cache_by_text[text]
Exemple #11
0
    def __init__(self, parent=None, **kwargs):
        """Creates a new comment.  If a parent is specified, the other
        comments in this discussion will have their left/right values adjusted
        appropriately.
        """

        super(Comment, self).__init__(**kwargs)

        self._parent = parent

        if not parent:
            # This comment is a brand new top-level one; its left and right
            # need to be higher than the highest right
            last_comment = Session.query(Comment) \
                .filter(Comment.discussion_id == self.discussion_id) \
                .order_by(Comment.right.desc()) \
                .first()
            if last_comment:
                self.left = last_comment.right + 1
                self.right = last_comment.right + 2
            else:
                # First comment at all
                self.left = 1
                self.right = 2

            return

        # Otherwise, we're replying to an existing comment.
        # The new comment's left should be the parent's old right, as it's
        # being inserted as the last descendant, and every left or right
        # value to the right of that should be bumped up by two.
        parent_right = parent.right

        self.left = parent_right
        self.right = parent_right + 1

        for side in ['left', 'right']:
            column = getattr(Comment.__table__.c, side)
            Session.execute(
                Comment.__table__.update(
                    and_(column >= parent_right,
                         Comment.discussion_id == self.discussion_id
                         ),
                    values={column: column + 2}
                    )
                )
Exemple #12
0
    def latest_note(self, recipient):
        """
        Returns the latest note in this note's thread, preferring one that was
        addressed to the provided recipient.
        """
        note_q = Session.query(Note).filter_by(original_note_id=self.original_note_id)
        latest_note = note_q.filter_by(to_user_id=recipient.id) \
            .order_by(Note.time.desc()) \
            .first()

        # TODO perf
        if not latest_note:
            # No note from this user on this thread
            latest_note = note_q \
                .order_by(Note.time.desc()) \
                .first()

        return latest_note
Exemple #13
0
    def metadatum(self, datum):
        """Returns an item from this user's metadata, with caching (so don't
        worry about calling this method repeatedly).

        Return value is a dict with 'description' and 'value' keys.
        """
        if not hasattr(self, '_metadata_cache'):
            self._metadata_cache = dict()
            for row in self.metadata:
                self._metadata_cache[row.field.key] = dict(
                    description=row.field.description,
                    value=row.value,
                    )

        if not datum in self._metadata_cache:
            datum_row = Session.query(UserMetadataField) \
                        .filter_by(key=datum) \
                        .one()
            self._metadata_cache[datum] = dict(
                description=datum_row.description,
                value='',
            )

        return self._metadata_cache[datum]
Exemple #14
0
 def can(self, permission):
     perm_q = Session.query(Permission) \
                     .with_parent(self.role) \
                     .filter_by(name=permission)
     return perm_q.count() > 0
Exemple #15
0
 def get_by_name(cls, name):
     """Fetch a role, given its name."""
     try:
         return Session.query(cls).filter_by(name=name).one()
     except InvalidRequestError:
         return None
Exemple #16
0
 def can(self, permission):
     perm_q = Session.query(Role).with_parent(self).filter(
         Role.permissions.any(name=permission)
     )
     return perm_q.count() > 0