Skip to content

Instantly share code, notes, and snippets.

@henryl
Created November 17, 2010 20:11
Show Gist options
  • Save henryl/703983 to your computer and use it in GitHub Desktop.
Save henryl/703983 to your computer and use it in GitHub Desktop.
rpc handler
class TaskListRPCHandler(RPCHandler, MessageMixin):
"""Task list related RPC methods."""
@RPCMethod(['POST'])
def archive_tasks(self, tasklist_id):
"""Hide tasks from appearing in the default tasklist view."""
tasklist = self.ds.TaskList.find_by_id(tasklist_id)
if not tasklist: return False
if tasklist.creator != self.current_user._id: return False
tasklist.archive_tasks()
return True
@RPCMethod(['POST'])
def new_tasklist(self, title=None):
"""Create a new tasklist."""
u = self.current_user
tasklist = self.ds.TaskList({
"members":[{"id":u._id,"weight":0}],
"creator":u._id,
"title":title if title else "<untitled>",
"ts":datetime.now(),
"next_w":0L,
"is_default":False
})
tasklist.save()
self.set_flash("newtl", tasklist._id)
return tasklist._id
@RPCMethod(['POST'])
def rename_tasklist(self, tasklist_id, title):
"""Rename a tasklist."""
tasklist = self.ds.TaskList.find_by_id(tasklist_id)
if not tasklist: return False
# TODO: permissions
tasklist.title = title
tasklist.save()
return True
class TaskRPCHandler(RPCHandler, MessageMixin):
"""Task related RPC methods."""
@RPCMethod(['POST'])
def ping(self, message):
return message
@RPCMethod(['GET'])
def get_task(self, task_id):
# TODO: no cache headers.
return self.ds.Task.find_one({'_id':ObjectId(task_id)})
@RPCMethod(['POST'])
def parse_new_task(self, tasklist_id, taskstr):
tasklist = self.ds.TaskList.find_by_id(tasklist_id)
if not tasklist: return False
u = self.current_user
taskstr = taskstr.strip()
task = self.ds.Task.quick_task(u, taskstr, commit=True)
tasklist.add_task(task)
self.new_messages(["Graphd.modules.sendMsg('newTask', %s, %s)" % (json_encode(task), json_encode({
'windowId':int(self.get_argument('window_id')),
}))], to_user_ids=task.assign)
return str(task._id)
@RPCMethod(['POST'])
def mark_task(self, task_id, is_complete=True):
task = self.ds.Task.find_one({"_id":ObjectId(task_id)})
if is_complete:
task.complete(self.current_user)
else:
task.uncomplete(self.current_user)
self.new_messages(["Graphd.modules.sendMsg('markTask', %s, %s)" % (json_encode(task), json_encode({
'windowId':int(self.get_argument('window_id')),
}))], to_user_ids=task.assign)
return str(task.id)
@RPCMethod(['POST'])
def sync_property(self, model, id, prop_name, value):
if not self.ds.exists_model(model):
return False
task = self.ds.get_model(model).find_one({"_id":ObjectId(id)})
if task and prop_name in task:
task[prop_name] = value
task.save()
return True
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment