Beispiel #1
0
 def patch(cls, user_from_email, request):
     case = cls.get_by_id(int(request.id))
     if case is None:
         raise endpoints.NotFoundException('Case not found.')
     if (case.owner != user_from_email.google_user_id) and not user_from_email.is_admin:
         raise endpoints.ForbiddenException('you are not the owner')
     EndpointsHelper.share_related_documents_after_patch(
         user_from_email,
         case,
         request
     )
     properties = ['owner', 'name', 'access', 'status', 'type_case', 'priority',
                   'description', 'case_origin']
     for p in properties:
         if hasattr(request, p):
             if (eval('case.' + p) != eval('request.' + p)) \
                     and (eval('request.' + p) and not (p in ['put', 'set_perm', 'put_index'])):
                 exec ('case.' + p + '= request.' + p)
     if request.closed_date:
         closed_date = datetime.datetime.strptime(
             request.closed_date,
             "%Y-%m-%dT%H:%M:00.000000"
         )
         case.closed_date = closed_date
     case_key_async = case.put_async()
     data = EndpointsHelper.get_data_from_index(str(case.key.id()))
     case.put_index(data)
     get_schema_request = CaseGetRequest(id=int(request.id))
     return cls.get_schema(user_from_email, get_schema_request)
Beispiel #2
0
 def patch(cls, user_from_email, request):
     pipeline = cls.get_by_id(int(request.id))
     if pipeline is None:
         raise endpoints.NotFoundException('Pipeline not found.')
     properties = ['owner', 'name', 'access', 'description']
     for p in properties:
         if hasattr(request, p):
             if (eval('pipeline.' + p) != eval('request.' + p)) \
                     and (eval('request.' + p) and not (p in ['put', 'set_perm', 'put_index'])):
                 exec('pipeline.' + p + '= request.' + p)
     pipeline_key_async = pipeline.put_async()
     data = EndpointsHelper.get_data_from_index(str(pipeline.key.id()))
     pipeline.put_index(data)
     get_schema_request = PipelineGetRequest(id=int(request.id))
     return cls.get_schema(user_from_email, get_schema_request)
Beispiel #3
0
 def patch(cls, user_from_email, request):
     pipeline = cls.get_by_id(int(request.id))
     if pipeline is None:
         raise endpoints.NotFoundException('Pipeline not found.')
     properties = ['owner', 'name', 'access',
                   'description']
     for p in properties:
         if hasattr(request, p):
             if (eval('pipeline.' + p) != eval('request.' + p)) \
                     and (eval('request.' + p) and not (p in ['put', 'set_perm', 'put_index'])):
                 exec ('pipeline.' + p + '= request.' + p)
     pipeline_key_async = pipeline.put_async()
     data = EndpointsHelper.get_data_from_index(str(pipeline.key.id()))
     pipeline.put_index(data)
     get_schema_request = PipelineGetRequest(id=int(request.id))
     return cls.get_schema(user_from_email, get_schema_request)
Beispiel #4
0
            except:
                from crm.iomodels.accounts import Account
                account_key = Account.get_key_by_name(
                    user_from_email=user_from_email, name=request.account)
                if account_key:
                    account = account_key.get()
                else:
                    is_new_account = True
                    account = Account(
                        name=request.account,
                        owner=user_from_email.google_user_id,
                        organization=user_from_email.organization,
                        access=request.access)
                    account_key_async = account.put_async()
                    account_key = account_key_async.get_result()
                    data = EndpointsHelper.get_data_from_index(
                        str(account.key.id()))
                    account.put_index(data)
        if account:
            # insert edges
            Edge.insert(start_node=account.key,
                        end_node=case_key_async,
                        kind='cases',
                        inverse_edge='parents')
            EndpointsHelper.update_edge_indexes(parent_key=case_key_async,
                                                kind='cases',
                                                indexed_edge=str(
                                                    account.key.id()))
            indexed = True

        contact = None
        if request.contact:
