-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[back][keyword] #393 ajout du modèle, de l’API et des tests pour les …
…mots-clés
- Loading branch information
Showing
15 changed files
with
786 additions
and
6 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
41 changes: 41 additions & 0 deletions
41
francoralite/apps/francoralite_api/migrations/0010_itemkeyword_keyword.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')}, | ||
}, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
57
francoralite/apps/francoralite_api/serializers/item_keyword.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
francoralite/apps/francoralite_api/tests/factories/item_keyword.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
francoralite/apps/francoralite_api/tests/factories/keyword.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.