-
-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/bben 144 creating test models #246
Closed
Closed
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f6525df
Add .idea/ to .gitignore IDEs
2752f94
First draft of the RepositoryValidation model
a67c100
Add new model definitions for RepositoryValidation model and related
4cf2baf
Creating CRUD definitions for RepositoryValidations model with tests …
b25642b
Create the Create-Update-Delete view for RepositoryValidation
cristophersfr 37334c8
Fix the CRUD for RepositoryValidation
cristophersfr fc42dd6
Add validations for creating RepositoryValidation models with existin…
cristophersfr 0420633
Remove translations attributes from RepositoryValidation model
cristophersfr 73bc0ae
Adjust the code for PEP8 correctly
cristophersfr ba28277
Add update action to RepositoryValidation endpoint
cristophersfr 3cac7c4
Change validation error from dict to array
cristophersfr 8e68fa3
Merge branch 'develop' of https://github.com/ilhasoft/bothub-engine i…
cristophersfr d266dc8
Remove multiple serializers
cristophersfr 8283817
Adjust ViewSet and Routers to serializer implementation
cristophersfr 5e214cd
Reduce name of validator
cristophersfr 0bbaca5
Adjust the tests to work with new implementations, though the tests a…
cristophersfr 577daf1
Remove multiple migrations for single feature and change it for one only
cristophersfr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
|
@@ -109,6 +109,7 @@ settings.ini | |
|
||
# IDEs | ||
.vscode/ | ||
.idea/ | ||
|
||
# db and staticfiles | ||
db.sqlite3 | ||
|
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
Empty file.
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,64 @@ | ||
from django.utils.translation import gettext as _ | ||
from django.core.exceptions import ValidationError as DjangoValidationError | ||
from rest_framework.exceptions import PermissionDenied | ||
from rest_framework.exceptions import NotFound | ||
from django_filters import rest_framework as filters | ||
|
||
from bothub.common.models import Repository | ||
from bothub.common.models import RepositoryValidation | ||
|
||
|
||
class ValidationFilter(filters.FilterSet): | ||
class Meta: | ||
model = RepositoryValidation | ||
fields = [ | ||
'text', | ||
'language', | ||
'intent', | ||
] | ||
|
||
repository_uuid = filters.CharFilter( | ||
field_name='repository_uuid', | ||
method='filter_repository_uuid', | ||
required=True, | ||
help_text=_('Repository\'s UUID')) | ||
language = filters.CharFilter( | ||
field_name='language', | ||
method='filter_language', | ||
help_text='Filter by language, default is repository base language') | ||
label = filters.CharFilter( | ||
field_name='label', | ||
method='filter_label', | ||
help_text=_( | ||
'Filter for validations with entities with specific label.' | ||
) | ||
) | ||
entity = filters.CharFilter( | ||
field_name='entity', | ||
method='filter_entity', | ||
help_text=_('Filter for validations with entity.')) | ||
|
||
def filter_repository_uuid(self, queryset, name, value): | ||
request = self.request | ||
try: | ||
repository = Repository.objects.get(uuid=value) | ||
authorization = repository.get_user_authorization(request.user) | ||
if not authorization.can_read: | ||
raise PermissionDenied() | ||
return repository.validations(queryset=queryset) | ||
except Repository.DoesNotExist: | ||
raise NotFound( | ||
_('Repository {} does not exist').format(value)) | ||
except DjangoValidationError: | ||
raise NotFound(_('Invalid repository_uuid')) | ||
|
||
def filter_language(self, queryset, name, value): | ||
return queryset.filter(repository_update__language=value) | ||
|
||
def filter_label(self, queryset, name, value): | ||
if value == 'other': | ||
return queryset.filter(entities__entity__label__isnull=True) | ||
return queryset.filter(entities__entity__label__value=value) | ||
|
||
def filter_entity(self, queryset, name, value): | ||
return queryset.filter(entities__entity__value=value) |
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,12 @@ | ||
from rest_framework import permissions | ||
|
||
from .. import READ_METHODS | ||
|
||
|
||
class RepositoryValidationPermission(permissions.BasePermission): | ||
def has_object_permission(self, request, view, obj): | ||
authorization = obj.repository_update.repository \ | ||
.get_user_authorization(request.user) | ||
if request.method in READ_METHODS: | ||
return authorization.can_read | ||
return authorization.can_contribute |
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,143 @@ | ||
from rest_framework import serializers | ||
from rest_framework.fields import empty | ||
from django.utils.translation import gettext as _ | ||
|
||
from bothub.common.models import Repository | ||
from bothub.common.models import RepositoryValidation | ||
from bothub.common.models import RepositoryValidationEntity | ||
from bothub.common import languages | ||
|
||
from bothub.api.v1.fields import EntityText | ||
from bothub.api.v1.fields import EntityValueField | ||
from bothub.api.v1.fields import LabelValueField | ||
|
||
from bothub.api.v1.validators import CanContributeInRepositoryValidator | ||
from bothub.api.v1.validators import EntityNotEqualLabelValidator | ||
|
||
from .validators import DoesIntentExistValidator | ||
from .validators import DoesEntityAndLabelExistValidator | ||
from .validators import IntentOrEntityValidator | ||
|
||
|
||
class RepositoryValidationEntitySerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = RepositoryValidationEntity | ||
fields = [ | ||
'repository_validation', | ||
'start', | ||
'end', | ||
'entity', | ||
'label', | ||
] | ||
|
||
repository_validation = serializers.PrimaryKeyRelatedField( | ||
queryset=RepositoryValidation.objects, | ||
required=False) | ||
|
||
entity = EntityValueField() | ||
|
||
label = LabelValueField( | ||
allow_blank=True, | ||
required=False) | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.validators.append(EntityNotEqualLabelValidator()) | ||
self.validators.append(DoesEntityAndLabelExistValidator()) | ||
|
||
def create(self, validated_data): | ||
repository_validation = validated_data.pop( | ||
'repository_validation', None | ||
) | ||
assert repository_validation | ||
label = validated_data.pop('label', empty) | ||
validation_entity = self.Meta.model.objects.create( | ||
repository_validation=repository_validation, | ||
**validated_data) | ||
if label is not empty: | ||
validation_entity.entity.set_label(label) | ||
validation_entity.entity.save(update_fields=['label']) | ||
return validation_entity | ||
|
||
def update(self, instance, validated_data): | ||
instance.start = validated_data.get('start', instance.start) | ||
instance.end = validated_data.get('end', instance.end) | ||
instance.entity = validated_data.get('entity', instance.entity) | ||
|
||
label = validated_data.pop('label', instance.label) | ||
if label is not empty: | ||
instance.entity.set_label(label) | ||
instance.entity.save(update_fields=['label']) | ||
|
||
instance.save() | ||
|
||
return instance | ||
|
||
|
||
class RepositoryValidationSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = RepositoryValidation | ||
fields = [ | ||
'id', | ||
'repository', | ||
'repository_update', | ||
'text', | ||
'language', | ||
'intent', | ||
'entities', | ||
] | ||
read_only_fields = [ | ||
'repository_update', | ||
'deleted_in', | ||
] | ||
|
||
id = serializers.PrimaryKeyRelatedField( | ||
read_only=True, | ||
style={'show': False}) | ||
text = EntityText(style={'entities_field': 'entities'}) | ||
repository = serializers.PrimaryKeyRelatedField( | ||
queryset=Repository.objects, | ||
validators=[ | ||
CanContributeInRepositoryValidator(), | ||
], | ||
write_only=True, | ||
style={'show': False}) | ||
repository_update = serializers.PrimaryKeyRelatedField( | ||
read_only=True, | ||
style={'show': False}) | ||
language = serializers.ChoiceField( | ||
languages.LANGUAGE_CHOICES, | ||
allow_blank=True, | ||
required=False) | ||
entities = RepositoryValidationEntitySerializer( | ||
many=True, | ||
style={'text_field': 'text'}) | ||
intent = serializers.CharField( | ||
validators=[ | ||
DoesIntentExistValidator(), | ||
] | ||
) | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.validators.append( | ||
IntentOrEntityValidator() | ||
) | ||
|
||
def create(self, validated_data): | ||
entities_data = validated_data.pop('entities') | ||
repository = validated_data.pop('repository') | ||
try: | ||
language = validated_data.pop('language') | ||
except KeyError: | ||
language = None | ||
repository_update = repository.current_update(language or None) | ||
validated_data.update({'repository_update': repository_update}) | ||
validation = self.Meta.model.objects.create(**validated_data) | ||
for entity_data in entities_data: | ||
entity_data.update({'repository_validation': validation.pk}) | ||
entity_serializer = RepositoryValidationEntitySerializer( | ||
data=entity_data) | ||
entity_serializer.is_valid(raise_exception=True) | ||
entity_serializer.save() | ||
return validation |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A API V1 em algum momento será obsoleta/descontinuada, ficando a V2 como padrão, então não faz sentido utilizar na V2 metodos, classes ou demais coisas vinda da V1.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Qual seria a melhor abordagem? Copiar essas implementações para a V2 ?