Skip to content

Commit

Permalink
Merge pull request #130 from Ilhasoft/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
AlisoSouza committed Dec 28, 2021
2 parents cde173d + d8a31c4 commit 5a1c04b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 50 deletions.
28 changes: 0 additions & 28 deletions connect/api/v1/organization/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
RequestPermissionOrganizationSerializer,
)

from connect.common import tasks

from connect.authentication.models import User
from connect.celery import app as celery_app
from connect.common.models import (
Expand Down Expand Up @@ -139,32 +137,6 @@ def remove_card_setup(
return JsonResponse(data={"status": True}, status=status.HTTP_200_OK)
return JsonResponse(data={"status": False}, status=status.HTTP_304_NOT_MODIFIED)

@action(
detail=True,
methods=["GET"],
url_name="get-contact-active-detailed",
url_path="grpc/get-contact-active-detailed/(?P<organization_uuid>[^/.]+)",
authentication_classes=[ExternalAuthentication],
permission_classes=[AllowAny],
)
def get_contact_active_detailed(self, request, organization_uuid):
organization = get_object_or_404(Organization, uuid=organization_uuid)

self.check_object_permissions(self.request, organization)

before = str(request.query_params.get("before") + " 00:00")
after = str(request.query_params.get("after") + " 00:00")

if not before or not after:
raise ValidationError(
_("Need to pass 'before' and 'after' in query params")
)
task = tasks.get_contacts_detailed.delay(str(organization_uuid), before, after)
task.wait()
contact_detailed = {'projects': task.result}
print(contact_detailed)
return JsonResponse(data=contact_detailed, status=status.HTTP_200_OK)

@action(
detail=True,
methods=["GET"],
Expand Down
31 changes: 29 additions & 2 deletions connect/api/v1/project/views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from django.utils.translation import ugettext_lazy as _
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import mixins
from rest_framework import mixins, status
from rest_framework.decorators import action
from rest_framework.exceptions import PermissionDenied
from rest_framework.filters import OrderingFilter, SearchFilter
from rest_framework.permissions import IsAuthenticated
from rest_framework.permissions import IsAuthenticated, AllowAny
from rest_framework.response import Response
from rest_framework.viewsets import GenericViewSet

Expand All @@ -15,6 +15,11 @@
from connect.celery import app as celery_app
from connect.common.models import OrganizationAuthorization, Project

from connect.middleware import ExternalAuthentication
from rest_framework.exceptions import ValidationError
from connect.common import tasks
from django.http import JsonResponse


class ProjectViewSet(
mixins.ListModelMixin,
Expand Down Expand Up @@ -83,3 +88,25 @@ def project_search(self, request, **kwargs): # pragma: no cover
task.wait() # pragma: no cover

return Response(task.result)

@action(
detail=True,
methods=["GET"],
url_name="get-contact-active-detailed",
url_path="grpc/get-contact-active-detailed/(?P<project_uuid>[^/.]+)",
authentication_classes=[ExternalAuthentication],
permission_classes=[AllowAny],
)
def get_contact_active_detailed(self, request, project_uuid):

before = str(request.query_params.get("before") + " 00:00")
after = str(request.query_params.get("after") + " 00:00")

if not before or not after:
raise ValidationError(
_("Need to pass 'before' and 'after' in query params")
)
task = tasks.get_contacts_detailed.delay(str(project_uuid), before, after)
task.wait()
contact_detailed = {'projects': task.result}
return JsonResponse(data=contact_detailed, status=status.HTTP_200_OK)
2 changes: 2 additions & 0 deletions connect/billing/gateways/stripe_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def purchase(self, money: float, identification, options: dict = None):
confirm=True,
metadata=options,
)
except IndexError:
return {"status": "FAILURE", "response": "Customer does not have a configured card"}
except self.stripe.error.CardError as error:
return {"status": "FAILURE", "response": error}
return {"status": "SUCCESS", "response": response}
Expand Down
39 changes: 19 additions & 20 deletions connect/common/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,27 +164,26 @@ def create_organization(organization_name: str, user_email: str):


@app.task(name='get_contacts_detailed')
def get_contacts_detailed(organization_uuid: str, before: str, after: str):
def get_contacts_detailed(project_uuid: str, before: str, after: str):
grpc_instance = utils.get_grpc_types().get("flow")
organization = Organization.objects.get(uuid=organization_uuid)
project = Project.objects.get(uuid=project_uuid)
response = []
for project in organization.project.all():
try:
contacts = grpc_instance.get_active_contacts(str(project.flow_organization), before, after)
active_contacts_ids = []
for contact in contacts:
active_contacts_ids.append(contact.uuid)
response.append(
{
'project_name': project.name,
'active_contacts': len(active_contacts_ids),
'contacts_ids': active_contacts_ids
}
)
return response
except grpc.RpcError as e:
if e.code() is not grpc.StatusCode.NOT_FOUND:
raise e
try:
contacts = grpc_instance.get_active_contacts(str(project.flow_organization), before, after)
active_contacts_ids = []
for contact in contacts:
active_contacts_ids.append(contact.uuid)
response.append(
{
'project_name': project.name,
'active_contacts': len(active_contacts_ids),
'contacts_ids': active_contacts_ids
}
)
return response
except grpc.RpcError as e:
if e.code() is not grpc.StatusCode.NOT_FOUND:
raise e


@app.task(name="create_project")
Expand Down Expand Up @@ -243,7 +242,7 @@ def search_project(organization_id: int, project_uuid: str, text: str):
@app.task()
def check_organization_free_plan():
limits = GenericBillingData.objects.first() if GenericBillingData.objects.all().exists() else GenericBillingData.objects.create()
for organization in Organization.objects.filter(organization_billing__plan='free'):
for organization in Organization.objects.filter(organization_billing__plan='free', is_suspended=False):
current_active_contacts = organization.active_contacts
if current_active_contacts > limits.free_active_contacts_limit:
organization.is_suspended = True
Expand Down

0 comments on commit 5a1c04b

Please sign in to comment.