Skip to content

Commit e506648

Browse files
authored
Merge pull request #4390 from rtfd/davidfischer/project-proxy-model-bug
Fix a proxy model bug related to ad-free
2 parents 1103473 + cd56a9c commit e506648

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

readthedocs/projects/models.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,12 +894,20 @@ def __init__(self, *args, **kwargs):
894894
pass
895895
super(APIProject, self).__init__(*args, **kwargs)
896896

897+
# Overwrite the database property with the value from the API
898+
self.ad_free = (not kwargs.pop('show_advertising', True))
899+
897900
def save(self, *args, **kwargs):
898901
return 0
899902

900903
def has_feature(self, feature_id):
901904
return feature_id in self.features
902905

906+
@property
907+
def show_advertising(self):
908+
"""Whether this project is ad-free (don't access the database)"""
909+
return not self.ad_free
910+
903911

904912
@python_2_unicode_compatible
905913
class ImportedFile(models.Model):

readthedocs/rtd_tests/tests/test_api.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from readthedocs.builds.models import Build, BuildCommandResult, Version
2121
from readthedocs.integrations.models import Integration
2222
from readthedocs.oauth.models import RemoteOrganization, RemoteRepository
23-
from readthedocs.projects.models import Feature, Project
23+
from readthedocs.projects.models import Feature, Project, APIProject
2424
from readthedocs.restapi.views.integrations import GitHubWebhookView
2525
from readthedocs.restapi.views.task_views import get_status_data
2626

@@ -564,6 +564,26 @@ def test_remote_organization_pagination(self):
564564
self.assertEqual(len(resp.data['results']), 25) # page_size
565565
self.assertIn('?page=2', resp.data['next'])
566566

567+
def test_init_api_project(self):
568+
project_data = {
569+
'name': 'Test Project',
570+
'slug': 'test-project',
571+
'show_advertising': True,
572+
}
573+
574+
api_project = APIProject(**project_data)
575+
self.assertEqual(api_project.slug, 'test-project')
576+
self.assertEqual(api_project.features, [])
577+
self.assertFalse(api_project.ad_free)
578+
self.assertTrue(api_project.show_advertising)
579+
580+
project_data['features'] = ['test-feature']
581+
project_data['show_advertising'] = False
582+
api_project = APIProject(**project_data)
583+
self.assertEqual(api_project.features, ['test-feature'])
584+
self.assertTrue(api_project.ad_free)
585+
self.assertFalse(api_project.show_advertising)
586+
567587

568588
class APIImportTests(TestCase):
569589

0 commit comments

Comments
 (0)