Beispiel #5
0
    def insert(cls, user_from_email, request):
        case = cls(
            owner=user_from_email.google_user_id,
            organization=user_from_email.organization,
            access=request.access,
            name=request.name,
            priority=request.priority,
            description=request.description
        )
        case_key = case.put_async()
        case_key_async = case_key.get_result()
        indexed = False
        status_key = None
        if request.status:
            status_key = ndb.Key(urlsafe=request.status)
            # insert edges
            Edge.insert(start_node=case_key_async,
                        end_node=status_key,
                        kind='status',
                        inverse_edge='related_cases')
        account = None
        if request.account:
            try:
                account_key = ndb.Key(urlsafe=request.account)
                account = account_key.get()
            except:
                from crm.iomodels.accounts import Account
                account_key = Account.get_key_by_name(
                    user_from_email=user_from_email,
                    name=request.account
                )
                if account_key:
                    account = account_key.get()
                else:
                    is_new_account = True
                    account = Account(
                        name=request.account,
                        owner=user_from_email.google_user_id,
                        organization=user_from_email.organization,
                        access=request.access
                    )
                    account_key_async = account.put_async()
                    account_key = account_key_async.get_result()
                    data = EndpointsHelper.get_data_from_index(str(account.key.id()))
                    account.put_index(data)
        if account:
            # insert edges
            Edge.insert(start_node=account.key,
                        end_node=case_key_async,
                        kind='cases',
                        inverse_edge='parents'
                        )
            EndpointsHelper.update_edge_indexes(
                parent_key=case_key_async,
                kind='cases',
                indexed_edge=str(account.key.id())
            )
            indexed = True

        contact = None
        if request.contact:
            try:
                contact_key = ndb.Key(urlsafe=request.contact)
                contact = contact_key.get()
            except:
                from crm.iomodels.contacts import Contact
                contact_key = Contact.get_key_by_name(
                    user_from_email=user_from_email,
                    name=request.contact
                )
                if contact_key:
                    contact = contact_key.get()
                else:
                    firstname = request.contact.split()[0]
                    lastname = " ".join(request.contact.split()[1:])
                    contact = Contact(
                        firstname=firstname,
                        lastname=lastname,
                        owner=user_from_email.google_user_id,
                        organization=user_from_email.organization,
                        access=request.access
                    )
                    contact_key_async = contact.put_async()
                    contact_key = contact_key_async.get_result()
                    if account:
                        data = EndpointsHelper.get_data_from_index(str(contact.key.id()))
                        contact.put_index(data)
                        Edge.insert(start_node=account.key,
                                    end_node=contact.key,
                                    kind='contacts',
                                    inverse_edge='parents')
                        EndpointsHelper.update_edge_indexes(
                            parent_key=contact.key,
                            kind='contacts',
                            indexed_edge=str(account.key.id())
                        )
            if contact:
                # insert edges
                Edge.insert(start_node=contact.key,
                            end_node=case_key_async,
                            kind='cases',
                            inverse_edge='parents')
                EndpointsHelper.update_edge_indexes(
                    parent_key=case_key_async,
                    kind='cases',
                    indexed_edge=str(contact.key.id())
                )
        for infonode in request.infonodes:
            Node.insert_info_node(
                case_key_async,
                iomessages.InfoNodeRequestSchema(
                    kind=infonode.kind,
                    fields=infonode.fields
                )
            )
        if not indexed:
            data = {'id': case_key_async.id()}
            case.put_index(data)
        current_status_schema = None
        if status_key:
            if status_key.get():
                current_status_schema = CaseStatusSchema(
                    name=status_key.get().status
                )

        case_schema = CaseSchema(
            id=str(case_key_async.id()),
            entityKey=case_key_async.urlsafe(),
            name=case.name,
            current_status=current_status_schema,
            priority=case.priority,
            created_at=case.created_at.strftime("%Y-%m-%dT%H:%M:00.000"),
            updated_at=case.updated_at.strftime("%Y-%m-%dT%H:%M:00.000")
        )
        return case_schema