Skip to content

Commit

Permalink
Merge pull request #4841 from dojutsu-user/make-form-adopting-project…
Browse files Browse the repository at this point in the history
…-choice-field

Make form for adopting project a choice field
  • Loading branch information
ericholscher committed Nov 1, 2018
2 parents fb3c875 + 3d65fd0 commit d7d03ef
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
12 changes: 9 additions & 3 deletions readthedocs/gold/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,21 @@ def get_subscription(self):


class GoldProjectForm(forms.Form):
project = forms.CharField(
project = forms.ChoiceField(
required=True,
help_text='Enter the project\'s slug'
help_text='Select a project.'
)

def __init__(self, *args, **kwargs):
def __init__(self, active_user, *args, **kwargs):
self.user = kwargs.pop('user', None)
self.projects = kwargs.pop('projects', None)
super(GoldProjectForm, self).__init__(*args, **kwargs)
self.fields['project'].choices = self.generate_choices(active_user)

def generate_choices(self, active_user):
queryset = Project.objects.filter(users=active_user)
choices = ((proj.slug, str(proj)) for proj in queryset)
return choices

def clean_project(self):
project_slug = self.cleaned_data.get('project', '')
Expand Down
8 changes: 6 additions & 2 deletions readthedocs/gold/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,17 @@ def projects(request):

if request.method == 'POST':
form = GoldProjectForm(
data=request.POST, user=gold_user, projects=gold_projects)
active_user=request.user, data=request.POST, user=gold_user, projects=gold_projects)
if form.is_valid():
to_add = Project.objects.get(slug=form.cleaned_data['project'])
gold_user.projects.add(to_add)
return HttpResponseRedirect(reverse('gold_projects'))
else:
form = GoldProjectForm()
# HACK: active_user=request.user is passed
# as argument to get the currently active
# user in the GoldProjectForm which is used
# to filter the choices based on the user.
form = GoldProjectForm(active_user=request.user)

return render(
request, 'gold/projects.html', {
Expand Down
11 changes: 2 additions & 9 deletions readthedocs/rtd_tests/tests/test_gold.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.core.urlresolvers import reverse
from django.test import TestCase

from django_dynamic_fixture import get
from django_dynamic_fixture import get, fixture

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

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

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

Expand All @@ -26,13 +26,6 @@ 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 d7d03ef

Please sign in to comment.