Skip to content

Commit d7d03ef

Browse files
authored
Merge pull request #4841 from dojutsu-user/make-form-adopting-project-choice-field
Make form for adopting project a choice field
2 parents fb3c875 + 3d65fd0 commit d7d03ef

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

readthedocs/gold/forms.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,21 @@ def get_subscription(self):
8282

8383

8484
class GoldProjectForm(forms.Form):
85-
project = forms.CharField(
85+
project = forms.ChoiceField(
8686
required=True,
87-
help_text='Enter the project\'s slug'
87+
help_text='Select a project.'
8888
)
8989

90-
def __init__(self, *args, **kwargs):
90+
def __init__(self, active_user, *args, **kwargs):
9191
self.user = kwargs.pop('user', None)
9292
self.projects = kwargs.pop('projects', None)
9393
super(GoldProjectForm, self).__init__(*args, **kwargs)
94+
self.fields['project'].choices = self.generate_choices(active_user)
95+
96+
def generate_choices(self, active_user):
97+
queryset = Project.objects.filter(users=active_user)
98+
choices = ((proj.slug, str(proj)) for proj in queryset)
99+
return choices
94100

95101
def clean_project(self):
96102
project_slug = self.cleaned_data.get('project', '')

readthedocs/gold/views.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,17 @@ def projects(request):
104104

105105
if request.method == 'POST':
106106
form = GoldProjectForm(
107-
data=request.POST, user=gold_user, projects=gold_projects)
107+
active_user=request.user, data=request.POST, user=gold_user, projects=gold_projects)
108108
if form.is_valid():
109109
to_add = Project.objects.get(slug=form.cleaned_data['project'])
110110
gold_user.projects.add(to_add)
111111
return HttpResponseRedirect(reverse('gold_projects'))
112112
else:
113-
form = GoldProjectForm()
113+
# HACK: active_user=request.user is passed
114+
# as argument to get the currently active
115+
# user in the GoldProjectForm which is used
116+
# to filter the choices based on the user.
117+
form = GoldProjectForm(active_user=request.user)
114118

115119
return render(
116120
request, 'gold/projects.html', {

readthedocs/rtd_tests/tests/test_gold.py

+2-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from django.core.urlresolvers import reverse
33
from django.test import TestCase
44

5-
from django_dynamic_fixture import get
5+
from django_dynamic_fixture import get, fixture
66

77
from readthedocs.gold.models import GoldUser, LEVEL_CHOICES
88
from readthedocs.projects.models import Project
@@ -14,7 +14,7 @@ class GoldViewTests(TestCase):
1414
def setUp(self):
1515
self.user = create_user(username='owner', password='test')
1616

17-
self.project = get(Project, slug='test')
17+
self.project = get(Project, slug='test', users=[fixture(), self.user])
1818

1919
self.golduser = get(GoldUser, user=self.user, level=LEVEL_CHOICES[0][0])
2020

@@ -26,13 +26,6 @@ def test_adding_projects(self):
2626
self.assertEqual(self.golduser.projects.count(), 1)
2727
self.assertEqual(resp.status_code, 302)
2828

29-
def test_incorrect_input_when_adding_projects(self):
30-
self.assertEqual(self.golduser.projects.count(), 0)
31-
incorrect_slug = 'xyz-random-incorrect-slug-xyz'
32-
self.assertEqual(Project.objects.filter(slug=incorrect_slug).count(), 0)
33-
resp = self.client.post(reverse('gold_projects'), data={'project': incorrect_slug})
34-
self.assertFormError(resp, form='form', field='project', errors='No project found.')
35-
3629
def test_too_many_projects(self):
3730
self.project2 = get(Project, slug='test2')
3831

0 commit comments

Comments
 (0)