Skip to content

Commit

Permalink
Merge pull request #895 from weni-ai/feature/templatetype-language
Browse files Browse the repository at this point in the history
Feature: Translation on TemplateType feature
  • Loading branch information
ericosta-dev authored Sep 13, 2024
2 parents 2cd3cdd + 7e481b5 commit 33551e4
Show file tree
Hide file tree
Showing 13 changed files with 398 additions and 112 deletions.
2 changes: 1 addition & 1 deletion connect/api/v2/template_projects/permission.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from rest_framework.permissions import BasePermission, SAFE_METHODS
from rest_framework.permissions import SAFE_METHODS, BasePermission


class IsAdminOrReadOnly(BasePermission):
Expand Down
105 changes: 93 additions & 12 deletions connect/api/v2/template_projects/serializers.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,121 @@
from rest_framework.serializers import ModelSerializer
from connect.template_projects.models import TemplateType, TemplateFeature, TemplateSuggestion
from rest_framework import serializers
from rest_framework.serializers import ModelSerializer, SerializerMethodField

from connect.template_projects.models import (
TemplateFeature,
TemplateSuggestion,
TemplateType,
)


class TemplateFeatureSerializer(ModelSerializer):
description = SerializerMethodField()
name = SerializerMethodField()

class Meta:
model = TemplateFeature
fields = "__all__"
fields = [
"description",
"name",
"type",
"feature_identifier",
"template_type",
]

def _get_language(self):
request = self.context.get("request")
return request.user.language

def get_description(self, obj):
lang = self._get_language()
return obj.translations.get(language=lang).description

def get_name(self, obj):
lang = self._get_language()
return obj.translations.get(language=lang).name


class TemplateTypeSerializer(ModelSerializer):

features = serializers.SerializerMethodField()
name = SerializerMethodField()
description = SerializerMethodField()
setup = SerializerMethodField()
photo = SerializerMethodField()
photo_description = SerializerMethodField()
category = SerializerMethodField()
features = SerializerMethodField()

class Meta:
model = TemplateType
fields = [
'uuid', 'category', 'description', 'name',
'level', 'setup', 'photo', 'features',
'photo_description', 'base_project_uuid'
"uuid",
"category",
"description",
"name",
"level",
"setup",
"photo",
"features",
"photo_description",
"base_project_uuid",
]

def _get_language(self):
request = self.context.get("request")
return request.user.language

def get_name(self, obj):
lang = self._get_language()
return obj.translations.get(language=lang).name

def get_description(self, obj):
lang = self._get_language()
return obj.translations.get(language=lang).description

def get_setup(self, obj):
lang = self._get_language()
return obj.translations.get(language=lang).setup

def get_photo(self, obj):
lang = self._get_language()
translation = obj.translations.get(language=lang)
return translation.photo.url if translation.photo else None

def get_photo_description(self, obj):
lang = self._get_language()
return obj.translations.get(language=lang).photo_description

def get_category(self, obj):
lang = self._get_language()
return obj.translations.get(language=lang).category

def get_features(self, obj):
return TemplateFeatureSerializer(obj.template_features.all(), many=True).data
return TemplateFeatureSerializer(
obj.template_features.all(), many=True, context=self.context
).data


class RetrieveTemplateSerializer(ModelSerializer):
description = SerializerMethodField()
name = SerializerMethodField()

class Meta:
model = TemplateType
fields = ['uuid', 'description', 'name']
fields = ["uuid", "description", "name"]

def _get_language(self):
request = self.context.get("request")
return request.user.language

class TemplateSuggestionSerializer(ModelSerializer):
def get_description(self, obj):
lang = self._get_language()
return obj.translations.get(language=lang).description

def get_name(self, obj):
lang = self._get_language()
return obj.translations.get(language=lang).name


class TemplateSuggestionSerializer(ModelSerializer):
class Meta:
model = TemplateSuggestion
fields = ['suggestion', 'created_at', 'status']
fields = ["suggestion", "created_at", "status"]
129 changes: 92 additions & 37 deletions connect/api/v2/template_projects/tests.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
import json
from django.test import TestCase, RequestFactory
from .views import TemplateTypeViewSet, TemplateFeatureViewSet, TemplateSuggestionViewSet

from django.test import RequestFactory, TestCase

from connect.api.v1.tests.utils import create_user_and_token
from connect.template_projects.models import TemplateType, TemplateFeature, TemplateSuggestion
from connect.template_projects.models import (
TemplateFeature,
TemplateSuggestion,
TemplateType,
)

from .views import (
TemplateFeatureViewSet,
TemplateSuggestionViewSet,
TemplateTypeViewSet,
)

class TemplateTypeViewSetTestCase(TestCase):

class TemplateTypeViewSetTestCase(TestCase):
def setUp(self):

self.factory = RequestFactory()
self.owner, self.owner_token = create_user_and_token("owner")

self.template_type_object = TemplateType.objects.create(
level=1,
category=["category"],
description="description",
name="name"
level=1, category=["category"], description="description", name="name"
)

def request(self, method, url, data=None, user=None, token=None, id=None):
Expand All @@ -33,16 +39,41 @@ def request(self, method, url, data=None, user=None, token=None, id=None):

def test_get_queryset(self):

