Skip to content

Commit

Permalink
Merge pull request #4721 from dojutsu-user/gold-member-add-project-form
Browse files Browse the repository at this point in the history
Fix the form for adopting a project
  • Loading branch information
ericholscher committed Oct 17, 2018
2 parents 8916742 + 728c9d0 commit d06d47b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
11 changes: 11 additions & 0 deletions readthedocs/gold/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
from builtins import object
from django import forms

from django.utils.translation import ugettext_lazy as _

from readthedocs.payments.forms import StripeModelForm, StripeResourceMixin
from readthedocs.projects.models import Project

from .models import LEVEL_CHOICES, GoldUser

Expand Down Expand Up @@ -88,6 +91,14 @@ def __init__(self, *args, **kwargs):
self.projects = kwargs.pop('projects', None)
super(GoldProjectForm, self).__init__(*args, **kwargs)

def clean_project(self):
project_slug = self.cleaned_data.get('project', '')
project_instance = Project.objects.filter(slug=project_slug)
if not project_instance.exists():
raise forms.ValidationError(_('No project found.'))
else:
return project_slug

def clean(self):
cleaned_data = super(GoldProjectForm, self).clean()
if self.projects.count() < self.user.num_supported_projects:
Expand Down
7 changes: 7 additions & 0 deletions readthedocs/rtd_tests/tests/test_gold.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ def test_adding_projects(self):
self.assertEqual(self.golduser.projects.count(), 1)
self.assertEqual(resp.status_code, 302)

def test_incorrect_input_when_adding_projects(self):
self.assertEqual(self.golduser.projects.count(), 0)
incorrect_slug = 'xyz-random-incorrect-slug-xyz'
self.assertEqual(Project.objects.filter(slug=incorrect_slug).count(), 0)
resp = self.client.post(reverse('gold_projects'), data={'project': incorrect_slug})
self.assertFormError(resp, form='form', field='project', errors='No project found.')

def test_too_many_projects(self):
self.project2 = get(Project, slug='test2')

Expand Down

0 comments on commit d06d47b

Please sign in to comment.