예제 #1
0
def update_user_language(user_email: str, language: str):
    utils.get_grpc_types().get("flow").update_language(
        user_email=user_email,
        language=language,
    )
    IntelligenceRESTClient().update_language(
        user_email=user_email,
        language=language,
    )
    return True
예제 #2
0
def destroy_classifier(classifier_uuid: str, user_email: str):
    grpc_instance = utils.get_grpc_types().get("flow")
    grpc_instance.delete_classifier(
        classifier_uuid=str(classifier_uuid),
        user_email=str(user_email),
    )
    return True
예제 #3
0
def get_messages(contact_uuid: str, before: str, after: str,
                 project_uuid: str):

    flow_instance = utils.get_grpc_types().get("flow")
    contact = Contact.objects.get(uuid=contact_uuid)
    project = Project.objects.get(uuid=project_uuid)
    message = flow_instance.get_message(str(project.flow_organization),
                                        str(contact.contact_flow_uuid), before,
                                        after)

    if len(message.uuid) == 0:
        return False
    try:
        Message.objects.get(message_flow_uuid=message.uuid)
    except Message.DoesNotExist:
        Message.objects.create(
            contact=contact,
            text=message.text,
            created_on=message.created_on,
            direction=message.direction,
            message_flow_uuid=message.uuid,
        )

    channel = Channel.create(
        channel_type=message.channel_type,
        channel_flow_id=message.channel_id,
        project=project,
    )
    contact.update_channel(channel)
    return True
예제 #4
0
def realease_channel(channel_uuid, user):
    grpc_instance = utils.get_grpc_types().get("flow")
    grpc_instance.release_channel(
        channel_uuid=channel_uuid,
        user=user,
    )
    return True
예제 #5
0
def update_project(organization_uuid: str, organization_name: str):
    grpc_instance = utils.get_grpc_types().get("flow")
    grpc_instance.update_project(
        organization_uuid=organization_uuid,
        organization_name=organization_name,
    )
    return True
예제 #6
0
def delete_project(inteligence_organization: int, user_email):
    grpc_instance = utils.get_grpc_types().get("flow")
    grpc_instance.delete_project(
        project_uuid=inteligence_organization,
        user_email=user_email,
    )
    return True
예제 #7
0
def check_organization_free_plan():
    limits = GenericBillingData.get_generic_billing_data_instance()
    flow_instance = utils.get_grpc_types().get("flow")
    for organization in Organization.objects.filter(
            organization_billing__plan="free", is_suspended=False):
        for project in organization.project.all():
            project_timezone = project.timezone
            now = pendulum.now(project_timezone)
            before = now.strftime("%Y-%m-%d %H:%M")
            # first day of month
            after = now.start_of('month').strftime("%Y-%m-%d %H:%M")

            contact_count = flow_instance.get_billing_total_statistics(
                project_uuid=str(project.flow_organization),
                before=before,
                after=after).get("active_contacts")
            project.contact_count = int(contact_count)
            project.save(update_fields=["contact_count"])
        current_active_contacts = organization.active_contacts
        if current_active_contacts > limits.free_active_contacts_limit:
            organization.is_suspended = True
            for project in organization.project.all():
                app.send_task(
                    "update_suspend_project",
                    args=[str(project.flow_organization), True],
                )
            organization.save(update_fields=["is_suspended"])
            organization.organization_billing.send_email_expired_free_plan(
                organization.name,
                organization.authorizations.values_list("user__email",
                                                        flat=True),
            )
    return True
