Skip to content

Commit

Permalink
Merge f6cd782 into 8e0d3f4
Browse files Browse the repository at this point in the history
  • Loading branch information
Douglas Paz committed Oct 23, 2018
2 parents 8e0d3f4 + f6cd782 commit 3f00ad8
Show file tree
Hide file tree
Showing 53 changed files with 1,396 additions and 21 deletions.
13 changes: 8 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ lint:
@PIPENV_DONT_LOAD_ENV=1 pipenv run flake8
@echo "${SUCCESS}${NC} The code is following the PEP8"

test:
@make development_mode_guard
@make check_environment
ifeq (test,$(firstword $(MAKECMDGOALS)))
RUN_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
$(eval $(RUN_ARGS):;@:)
endif

test: development_mode_guard check_environment
@make migrate CHECK_ENVIRONMENT=false
@make collectstatic CHECK_ENVIRONMENT=false
@PIPENV_DONT_LOAD_ENV=1 SECRET_KEY=SK SUPPORTED_LANGUAGES="en|pt" pipenv run coverage run manage.py test
@PIPENV_DONT_LOAD_ENV=1 pipenv run coverage report -m
@PIPENV_DONT_LOAD_ENV=1 SECRET_KEY=SK SUPPORTED_LANGUAGES="en|pt" pipenv run coverage run manage.py test $(RUN_ARGS)
@if [ ! $(RUN_ARGS) ]; then PIPENV_DONT_LOAD_ENV=1 pipenv run coverage report -m; fi;

migrate:
@make check_environment
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file added bothub/api/v1/tests/__init__.py
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions bothub/api/v2/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from rest_framework import permissions


READ_METHODS = permissions.SAFE_METHODS
WRITE_METHODS = ['POST', 'PUT', 'PATCH']
ADMIN_METHODS = ['DELETE']
Empty file.
12 changes: 12 additions & 0 deletions bothub/api/v2/example/permissions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from rest_framework import permissions

from .. import READ_METHODS


class RepositoryExamplePermission(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
59 changes: 59 additions & 0 deletions bothub/api/v2/example/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from django.utils.translation import gettext as _
from rest_framework import serializers

from bothub.common.models import RepositoryExample
from bothub.common.models import RepositoryExampleEntity


class RepositoryExampleEntitySerializer(serializers.ModelSerializer):
class Meta:
model = RepositoryExampleEntity
fields = [
'id',
'repository_example',
'start',
'end',
'entity',
'label',
'created_at',
'value',
]

repository_example = serializers.PrimaryKeyRelatedField(
queryset=RepositoryExample.objects,
help_text=_('Example\'s ID'))
entity = serializers.SerializerMethodField()
label = serializers.SerializerMethodField()

def get_entity(self, obj):
return obj.entity.value

def get_label(self, obj):
if not obj.entity.label:
return None
return obj.entity.label.value


class RepositoryExampleSerializer(serializers.ModelSerializer):
class Meta:
model = RepositoryExample
fields = [
'id',
'repository_update',
'deleted_in',
'text',
'intent',
'language',
'created_at',
'entities',
'translations',
]
read_only_fields = [
'repository_update',
'deleted_in',
'translations',
]

entities = RepositoryExampleEntitySerializer(
many=True,
read_only=True)
Empty file.
102 changes: 102 additions & 0 deletions bothub/api/v2/examples/filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
from django.utils.translation import gettext as _
from django.core.exceptions import ValidationError as DjangoValidationError
from django.db.models import Q
from django.db.models import Count
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 RepositoryExample


class ExamplesFilter(filters.FilterSet):
class Meta:
model = RepositoryExample
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')
has_translation = filters.BooleanFilter(
field_name='has_translation',
method='filter_has_translation',
help_text=_('Filter for examples with or without translation'))
has_not_translation_to = filters.CharFilter(
field_name='has_not_translation_to',
method='filter_has_not_translation_to')
order_by_translation = filters.CharFilter(
field_name='order_by_translation',
method='filter_order_by_translation',
help_text=_('Order examples with translation by language'))
label = filters.CharFilter(
field_name='label',
method='filter_label',
help_text=_('Filter for examples with entities with specific label.'))
entity = filters.CharFilter(
field_name='entity',
method='filter_entity',
help_text=_('Filter for examples 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.examples(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_has_translation(self, queryset, name, value):
annotated_queryset = queryset.annotate(
translation_count=Count('translations'))
if value:
return annotated_queryset.filter(
translation_count__gt=0)
else:
return annotated_queryset.filter(
translation_count=0)

def filter_has_not_translation_to(self, queryset, name, value):
annotated_queryset = queryset.annotate(
translation_count=Count(
'translations',
filter=Q(translations__language=value)))
return annotated_queryset.filter(translation_count=0)

def filter_order_by_translation(self, queryset, name, value):
inverted = value[0] == '-'
language = value[1:] if inverted else value
result_queryset = queryset.annotate(
translation_count=Count(
'translations',
filter=Q(translations__language=language)))
result_queryset = result_queryset.order_by(
'-translation_count' if inverted else 'translation_count')
return result_queryset

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)
Loading

0 comments on commit 3f00ad8

Please sign in to comment.