diff --git a/weni/api/v1/account/views.py b/weni/api/v1/account/views.py index 20d52e8f..cdae7ee2 100644 --- a/weni/api/v1/account/views.py +++ b/weni/api/v1/account/views.py @@ -15,6 +15,7 @@ ) from weni.api.v1.keycloak import KeycloakControl from weni.authentication.models import User +from weni.common.models import Service from weni.utils import upload_photo_rocket @@ -67,7 +68,7 @@ def upload_photo(self, request, **kwargs): # pragma: no cover # Update avatar in all rocket chat registered for service in self.request.user.service_status.filter( - service__rocket_chat=True + service__type_service=Service.TYPE_SERVICE_CHAT ): upload_photo_rocket( server_rocket=service.service.url, diff --git a/weni/api/v1/dashboard/serializers.py b/weni/api/v1/dashboard/serializers.py index 497722ff..d716f3d6 100644 --- a/weni/api/v1/dashboard/serializers.py +++ b/weni/api/v1/dashboard/serializers.py @@ -1,8 +1,9 @@ from django.conf import settings +from django.utils.translation import ugettext_lazy as _ from rest_framework import serializers from weni.authentication.models import User -from weni.common.models import Newsletter, ServiceStatus +from weni.common.models import Newsletter, ServiceStatus, Service class NewsletterSerializer(serializers.ModelSerializer): @@ -22,6 +23,7 @@ class Meta: "service__url", "service__default", "service__last_updated", + "service__type_service", "created_at", ] ref_name = None @@ -30,6 +32,13 @@ class Meta: service__url = serializers.CharField(source="service.url") service__default = serializers.BooleanField(source="service.default") service__last_updated = serializers.DateTimeField(source="service.last_updated") + service__type_service = serializers.ChoiceField( + choices=Service.TYPE_SERVICE_CHOICES, + default=Service.TYPE_SERVICE_CHAT, + label=_("Type Service"), + source="service.type_service", + read_only=True + ) class DashboardInfoSerializer(serializers.ModelSerializer): @@ -45,7 +54,7 @@ def get_menu(self, obj): "inteligence": settings.INTELIGENCE_URL, "flows": settings.FLOWS_URL, "chat": list( - obj.service_status.filter(service__rocket_chat=True).values_list( + obj.service_status.filter(service__type_service=Service.TYPE_SERVICE_CHAT).values_list( "service__url", flat=True ) ), diff --git a/weni/api/v1/tests/test_dashboard.py b/weni/api/v1/tests/test_dashboard.py index 6c09362b..fc8ac974 100644 --- a/weni/api/v1/tests/test_dashboard.py +++ b/weni/api/v1/tests/test_dashboard.py @@ -40,7 +40,7 @@ def setUp(self): self.factory = RequestFactory() self.service = Service.objects.create( - url="http://test-rocketchat.com", status=False, rocket_chat=True + url="http://test-rocketchat.com", status=False, type_service=Service.TYPE_SERVICE_CHAT ) self.user, self.token = create_user_and_token() diff --git a/weni/common/migrations/0005_service_type_service.py b/weni/common/migrations/0005_service_type_service.py new file mode 100644 index 00000000..85c3bf8f --- /dev/null +++ b/weni/common/migrations/0005_service_type_service.py @@ -0,0 +1,36 @@ +# Generated by Django 2.2.17 on 2021-01-22 19:07 + +from django.db import migrations, models + + +def noop(apps, schema_editor): # pragma: no cover + pass + + +def migrate(apps, schema_editor): # pragma: no cover + Service = apps.get_model("common", "Service") + + for service in Service.objects.all(): + if service.rocket_chat: + service.type_service = "type_service_chat" + service.save(update_fields=["type_service"]) + + +class Migration(migrations.Migration): + + dependencies = [ + ('common', '0004_auto_20210114_1405'), + ] + + operations = [ + migrations.AddField( + model_name='service', + name='type_service', + field=models.CharField(choices=[('type_service_flows', 'Flows service'), ('type_service_inteligence', 'Inteligence Service'), ('type_service_chat', 'Chat Service')], default='type_service_chat', max_length=50, verbose_name='type service'), + ), + migrations.RunPython(migrate, noop), + migrations.RemoveField( + model_name='service', + name='rocket_chat', + ), + ] diff --git a/weni/common/models.py b/weni/common/models.py index 3610b4a5..1ed13691 100644 --- a/weni/common/models.py +++ b/weni/common/models.py @@ -22,9 +22,33 @@ class Service(models.Model): class Meta: verbose_name = _("service") + TYPE_SERVICE_FLOWS = "type_service_flows" + TYPE_SERVICE_INTELIGENCE = "type_service_inteligence" + TYPE_SERVICE_CHAT = "type_service_chat" + + TYPE_SERVICE_CHOICES = [ + ( + TYPE_SERVICE_FLOWS, + _("Flows service"), + ), + ( + TYPE_SERVICE_INTELIGENCE, + _("Inteligence Service"), + ), + ( + TYPE_SERVICE_CHAT, + _("Chat Service"), + ), + ] + url = models.URLField(_("service url"), unique=True) status = models.BooleanField(_("status service"), default=False) - rocket_chat = models.BooleanField(_("is it a rocketchat server?"), default=False) + type_service = models.CharField( + _("type service"), + max_length=50, + choices=TYPE_SERVICE_CHOICES, + default=TYPE_SERVICE_CHAT, + ) created_at = models.DateTimeField(_("created at"), auto_now_add=True) last_updated = models.DateTimeField(_("last updated"), auto_now_add=True) default = models.BooleanField(_("service default"), default=False)