Skip to content

Commit

Permalink
[back][keyword] #393 ajout du modèle, de l’API et des tests pour les …
Browse files Browse the repository at this point in the history
…mots-clés
  • Loading branch information
fab2713 committed Dec 12, 2023
1 parent 4273588 commit 952b835
Show file tree
Hide file tree
Showing 15 changed files with 786 additions and 6 deletions.
92 changes: 86 additions & 6 deletions etc/keycloak/auth.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Generated by Django 3.1.14 on 2023-12-11 17:49

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('francoralite_api', '0009_block'),
]

operations = [
migrations.CreateModel(
name='Keyword',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=255, unique=True, verbose_name='Nom')),
('notes', models.TextField(blank=True, null=True, verbose_name='Notes')),
],
options={
'verbose_name_plural': 'keywords',
'db_table': 'api_keyword',
'ordering': ['name'],
},
),
migrations.CreateModel(
name='ItemKeyword',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('item', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='francoralite_api.item', verbose_name='Item')),
('keyword', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='francoralite_api.keyword', verbose_name='Keyword')),
],
options={
'verbose_name_plural': 'item_keywords',
'db_table': 'api_item_keyword',
'ordering': [],
'unique_together': {('item', 'keyword')},
},
),
]
2 changes: 2 additions & 0 deletions francoralite/apps/francoralite_api/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from .item_domain_tale import ItemDomainTale
from .item_usefulness import ItemUsefulness
from .item_dance import ItemDance
from .item_keyword import ItemKeyword
from .item_thematic import ItemThematic
from .item_language import ItemLanguage
from .item_musical_organization import ItemMusicalOrganization
Expand All @@ -46,6 +47,7 @@
from .item_performance import ItemPerformance
from .musical_organization import MusicalOrganization
from .musical_group import MusicalGroup
from .keyword import Keyword
from .thematic import Thematic
from .dance import Dance
from .domain_tale import DomainTale
Expand Down
31 changes: 31 additions & 0 deletions francoralite/apps/francoralite_api/models/item_keyword.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Authors: Luc LEGER / Coopérative ARTEFACTS <[email protected]>


from django.db import models
from django.utils.translation import gettext_lazy as _

# Add nested/related tables
from .item import Item
from .keyword import Keyword


class ItemKeyword(models.Model):
# Description of the table
"Relation between an item and its keywords"

# List of the fields
keyword = models.ForeignKey(Keyword, verbose_name=_(
'Keyword'), on_delete=models.CASCADE)
item = models.ForeignKey(Item, verbose_name=_('Item'),
on_delete=models.CASCADE)

class Meta:
app_label = 'francoralite_api'
db_table = 'api_item_keyword'
verbose_name_plural = _('item_keywords')
ordering = []
unique_together = (('item', 'keyword'), )
27 changes: 27 additions & 0 deletions francoralite/apps/francoralite_api/models/keyword.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Authors: Luc LEGER / Cooperative Artefacts <[email protected]>


from django.db import models
from django.utils.translation import gettext_lazy as _


class Keyword(models.Model):
# Description of the table
"Keyword used by a deposit (for each item)"

# List of the fields
name = models.CharField(_('Nom'), unique=True, max_length=255)
notes = models.TextField(_('Notes'), null=True, blank=True)

class Meta:
app_label = 'francoralite_api'
db_table = 'api_keyword'
verbose_name_plural = _('keywords')
ordering = ['name']

def __unicode__(self):
return self.name
57 changes: 57 additions & 0 deletions francoralite/apps/francoralite_api/serializers/item_keyword.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# -*- coding: utf-8 -*-
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Authors: Luc LEGER / Coopérative ARTEFACTS <[email protected]>
from rest_framework import serializers

from .asymetric_related_field import AsymetricRelatedField

# Add nested/related serializers
from .item import ItemSerializer
from .keyword import KeywordSerializer

from ..models.item_keyword import (
ItemKeyword as ItemKeywordModel)


class ItemKeywordSerializer(serializers.ModelSerializer):
"""
Common serializer for all ItemKeyword actions
"""

# fields of the serializer
item = AsymetricRelatedField.from_serializer(
ItemSerializer, kwargs={'required': True})
keyword = AsymetricRelatedField.from_serializer(
KeywordSerializer, kwargs={'required': True})

class Meta:
model = ItemKeywordModel
fields = '__all__'

# TODO : use it with with a complete create
# def create(self, validated_data):
# """
# Overriding the default create method of the Model serializer.
# :param validated_data: data containing all the details
# of ItemKeyword
# :return: returns a successfully created ext_collection record
# """
#
# # FIXIT : Add nested/related data
# collection_data = validated_data.pop('collection')
# # Create an oject Mediacollection with the data converted in dict
# collection = MediacollectionModel.objects.create(**collection_data)
#
# collector_data = validated_data.pop('collector')
# # Create an oject collector (Authority)
# # with the data converted in dict
# collector = AuthorityModel.objects.create(**collector_data)
#
# # Create an oject item_keywords
# item_keywords = \
# ItemKeywordModel.objects.create(
# collection=collection, collector=collector, **validated_data)
#
# return item_keywords
26 changes: 26 additions & 0 deletions francoralite/apps/francoralite_api/serializers/keyword.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Authors: Luc LEGER / Coopérative ARTEFACTS <[email protected]>

from rest_framework import serializers
from rest_framework.validators import UniqueValidator
from ..models.keyword import (
Keyword as KeywordModel)


class KeywordSerializer(serializers.ModelSerializer):
"""
Common serializer for all Keyword actions
"""

name = serializers.CharField(
required=True,
validators=[UniqueValidator(queryset=KeywordModel.objects.all())]
)
notes = serializers.CharField(required=False, allow_blank=True)

class Meta:
model = KeywordModel
fields = '__all__'
29 changes: 29 additions & 0 deletions francoralite/apps/francoralite_api/tests/factories/item_keyword.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Authors: Luc LEGER / Coopérative ARTEFACTS <[email protected]>
"""
item_keyword factory to execute tests
"""

import factory
from ...models.item_keyword import ItemKeyword
# import nested/related factories
from .keyword import KeywordFactory


class ItemKeywordFactory(factory.django.DjangoModelFactory):
"""
ItemKeyword factory
"""

class Meta:
model = ItemKeyword
django_get_or_create = (
'item',
'keyword',)

# Nested/related factories
item = factory.SubFactory("francoralite.apps.francoralite_api.tests.factories.item.ItemFactory")
keyword = factory.SubFactory(KeywordFactory)
23 changes: 23 additions & 0 deletions francoralite/apps/francoralite_api/tests/factories/keyword.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Authors: Luc LEGER / Coopérative ARTEFACTS <[email protected]>
"""
Keyword factory to execute tests
"""

import factory
from ...models.keyword import Keyword


class KeywordFactory(factory.django.DjangoModelFactory):
"""
Keyword factory
"""

class Meta:
model = Keyword

name = factory.Sequence(lambda n: 'theme%d' % n)
notes = factory.Faker('paragraph', nb_sentences=1)
Loading

0 comments on commit 952b835

Please sign in to comment.