Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 83 additions & 2 deletions ifbcat_api/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from django.contrib.contenttypes.models import ContentType
from django.contrib.postgres.lookups import Unaccent
from django.core.exceptions import ValidationError
from django.db.models import Count, Q, When, Value, BooleanField, Case, CharField, F
from django.db.models import Count, Q, When, Value, BooleanField, Case, CharField, F, Exists, OuterRef
from django.db.models.functions import Upper, Length
from django.forms import modelform_factory
from django.http import HttpResponseRedirect
Expand Down Expand Up @@ -965,12 +965,88 @@ class ElixirPlatformAdmin(
)


class UsedAsTeamFunderListFilter(admin.SimpleListFilter):
title = 'Used as team funder'
parameter_name = 'as_team_funder'

def lookups(self, request, model_admin):
return (
("True", "True"),
("False", "False"),
)

def queryset(self, request, queryset):
if self.value() is None:
return queryset
# teamsFunders
return queryset.annotate(
as_team_funder=Exists(models.Team.objects.filter(fundedBy__pk=OuterRef('pk'))),
).filter(as_team_funder=self.value() == "True")


class UsedAsTeamAffiliationListFilter(admin.SimpleListFilter):
title = 'Used as team affiliation'
parameter_name = 'as_team_affiliation'

def lookups(self, request, model_admin):
return (
("True", "True"),
("False", "False"),
)

def queryset(self, request, queryset):
if self.value() is None:
return queryset
# teamsFunders
return queryset.annotate(
as_team_affiliation=Exists(models.Team.objects.filter(affiliatedWith__pk=OuterRef('pk'))),
).filter(as_team_affiliation=self.value() == "True")


class OrganisationAdminForm(forms.ModelForm):
"""
https://stackoverflow.com/a/39648244/2144569
"""

class Meta:
model = Group
exclude = []

# Add the users field.
funded_team = forms.ModelMultipleChoiceField(
queryset=models.Team.objects.all(),
required=False,
# Use the pretty 'filter_horizontal widget'.
widget=FilteredSelectMultiple('users', False),
)

# Add the users field.
affiliated_team = forms.ModelMultipleChoiceField(
queryset=models.Team.objects.all(),
required=False,
# Use the pretty 'filter_horizontal widget'.
widget=FilteredSelectMultiple('users', False),
)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self.instance.pk:
self.fields['funded_team'].initial = self.instance.teamsFunders.all()
self.fields['affiliated_team'].initial = self.instance.teamsAffiliatedWith.all()

def _save_m2m(self):
super()._save_m2m()
self.instance.teamsFunders.set(self.cleaned_data['funded_team'])
self.instance.teamsAffiliatedWith.set(self.cleaned_data['affiliated_team'])


@admin.register(models.Organisation)
class OrganisationAdmin(
PermissionInClassModelAdmin,
AllFieldInAutocompleteModelAdmin,
ViewInApiModelByNameAdmin,
):
form = OrganisationAdminForm
search_fields = (
'name',
'description',
Expand All @@ -979,7 +1055,12 @@ class OrganisationAdmin(
'fields__field',
'city',
)
list_filter = ('fields',)
list_filter = (
'fields',
UsedAsTeamFunderListFilter,
UsedAsTeamAffiliationListFilter,
)
readonly_fields = ('fields',)


@admin.register(models.Field)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Generated by Django 4.2.24 on 2025-10-17 12:32

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('ifbcat_api', '0197_working_on_expertise_description_logo_url_keywords'),
]

operations = [
migrations.AlterField(
model_name='team',
name='affiliatedWith',
field=models.ManyToManyField(
blank=True,
help_text='Organisation(s) to which the team is affiliated. i.e: "Laboratoire" or "unité" in french',
related_name='teamsAffiliatedWith',
to='ifbcat_api.organisation',
),
),
migrations.AlterField(
model_name='team',
name='fundedBy',
field=models.ManyToManyField(
help_text='Organisation(s) that funds the team. i.e the "tutelle" in french',
related_name='teamsFunders',
to='ifbcat_api.organisation',
),
),
migrations.AlterField(
model_name='team',
name='tools',
field=models.ManyToManyField(
blank=True,
help_text='Tool(s) developped by the team. You just need to provide a BioTools id.',
related_name='teams',
to='ifbcat_api.tool',
),
),
]
6 changes: 3 additions & 3 deletions ifbcat_api/model/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class CertificationType(models.TextChoices):
fundedBy = models.ManyToManyField(
Organisation,
related_name='teamsFunders',
help_text="Organisation(s) that funds the team.",
help_text="Organisation(s) that funds the team. i.e the \"tutelle\" in french",
)
publications = models.ManyToManyField(
Doi,
Expand All @@ -170,13 +170,13 @@ class CertificationType(models.TextChoices):
Organisation,
blank=True,
related_name='teamsAffiliatedWith',
help_text="Organisation(s) to which the team is affiliated.",
help_text="Organisation(s) to which the team is affiliated. i.e: \"Laboratoire\" or \"unité\" in french",
)
tools = models.ManyToManyField(
Tool,
blank=True,
related_name='teams',
help_text="Tool(s) developped by the team.",
help_text="Tool(s) developped by the team. You just need to provide a BioTools id.",
)
closing_date = models.DateField(
help_text="After this date the team is closed. Leave blank if the team is still open/active.",
Expand Down
4 changes: 2 additions & 2 deletions ifbcat_api/tests/test_no_views_crash.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ def test_all_at_once_to_spare_resource(self):
)
try:
response = self.client.get(url_list)
except FieldError:
self.assertTrue(False, msg)
except FieldError as e:
self.assertTrue(False, msg + f' with error {str(e)}')
status_code = 200
self.assertEqual(
response.status_code,
Expand Down