Esempio n. 1
0
class LawPalAbridgeService(object):
    """
    Wrapper for wrapping the abridge service
    allows us to disable it in testing and based on settings
    """
    ABRIDGE_ENABLED = False
    service = None

    def __init__(self, user, ABRIDGE_ENABLED=False, **kwargs):
        self.ABRIDGE_ENABLED = ABRIDGE_ENABLED

        assert user.__class__.__name__ == 'User', 'user must be a auth.User object'

        self.service = None  # reset to None by default
        if self.ABRIDGE_ENABLED:  # check if enabled
            self.service = AbridgeService(user=user)

    def create_event(self, content, content_group, content_block='default', **kwargs):
        if self.service is not None and self.ABRIDGE_ENABLED is True:
            self.service.create_event(content_block=content_block,
                                      content_group=content_group,
                                      content=content)

    @classmethod
    def render_reminder_template(cls, **kwargs):
        # used in ReminderService to create messages which are NOT also a notification
        context = template.loader.Context(kwargs)
        t = TEMPLATES.get('reminder')
        return t.render(context)

    @classmethod
    def render_message_template(cls, user, **kwargs):
        # used to render abridge-message with notification-templates
        verb_slug = kwargs.pop('verb_slug')
        message = kwargs.pop('message')

        t = get_notification_template(verb_slug)
        ctx = get_notification_context(kwargs, user)
        ctx.update({
            # 'notice_pk': notice.pk,
            # 'date': notice.message.date,
            'message': message,
        })

        context = template.loader.Context(ctx)
        return t.render(context)
Esempio n. 2
0
class LawPalAbridgeService(object):
    """
    Service to use when sending abridge events
    to the abridge service
    """
    user = None
    abridge = None
    content_group = None
    events = []

    def __init__(self, user, **kwargs):
        self.user = user
        self.events = []
        self.content_group = kwargs.get('content_group', 'General')

        self.abridge = AbridgeService(user=user,
                                      check_user=kwargs.get('check_user', False))

        logger.debug('Initialized LawPalAbridgeService')

    def append_card(self, content, **kwargs):
        logger.debug('Trying to append card to event.kwargs')

        date = datetime.datetime.utcnow()

        kwargs.update({
            'url': kwargs.get('url', False),
            'profile_photo': kwargs.get('profile_photo', False),
            'event_date': date,
            'natural_event_date': naturaltime(date),
        })

        card_service = CardService(content=content, **kwargs)

        logger.debug('Successfully appended card to event.kwargs')

        # send our rendered card through
        return card_service.preview()

    def add_event(self, content, **kwargs):
        """
        add our custom event object
        """
        content_group = kwargs.pop('content_group', self.content_group)

        user = kwargs.pop('user', self.user)

        user = Bunch(email=user.email, first_name=user.first_name, last_name=user.last_name)

        card = self.append_card(content=content, **kwargs)

        if card is not None:
            # if we have a card then return it and use it in the data
            kwargs.update({
                'card': card,
            })

        event = Bunch({
            'user': user,
            'content_group': content_group,
            'content': content,
            'data': kwargs
        })

        self.events.append(event)  # add teh event to be sent

        logger.debug('Added event {content_group}'.format(content_group=content_group))

    def send(self):
        """
        process our events and send to abridge service
        """
        logger.debug('Sending accumulated LawPalAbridgeService events')
        if self.events:

            for e in self.events:

                logger.debug('content_group: {content_group} user: {user}'.format(content_group=e.content_group, user=e.user.email))

                self.abridge.create_event(content_group=e.content_group,
                                          content=e.content,
                                          user=e.user,
                                          **e.data)