Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/quickstart-django.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ class CounterReflex(Reflex):
```
{% endcode %}

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.
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)).

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.

Yes, it really is that simple.

Expand Down
2 changes: 1 addition & 1 deletion docs/reflexes.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Server side reflexes inherit from `sockpuppet.Reflex`. They hold logic responsib
* Sockpuppet: the name of this project, which has a JS websocket client and a django based server component, which is based on django-channels.
* Stimulus: an incredibly simple yet powerful JS framework by the creators of Rails
* "a Reflex": used to describe the full, round-trip life-cycle of a Sockpuppet operation, from client to server and back again
* 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.
* 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.
* 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
* 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
* 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
Expand Down
30 changes: 7 additions & 23 deletions sockpuppet/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from importlib import import_module
from functools import wraps
import inspect
from os import walk, path
import sys
from urllib.parse import urlparse

Expand Down Expand Up @@ -117,28 +116,13 @@ def append_reflex():
}
)

modpath = config.module.__path__[0]

for dirpath, dirnames, filenames in walk(modpath):
if dirpath == modpath and 'reflexes.py' in filenames:
# classes in reflexes.py
import_path = '{}.reflexes'.format(config.name)
import_module(import_path)
append_reflex()

elif dirpath == path.join(modpath, 'reflexes'):
# assumes reflexes folder is placed directly in app.
import_path = '{config_name}.reflexes.{reflex_file}'

for filename in filenames:
# eliminates empty values in the filename before getting the
# module name from the filename.
name = [file for file in filename.split('.') if file][0]
full_import_path = import_path.format(
config_name=config.name, reflex_file=name
)
import_module(full_import_path)
append_reflex()
reflex_module_path = f'{config.name}.reflexes'
try:
import_module(reflex_module_path)
append_reflex()
except ModuleNotFoundError:
# No reflexes.py or reflexes module was found in the app
pass

def reflex_message(self, data, **kwargs):
logger.debug('Json: %s', data)
Expand Down
7 changes: 7 additions & 0 deletions tests/example/reflexes/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from .example_reflex import ExampleReflex, DecrementReflex, ParamReflex

__all__ = [
'ExampleReflex',
'DecrementReflex',
'ParamReflex'
]