jazzband / django-ddp

Django/PostgreSQL implementation of the Meteor server.
MIT License
167 stars 29 forks source link

Arguments to Publication.get_queries() #52

Open rantecki opened 8 years ago

rantecki commented 8 years ago

I'm running into issues when defining get_queries() on publications with additional/optional arguments.

This is the example from the docs of passing an argument to get_queries:

class BooksByAuthorEmail(Publication):
    def get_queries(self, author_email):
        return [
            models.Author.objects.filter(
                email=author_email,
            ),
            models.Book.objects.filter(
                author__email=author_email,
            ),
        ]

Looks straight-forward enough, however on experimentation it appears that an additional parameter is injected at the start (a named tuple called 'Env' containing the user and subscription created time). So I assumed that the docs were out of date, and it should actually be this:

class BooksByAuthorEmail(Publication):
    def get_queries(self, env, author_email):
        return [
            models.Author.objects.filter(
                email=author_email,
            ),
            models.Book.objects.filter(
                author__email=author_email,
            ),
        ]

The Env argument is actually quite handy. However, even then there appear to be times when get_queries is called with no arguments at all, or with False as the first parameter. The end result is that making any kind of assumption about arguments passed to get_queries quickly ends in a TypeError and the whole process freezes up.

Traceback (most recent call last):
  File "/home/richard/.pyenv/versions/playversity/src/dddp/dddp/websocket.py", line 276, in process_ddp
    self.dispatch(msg, data)
  File "/home/richard/.pyenv/versions/playversity/local/lib/python2.7/site-packages/django/utils/decorators.py", line 184, in inner
    return func(*args, **kwargs)
  File "/home/richard/.pyenv/versions/playversity/src/dddp/dddp/websocket.py", line 351, in dispatch
    handler(**kwargs)
  File "/home/richard/.pyenv/versions/playversity/src/dddp/dddp/websocket.py", line 476, in recv_method
    self.api.method(method, params, id_)
  File "/home/richard/.pyenv/versions/playversity/src/dddp/dddp/api.py", line 768, in method
    result = handler(*params)
  File "/home/richard/.pyenv/versions/playversity/src/dddp/dddp/accounts/ddp.py", line 469, in login
    return self.login_with_resume_token(params)
  File "/home/richard/.pyenv/versions/playversity/src/dddp/dddp/accounts/ddp.py", line 514, in login_with_resume_token
    self.do_login(user)
  File "/home/richard/.pyenv/versions/playversity/src/dddp/dddp/accounts/ddp.py", line 441, in do_login
    self.update_subs(user.pk)
  File "/home/richard/.pyenv/versions/playversity/src/dddp/dddp/accounts/ddp.py", line 267, in update_subs
    in API.sub_unique_objects(sub, params, pub)
  File "/home/richard/.pyenv/versions/playversity/src/dddp/dddp/api.py", line 629, in sub_unique_objects
    in pub.user_queries(sub.user, *params)
  File "/home/richard/.pyenv/versions/playversity/src/dddp/dddp/api.py", line 558, in user_queries
    return get_queries(*params)
TypeError: get_queries() takes at least 2 arguments (1 given)
TypeError('get_queries() takes at least 2 arguments (1 given)',)
> /home/richard/.pyenv/versions/playversity/src/dddp/dddp/api.py(558)user_queries()
    557         try:
--> 558             return get_queries(*params)
    559         finally:

Combining that with optional arguments makes the whole thing unworkable and I'm reduced to trying to derive what arguments are being passed from *args or **kwargs.

Am I missing something here?