def get_hash_id(obj): return to_hash(obj.group_id)
def auto_approve_tasks(): now = timezone.now() if now.weekday() in [5, 6]: return 'WEEKEND' if now.weekday() == 0 and now.hour < 15: return 'MONDAY' cursor = connection.cursor() # noinspection SqlResolve query = ''' WITH taskworkers AS ( SELECT tw.id, p.id project_id, p.group_id project_gid, tw.task_id, u.id user_id, u.username, u_worker.username worker_username FROM crowdsourcing_taskworker tw INNER JOIN crowdsourcing_task t ON tw.task_id = t.id INNER JOIN crowdsourcing_project p ON t.project_id = p.id INNER JOIN auth_user u ON p.owner_id = u.id INNER JOIN auth_user u_worker ON tw.worker_id = u_worker.id WHERE tw.submitted_at + INTERVAL %(auto_approve_freq)s < NOW() AND tw.status=%(submitted)s) UPDATE crowdsourcing_taskworker tw_up SET status=%(accepted)s, approved_at = %(approved_at)s FROM taskworkers WHERE taskworkers.id=tw_up.id RETURNING tw_up.id, tw_up.worker_id, taskworkers.task_id, taskworkers.user_id, taskworkers.username, taskworkers.project_gid, taskworkers.worker_username ''' cursor.execute(query, {'submitted': models.TaskWorker.STATUS_SUBMITTED, 'accepted': models.TaskWorker.STATUS_ACCEPTED, 'approved_at': now, 'auto_approve_freq': '{} hour'.format(settings.AUTO_APPROVE_FREQ)}) task_workers = cursor.fetchall() for w in task_workers: task_workers.append({'id': w[0]}) post_approve.delay(w[2], 1) redis_publisher = RedisPublisher(facility='notifications', users=[w[4], w[6]]) message = RedisMessage( json.dumps({"event": 'TASK_APPROVED', "project_gid": w[5], "project_key": to_hash(w[5])})) redis_publisher.publish_message(message) return 'SUCCESS'