Skip to content

Commit

Permalink
Black linter
Browse files Browse the repository at this point in the history
  • Loading branch information
ericosta-dev committed Sep 13, 2024
1 parent 9a91bf8 commit 7e481b5
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 99 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
45 changes: 29 additions & 16 deletions connect/api/v2/template_projects/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
from rest_framework.serializers import ModelSerializer, SerializerMethodField
from connect.template_projects.models import TemplateType, TemplateFeature, TemplateSuggestion

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


class TemplateFeatureSerializer(ModelSerializer):
Expand All @@ -9,15 +14,15 @@ class TemplateFeatureSerializer(ModelSerializer):
class Meta:
model = TemplateFeature
fields = [
'description',
'name',
'type',
'feature_identifier',
'template_type',
"description",
"name",
"type",
"feature_identifier",
"template_type",
]

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

def get_description(self, obj):
Expand All @@ -42,13 +47,20 @@ class TemplateTypeSerializer(ModelSerializer):
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')
request = self.context.get("request")
return request.user.language

def get_name(self, obj):
Expand Down Expand Up @@ -77,7 +89,9 @@ def get_category(self, obj):
return obj.translations.get(language=lang).category

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


class RetrieveTemplateSerializer(ModelSerializer):
Expand All @@ -86,10 +100,10 @@ class RetrieveTemplateSerializer(ModelSerializer):

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

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

def get_description(self, obj):
Expand All @@ -102,7 +116,6 @@ def get_name(self, obj):


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"])
33 changes: 22 additions & 11 deletions connect/api/v2/template_projects/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
from rest_framework.viewsets import ModelViewSet
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from connect.template_projects.models import TemplateType, TemplateFeature, TemplateSuggestion
from .serializers import TemplateTypeSerializer, RetrieveTemplateSerializer, TemplateFeatureSerializer, TemplateSuggestionSerializer
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

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

from .permission import IsAdminOrReadOnly
from .serializers import (
RetrieveTemplateSerializer,
TemplateFeatureSerializer,
TemplateSuggestionSerializer,
TemplateTypeSerializer,
)


class TemplateTypeViewSet(ModelViewSet):
Expand All @@ -14,10 +25,10 @@ class TemplateTypeViewSet(ModelViewSet):

def get_queryset(self):
queryset = self.queryset
id = self.request.query_params.get('id', None)
name = self.request.query_params.get('name', None)
category = self.request.query_params.get('category', None)
uuid = self.request.query_params.get('uuid', None)
id = self.request.query_params.get("id", None)
name = self.request.query_params.get("name", None)
category = self.request.query_params.get("category", None)
uuid = self.request.query_params.get("uuid", None)

if name:
queryset = self.queryset.filter(name__iexact=name)
Expand Down Expand Up @@ -48,8 +59,8 @@ class TemplateFeatureViewSet(ModelViewSet):

def get_queryset(self):
queryset = self.queryset
id = self.request.query_params.get('id', None)
name = self.request.query_params.get('name', None)
id = self.request.query_params.get("id", None)
name = self.request.query_params.get("name", None)
if name:
queryset = self.queryset.filter(name__iexact=name)
if id:
Expand All @@ -64,7 +75,7 @@ class TemplateSuggestionViewSet(ModelViewSet):

def get_queryset(self):
queryset = self.queryset
id = self.request.query_params.get('id', None)
id = self.request.query_params.get("id", None)
if id:
queryset = self.queryset.filter(pk=id)
return queryset
4 changes: 2 additions & 2 deletions connect/template_projects/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@


class TemplateProjectsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'connect.template_projects'
default_auto_field = "django.db.models.BigAutoField"
name = "connect.template_projects"

def ready(self):
from connect.template_projects.signals import create_template_type # noqa: F401
Loading

0 comments on commit 7e481b5

Please sign in to comment.