Allow staff to upload photos has another user.
All checks were successful
Docker / build (release) Successful in 9s
All checks were successful
Docker / build (release) Successful in 9s
This commit is contained in:
parent
b0027be96c
commit
1de1cb4086
4 changed files with 48 additions and 14 deletions
|
|
@ -7,12 +7,15 @@ import datetime
|
|||
from crispy_forms.helper import FormHelper
|
||||
from crispy_forms.layout import Div, Layout, Submit
|
||||
from django import forms
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.utils.text import slugify
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django_select2.forms import ModelSelect2MultipleWidget
|
||||
from django_select2.forms import ModelSelect2MultipleWidget, ModelSelect2Widget
|
||||
|
||||
from .models import Gallery, Tag
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
|
||||
class MultipleFileInput(forms.ClearableFileInput):
|
||||
allow_multiple_selected = True
|
||||
|
|
@ -87,11 +90,27 @@ class UploadForm(forms.Form):
|
|||
)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
is_staff = kwargs.pop("is_staff", False)
|
||||
super().__init__(*args, **kwargs)
|
||||
if is_staff:
|
||||
self.fields["owner"] = forms.ModelChoiceField(
|
||||
User.objects.all(),
|
||||
label=_("Upload as"),
|
||||
required=False,
|
||||
empty_label=_("-- Myself --"),
|
||||
widget=ModelSelect2Widget(
|
||||
model=User,
|
||||
search_fields=["username__icontains", "first_name__icontains", "last_name__icontains"],
|
||||
attrs={
|
||||
"data-minimum-input-length": 0,
|
||||
"data-placeholder": "-- Myself --",
|
||||
},
|
||||
),
|
||||
)
|
||||
self.helper = FormHelper()
|
||||
self.helper.include_media = False
|
||||
self.helper.use_custom_control = False
|
||||
self.helper.layout = Layout(
|
||||
layout_fields = [
|
||||
"file_field",
|
||||
"gallery",
|
||||
"new_gallery_title",
|
||||
|
|
@ -102,8 +121,11 @@ class UploadForm(forms.Form):
|
|||
),
|
||||
"new_gallery_description",
|
||||
"new_gallery_tags",
|
||||
Submit("submit", _("Upload"), css_class="btn btn-success mt-2"),
|
||||
)
|
||||
]
|
||||
if is_staff:
|
||||
layout_fields.append("owner")
|
||||
layout_fields.append(Submit("submit", _("Upload"), css_class="btn btn-success mt-2"))
|
||||
self.helper.layout = Layout(*layout_fields)
|
||||
|
||||
def clean(self):
|
||||
cleaned_data = super().clean()
|
||||
|
|
@ -119,6 +141,9 @@ class UploadForm(forms.Form):
|
|||
|
||||
return cleaned_data
|
||||
|
||||
def get_owner(self, fallback):
|
||||
return self.cleaned_data.get("owner") or fallback
|
||||
|
||||
def get_or_create_gallery(self):
|
||||
"""
|
||||
Get or create gallery
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue