Skip to content

Commit

Permalink
Allow override of layer and envelope for pubsub channels. Closes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
robinharms committed Mar 7, 2024
1 parent af6bbc4 commit a0d302d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Changelog


## dev

* RecheckSubscriptionsSchema subscriptions changed from set to list to
fix common serialization problems.
* PubSub and context channels accepts arguments `envelope_name` and `layer_name` in case
they need to be overridden. (#2)

## 1.0.0 (2024-03-07)

Expand Down
4 changes: 3 additions & 1 deletion envelope/channels/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def get_channel(
) -> ContextChannel:
ch = get_context_channel(channel_type)
# This may cause errors right?
return ch(pk, consumer_name)
return ch(pk, consumer_channel=consumer_name)


@add_message(WS_INCOMING)
Expand All @@ -69,6 +69,8 @@ def get_app_state(self, channel: ContextChannel) -> list | None:
return list(app_state)

async def pre_queue(self, consumer: WebsocketConsumer, **kwargs) -> Subscribed:
if self.mm.consumer_name is None:
self.mm.consumer_name = consumer.channel_name
channel = self.get_channel(
self.data.channel_type, self.data.pk, self.mm.consumer_name
)
Expand Down
30 changes: 27 additions & 3 deletions envelope/channels/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,15 @@ def channel_name(self) -> str:
def __init__(
self,
consumer_channel: str | None = None,
*,
envelope_name: str | None = None,
layer_name: str | None = None,
):
self.consumer_channel = consumer_channel
if envelope_name:
self.envelope_name = envelope_name
if layer_name:
self.layer_name = layer_name

async def subscribe(self):
if not self.consumer_channel: # pragma: no coverage
Expand Down Expand Up @@ -98,9 +105,16 @@ def __init__(
self,
pk: int,
consumer_channel: str | None = None,
*,
envelope_name: str | None = None,
layer_name: str | None = None,
):
self.pk = pk
super().__init__(consumer_channel)
super().__init__(
consumer_channel=consumer_channel,
envelope_name=envelope_name,
layer_name=layer_name,
)

@property
def channel_name(self) -> str:
Expand All @@ -126,10 +140,20 @@ def permission(self) -> str | None:

@classmethod
def from_instance(
cls, instance: models.Model, consumer_channel: str | None = None
cls,
instance: models.Model,
consumer_channel: str | None = None,
*,
envelope_name: str | None = None,
layer_name: str | None = None,
) -> ContextChannel:
assert isinstance(instance, cls.model), f"Instance must be a {cls.model}"
inst = cls(instance.pk, consumer_channel)
inst = cls(
instance.pk,
consumer_channel=consumer_channel,
envelope_name=envelope_name,
layer_name=layer_name,
)
# Set context straight away to avoid lookup
inst.context = instance
return inst
Expand Down
1 change: 0 additions & 1 deletion envelope/deferred_jobs/tests/test_async_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ async def test_dispatch_disconnection_job_no_user(self):
async def test_maybe_update_connection(self):
self.mock_consumer.user_pk = self.user.pk
self.mock_consumer.connection_update_interval = 10
self.user = self.user

with patch(
"django_rq.queues.get_redis_connection",
Expand Down

0 comments on commit a0d302d

Please sign in to comment.