Intitial teams support #147
@ -129,7 +129,7 @@ class ExtensionUpdateForm(forms.ModelForm):
|
||||
def __init__(self, *args, **kwargs):
|
||||
"""Pass the request and initialise all the nested form(set)s."""
|
||||
self.request = kwargs.pop('request')
|
||||
self.team_choices_add_empty = kwargs.pop('team_choices_add_empty', False)
|
||||
self.add_invalid_team_choice = kwargs.pop('add_invalid_team_choice', False)
|
||||
super().__init__(*args, **kwargs)
|
||||
if self.request.POST:
|
||||
edit_preview_formset = EditPreviewFormSet(
|
||||
@ -171,7 +171,7 @@ class ExtensionUpdateForm(forms.ModelForm):
|
||||
if self.instance.team:
|
||||
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:
|
||||
if self.add_invalid_team_choice:
|
||||
choices.insert(0, (self.invalid_team_value, '-----'))
|
||||
self.fields['team'] = forms.ChoiceField(
|
||||
choices=choices,
|
||||
|
@ -128,19 +128,18 @@ class ExtensionManager(models.Manager):
|
||||
def unlisted(self):
|
||||
return self.exclude(status=self.model.STATUSES.APPROVED)
|
||||
|
||||
def authored_by(self, user):
|
||||
def _authored_by_filter(self, user):
|
||||
filter = Q(maintainer__user_id=user.pk)
|
||||
user_teams = user.teams.all()
|
||||
if user_teams:
|
||||
filter = filter | Q(team__in=[t.pk for t in user_teams])
|
||||
return self.filter(filter).distinct()
|
||||
return filter
|
||||
|
||||
def authored_by(self, user):
|
||||
return self.filter(self._authored_by_filter(user))
|
||||
|
||||
def listed_or_authored_by(self, user):
|
||||
filter = Q(status=self.model.STATUSES.APPROVED) | Q(maintainer__user_id=user.pk)
|
||||
user_teams = user.teams.all()
|
||||
if user_teams:
|
||||
filter = filter | Q(team__in=[t.pk for t in user_teams])
|
||||
return self.filter(filter).distinct()
|
||||
return self.filter(Q(status=self.model.STATUSES.APPROVED) | self._authored_by_filter(user))
|
||||
|
||||
|
||||
class Extension(CreatedModifiedMixin, RatingMixin, TrackChangesMixin, models.Model):
|
||||
|
@ -375,7 +375,7 @@ class DraftExtensionView(
|
||||
extension_form = ExtensionUpdateForm(
|
||||
instance=self.extension,
|
||||
request=self.request,
|
||||
team_choices_add_empty=True,
|
||||
add_invalid_team_choice=True,
|
||||
)
|
||||
context['extension_form'] = extension_form
|
||||
context['edit_preview_formset'] = extension_form.edit_preview_formset
|
||||
@ -392,7 +392,7 @@ class DraftExtensionView(
|
||||
self.request.FILES,
|
||||
instance=self.extension,
|
||||
request=self.request,
|
||||
team_choices_add_empty=True,
|
||||
add_invalid_team_choice=True,
|
||||
)
|
||||
if form.is_valid() and extension_form.is_valid():
|
||||
return self.form_valid(form, extension_form)
|
||||
|
@ -24,7 +24,7 @@ class ExtensionQuerysetMixin:
|
||||
if self.request.user.is_staff:
|
||||
return Extension.objects.all()
|
||||
if self.request.user.is_authenticated:
|
||||
return Extension.objects.listed_or_authored_by(user=self.request.user)
|
||||
return Extension.objects.listed_or_authored_by(self.request.user).distinct()
|
||||
return Extension.objects.listed
|
||||
|
||||
|
||||
|
@ -18,8 +18,10 @@ class UploadFileView(LoginRequiredMixin, CreateView):
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
drafts = Extension.objects.authored_by(user=self.request.user).filter(
|
||||
status=Extension.STATUSES.INCOMPLETE
|
||||
drafts = (
|
||||
Extension.objects.authored_by(self.request.user)
|
||||
.filter(status=Extension.STATUSES.INCOMPLETE)
|
||||
.distinct()
|
||||
)
|
||||
context['drafts'] = drafts
|
||||
return context
|
||||
@ -41,7 +43,7 @@ class UploadFileView(LoginRequiredMixin, CreateView):
|
||||
if parsed_extension_fields:
|
||||
# Try to look up extension by the same author and file info
|
||||
extension = (
|
||||
Extension.objects.authored_by(user=self.request.user)
|
||||
Extension.objects.authored_by(self.request.user)
|
||||
.filter(type=self.file.type, **parsed_extension_fields)
|
||||
.first()
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user