예제 #8
0
    def CreateWACChannel(self, request, context):
        serializer = CreateWACChannelRequestSerializer(message=request)

        if serializer.is_valid(raise_exception=True):
            project_uuid = serializer.validated_data.get("project_uuid")

            project = Project.objects.get(uuid=project_uuid)

            grpc_instance = utils.get_grpc_types().get("flow")

            try:
                response = grpc_instance.create_wac_channel(
                    user=serializer.validated_data.get("user"),
                    flow_organization=str(project.flow_organization),
                    config=serializer.validated_data.get("config"),
                    phone_number_id=serializer.validated_data.get(
                        "phone_number_id"),
                )

            except grpc.RpcError as error:
                if error.code() is grpc.StatusCode.INVALID_ARGUMENT:
                    self.context.abort(grpc.StatusCode.INVALID_ARGUMENT,
                                       "Bad Request")
                raise error

            return ChannelCreateResponse(
                uuid=response.uuid,
                name=response.name,
                config=response.config,
                address=response.address,
            )
예제 #9
0
def sync_channels_statistics():
    flow_instance = utils.get_grpc_types().get("flow")
    for project in Project.objects.all():
        project.extra_active_integration = len(
            list(
                flow_instance.list_channel(
                    project_uuid=str(project.flow_organization))))
        project.save(update_fields=["extra_active_integration"])
예제 #10
0
def create_project(project_name: str, user_email: str, project_timezone: str):
    grpc_instance = utils.get_grpc_types().get("flow")

    project = grpc_instance.create_project(
        project_name=project_name,
        user_email=user_email,
        project_timezone=project_timezone,
    )
    return {"id": project.id, "uuid": project.uuid}
예제 #11
0
def sync_project_statistics():
    flow_instance = utils.get_grpc_types().get("flow")
    for project in Project.objects.all():
        statistic_project_result = flow_instance.get_project_statistic(
            project_uuid=str(project.flow_organization), )

        if len(statistic_project_result) > 0:
            project.flow_count = int(
                statistic_project_result.get("active_flows"))
            project.save(update_fields=["flow_count"])
예제 #12
0
def get_billing_total_statistics(project_uuid: str, before: str, after: str):
    grpc_instance = utils.get_grpc_types().get("flow")

    contact_count = grpc_instance.get_billing_total_statistics(
        project_uuid=str(project_uuid),
        before=before,
        after=after,
    )

    return contact_count
예제 #13
0
def retrieve_classifier(classifier_uuid: str):
    grpc_instance = utils.get_grpc_types().get("flow")
    response = grpc_instance.get_classifier(
        classifier_uuid=str(classifier_uuid), )
    return dict(
        authorization_uuid=response.get("access_token"),
        classifier_type=response.get("classifier_type"),
        name=response.get("name"),
        is_active=response.get("is_active"),
        uuid=response.get("uuid"),
    )
예제 #14
0
    def ReleaseChannel(self, request, context):
        serializer = ReleaseChannelRequestSerializer(message=request)
        serializer.is_valid(raise_exception=True)

        grpc_instance = utils.get_grpc_types().get("flow")
        grpc_instance.release_channel(
            channel_uuid=serializer.validated_data.get("channel_uuid"),
            user=serializer.validated_data.get("user"),
        )

        return empty_pb2.Empty()
예제 #15
0
def sync_project_information():
    flow_instance = utils.get_grpc_types().get("flow")
    for project in Project.objects.all():
        flow_result = flow_instance.get_project_info(
            project_uuid=str(project.flow_organization))
        if len(flow_result) > 0:
            project.name = flow_result.get("name")
            project.timezone = str(flow_result.get("timezone"))
            project.date_format = str(flow_result.get("date_format"))
            project.flow_id = flow_result.get("id")
            project.save(
                update_fields=["name", "timezone", "date_format", "flow_id"])
예제 #16
0
def search_project(organization_id: int, project_uuid: str, text: str):
    flow_result = (utils.get_grpc_types().get("flow").get_project_flows(
        project_uuid=project_uuid,
        flow_name=text,
    ))
    inteligence_result = (
        IntelligenceRESTClient().get_organization_intelligences(
            intelligence_name=text, organization_id=organization_id))
    return {
        "flow": flow_result,
        "inteligence": inteligence_result,
    }
