Skip to content

Commit 6de91d8

Browse files
committed
Require reflexes to be defined in a reflexes.py module or a reflexes package for ease of discovery.
1 parent 8f2df11 commit 6de91d8

File tree

4 files changed

+18
-24
lines changed

4 files changed

+18
-24
lines changed

docs/quickstart-django.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ class CounterReflex(Reflex):
9090
```
9191
{% endcode %}
9292

93-
Sockpuppet maps your requests to Reflex classes that live in your `your_app/reflexes` folder or reflexes that exist in the file `your_app/reflex.py`. In this example, the increment method is executed and the count is incremented by 1. The `self.count` instance variable is passed to the template when it is re-rendered.
93+
Sockpuppet maps your requests to Reflex classes that live in your `your_app/reflexes.py` module, or in a `your_app/reflexes` package with an `__init__.py` file that imports all the Reflex subclasses in that directory (similar to [organizing Django models in a package](https://docs.djangoproject.com/en/3.1/topics/db/models/#organizing-models-in-a-package)).
94+
95+
In this example, the increment method is executed and the count is incremented by 1. The `self.count` instance variable is passed to the template when it is re-rendered.
9496

9597
Yes, it really is that simple.
9698

docs/reflexes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Server side reflexes inherit from `sockpuppet.Reflex`. They hold logic responsib
1111
* Sockpuppet: the name of this project, which has a JS websocket client and a django based server component, which is based on django-channels.
1212
* Stimulus: an incredibly simple yet powerful JS framework by the creators of Rails
1313
* "a Reflex": used to describe the full, round-trip life-cycle of a Sockpuppet operation, from client to server and back again
14-
* Reflex class: a python class that inherits from `sockpuppet.Reflex` and lives in your `reflexes` folder or `reflex.py`, this is where your Reflex actions are implemented.
14+
* Reflex class: a python class that inherits from `sockpuppet.Reflex` and lives in your `your_app/reflexes` package or `your_app/reflexes.py` file. This is where your Reflex actions are implemented.
1515
* Reflex action: a method in a Reflex class, called in response to activity in the browser. It has access to several special accessors containing all of the Reflex controller element's attributes
1616
* Reflex controller: a Stimulus controller that imports the StimulusReflex client library. It has a `stimulate` method for triggering Reflexes and like all Stimulus controllers, it's aware of the element it is attached to - as well as any Stimulus [targets](https://stimulusjs.org/reference/targets) in its DOM hierarchy
1717
* Reflex controller element: the DOM element upon which the `data-reflex` attribute is placed, which often has data attributes intended to be delivered to the server during a Reflex action

sockpuppet/consumer.py

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -117,28 +117,13 @@ def append_reflex():
117117
}
118118
)
119119

120-
modpath = config.module.__path__[0]
121-
122-
for dirpath, dirnames, filenames in walk(modpath):
123-
if dirpath == modpath and 'reflexes.py' in filenames:
124-
# classes in reflexes.py
125-
import_path = '{}.reflexes'.format(config.name)
126-
import_module(import_path)
127-
append_reflex()
128-
129-
elif dirpath == path.join(modpath, 'reflexes'):
130-
# assumes reflexes folder is placed directly in app.
131-
import_path = '{config_name}.reflexes.{reflex_file}'
132-
133-
for filename in filenames:
134-
# eliminates empty values in the filename before getting the
135-
# module name from the filename.
136-
name = [file for file in filename.split('.') if file][0]
137-
full_import_path = import_path.format(
138-
config_name=config.name, reflex_file=name
139-
)
140-
import_module(full_import_path)
141-
append_reflex()
120+
reflex_module_path = f'{config.name}.reflexes'
121+
try:
122+
import_module(reflex_module_path)
123+
append_reflex()
124+
except ModuleNotFoundError:
125+
# No reflexes.py or reflexes module was found in the app
126+
pass
142127

143128
def reflex_message(self, data, **kwargs):
144129
logger.debug('Json: %s', data)

tests/example/reflexes/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from .example_reflex import ExampleReflex, DecrementReflex, ParamReflex
2+
3+
__all__ = [
4+
'ExampleReflex',
5+
'DecrementReflex',
6+
'ParamReflex'
7+
]

0 commit comments

Comments
 (0)