63 lines
2.6 KiB
Python
63 lines
2.6 KiB
Python
from django import forms
|
|
|
|
from .models import Article, Topic
|
|
|
|
|
|
class ArticleFilterForm(forms.Form):
|
|
q = forms.CharField(
|
|
required=False,
|
|
max_length=120,
|
|
label='Search',
|
|
widget=forms.TextInput(
|
|
attrs={
|
|
'placeholder': 'Search startup, AI, funding, product…',
|
|
'class': 'form-control form-control-lg search-input',
|
|
}
|
|
),
|
|
)
|
|
topic = forms.ChoiceField(
|
|
required=False,
|
|
label='Topic',
|
|
widget=forms.Select(attrs={'class': 'form-select form-select-lg'}),
|
|
)
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
topic_choices = [('', 'All topics')]
|
|
topic_choices.extend((topic.slug, topic.name) for topic in Topic.objects.order_by('name'))
|
|
self.fields['topic'].choices = topic_choices
|
|
|
|
|
|
class OriginalArticleForm(forms.ModelForm):
|
|
class Meta:
|
|
model = Article
|
|
fields = ['title', 'topic', 'author_name', 'excerpt', 'content', 'is_featured']
|
|
widgets = {
|
|
'title': forms.TextInput(attrs={'class': 'form-control form-control-lg', 'placeholder': 'Write a sharp headline'}),
|
|
'topic': forms.Select(attrs={'class': 'form-select form-select-lg'}),
|
|
'author_name': forms.TextInput(attrs={'class': 'form-control form-control-lg', 'placeholder': 'Editor or columnist name'}),
|
|
'excerpt': forms.Textarea(attrs={'class': 'form-control', 'rows': 3, 'placeholder': 'A short dek for cards and search results'}),
|
|
'content': forms.Textarea(attrs={'class': 'form-control', 'rows': 10, 'placeholder': 'Draft the story, analysis, or curated roundup'}),
|
|
'is_featured': forms.CheckboxInput(attrs={'class': 'form-check-input'}),
|
|
}
|
|
labels = {
|
|
'excerpt': 'Deck / summary',
|
|
'is_featured': 'Pin as featured story',
|
|
}
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
self.fields['topic'].queryset = Topic.objects.order_by('name')
|
|
self.fields['topic'].empty_label = 'Choose a topic'
|
|
self.fields['topic'].required = True
|
|
|
|
def clean(self):
|
|
cleaned_data = super().clean()
|
|
excerpt = cleaned_data.get('excerpt', '')
|
|
content = cleaned_data.get('content', '')
|
|
if len(excerpt.strip()) < 20:
|
|
self.add_error('excerpt', 'Please add at least 20 characters so cards feel editorially complete.')
|
|
if len(content.strip()) < 80:
|
|
self.add_error('content', 'Please write at least 80 characters to create a publishable story.')
|
|
return cleaned_data
|