class PollForm(forms.ModelForm): is_active = forms.BooleanField(required=False) backend = forms.ModelChoiceField(OrgBackend.objects.none(), required=False) title = forms.CharField(max_length=255, widget=forms.Textarea) category = CategoryChoiceField(Category.objects.none()) category_image = forms.ModelChoiceField(CategoryImage.objects.none(), required=False) def __init__(self, *args, **kwargs): self.org = kwargs["org"] del kwargs["org"] super(PollForm, self).__init__(*args, **kwargs) self.fields["category"].queryset = Category.objects.filter(org=self.org) self.fields["backend"].queryset = OrgBackend.objects.filter(org=self.org, is_active=True).order_by("slug") # only display category images for this org which are active self.fields["category_image"].queryset = CategoryImage.objects.filter( category__org=self.org, is_active=True ).order_by("category__name", "name") def clean(self): cleaned_data = self.cleaned_data if not self.org.backends.filter(is_active=True).exists(): raise ValidationError(_("Your org does not have any API token configuration.")) return cleaned_data class Meta: model = Poll fields = ("is_active", "is_featured", "backend", "title", "category", "category_image")
class StoryForm(forms.ModelForm): category = CategoryChoiceField(Category.objects.none()) def __init__(self, *args, **kwargs): self.org = kwargs["org"] del kwargs["org"] super(StoryForm, self).__init__(*args, **kwargs) # We show all categories even inactive one in the dropdown qs = Category.objects.filter(org=self.org).order_by("name") self.fields["category"].queryset = qs class Meta: model = Story fields = ( "is_active", "title", "featured", "summary", "content", "written_by", "audio_link", "video_id", "tags", "category", )
class PollForm(forms.ModelForm): is_active = forms.BooleanField(required=False) flow_uuid = forms.ChoiceField(choices=[]) poll_date = forms.DateTimeField(required=False) title = forms.CharField(max_length=255, widget=forms.Textarea) category = CategoryChoiceField(Category.objects.none()) category_image = forms.ModelChoiceField(CategoryImage.objects.none(), required=False) def __init__(self, *args, **kwargs): self.org = kwargs['org'] del kwargs['org'] super(PollForm, self).__init__(*args, **kwargs) self.fields['category'].queryset = Category.objects.filter( org=self.org) # find all the flows on this org, create choices for those flows = self.org.get_flows() self.fields['flow_uuid'].choices = [ (f['uuid'], f['name'] + " (" + f.get('date_hint', "--") + ")") for f in sorted(flows.values(), key=lambda k: k['name'].lower().strip()) ] # only display category images for this org which are active self.fields['category_image'].queryset = CategoryImage.objects.filter( category__org=self.org, is_active=True).order_by('category__name', 'name') def clean(self): cleaned_data = self.cleaned_data poll_date = cleaned_data.get('poll_date') flow_uuid = cleaned_data.get('flow_uuid') flows = self.org.get_flows() flow = flows.get(flow_uuid) if not poll_date and flow: date = flow.get('created_on', None) if date: poll_date = json_date_to_datetime(date) if not poll_date: poll_date = timezone.now() cleaned_data['poll_date'] = poll_date return cleaned_data class Meta: model = Poll fields = ('is_active', 'is_featured', 'flow_uuid', 'title', 'poll_date', 'category', 'category_image')
class CategoryImageForm(forms.ModelForm): category = CategoryChoiceField(Category.objects.none()) def __init__(self, *args, **kwargs): self.org = kwargs['org'] del kwargs['org'] super(CategoryImageForm, self).__init__(*args, **kwargs) self.fields['category'].queryset = Category.objects.filter(org=self.org).order_by('name') class Meta: model = CategoryImage fields = ('is_active', 'name', 'category', 'image')
class CategoryImageForm(forms.ModelForm): category = CategoryChoiceField(Category.objects.none()) def __init__(self, *args, **kwargs): self.org = kwargs["org"] del kwargs["org"] super(CategoryImageForm, self).__init__(*args, **kwargs) self.fields["category"].queryset = Category.objects.filter( org=self.org).order_by("name") class Meta: model = CategoryImage fields = ("is_active", "name", "category", "image")
class NewsForm(forms.ModelForm): category = CategoryChoiceField(Category.objects.none()) def __init__(self, *args, **kwargs): self.org = kwargs['org'] del kwargs['org'] super(NewsForm, self).__init__(*args, **kwargs) self.fields['category'].queryset = Category.objects.filter( org=self.org).order_by('name') class Meta: model = NewsItem fields = ('is_active', 'title', 'description', 'link', 'category', 'org')
class VideoForm(forms.ModelForm): category = CategoryChoiceField(Category.objects.none()) def __init__(self, *args, **kwargs): self.org = kwargs["org"] del kwargs["org"] super(VideoForm, self).__init__(*args, **kwargs) self.fields["category"].queryset = Category.objects.filter( org=self.org).order_by("name") class Meta: model = Video fields = ("is_active", "title", "description", "video_id", "category", "org")
class StoryForm(forms.ModelForm): category = CategoryChoiceField(Category.objects.none()) def __init__(self, *args, **kwargs): self.org = kwargs['org'] del kwargs['org'] super(StoryForm, self).__init__(*args, **kwargs) # We show all categories even inactive one in the dropdown qs = Category.objects.filter(org=self.org).order_by('name') self.fields['category'].queryset = qs class Meta: model = Story fields = ('is_active', 'title', 'featured', 'summary', 'content', 'written_by', 'audio_link', 'video_id', 'tags', 'category')