Skip to content

Commit

Permalink
Remove all references to old card update method
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Wolfe committed Aug 2, 2024
1 parent da26ade commit 6e672a5
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 138 deletions.
2 changes: 1 addition & 1 deletion djautotask/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
VERSION = (1, 6, 0, 'final')
VERSION = (1, 6, 1, 'final')

# pragma: no cover
if VERSION[-1] != "final":
Expand Down
49 changes: 2 additions & 47 deletions djautotask/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,22 +427,6 @@ def _format_request_body(self, body, key, value):

return body

def _legacy_format_fields(self, api_entity, inserted_fields):
# TODO Used by models to update fields in Autotask
# NoteSynchronizer, TimeEntrySynchronizer,
# SecondaryResourceSynchronizer, ServicallSynchronizer
# (and child classes) still rely on this method.
# Once calls to those synchronizers update and create methods
# are updated, this method can be removed.
body = {'id': api_entity.id} if api_entity.id else dict()

for field, value in inserted_fields.items():
if field in api_entity.AUTOTASK_FIELDS:
key = api_entity.AUTOTASK_FIELDS[field]
body = self._format_request_body(body, key, value)

return body

def fetch_resource(self, next_url=None, retry_counter=None, method='get',
*args, **kwargs):
"""
Expand Down Expand Up @@ -648,7 +632,7 @@ def get_single(self, instance_id):
return response

def update(self, instance, changed_fields):
body = self._legacy_format_fields(instance, changed_fields)
body = self._format_fields(instance, changed_fields)
return self.request('patch', self.get_api_url(), body)

def create(self, instance, **kwargs):
Expand Down Expand Up @@ -677,9 +661,8 @@ def get_child_url(self, parent_id):
)

def update(self, instance, parent, changed_fields):
# Only for updating records with models in the DB, not Dummy syncs
endpoint_url = self.get_child_url(parent.id)
body = self._legacy_format_fields(instance, changed_fields)
body = self._format_fields(instance, changed_fields)
return self.request('patch', endpoint_url, body)

def create(self, instance, parent, **kwargs):
Expand Down Expand Up @@ -729,16 +712,6 @@ def count(self, next_url, *args, **kwargs):
# Make get request using Api conditions
return self.fetch_resource(next_url, *args, **kwargs)

def update(self, instance, changed_fields):
# TODO Can be moved to parent class once _legacy_format_fields is
# removed
body = self._format_fields(instance, changed_fields)
return self.request('patch', self.get_api_url(), body)

def legacy_update(self, instance, changed_fields):
body = self._legacy_format_fields(instance, changed_fields)
return self.request('patch', self.get_api_url(), body)


class BillingCodesAPIClient(AutotaskAPIClient):
API = 'BillingCodes'
Expand All @@ -762,18 +735,6 @@ class TasksAPIClient(ChildAPIMixin, AutotaskAPIClient):
CHILD_API = API
# For tasks, child API endpoint is the same

def update(self, instance, parent, changed_fields):
# TODO Can be moved to parent class once _legacy_format_fields is
# removed.
endpoint_url = self.get_child_url(parent.id)
body = self._format_fields(instance, changed_fields)
return self.request('patch', endpoint_url, body)

def legacy_update(self, instance, parent, changed_fields):
endpoint_url = self.get_child_url(parent.id)
body = self._legacy_format_fields(instance, changed_fields)
return self.request('patch', endpoint_url, body)


class TicketNotesAPIClient(ChildAPIMixin, AutotaskAPIClient):
API = 'TicketNotes'
Expand Down Expand Up @@ -816,12 +777,6 @@ class ProjectsAPIClient(AutotaskAPIClient):
def get(self, next_url, *args, **kwargs):
return self.fetch_resource(next_url, method='post', *args, **kwargs)

def update(self, instance, changed_fields):
# TODO Can be moved to parent class once _legacy_format_fields is
# removed.
body = self._format_fields(instance, changed_fields)
return self.request('patch', self.get_api_url(), body)


class TicketCategoriesAPIClient(AutotaskAPIClient):
API = 'TicketCategories'
Expand Down
67 changes: 3 additions & 64 deletions djautotask/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from django.db.models import Q
from django_extensions.db.models import TimeStampedModel
from django.utils import timezone
from djautotask import api
from model_utils import FieldTracker

from djautotask.api import ProjectsAPIClient, TaskNotesAPIClient, \
Expand All @@ -31,37 +30,7 @@ def duration(self):
return self.end_time - self.start_time


class ATUpdateMixin:

def get_updated_object(self, **kwargs):
changed_field_keys = kwargs.get('changed_fields')

updated_object = {}
if changed_field_keys:
for field in changed_field_keys:
updated_object[field] = getattr(self, field)

return updated_object

def save(self, *args, **kwargs):
"""
Save the object.
If update_at as a kwarg is True, then update Autotask with changes.
"""
changed_fields = kwargs.pop('changed_fields', None)
update_at = kwargs.pop('update_at', False)
impersonation_resource = kwargs.pop('impersonation_resource', None)

if update_at and changed_fields:
self.update_at(
impersonation_resource=impersonation_resource,
changed_fields=changed_fields
)

super().save(**kwargs)


class Ticket(ATUpdateMixin, TimeStampedModel):
class Ticket(TimeStampedModel):
ticket_number = models.CharField(blank=True, null=True, max_length=50)
completed_date = models.DateTimeField(blank=True, null=True)
create_date = models.DateTimeField(blank=True, null=True)
Expand Down Expand Up @@ -170,16 +139,6 @@ def __str__(self):
def get_account(self):
return self.account

def update_at(self, **kwargs):

api_client = api.TicketsAPIClient(
impersonation_resource=kwargs.get('impersonation_resource'),
)
return api_client.legacy_update(
self,
self.get_updated_object(**kwargs)
)


class AvailablePicklistManager(models.Manager):
""" Return only active Picklist objects. """
Expand Down Expand Up @@ -614,7 +573,7 @@ def get_queryset(self):
)


class Project(ATUpdateMixin, TimeStampedModel):
class Project(TimeStampedModel):
name = models.CharField(max_length=100)
number = models.CharField(null=True, max_length=50)
description = models.CharField(max_length=2000)
Expand Down Expand Up @@ -675,15 +634,6 @@ class Meta:
def __str__(self):
return self.name

def update_at(self, **kwargs):
api_client = api.ProjectsAPIClient(
impersonation_resource=kwargs.get('impersonation_resource'),
)
return api_client.update(
self,
self.get_updated_object(**kwargs)
)


class Phase(TimeStampedModel):
title = models.CharField(blank=True, null=True, max_length=255)
Expand Down Expand Up @@ -712,7 +662,7 @@ def __str__(self):
return self.title


class Task(ATUpdateMixin, TimeStampedModel):
class Task(TimeStampedModel):
MAX_DESCRIPTION = 8000
title = models.CharField(blank=True, null=True, max_length=255)
number = models.CharField(blank=True, null=True, max_length=50)
Expand Down Expand Up @@ -788,17 +738,6 @@ def __str__(self):
def get_account(self):
return getattr(self.project, 'account', None)

def update_at(self, **kwargs):

api_client = api.TasksAPIClient(
impersonation_resource=kwargs.get('impersonation_resource'),
)
return api_client.legacy_update(
self,
self.project,
self.get_updated_object(**kwargs),
)


class TaskSecondaryResource(TimeStampedModel):
resource = models.ForeignKey(
Expand Down
27 changes: 1 addition & 26 deletions djautotask/tests/test_model.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,11 @@
import pytz
from unittest.mock import patch

from django.test import TestCase
from django.utils import timezone
from django.test import override_settings
from datetime import timedelta

from djautotask.models import TimeEntry, OFFSET_TIMEZONE, Ticket
from . import mocks as mk


class TestTicket(TestCase):
API_URL = 'https://localhost/'

def setUp(self):
mk.init_api_rest_connection(return_value=self.API_URL)

def test_save_calls_update_at_when_kwarg_passed(self):
ticket = Ticket.objects.create(
title='test',
due_date_time=timezone.now(),
)
with patch('djautotask.api.TicketsAPIClient') as mock_ticketapiclient:
instance = mock_ticketapiclient.return_value
ticket.save()
self.assertFalse(instance.legacy_update.called)
ticket.save(update_at=True)
self.assertFalse(instance.legacy_update.called)
# 'update_at' is called
ticket.save(update_at=True,
changed_fields=['title', 'due_date_time'])
self.assertTrue(instance.legacy_update.called)
from djautotask.models import TimeEntry, OFFSET_TIMEZONE


class TestTimeEntry(TestCase):
Expand Down

0 comments on commit 6e672a5

Please sign in to comment.