예제 #17
0
def list_channels(project_uuid, channel_type):
    grpc_instance = utils.get_grpc_types().get("flow")
    response = list(
        grpc_instance.list_channel(project_uuid=project_uuid,
                                   channel_type=channel_type))
    channels = []
    for channel in response:
        channels.append({
            "uuid": channel.uuid,
            "name": channel.name,
            "config": channel.config,
            "address": channel.address,
        })
    return channels
예제 #18
0
    def DestroyClassifier(self, request, context):
        serializer = DestroyClassifierRequestSerializer(message=request)

        if serializer.is_valid(raise_exception=True):
            classifier_uuid = serializer.validated_data.get("uuid")
            user_email = serializer.validated_data.get("user_email")

            grpc_instance = utils.get_grpc_types().get("flow")
            grpc_instance.delete_classifier(
                classifier_uuid=str(classifier_uuid),
                user_email=str(user_email),
            )

            return empty_pb2.Empty()
예제 #19
0
def create_channel(user, project_uuid, data, channeltype_code):
    grpc_instance = utils.get_grpc_types().get("flow")

    try:
        response = grpc_instance.create_channel(
            user=user,
            project_uuid=project_uuid,
            data=data,
            channeltype_code=channeltype_code)
        return dict(uuid=response.uuid,
                    name=response.name,
                    config=response.config,
                    address=response.address)
    except grpc.RpcError as error:
        raise error
예제 #20
0
def update_user_permission_project(flow_organization: str, project_uuid: str,
                                   user_email: str, permission: int):
    flow_instance = utils.get_grpc_types().get("flow")

    integrations_client = IntegrationsRESTClient()

    flow_instance.update_user_permission_project(
        organization_uuid=flow_organization,
        user_email=user_email,
        permission=permission,
    )
    integrations_client.update_user_permission_project(
        project_uuid=project_uuid, user_email=user_email, role=permission)

    return True
예제 #21
0
def create_wac_channel(user, flow_organization, config, phone_number_id):
    grpc_instance = utils.get_grpc_types().get("flow")
    try:
        response = grpc_instance.create_wac_channel(
            user=user,
            flow_organization=str(flow_organization),
            config=config,
            phone_number_id=phone_number_id,
        )
        return dict(uuid=response.uuid,
                    name=response.name,
                    config=response.config,
                    address=response.address)
    except grpc.RpcError as error:
        raise error
예제 #22
0
    def RetrieveClassifier(self, request, context):
        serializer = RetrieveClassifierRequestSerializer(message=request)

        if serializer.is_valid(raise_exception=True):
            classifier_uuid = serializer.validated_data.get("uuid")

            grpc_instance = utils.get_grpc_types().get("flow")
            response = grpc_instance.get_classifier(
                classifier_uuid=str(classifier_uuid), )

            return ClassifierResponse(
                authorization_uuid=response.get("access_token"),
                classifier_type=response.get("classifier_type"),
                name=response.get("name"),
                is_active=response.get("is_active"),
                uuid=response.get("uuid"),
            )
예제 #23
0
def create_classifier(project_uuid: str, user_email: str, classifier_name: str,
                      access_token):
    grpc_instance = utils.get_grpc_types().get("flow")
    response = grpc_instance.create_classifier(
        project_uuid=project_uuid,
        user_email=user_email,
        classifier_type="bothub",
        classifier_name=classifier_name,
        access_token=access_token,
    )
    return dict(
        authorization_uuid=response.access_token,
        classifier_type=response.classifier_type,
        name=response.name,
        is_active=response.is_active,
        uuid=response.uuid,
    )
예제 #24
0
    def Channel(self, request, context):
        grpc_instance = utils.get_grpc_types().get("flow")
        channel_type = getattr(request, "channel_type")

        for project in Project.objects.all():
            response = grpc_instance.list_channel(
                project_uuid=str(project.flow_organization),
                channel_type=channel_type,
            )

            for channel in response:
                yield ChannelListResponse(
                    uuid=channel.uuid,
                    name=channel.name,
                    config=channel.config,
                    address=channel.address,
                    project_uuid=str(project.uuid),
                )
