Change form field from 'Char' to 'Choice' field
@@ -10,6 +10,7 @@ from django.utils.translation import ugettext_lazy as _
from readthedocs.payments.forms import StripeModelForm, StripeResourceMixin
from readthedocs.projects.models import Project
+from readthedocs.projects.models import Project
from .models import LEVEL_CHOICES, GoldUser
@@ -82,15 +83,21 @@ class GoldSubscriptionForm(StripeResourceMixin, StripeModelForm):
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=None, *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', '')
