Intitial teams support #147

Merged
Oleg-Komarov merged 34 commits from teams-support into main 2024-05-23 19:43:54 +02:00
2 changed files with 19 additions and 3 deletions
Showing only changes of commit fd05069c6d - Show all commits

View File

@ -117,6 +117,7 @@ class ExtensionUpdateForm(forms.ModelForm):
) )
msg_need_previews = _('Please add at least one preview.') msg_need_previews = _('Please add at least one preview.')
msg_duplicate_file = _('Please select another file instead of the duplicate.') msg_duplicate_file = _('Please select another file instead of the duplicate.')
invalid_team_value = '-1'
class Meta: class Meta:
model = extensions.models.Extension model = extensions.models.Extension
@ -128,6 +129,7 @@ class ExtensionUpdateForm(forms.ModelForm):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
"""Pass the request and initialise all the nested form(set)s.""" """Pass the request and initialise all the nested form(set)s."""
self.request = kwargs.pop('request') self.request = kwargs.pop('request')
self.team_choices_add_empty = kwargs.pop('team_choices_add_empty', False)
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
if self.request.POST: if self.request.POST:
edit_preview_formset = EditPreviewFormSet( edit_preview_formset = EditPreviewFormSet(
@ -168,8 +170,11 @@ class ExtensionUpdateForm(forms.ModelForm):
team_pk = None team_pk = None
if self.instance.team: if self.instance.team:
team_pk = self.instance.team.pk team_pk = self.instance.team.pk
choices = [(None, self.request.user), *[(team.pk, team.name) for team in user_teams]]
if self.team_choices_add_empty:
choices.insert(0, (self.invalid_team_value, '-----'))
self.fields['team'] = forms.ChoiceField( self.fields['team'] = forms.ChoiceField(
choices=[(None, self.request.user), *[(team.pk, team.name) for team in user_teams]], choices=choices,
required=False, required=False,
initial=team_pk, initial=team_pk,
) )
@ -220,6 +225,9 @@ class ExtensionUpdateForm(forms.ModelForm):
return return
team_pk = self.cleaned_data['team'] team_pk = self.cleaned_data['team']
if team_pk == self.invalid_team_value:
self.add_error('team', _('Please select a value'))
return
if team_pk: if team_pk:
team = self.request.user.teams.filter(pk=team_pk).first() team = self.request.user.teams.filter(pk=team_pk).first()
if not team: if not team:

View File

@ -372,7 +372,11 @@ class DraftExtensionView(
"""Add all the additional forms to the context.""" """Add all the additional forms to the context."""
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
if not extension_form: if not extension_form:
extension_form = ExtensionUpdateForm(instance=self.extension, request=self.request) extension_form = ExtensionUpdateForm(
instance=self.extension,
request=self.request,
team_choices_add_empty=True,
)
context['extension_form'] = extension_form context['extension_form'] = extension_form
context['edit_preview_formset'] = extension_form.edit_preview_formset context['edit_preview_formset'] = extension_form.edit_preview_formset
context['add_preview_formset'] = extension_form.add_preview_formset context['add_preview_formset'] = extension_form.add_preview_formset
@ -384,7 +388,11 @@ class DraftExtensionView(
"""Handle bound forms and valid/invalid logic with the extra forms.""" """Handle bound forms and valid/invalid logic with the extra forms."""
form = self.get_form() form = self.get_form()
extension_form = ExtensionUpdateForm( extension_form = ExtensionUpdateForm(
self.request.POST, self.request.FILES, instance=self.extension, request=self.request self.request.POST,
self.request.FILES,
instance=self.extension,
request=self.request,
team_choices_add_empty=True,
) )
if form.is_valid() and extension_form.is_valid(): if form.is_valid() and extension_form.is_valid():
return self.form_valid(form, extension_form) return self.form_valid(form, extension_form)