예제 #25
0
def sync_repositories_statistics():
    flow_instance = utils.get_grpc_types().get("flow")
    ai_client = IntelligenceRESTClient()

    for project in Project.objects.all():
        classifiers_project = flow_instance.get_classifiers(
            project_uuid=str(project.flow_organization),
            classifier_type="bothub",
            is_active=True,
        )
        try:
            intelligence_count = int(
                ai_client.get_count_intelligences_project(
                    classifiers=classifiers_project, ).get(
                        "repositories_count"))
        except Exception:
            intelligence_count = 0

        project.intelligence_count = intelligence_count
        project.save(update_fields=["inteligence_count"])
예제 #26
0
def get_contacts_detailed(project_uuid: str, before: str, after: str):
    grpc_instance = utils.get_grpc_types().get("flow")
    project = Project.objects.get(uuid=project_uuid)
    response = []
    try:
        contacts = grpc_instance.get_active_contacts(
            str(project.flow_organization), before, after)
        active_contacts_info = []
        for contact in contacts:
            active_contacts_info.append({
                "name": contact.name,
                "uuid": contact.uuid
            })
        response.append({
            "project_name": project.name,
            "active_contacts": len(active_contacts_info),
            "contacts_info": active_contacts_info,
        })
        return response
    except grpc.RpcError as e:
        if e.code() is not grpc.StatusCode.NOT_FOUND:
            raise e
예제 #27
0
    def Classifier(self, request, context):
        serializer = ClassifierRequestSerializer(message=request)

        if serializer.is_valid(raise_exception=True):
            project_uuid = serializer.validated_data.get("project_uuid")

            project = Project.objects.get(uuid=project_uuid)

            grpc_instance = utils.get_grpc_types().get("flow")
            response = grpc_instance.get_classifiers(
                project_uuid=str(project.flow_organization),
                classifier_type="bothub",
                is_active=True,
            )

            for i in response:
                yield ClassifierResponse(
                    authorization_uuid=i.get("authorization_uuid"),
                    classifier_type=i.get("classifier_type"),
                    name=i.get("name"),
                    is_active=i.get("is_active"),
                    uuid=i.get("uuid"),
                )
예제 #28
0
def list_classifier(project_uuid: str):
    grpc_instance = utils.get_grpc_types().get("flow")
    response = grpc_instance.get_classifiers(
        project_uuid=str(project_uuid),
        classifier_type="bothub",
        is_active=True,
    )

    classifiers = {"data": []}
    for i in response:
        classifiers["data"].append({
            "authorization_uuid":
            i.get("authorization_uuid"),
            "classifier_type":
            i.get("classifier_type"),
            "name":
            i.get("name"),
            "is_active":
            i.get("is_active"),
            "uuid":
            i.get("uuid"),
        })
    return classifiers
예제 #29
0
    def CreateClassifier(self, request, context):
        serializer = CreateClassifierRequestSerializer(message=request)

        if serializer.is_valid(raise_exception=True):
            project_uuid = serializer.validated_data.get("project_uuid")

            project = Project.objects.get(uuid=project_uuid)

            grpc_instance = utils.get_grpc_types().get("flow")
            response = grpc_instance.create_classifier(
                project_uuid=str(project.flow_organization),
                user_email=serializer.validated_data.get("user"),
                classifier_type="bothub",
                classifier_name=serializer.validated_data.get("name"),
                access_token=serializer.validated_data.get("access_token"),
            )

            return ClassifierResponse(
                authorization_uuid=response.get("access_token"),
                classifier_type=response.get("classifier_type"),
                name=response.get("name"),
                is_active=response.get("is_active"),
                uuid=response.get("uuid"),
            )
예제 #30
0
def delete_user_permission_project(project_uuid: str, user_email: str,
                                   permission: int):
    utils.get_grpc_types().get("flow").delete_user_permission_project(
        project_uuid=project_uuid,
        user_email=user_email,
        permission=permission)