response = self.request({"get": "list"}, "/v2/projects/template-type", user=self.owner, token=self.owner_token)
response = self.request(
{"get": "list"},
"/v2/projects/template-type",
user=self.owner,
token=self.owner_token,
)
self.assertEqual(response["count"], 1)
response = self.request({"get": "list"}, "/v2/projects/template-type?name=name", user=self.owner, token=self.owner_token)
response = self.request(
{"get": "list"},
"/v2/projects/template-type?name=name",
user=self.owner,
token=self.owner_token,
)
self.assertEqual(response["count"], 1)
response = self.request({"get": "list"}, "/v2/projects/template-type?category=category", user=self.owner, token=self.owner_token)
response = self.request(
{"get": "list"},
"/v2/projects/template-type?category=category",
user=self.owner,
token=self.owner_token,
)
self.assertEqual(response["count"], 1)
response = self.request({"get": "list"}, "/v2/projects/template-type?id=1", user=self.owner, token=self.owner_token)
response = self.request(
{"get": "list"},
"/v2/projects/template-type?id=1",
user=self.owner,
token=self.owner_token,
)
self.assertEqual(response["count"], 1)
obj_uuid = self.template_type_object.uuid
response = self.request({"get": "list"}, f"/v2/projects/template-type?uuid={obj_uuid}", user=self.owner, token=self.owner_token)
response = self.request(
{"get": "list"},
f"/v2/projects/template-type?uuid={obj_uuid}",
user=self.owner,
token=self.owner_token,
)
self.assertEqual(response["count"], 1)

def test_retrieve(self):
Expand All @@ -54,31 +85,27 @@ def test_retrieve(self):
f"/v2/projects/template-type/{template_id}",
user=self.owner,
token=self.owner_token,
id=template_id
id=template_id,
)
self.assertEqual(response["name"], "name")


class TemplateFeatureViewSetTest(TestCase):

def setUp(self):

self.factory = RequestFactory()
self.owner, self.owner_token = create_user_and_token("owner")

self.template_type_object = TemplateType.objects.create(
level=1,
category=["category"],
description="description",
name="name"
level=1, category=["category"], description="description", name="name"
)

self.template_feature_object = TemplateFeature.objects.create(
name="name",
description="description",
template_type=self.template_type_object,
feature_identifier="chatgpt",
type="text"
type="text",
)

def request(self, method, url, data=None, user=None, token=None, id=None):
Expand All @@ -94,52 +121,80 @@ def request(self, method, url, data=None, user=None, token=None, id=None):

def test_get_queryset(self):

response = self.request({"get": "list"}, "/v2/projects/template-feature", user=self.owner, token=self.owner_token)
response = self.request(
{"get": "list"},
"/v2/projects/template-feature",
user=self.owner,
token=self.owner_token,
)
self.assertEqual(response["count"], 1)
response = self.request({"get": "list"}, "/v2/projects/template-feature?name=name", user=self.owner, token=self.owner_token)
response = self.request(
{"get": "list"},
"/v2/projects/template-feature?name=name",
user=self.owner,
token=self.owner_token,
)
self.assertEqual(response["count"], 1)
response = self.request({"get": "list"}, "/v2/projects/template-feature?template_type=1", user=self.owner, token=self.owner_token)
response = self.request(
{"get": "list"},
"/v2/projects/template-feature?template_type=1",
user=self.owner,
token=self.owner_token,
)
self.assertEqual(response["count"], 1)


class TemplateSuggestionViewSetTest(TestCase):

def setUp(self):

self.factory = RequestFactory()
self.owner, self.owner_token = create_user_and_token("owner")

self.template_suggestion_object = TemplateSuggestion.objects.create(
suggestion="suggestion",
type="type",
status="status"
suggestion="suggestion", type="type", status="status"
)

def request(self, method, url, data=None, user=None, token=None, id=None):

headers = {"HTTP_AUTHORIZATION": "Token {}".format(token.key)} if token else {}
if method == {"post": "create"}:
request = self.factory.post(path=url, data=data, **headers, content_type="application/json")
request = self.factory.post(
path=url, data=data, **headers, content_type="application/json"
)
else:
request = self.factory.request(method=method, path=url, data=data, **headers)
request = self.factory.request(
method=method, path=url, data=data, **headers
)
response = TemplateSuggestionViewSet.as_view(method)(request, pk=id)
response.render()
content_data = json.loads(response.content)
return content_data

def test_get_queryset_suggestion_endpoints(self):

response = self.request({"get": "list"}, "/v2/projects/template-suggestion", user=self.owner, token=self.owner_token)
response = self.request(
{"get": "list"},
"/v2/projects/template-suggestion",
user=self.owner,
token=self.owner_token,
)
self.assertEqual(response["count"], 1)
response = self.request({"get": "list"}, "/v2/projects/template-suggestion?id=1", user=self.owner, token=self.owner_token)
response = self.request(
{"get": "list"},
"/v2/projects/template-suggestion?id=1",
user=self.owner,
token=self.owner_token,
)
self.assertEqual(response["count"], 1)

def test_post_endpoint(self):

data = {
"suggestion": "test 2",
"type": "template",
"status": "pending"
}
response = self.request({"post": "create"}, "/v2/projects/template-suggestion", user=self.owner, token=self.owner_token, data=data)
data = {"suggestion": "test 2", "type": "template", "status": "pending"}
response = self.request(
{"post": "create"},
"/v2/projects/template-suggestion",
user=self.owner,
token=self.owner_token,
data=data,
)
self.assertEqual(response["suggestion"], data["suggestion"])
Loading

0 comments on commit 33551e4

Please sign in to comment.