Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from . import base
from groupy import utils
# use a class registry to enable factory creation of attachment objects
class AttachmentMeta(type):
_types = {}
def __init__(cls, name, bases, attrs):
cls._types[name.lower()] = cls
class Attachment(base.Resource, metaclass=AttachmentMeta):
"""Base attachment class.
Every attachment has a type and additional data.
:param str type: attachment type
:param kwargs data: additional attachment data
"""
def __init__(self, type, **data):
data['type'] = type
self.data = data
def to_json(self):
"""Return the attachment as JSON serializable dict.
:return: serializable attachment data
return response.ok
def destroy(self, bot_id):
"""Destroy a bot.
:param str bot_id: the ID of the bot to destroy
:return: ``True`` if successful
:rtype: bool
"""
url = utils.urljoin(self.url, 'destroy')
payload = {'bot_id': bot_id}
response = self.session.post(url, json=payload)
return response.ok
class Bot(base.ManagedResource):
"""A bot."""
def __repr__(self):
klass = self.__class__.__name__
return '<{}(name={!r})>'.format(klass, self.name)
def __eq__(self, other):
return self.bot_id == other.bot_id
def post(self, text, attachments=None):
"""Post a message as the bot.
:param str text: the text of the message
:param attachments: a list of attachments
:type attachments: :class:`list`
:return: ``True`` if successful
def __init__(self, group_id, owner_id, status):
self.group_id = group_id
self.owner_id = owner_id
self.status = status
self.reason = self.status_texts.get(status, 'unknown')
@property
def is_success(self):
"""Return ``True`` if the request was successful."""
return self.status == self.success_code
def __bool__(self):
return self.is_success
class Group(base.ManagedResource):
"""A group."""
def __init__(self, manager, **data):
super().__init__(manager, **data)
self.messages = messages.Messages(self.manager.session, self.id)
self.gallery = messages.Gallery(self.manager.session, self.group_id)
self.leaderboard = messages.Leaderboard(self.manager.session, self.id)
self.memberships = memberships.Memberships(self.manager.session, self.id)
self._bots = bots.Bots(self.manager.session)
self._user = user.User(self.manager.session)
members = self.data.get('members') or []
self.members = [memberships.Member(self.manager, self.id, **m) for m in members]
self.created_at = utils.get_datetime(self.data['created_at'])
self.updated_at = utils.get_datetime(self.data['updated_at'])
def remove(self, membership_id):
"""Remove a member from the group.
:param str membership_id: the ID of a member in this group
:return: ``True`` if the member was successfully removed
:rtype: bool
"""
path = '{}/remove'.format(membership_id)
url = utils.urljoin(self.url, path)
payload = {'membership_id': membership_id}
response = self.session.post(url, json=payload)
return response.ok
class Member(base.ManagedResource):
"""A user's membership in a particular group.
Members have both an ID and a membership ID. The membership ID is unique
to the combination of user and group.
It can be helpful to think of a "memmber" as a "membership." That is, a
specific user in a specific group. Thus, two ``Member`` objects are
equal only if their ``id`` fields are equal,. As a consequence, the two
``Member`` objects representing user A in two groups X and Y will _not_
be equal.
:param manager: a manager for the group of the membership
:type manager: :class:`~groupy.api.base.Manager`
:param str group_id: the group_id of the membership
:param kwargs data: additional membership data
"""