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