Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comparing datetime #52

Closed
luisdemarchi opened this issue May 5, 2017 · 10 comments
Closed

Comparing datetime #52

luisdemarchi opened this issue May 5, 2017 · 10 comments

Comments

@luisdemarchi
Copy link

I need to return all inactive users, searching for more than 60 seconds without updating the information. But the return is always empty.

I've tried to put a simpler example by looking for the smaller (or larger) field than the current date. Both queries (greater equal or less equal) return empty.

>>> for user in User.query(User.last_seen <= datetime.datetime.now()):
...     print(user.last_seen)
...     (return NULL)

Listing all the records to ensure there is information:

>>> for user in User.all():
...     print(user.last_seen)
...     
2017-05-04 21:58:12.117880
2017-05-04 21:58:12.372056
2017-05-04 18:59:11.817694
2017-0....+50
@coleifer
Copy link
Owner

coleifer commented May 5, 2017

What field class are you using for last_seen?

@luisdemarchi
Copy link
Author

@coleifer Following is the actual structure

# Presence REDIS DB (no-SQL)
db = walrus.Database(host='localhost', port=6379, db=0)


class Presence(walrus.Model):
    database = db
    channel_name = walrus.TextField(primary_key=True)
    user = walrus.TextField(fts=True, index=True)
    last_seen = walrus.DateTimeField(default=datetime.datetime.now)
    is_accessible = walrus.BooleanField(index=True, default=False)

@coleifer
Copy link
Owner

coleifer commented May 9, 2017

Aha. The problem is the way you are associating the Presence model with the db object.

You should use this instead:

class Presence(walrus.Model):
    __database__ = db  # Double underscores around "database"
    channel_name ... etc.

@coleifer coleifer closed this as completed May 9, 2017
@luisdemarchi
Copy link
Author

luisdemarchi commented May 9, 2017

@coleifer Sorry, but it's in the documentation if this parameter is not set will take the default. So if I'm wrong the variable should pick the default. I made this change and got several different errors in each of the queries that worked before.

PS: Remembering that I am from Brazil and I am supported by the translator.

>>> Presence.create(channel_name="asdfhkasdlfkjashfdaskjhfsakjhdfa")
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Volumes/MacHD/Users/Git/playboy-connect-webservice/.env/lib/python3.6/site-packages/walrus/models.py", line 756, in create
    instance.save()
  File "/Volumes/MacHD/Users/Git/playboy-connect-webservice/.env/lib/python3.6/site-packages/walrus/models.py", line 942, in save
    self.delete(for_update=True)
  File "/Volumes/MacHD/Users/Git/playboy-connect-webservice/.env/lib/python3.6/site-packages/walrus/models.py", line 906, in delete
    original_instance = self.load(hash_key, convert_key=False)
  File "/Volumes/MacHD/Users/Git/playboy-connect-webservice/.env/lib/python3.6/site-packages/walrus/models.py", line 877, in load
    if not cls.database.hash_exists(primary_key):
AttributeError: 'NoneType' object has no attribute 'hash_exists'


>>> for presence in Presence.all():
...     print(presence.last_seen)
...     
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Volumes/MacHD/Users/Git/playboy-connect-webservice/.env/lib/python3.6/site-packages/walrus/models.py", line 777, in all
    for result in cls._query.all_index():
  File "/Volumes/MacHD/Users/Git/playboy-connect-webservice/.env/lib/python3.6/site-packages/walrus/models.py", line 374, in all_index
    return self.model_class.database.Set(self.make_key('all'))
AttributeError: 'NoneType' object has no attribute 'Set'

It is noteworthy that before I could read and write, I just could not return this query. So I find it strange to be a problem connecting to the bank.

@coleifer
Copy link
Owner

coleifer commented May 9, 2017

@luisdemarchi -- I think you must be running an older version of walrus. Can you please try upgrading? Depending on how you installed it, you can probably run pip install -U walrus.

@luisdemarchi
Copy link
Author

@coleifer:

pip install -U walrus
Requirement already up-to-date: walrus in ./.env/lib/python3.6/site-packages
Requirement already up-to-date: redis in ./.env/lib/python3.6/site-packages (from walrus)

@coleifer coleifer reopened this May 9, 2017
@coleifer
Copy link
Owner

coleifer commented May 9, 2017

I'm sorry -- I thought that I had released those changes (database -> __database__) but it turns out that they're just sitting in the master branch right now. So your code is correct for the current release. Reopening this to look into it some more.

@coleifer
Copy link
Owner

coleifer commented May 9, 2017

A-ha, I know what the issue is! You need to specify index=True to enable filtering using a field. In your case, the last_seen field needs to have an index.

class Presence(walrus.Model):
    __database__ = db  # If 0.4.0 or newer, use double-underscores.
    channel_name = walrus.TextField(primary_key=True)
    user = walrus.TextField(fts=True, index=True)
    last_seen = walrus.DateTimeField(default=datetime.datetime.now, index=True)  # Add "index=True".
    is_accessible = walrus.BooleanField(index=True, default=False)

Docs: http://walrus.readthedocs.io/en/latest/models.html#filtering-records

@coleifer coleifer closed this as completed May 9, 2017
@luisdemarchi
Copy link
Author

@coleifer Wow! Sorry for this grotesque mistake, this requirement is very clear in its documentation. Thanks a lot for the help. Once the app is ready and published, if possible I'll let you know;)

@coleifer
Copy link
Owner

Not a problem, I'm sorry I didn't notice it sooner myself. Glad it's working now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants