Skip to content
This repository has been archived by the owner on Feb 21, 2022. It is now read-only.

Commit

Permalink
Refactor several aspects of the code
Browse files Browse the repository at this point in the history
Now managers are not changing the local state, and just work remotely
and only append requests to the queue (thus they are suited for
applications that want a more stateless approach).

Also, the models' instances (objects) are using the aforementioned
managers to append requests to the queue, and additionally they are
modifying themselves, too.

And by having the objects reuse the managers functions, we avoid
having similar code twice (the appending of requests to the queue part).
  • Loading branch information
lefcha committed Jun 10, 2015
1 parent efa064d commit 197aad1
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 405 deletions.
3 changes: 1 addition & 2 deletions todoist/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
from todoist.managers.filters import FiltersManager
from todoist.managers.invitations import InvitationsManager
from todoist.managers.live_notifications import LiveNotificationsManager
from todoist.managers.notes import NotesManager
from todoist.managers.notes import NotesManager, ProjectNotesManager
from todoist.managers.projects import ProjectsManager
from todoist.managers.project_notes import ProjectNotesManager
from todoist.managers.items import ItemsManager
from todoist.managers.labels import LabelsManager
from todoist.managers.reminders import RemindersManager
Expand Down
24 changes: 7 additions & 17 deletions todoist/managers/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class FiltersManager(Manager, AllMixin, GetByIdMixin, SyncMixin):

def add(self, name, query, **kwargs):
"""
Adds a filter to the local state, and appends the equivalent request to
Creates a local filter object, and appends the equivalent request to
the queue.
"""
obj = models.Filter({'name': name, 'query': query}, self.api)
Expand All @@ -29,12 +29,9 @@ def add(self, name, query, **kwargs):

def update(self, filter_id, **kwargs):
"""
Updates filter, and appends the equivalent request to the queue.
Updates a filter remotely, by appending the equivalent request to the
queue.
"""
obj = self.get_by_id(filter_id)
if obj:
obj.data.update(kwargs)

