Skip to content

Commit

Permalink
💥 Set member level as manytomany field
Browse files Browse the repository at this point in the history
  • Loading branch information
hypsug0 committed Nov 13, 2024
1 parent e35a662 commit eadc8a7
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 34 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/), and this

## unreleased

## 1.4.0 - 2024-11-13

### Changed

* member_level field on ManyToMany field is now (fix #22).

## 1.3.2 - 2024-11-13

### Changed
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "dj_sinp_organisms"
version = "1.3.2"
version = "1.4.0"
description = "Django app to manage french SINP organisms standard"
authors = ["dbChiro project", "hypsug0"]
license = "AGPLv3"
Expand Down
Empty file removed release
Empty file.
Empty file removed release.md
Empty file.
3 changes: 1 addition & 2 deletions sinp_organisms/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ class OrganismMemberAdmin(admin.ModelAdmin):
list_display = (
"member",
"organism",
"member_level",
)
list_filter = ("organism", "member_level")
list_filter = ("organism", "member")


admin.site.register(Organism, OrganismAdmin)
Expand Down
4 changes: 0 additions & 4 deletions sinp_organisms/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,8 @@ def get_by_natural_key(
self,
member_username: str,
organism_uuid: UUID,
member_level_code: str,
member_level_type_code: str,
):
return self.get(
member__username=member_username,
organism__uuid=organism_uuid,
member_level__code=member_level_code,
member_level__type__mnemonic=member_level_type_code,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Generated by Django 4.2.16 on 2024-11-13 21:16

from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
(
"sinp_nomenclatures",
"0002_remove_nomenclature_active_nomenclature_status_and_more",
),
("sinp_organisms", "0001_initial"),
]

operations = [
migrations.AlterUniqueTogether(
name="organismmember",
unique_together={("member", "organism")},
),
migrations.RemoveField(
model_name="organismmember",
name="member_level",
),
migrations.AddField(
model_name="organismmember",
name="member_level",
field=models.ManyToManyField(
limit_choices_to={"type__mnemonic": "member_level"},
related_name="member_level",
to="sinp_nomenclatures.nomenclature",
verbose_name="Niveau du membre",
),
),
]
22 changes: 11 additions & 11 deletions sinp_organisms/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,8 @@ class OrganismMember(BaseModel):
verbose_name=_("Organisme"),
# related_name="members_set",
)
member_level = models.ForeignKey(
member_level = models.ManyToManyField(
Nomenclature,
on_delete=models.CASCADE,
limit_choices_to={"type__mnemonic": "member_level"},
related_name="member_level",
verbose_name=_("Niveau du membre"),
Expand All @@ -157,21 +156,22 @@ class OrganismMember(BaseModel):

class Meta:
verbose_name_plural = _("Membre des organismes")
unique_together = ("member", "organism", "member_level")
unique_together = (
"member",
"organism",
)

def __str__(self):
return f"{self.member} [{self.member_level}]"
return f"{self.member} [{self.organism}]"

def natural_key(self) -> Tuple[UUID, UUID, str, str]:
def natural_key(self) -> Tuple[
str,
UUID,
]:
"""_summary_
Returns:
Tuple[UUID,UUID, UUID,str,str]: A tuple with respectively
member UUID, organism UUID, member_level code
"""
return (
self.member.username,
self.organism.uuid,
self.member_level.code,
self.member_level.type.mnemonic,
)
return (self.member.username, self.organism.uuid)
30 changes: 21 additions & 9 deletions sinp_organisms/serializers.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
from django.contrib.auth import get_user_model
from rest_framework.serializers import ModelSerializer
from sinp_nomenclatures.serializers import NomenclatureSerializer

from .models import Organism, OrganismMember

User = get_user_model()

class OrganismMemberSerializer(ModelSerializer):
member_level = NomenclatureSerializer(read_only=True)

class UserSerializer(ModelSerializer):
class Meta:
model = OrganismMember
fields = [
"id",
"member",
"member_level",
]
model = User
fields = ("id", "username", "email")


class OrganismSerializer(ModelSerializer):

class Meta:
model = Organism
fields = [
Expand All @@ -41,9 +39,23 @@ class Meta:
]


class OrganismMemberSerializer(ModelSerializer):
member_level = NomenclatureSerializer(read_only=True, many=True)
organism = OrganismSerializer(read_only=True)
member = UserSerializer(read_only=True)

class Meta:
model = OrganismMember
fields = [
"organism",
"member",
"member_level",
]


class OrganismDetailledSerializer(OrganismSerializer):
action_scope = NomenclatureSerializer(read_only=True)
geographic_area = NomenclatureSerializer(read_only=True, many=True)
status = NomenclatureSerializer(read_only=True)
type = NomenclatureSerializer(read_only=True)
# members = OrganismMemberSerializer(many=True)
members = OrganismMemberSerializer(many=True)
12 changes: 6 additions & 6 deletions sinp_organisms/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ def setUp(self):
self.org_member1 = OrganismMember.objects.create(
member=self.user1,
organism=self.org1,
member_level=Nomenclature.objects.get(
)
self.org_member1.member_level.set(
Nomenclature.objects.filter(
type__mnemonic="member_level", code="manager"
),
)
)

def test_organisms(self):
Expand All @@ -54,8 +56,6 @@ def test_organisms(self):
*(
"user1",
org1_uuid,
"manager",
"member_level",
)
)
user2 = User.objects.get_by_natural_key(
Expand All @@ -69,8 +69,8 @@ def test_organisms(self):
self.assertEqual(org1_1, org1_2)
self.assertEqual(user1, member1.member)
self.assertIn(member1, org1_1.organismmember_set.all())
self.assertEqual(str(member1), f"{user1} [Gestionnaire]")
self.assertEqual(str(member1), f"{user1} [ORG1]")
self.assertEqual(
member1.natural_key(),
(user1.username, org1_1.uuid, "manager", "member_level"),
(user1.username, org1_1.uuid),
)
2 changes: 1 addition & 1 deletion sinp_organisms/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ class OrganismMemberViewset(ModelViewSet):
permission_classes = [DjangoModelPermissions]
queryset = (
OrganismMember.objects.select_related("member")
.select_related("member_level")
.prefetch_related("member_level")
.all()
)
Empty file removed test.yaml
Empty file.

0 comments on commit eadc8a7

Please sign in to comment.