Add automatic tag collections
This commit is contained in:
+25
-11
@@ -1,6 +1,7 @@
|
||||
from django import forms
|
||||
from django.contrib.auth.forms import UserCreationForm
|
||||
from django.contrib.auth.models import User
|
||||
from django.db.models import Q
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from .models import ApiKey, Attachment, Collection, Item, Kanban, Tag, Team, TeamInvite, UserInvite, UserPreference
|
||||
|
||||
@@ -134,34 +135,47 @@ class KanbanForm(forms.ModelForm):
|
||||
|
||||
|
||||
class CollectionForm(forms.ModelForm):
|
||||
team = forms.ModelChoiceField(queryset=Team.objects.none(), required=False, label=_('Team'), widget=forms.Select(attrs={'class': 'form-select'}))
|
||||
|
||||
def __init__(self, *args, user=None, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields['visibility'].choices = [
|
||||
(Item.Visibility.PRIVATE, _('Private')),
|
||||
(Item.Visibility.PUBLIC, _('Public')),
|
||||
]
|
||||
if user and user.is_authenticated:
|
||||
team_slugs = user.team_memberships.values_list('team__slug', flat=True)
|
||||
self.fields['tags'].queryset = Tag.objects.filter(name__in=team_slugs).order_by('name')
|
||||
team_ids = user.team_memberships.values_list('team_id', flat=True)
|
||||
self.fields['team'].queryset = Team.objects.filter(pk__in=team_ids).order_by('name')
|
||||
self.fields['team'].initial = self.instance.team_id if self.instance and self.instance.pk else None
|
||||
item_scope = Q(items__owner=user) | Q(items__team_id__in=team_ids)
|
||||
team_slugs = Team.objects.values_list('slug', flat=True)
|
||||
self.fields['filter_tags'].queryset = Tag.objects.filter(item_scope).exclude(name__in=team_slugs).distinct().order_by('name')
|
||||
else:
|
||||
self.fields['tags'].queryset = Tag.objects.none()
|
||||
self.fields['team'].queryset = Team.objects.none()
|
||||
self.fields['filter_tags'].queryset = Tag.objects.none()
|
||||
|
||||
def clean_tags(self):
|
||||
tags = self.cleaned_data['tags']
|
||||
if tags.count() > 1:
|
||||
raise forms.ValidationError(_('Select at most one team.'))
|
||||
return tags
|
||||
def clean(self):
|
||||
cleaned = super().clean()
|
||||
if cleaned.get('mode') == Collection.Mode.TAGS and not cleaned.get('filter_tags'):
|
||||
self.add_error('filter_tags', _('Select at least one tag for a tag collection.'))
|
||||
return cleaned
|
||||
|
||||
class Meta:
|
||||
model = Collection
|
||||
fields = ['title', 'description', 'visibility', 'tags']
|
||||
fields = ['title', 'description', 'visibility', 'mode', 'team', 'filter_tags']
|
||||
labels = {
|
||||
'title': _('Title'),
|
||||
'description': _('Description'),
|
||||
'visibility': _('Visibility'),
|
||||
'tags': _('Team'),
|
||||
'mode': _('Collection type'),
|
||||
'filter_tags': _('Content tags'),
|
||||
}
|
||||
widgets = {
|
||||
'title': forms.TextInput(attrs={'class': 'form-control'}),
|
||||
'description': forms.Textarea(attrs={'class': 'form-control', 'rows': 4}),
|
||||
'visibility': forms.Select(attrs={'class': 'form-select'}),
|
||||
'tags': forms.CheckboxSelectMultiple(),
|
||||
'mode': forms.Select(attrs={'class': 'form-select', 'x-model': 'mode'}),
|
||||
'filter_tags': forms.CheckboxSelectMultiple(),
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user