Skip to content

Commit

Permalink
Ensure that retrieve_timestamp returns a string and not bytes (#1970)
Browse files Browse the repository at this point in the history
Fixes: [#1969](#1969)
  • Loading branch information
cravindra authored Mar 21, 2020
1 parent 3fb38dc commit 2736557
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
22 changes: 22 additions & 0 deletions cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,28 @@ def fix_task_date():
db.engine.execute(query, created=fixed_created, id=task.id)


def fix_task_run_created_date():
"""Fix Date format in Task."""
import re
from datetime import datetime
with app.app_context():
query = text(
'''SELECT id, created FROM task_run WHERE created LIKE ('\x%')''')
results = db.engine.execute(query)
task_runs = results.fetchall()
for task_run in task_runs:
# It's a hex string
try:
hex_check = task_run.created.replace('\\x', '')
int(hex_check, 16)
fixed_created = bytes.fromhex(hex_check).decode()
except ValueError:
fixed_created = task_run.created
print(fixed_created)
query = text('''UPDATE task_run SET created=:created WHERE id=:id''')
db.engine.execute(query, created=fixed_created, id=task_run.id)


def delete_hard_bounces():
'''Delete fake accounts from hard bounces.'''
del_users = 0
Expand Down
3 changes: 2 additions & 1 deletion pybossa/contributions_guard.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ def check_task_stamped(self, task, user):

def retrieve_timestamp(self, task, user):
key = self._create_key(task, user)
return self.conn.get(key)
timestamp = self.conn.get(key)
return timestamp and timestamp.decode()

def _create_key(self, task, user):
user_id = user['user_id'] or user['user_ip']
Expand Down
6 changes: 3 additions & 3 deletions test/test_contributions_guard.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ def test_retrieve_timestamp_returns_None_for_non_stamped_task(self):
assert self.guard.retrieve_timestamp(self.task, self.auth_user) is None

@patch('pybossa.contributions_guard.make_timestamp')
def test_retrieve_timestamp_returs_the_timestamp_for_stamped_task(self, make_timestamp):
make_timestamp.return_value = b'now'
def test_retrieve_timestamp_returns_the_timestamp_for_stamped_task(self, make_timestamp):
make_timestamp.return_value = 'now'
self.guard.stamp(self.task, self.auth_user)

assert self.guard.retrieve_timestamp(self.task, self.auth_user) == b'now'
assert self.guard.retrieve_timestamp(self.task, self.auth_user) == 'now'

0 comments on commit 2736557

Please sign in to comment.