From 6e672a52d65e4be42613b3caaa60cdfa53d241c9 Mon Sep 17 00:00:00 2001 From: Sam Wolfe Date: Fri, 2 Aug 2024 13:51:12 -0700 Subject: [PATCH] Remove all references to old card update method --- djautotask/__init__.py | 2 +- djautotask/api.py | 49 +------------------------ djautotask/models.py | 67 ++-------------------------------- djautotask/tests/test_model.py | 27 +------------- 4 files changed, 7 insertions(+), 138 deletions(-) diff --git a/djautotask/__init__.py b/djautotask/__init__.py index 4ffc866..bb321c1 100644 --- a/djautotask/__init__.py +++ b/djautotask/__init__.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -VERSION = (1, 6, 0, 'final') +VERSION = (1, 6, 1, 'final') # pragma: no cover if VERSION[-1] != "final": diff --git a/djautotask/api.py b/djautotask/api.py index fb3556b..4ab2690 100644 --- a/djautotask/api.py +++ b/djautotask/api.py @@ -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): """ @@ -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): @@ -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): @@ -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' @@ -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' @@ -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' diff --git a/djautotask/models.py b/djautotask/models.py index 2c5bc44..b30f271 100644 --- a/djautotask/models.py +++ b/djautotask/models.py @@ -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, \ @@ -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) @@ -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. """ @@ -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) @@ -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) @@ -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) @@ -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( diff --git a/djautotask/tests/test_model.py b/djautotask/tests/test_model.py index 9cf3290..a097160 100644 --- a/djautotask/tests/test_model.py +++ b/djautotask/tests/test_model.py @@ -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):