Ejemplo n.º 1
0
    def find_one_by_group_id(self, group_id):
        """
        Lazy-loads a single :class:`Group` by ID

        :param group_id: The group identifier
        :type group_id: :class:`int` or :class:`str`
        :rtype: :class:`Group` or :class:`None`

        Usage::

            >>> from emma.model.account import Account
            >>> acct = Account(1234, "08192a3b4c5d6e7f", "f7e6d5c4b3a29180")
            >>> acct.groups.find_one_by_group_id(0) # does not exist
            raises <KeyError>
            >>> acct.groups.find_one_by_group_id(123)
            <Group>
            >>> acct.groups[123]
            <Group>
        """
        group_id = int(group_id)
        path = '/groups/%s' % group_id
        if group_id not in self._dict:
            group = emma.model.group
            raw = self.account.adapter.get(path)
            if raw:
                self._dict[group_id] = group.Group(self.account, raw)

        return (group_id in self._dict) and self._dict[group_id] or None
Ejemplo n.º 2
0
    def fetch_all(self):
        """
        Lazy-loads the full set of :class:`Group` objects

        :rtype: :class:`dict` of :class:`Group` objects

        Usage::

            >>> from emma.model.account import Account
            >>> acct = Account(1234, "08192a3b4c5d6e7f", "f7e6d5c4b3a29180")
            >>> mlng = acct.mailings[123]
            >>> mlng.groups.fetch_all()
            {123: <Group>, 321: <Group>, ...}

        """
        if 'mailing_id' not in self.mailing:
            raise ex.NoMailingIdError()
        group = emma.model.group
        path = '/mailings/%s/groups' % self.mailing['mailing_id']
        if not self._dict:
            self._dict = dict(
                (x['group_id'], group.Group(self.mailing.account, x))
                    for x in self.mailing.account.adapter.paginated_get(path))
        return self._dict