0

I built a whatsapp chatbot that takes in keywords and returns some output dependent on the keyword sent. The data is then sent to a database. I would like to count the number of times the userid of the person sending the keyword appears in my database today. Userid in this context is the person's number.

I started off by just simply trying to count how many times a number appears, but cannot get this first part right

Here is a sample of my code

class Users(db.Model):
    __tablename__ = 'users'
    cell_number = db.Column(db.Integer, primary_key = True)
    interaction_date = db.Column(db.DateTime, default = datetime.now(pytz.timezone('Africa/Harare')))
    request_key = db.Column(db.String(64000))
    counter = db.Column(db.Integer)
...
    total = user_object.query.filter_by(user_object.cell_number).filter_by(user_object.interaction_date == func.current_date()).count()

This returns the error message:

TypeError: filter_by() takes 1 positional argument but 2 were given

UPDATE

After reading the comment sent I made a few changes to my code which changed the issue slightly, so now I am returning a count for userids, however I cannot seem to filter for interactions that occurred today.

Here is my modified code:

interaction_date = Users.interaction_date
        current = datetime.now(pytz.timezone('Africa/Harare')).strftime('%d-%m-%y')
        total = Users.query.filter(user_object.cell_number == cleaned_number).filter(interaction_date == current).count()

This however returns a count of 0 despite there being atleast 17 interactions with the number i am using. Here is a sample of my database

cell_number  interaction_date      request_key 
123          2020-03-25 13:51:44    xv
123          2020-03-25 13:51:50    cv
121          2020-03-24 22:22:30    dfdf

Its important to add that interaction_date returns the value None, I am not sure why I am not retrieving the interaction_date value from my database

Emm
  • 2,367
  • 3
  • 24
  • 50

1 Answers1

0

You are comparing a datetime object to a string in the filter below because you are using strftime method to convert a datetime object to a string in the datetime.now() method.

current = datetime.now(pytz.timezone('Africa/Harare')).strftime('%d-%m-%y')
total = Users.query.filter(user_object.cell_number == cleaned_number).filter(interaction_date == current).count()

So remove the strftime method call and compare the days from the datetime object directly.

current = datetime.now(pytz.timezone('Africa/Harare'))
total = Users.query.filter(user_object.cell_number == cleaned_number).filter(interaction_date.day == current.day).count()
  • The problem with this is that interaction_date seems to return the value None. – Emm Mar 25 '20 at 18:18
  • `interaction_date = Users.interaction_date` this returns None?. If not can you give me an example of the this variable. Asking cause I was under the impression that `interaction_date` variable returns a datetime object. – prajwal k Hebbar Mar 25 '20 at 18:28
  • Its a datetime in the mysql databas, but I get a NoneType in flask – Emm Mar 25 '20 at 18:49
  • Try `total = Users.query.filter(user_object.cell_number == cleaned_number).filter(Users.interaction_date.day == current.day).count()`. – prajwal k Hebbar Mar 25 '20 at 19:37
  • You can also try `total = Users.query.filter(user_object.cell_number == cleaned_number, Users.interaction_date == current.day).count()` – prajwal k Hebbar Mar 25 '20 at 19:39