Exemple #1
0
    def _developers(self):
        """Returns a list of users who are developers of this client."""

        devs = Account._byID(list(self._developer_ids))
        return [
            dev for dev in devs.itervalues() if not (dev._deleted or dev._spam)
        ]
Exemple #2
0
    def get_participant_account(self):
        if self.is_internal:
            return None

        try:
            convo_participant = ModmailConversationParticipant.get_participant(
                self.id)
            participant = Account._byID(convo_participant.account_id)
        except NotFound:
            if not self.is_auto:
                raise
            return None

        if participant._deleted:
            raise NotFound

        return participant
Exemple #3
0
    def top_promoters(cls, start_date, end_date = None):
        end_date = end_date or datetime.datetime.now(g.tz)
        q = cls.for_date_range(start_date, end_date)

        d = start_date
        res = []
        accounts = Account._byID([i.account_id for i in q],
                                 return_dict = True, data = True)
        res = {}
        for i in q:
            if i.bid is not None and i.actual_start is not None:
                r = res.setdefault(i.account_id, [0, 0, set()])
                r[0] += i.bid
                r[1] += i.refund
                r[2].add(i.thing_name)
        res = [ ([accounts[k]] + v) for (k, v) in res.iteritems() ]
        res.sort(key = lambda x: x[1] - x[2], reverse = True)

        return res
Exemple #4
0
    def top_promoters(cls, start_date, end_date=None):
        end_date = end_date or datetime.datetime.now(g.tz)
        q = cls.for_date_range(start_date, end_date)

        d = start_date
        res = []
        accounts = Account._byID([i.account_id for i in q],
                                 return_dict=True,
                                 data=True)
        res = {}
        for i in q:
            if i.bid is not None and i.actual_start is not None:
                r = res.setdefault(i.account_id, [0, 0, set()])
                r[0] += i.bid
                r[1] += i.refund
                r[2].add(i.thing_name)
        res = [([accounts[k]] + v) for (k, v) in res.iteritems()]
        res.sort(key=lambda x: x[1] - x[2], reverse=True)

        return res
Exemple #5
0
def update_sr_mods_modmail_icon(sr):
    """Helper method to set the modmail icon for mods with mail permissions
    for the passed sr.

    Method will lookup all moderators for the passed sr and query whether they
    have unread conversations that exist. If the user has unreads the modmail
    icon will be lit up, if they do not it will be disabled.

    Args:
    sr  -- Subreddit object to fetch mods with mail perms from
    """

    mods_with_perms = sr.moderators_with_perms()
    modmail_user_ids = [mod_id for mod_id, perms in mods_with_perms.iteritems()
                        if 'mail' in perms or 'all' in perms]

    mod_accounts = Account._byID(modmail_user_ids, ignore_missing=True,
                                 return_dict=False)

    mail_exists_by_user = (ModmailConversationUnreadState
                           .users_unreads_exist(mod_accounts))

    for mod in mod_accounts:
        set_modmail_icon(mod, bool(mail_exists_by_user.get(mod._id)))
Exemple #6
0
    def to_serializable(self, author=None):
        if not author:
            from r2.models import Account
            author = Account._byID(self.account_id)

        name = author.name
        author_id = author._id
        if author._deleted:
            name = '[deleted]'
            author_id = None

        return {
            'id': to36(self.id),
            'author': {
                'id': author_id,
                'name': name,
                'isAdmin': author.employee,
                'isMod': True,
                'isHidden': False,
                'isDeleted': author._deleted
            },
            'actionTypeId': self.action_type_id,
            'date': self.date.isoformat(),
        }
Exemple #7
0
    def _developers(self):
        """Returns a list of users who are developers of this client."""

        devs = Account._byID(list(self._developer_ids))
        return [dev for dev in devs.itervalues()
                if not (dev._deleted or dev._spam)]
Exemple #8
0
    def _developers(self):
        """Returns a list of users who are developers of this client."""

        devs = Account._byID(list(self._developer_ids), return_dict=False)
        return [dev for dev in devs if not dev._deleted]
Exemple #9
0
    def _developers(self):
        """Returns a list of users who are developers of this client."""

        devs = Account._byID(list(self._developer_ids), return_dict=False)
        return [dev for dev in devs if not dev._deleted]
Exemple #10
0
    def to_serializable(self, authors_dict=None, entity=None,
                        all_messages=False, current_user=None):
        # Lookup authors if they are not passed
        if not authors_dict:
            from r2.models import Account
            authors_dict = Account._byID(
                    set(self.author_ids) | set(self.mod_action_account_ids),
                    return_dict=True)

        # Lookup entity if it is not passed
        if not entity:
            entity = Subreddit._by_fullname(self.owner_fullname)

        serializable_authors = []

        for message in self.messages:
            author = authors_dict.get(message.author_id)
            serializable_authors.append(
                to_serializable_author(author, entity,
                                       current_user,
                                       is_hidden=message.is_author_hidden)
            )

        last_unread = getattr(self, 'last_unread', None)
        if last_unread is not None:
            last_unread = last_unread.isoformat()

        parsed_last_user_update = None
        parsed_last_mod_update = None

        min_tz_aware_datetime = datetime.min.replace(tzinfo=g.tz)
        if self.last_user_update != min_tz_aware_datetime:
            parsed_last_user_update = self.last_user_update.isoformat()

        if self.last_mod_update != min_tz_aware_datetime:
            parsed_last_mod_update = self.last_mod_update.isoformat()

        result_dict = {
            'state': self.state,
            'lastUpdated': self.last_updated.isoformat(),
            'lastUserUpdate': parsed_last_user_update,
            'lastModUpdate': parsed_last_mod_update,
            'lastUnread': last_unread,
            'isInternal': self.is_internal,
            'isAuto': self.is_auto,
            'numMessages': self.num_messages,
            'owner': {
                'id': self.owner_fullname,
                'type': entity._type_name,
                'displayName': entity.name
            },
            'isHighlighted': self.is_highlighted,
            'id': to36(self.id),
            'subject': self.subject,
            'authors': serializable_authors,
        }

        if all_messages:
            for mod_action in self.mod_actions:
                author = authors_dict.get(mod_action.account_id)
                serializable_authors.append(
                    to_serializable_author(author, entity,
                                           current_user,
                                           is_hidden=False)
                )

            result_dict.update({
                'objIds': self.ordered_msg_and_action_ids,
                'messages': {
                    to36(message.id): message.to_serializable(
                        entity,
                        authors_dict.get(message.author_id),
                        current_user
                    )
                    for message in self.messages
                },
                'modActions': {
                    to36(mod_action.id): mod_action.to_serializable()
                    for mod_action in self.mod_actions
                }
            })
        else:
            result_dict.update({
                'objIds': [
                    {'key': 'messages', 'id': to36(self.messages[0].id)}
                ]
            })

        return result_dict