Changeset View
Changeset View
Standalone View
Standalone View
blog/forms.py
| from django import forms | from django import forms | ||||
| from blog.models import Revision, Post | from blog.models import Revision, Post | ||||
| from films.models import Film | from films.models import Film | ||||
| from static_assets.models import StorageLocation | |||||
| class PostAddForm(forms.ModelForm): | class PostAddForm(forms.ModelForm): | ||||
| # Post fields | # Post fields | ||||
| film = forms.ModelChoiceField(queryset=Film.objects.all(), required=False) | film = forms.ModelChoiceField(queryset=Film.objects.all(), required=False) | ||||
| slug = forms.SlugField( | slug = forms.SlugField( | ||||
| required=False, | required=False, | ||||
| help_text='Optional. If not provided, it will be created based on the title.', | help_text='Optional. If not provided, it will be created based on the title.', | ||||
| ) | ) | ||||
| # Revision fields | # Revision fields | ||||
| title = forms.CharField(max_length=512) | title = forms.CharField(max_length=512) | ||||
| topic = forms.CharField( | topic = forms.CharField( | ||||
| max_length=128, | max_length=128, | ||||
| help_text=f'Category of the announcement in the post; ' | help_text=f'Category of the announcement in the post; ' | ||||
| f'e.g. <cite>New Open Movie Announcement</cite>.', | f'e.g. <cite>New Open Movie Announcement</cite>.', | ||||
| ) | ) | ||||
| description = forms.CharField( | description = forms.CharField( | ||||
| max_length=512, | max_length=512, | ||||
| required=False, | required=False, | ||||
| help_text='An optional short description displayed on the blog card.', | help_text='An optional short description displayed on the blog card.', | ||||
| ) | ) | ||||
| content = forms.TextInput() | content = forms.TextInput() | ||||
| thumbnail = forms.ImageField() | thumbnail = forms.ImageField() | ||||
| storage_location = forms.ModelChoiceField( | |||||
| queryset=StorageLocation.objects.all(), | |||||
| required=False, | |||||
| help_text='If `Film` is set, it will be set automatically to the film\'s storage location.', | |||||
| ) | |||||
| is_published = forms.BooleanField(required=False) | is_published = forms.BooleanField(required=False) | ||||
| class Meta: | class Meta: | ||||
| model = Revision | model = Revision | ||||
| fields = ( | fields = ( | ||||
| 'film', | 'film', | ||||
| 'slug', | 'slug', | ||||
| 'is_published', | 'is_published', | ||||
| 'title', | 'title', | ||||
| 'topic', | 'topic', | ||||
| 'description', | 'description', | ||||
| 'content', | 'content', | ||||
| 'thumbnail', | 'thumbnail', | ||||
| 'storage_location', | |||||
| ) | ) | ||||
| def create_post(self) -> Post: | def create_post(self) -> Post: | ||||
| """ | """ | ||||
| Creates the revision's related Post instance without saving it to the database. | Creates the revision's related Post instance without saving it to the database. | ||||
| The form does not contain all the required fields, so it cannot be saved yet. | The form does not contain all the required fields, so it cannot be saved yet. | ||||
| """ | """ | ||||
| post_fields = ('film', 'slug') | post_fields = ('film', 'slug') | ||||
| post_data = { | post_data = { | ||||
| field: value for field, value in self.cleaned_data.items() if field in post_fields | field: value for field, value in self.cleaned_data.items() if field in post_fields | ||||
| } | } | ||||
| return Post(**post_data) | return Post(**post_data) | ||||
| def clean(self): | def clean(self): | ||||
| cleaned_data = super().clean() | cleaned_data = super().clean() | ||||
| storage_location = cleaned_data.get('storage_location') | |||||
| film = cleaned_data.get('film') | film = cleaned_data.get('film') | ||||
| if storage_location is None: | |||||
| # For film-related posts, use the film's storage location. | |||||
| if film is not None: | |||||
| cleaned_data['storage_location'] = film.storage_location | |||||
| else: | |||||
| msg = 'Storage location has to be set if the post is not related to a film.' | |||||
| self.add_error('storage_location', forms.ValidationError(msg)) | |||||
| class PostChangeForm(forms.ModelForm): | class PostChangeForm(forms.ModelForm): | ||||
| """ | """ | ||||
| Used to create a new post revision; the thumbnail field is made optional. | Used to create a new post revision; the thumbnail field is made optional. | ||||
| It is not possible to set an initial value in a FileField, but we don't want to force | It is not possible to set an initial value in a FileField, but we don't want to force | ||||
| the user to have to manually upload the thumbnail if it does not change. | the user to have to manually upload the thumbnail if it does not change. | ||||
| """ | """ | ||||
| thumbnail = forms.ImageField(required=False) | thumbnail = forms.ImageField(required=False) | ||||
| class Meta: | class Meta: | ||||
| model = Revision | model = Revision | ||||
| fields = ('title', 'topic', 'description', 'content', 'thumbnail', 'is_published') | fields = ('title', 'topic', 'description', 'content', 'thumbnail', 'is_published') | ||||