Example #1
0
 def test_wrong_type(self):
     self.app.settings.THORN_EVENT_CHOICES = 3
     with self.assertRaises(ImproperlyConfigured):
         event_choices(app=self.app)
Example #2
0
 def test(self):
     self.app.settings.THORN_EVENT_CHOICES = ('a.b', 'c.d', 'e.f')
     self.assertListEqual(
         event_choices(app=self.app),
         [('a.b', 'a.b'), ('c.d', 'c.d'), ('e.f', 'e.f')],
     )
Example #3
0
 def test(self, app):
     app.settings.THORN_EVENT_CHOICES = ('a.b', 'c.d', 'e.f')
     assert event_choices(app=app) == [('a.b', 'a.b'), ('c.d', 'c.d'),
                                       ('e.f', 'e.f')]
Example #4
0
 def test_wrong_type(self, app):
     app.settings.THORN_EVENT_CHOICES = 3
     with pytest.raises(ImproperlyConfigured):
         event_choices(app=app)
Example #5
0
 def test(self, app):
     app.settings.THORN_EVENT_CHOICES = ('a.b', 'c.d', 'e.f')
     assert event_choices(app=app) == [
         ('a.b', 'a.b'), ('c.d', 'c.d'), ('e.f', 'e.f')
     ]
Example #6
0
 def test_wrong_type(self, app):
     app.settings.THORN_EVENT_CHOICES = 3
     with pytest.raises(ImproperlyConfigured):
         event_choices(app=app)
Example #7
0
class Subscriber(models.Model, SubscriberModelMixin):
    objects = SubscriberManager()

    uuid = models.UUIDField(
        _('UUID'),
        default=uuid4,
        editable=False,
        unique=True,
        null=False,
        help_text=_('Unique identifier for this subscriber.'))

    event = models.CharField(
        _('event'),
        max_length=CHAR_MAX_LENGTH,
        choices=event_choices(),
        db_index=True,
        help_text=_('Name of event to connect with'),
    )

    url = models.URLField(
        _('URL'),
        max_length=CHAR_MAX_LENGTH,
        db_index=True,
        help_text=_('Callback URL'),
    )

    user = models.ForeignKey(
        django_settings.AUTH_USER_MODEL,
        related_name='%(app_label)s_%(class)s',
        null=True,
    )

    hmac_secret = models.TextField(
        _('HMAC Secret'),
        default=random_secret64,
        help_text=_('Specify HMAC secret for endpoints to verify'),
    )

    hmac_digest = models.CharField(
        _('HMAC Digest Type'),
        max_length=64,
        choices=[
            ('SHA512', 'sha512'),
            ('SHA256', 'sha256'),
            ('SHA1', 'sha1'),
        ],
        default='sha256',
        help_text=_('Specify HMAC digest type (use sha256 if uncertain)'),
    )

    content_type = models.CharField(
        _('content type'),
        max_length=CHAR_MAX_LENGTH,
        choices=zip(CONTENT_TYPES, CONTENT_TYPES),
        default=MIME_JSON,
        help_text='Desired content type for requests to this callback.')

    created_at = models.DateTimeField(_('created at'),
                                      editable=False,
                                      auto_now_add=True)

    updated_at = models.DateTimeField(
        _('updated_at'),
        editable=False,
        auto_now=True,
    )

    class Meta:
        verbose_name = _('subscriber')
        verbose_name_plural = _('subscriber')
        # ordering by hostname for ability to optimize for keepalive.
        ordering = ['url', '-created_at']
        get_latest_by = 'updated_at'
        unique_together = ('url', 'event')

    def user_ident(self):
        return self.user and self.user.pk

    def __str__(self):
        return '{0} -> {1}'.format(
            self.event,
            text.Truncator(self.url).chars(43),
        )