diff --git a/readthedocs/projects/models.py b/readthedocs/projects/models.py index 59fd74a96f9..49aadf02bf6 100644 --- a/readthedocs/projects/models.py +++ b/readthedocs/projects/models.py @@ -894,12 +894,20 @@ def __init__(self, *args, **kwargs): pass super(APIProject, self).__init__(*args, **kwargs) + # Overwrite the database property with the value from the API + self.ad_free = (not kwargs.pop('show_advertising', True)) + def save(self, *args, **kwargs): return 0 def has_feature(self, feature_id): return feature_id in self.features + @property + def show_advertising(self): + """Whether this project is ad-free (don't access the database)""" + return not self.ad_free + @python_2_unicode_compatible class ImportedFile(models.Model): diff --git a/readthedocs/rtd_tests/tests/test_api.py b/readthedocs/rtd_tests/tests/test_api.py index 0f33b69697c..1148f07f3ff 100644 --- a/readthedocs/rtd_tests/tests/test_api.py +++ b/readthedocs/rtd_tests/tests/test_api.py @@ -20,7 +20,7 @@ from readthedocs.builds.models import Build, BuildCommandResult, Version from readthedocs.integrations.models import Integration from readthedocs.oauth.models import RemoteOrganization, RemoteRepository -from readthedocs.projects.models import Feature, Project +from readthedocs.projects.models import Feature, Project, APIProject from readthedocs.restapi.views.integrations import GitHubWebhookView from readthedocs.restapi.views.task_views import get_status_data @@ -564,6 +564,26 @@ def test_remote_organization_pagination(self): self.assertEqual(len(resp.data['results']), 25) # page_size self.assertIn('?page=2', resp.data['next']) + def test_init_api_project(self): + project_data = { + 'name': 'Test Project', + 'slug': 'test-project', + 'show_advertising': True, + } + + api_project = APIProject(**project_data) + self.assertEqual(api_project.slug, 'test-project') + self.assertEqual(api_project.features, []) + self.assertFalse(api_project.ad_free) + self.assertTrue(api_project.show_advertising) + + project_data['features'] = ['test-feature'] + project_data['show_advertising'] = False + api_project = APIProject(**project_data) + self.assertEqual(api_project.features, ['test-feature']) + self.assertTrue(api_project.ad_free) + self.assertFalse(api_project.show_advertising) + class APIImportTests(TestCase):