args = {'id': filter_id}
args.update(kwargs)
cmd = {
Expand All @@ -46,12 +43,9 @@ def update(self, filter_id, **kwargs):

def delete(self, filter_id):
"""
Deletes filter, and appends the equivalent request to the queue.
Deletes a filter remotely, by appending the equivalent request to the
queue.
"""
obj = self.get_by_id(filter_id)
if obj:
self.state[self.state_name].remove(obj)

cmd = {
'type': 'filter_delete',
'uuid': self.api.generate_uuid(),
Expand All @@ -63,13 +57,9 @@ def delete(self, filter_id):

def update_orders(self, id_order_mapping):
"""
Updates in the local state the orders of multiple filters, and appends
the equivalent request to the queue.
Updates the orders of multiple filters remotely, by appending the
equivalent request to the queue.
"""
for filter_id in id_order_mapping.keys():
obj = self.get_by_id(filter_id)
if obj:
obj['item_order'] = id_order_mapping[filter_id]
cmd = {
'type': 'filter_update_orders',
'uuid': self.api.generate_uuid(),
Expand Down
91 changes: 20 additions & 71 deletions todoist/managers/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class ItemsManager(Manager, AllMixin, GetByIdMixin, SyncMixin):

def add(self, content, project_id, **kwargs):
"""
Adds an item to the local state, and appends the equivalent request to
the queue.
Creates a local item object, by appending the equivalent request to the
queue.
"""
obj = models.Item({'content': content, 'project_id': project_id},
self.api)
Expand All @@ -29,10 +29,10 @@ def add(self, content, project_id, **kwargs):
return obj

def update(self, item_id, **kwargs):
obj = self.get_by_id(item_id)
if obj:
obj.data.update(kwargs)

"""
Updates an item remotely, by appending the equivalent request to the
queue.
"""
args = {'id': item_id}
args.update(kwargs)
cmd = {
Expand All @@ -44,13 +44,9 @@ def update(self, item_id, **kwargs):

def delete(self, item_ids):
"""
Deletes items, and appends the equivalent request to the queue.
Deletes items remotely, by appending the equivalent request to the
queue.
"""
for item_id in item_ids:
obj = self.get_by_id(item_id)
if obj:
self.state[self.state_name].remove(obj)

cmd = {
'type': 'item_delete',
'uuid': self.api.generate_uuid(),
Expand All @@ -62,15 +58,9 @@ def delete(self, item_ids):

def move(self, project_items, to_project):
"""
Moves items to another project, and appends the equivalent request to
the queue.
Moves items to another project remotely, by appending the equivalent
request to the queue.
"""
for _, item_ids in project_items.items():
for item_id in item_ids:
obj = self.get_by_id(item_id)
if obj:
obj['project_id'] = to_project

cmd = {
'type': 'item_move',
'uuid': self.api.generate_uuid(),
Expand All @@ -83,15 +73,9 @@ def move(self, project_items, to_project):

def complete(self, project_id, item_ids, force_history=0):
"""
Marks items as completed, and appends the equivalent request to the
Marks items as completed remotely, by appending the equivalent request to the
queue.
"""
for item_id in item_ids:
obj = self.get_by_id(item_id)
if obj:
obj['checked'] = 1
obj['in_history'] = force_history

cmd = {
'type': 'item_complete',
'uuid': self.api.generate_uuid(),
Expand All @@ -106,20 +90,9 @@ def complete(self, project_id, item_ids, force_history=0):
def uncomplete(self, project_id, item_ids, update_item_orders=1,
restore_state=None):
"""
Marks items as not completed, and appends the equivalent request to the
Marks items as not completed remotely, by appending the equivalent request to the
queue.
"""
for item_id in item_ids:
obj = self.get_by_id(item_id)
if obj:
obj['checked'] = 0
obj['in_history'] = 0
if restore_state and item_id in restore_state:
obj['in_history'] = restore_state[item_id][0]
obj['checked'] = restore_state[item_id][1]
obj['item_order'] = restore_state[item_id][2]
obj['indent'] = restore_state[item_id][3]

args = {
'project_id': project_id,
'ids': item_ids,
Expand All @@ -137,15 +110,9 @@ def uncomplete(self, project_id, item_ids, update_item_orders=1,
def update_date_complete(self, item_id, new_date_utc, date_string,
is_forward):
"""
Completes a recurring task, and appends the equivalent request to the
queue.
Completes a recurring task remotely, by appending the equivalent
request to the queue.
"""
obj = self.get_by_id(item_id)
if obj:
obj['new_date_utc'] = new_date_utc
obj['date_string'] = date_string
obj['is_forward'] = is_forward

cmd = {
'type': 'item_update_date_complete',
'uuid': self.api.generate_uuid(),
Expand All @@ -160,16 +127,9 @@ def update_date_complete(self, item_id, new_date_utc, date_string,

def uncomplete_update_meta(self, project_id, ids_to_metas):
"""
Marks an item as completed in the local state, and appends the
equivalent request to the queue.
Marks an item as completed remotely, by appending the equivalent
request to the queue.
"""
for item_id in ids_to_metas.keys():
obj = self.get_by_id(item_id)
if obj:
obj['in_history'] = ids_to_metas[item_id][0]
obj['checked'] = ids_to_metas[item_id][1]
obj['item_order'] = ids_to_metas[item_id][2]

cmd = {
'type': 'item_uncomplete_update_meta',
'uuid': self.api.generate_uuid(),
Expand All @@ -182,15 +142,9 @@ def uncomplete_update_meta(self, project_id, ids_to_metas):

def update_orders_indents(self, ids_to_orders_indents):
"""
Updates in the local state the order and indents of multiple items, and
appends the equivalent request to the queue.
Updates the order and indents of multiple items remotely, by appending
the equivalent request to the queue.
"""
for item_id in ids_to_orders_indents.keys():
obj = self.get_by_id(item_id)
if obj:
obj['item_order'] = ids_to_orders_indents[item_id][0]
obj['indent'] = ids_to_orders_indents[item_id][1]

cmd = {
'type': 'item_update_orders_indents',
'uuid': self.api.generate_uuid(),
Expand All @@ -202,14 +156,9 @@ def update_orders_indents(self, ids_to_orders_indents):

def update_day_orders(self, ids_to_orders):
"""
Updates in the local state the day orders of multiple items, and
appends the equivalent request to the queue.
Updates in the local state the day orders of multiple items remotely,
by appending the equivalent request to the queue.
"""
for item_id in ids_to_orders.keys():
obj = self.get_by_id(item_id)
if obj:
obj['day_order'] = ids_to_orders[item_id]

cmd = {
'type': 'item_update_day_orders',
'uuid': self.api.generate_uuid(),
Expand Down
26 changes: 8 additions & 18 deletions todoist/managers/labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class LabelsManager(Manager, AllMixin, GetByIdMixin, SyncMixin):

def add(self, name, **kwargs):
"""
Registers a label in the local state, and appends the equivalent
request to the queue.
Creates a local label object, and appends the equivalent request to the
queue.
"""
obj = models.Label({'name': name}, self.api)
obj.temp_id = obj['id'] = self.api.generate_uuid()
Expand All @@ -29,12 +29,9 @@ def add(self, name, **kwargs):

def update(self, label_id, **kwargs):
"""
Updates label, and appends the equivalent request to the queue.
Updates a label remotely, by appending the equivalent request to the
queue.
"""
obj = self.get_by_id(label_id)
if obj:
obj.data.update(kwargs)

args = {'id': label_id}
args.update(kwargs)
cmd = {
Expand All @@ -46,12 +43,9 @@ def update(self, label_id, **kwargs):

def delete(self, label_id):
"""
Deletes label, and appends the equivalent request to the queue.
Deletes a label remotely, by appending the equivalent request to the
queue.
"""
obj = self.get_by_id(label_id)
if obj:
self.state[self.state_name].remove(obj)

cmd = {
'type': 'label_delete',
'uuid': self.api.generate_uuid(),
Expand All @@ -63,13 +57,9 @@ def delete(self, label_id):

def update_orders(self, id_order_mapping):
"""
Updates in the local state the orders of multiple labels, and appends
the equivalent request to the queue.
Updates the orders of multiple labels remotely, by appending the
equivalent request to the queue.
"""
for filter_id in id_order_mapping.keys():
obj = self.get_by_id(filter_id)
if obj:
obj['item_order'] = id_order_mapping[filter_id]
cmd = {
'type': 'label_update_orders',
'uuid': self.api.generate_uuid(),
Expand Down
1 change: 0 additions & 1 deletion todoist/managers/live_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def mark_as_read(self, seq_no):
Sets in the local state the last notification read, and appends the
equivalent request to the queue.
"""
self.state[self.state_name] = seq_no
cmd = {
'type': 'live_notifications_mark_as_read',
'uuid': self.api.generate_uuid(),
Expand Down

0 comments on commit 197aad1

Please sign in to comment.