예제 #1
0
 def get_client_from_crm(self):
     try:
         motius_crm = MotiusCrm()
         client = motius_crm.get_client(self.crm_client_id)
         return client
     except:
         return None
예제 #2
0
class ClientProfileAdminForm(forms.ModelForm):
    readonly_fields = ['public_id']
    user = ModelSelect2Field(
        queryset=get_user_model().objects.filter(groups__name='client'),
        required=False,
        widget=Select2Widget(
            select2_options={
                'width': '220px',
            }
        )
    )

    account_manager = ModelSelect2Field(
        queryset=get_user_model().objects.filter(team_profile__isnull=False),
        required=False,
        widget=Select2Widget(
            select2_options={
                'width': '220px',
            }
        )
    )

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.motius_crm = None

        # Try to get CRM accounts and show them in a dropdown
        try:
            self.motius_crm = MotiusCrm()
            self.fields['crm_id'] = Select2ChoiceField(
                label=_('CRM Account'),
                choices=self.motius_crm.get_client_choices(),
                required=False,
                widget=Select2Widget(select2_options={'width': '220px'})
            )
        except SugarError:
            self.fields['crm_id'] = forms.CharField(label=_('CRM ID'), required=False)

    def save(self, commit=True):
        instance = super().save(commit=False)

        # Import address from CRM
        if self.motius_crm and instance.crm_id:
            crm_entry = self.motius_crm.get_client(instance.crm_id)
            address_fields = self.motius_crm.get_address_from_client(crm_entry)
            if crm_entry:
                for key, value in address_fields.items():
                    if not getattr(instance, key) and value:
                        setattr(instance, key, value)

                account_manager = TeamProfile.objects.filter(crm_id=crm_entry['assigned_user_id'])
                if account_manager.exists():
                    instance.account_manager = account_manager[:1].get().user
        return instance

    class Meta:
        exclude = []
예제 #3
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.motius_crm = None

        # Try to get CRM accounts and show them in a dropdown
        try:
            self.motius_crm = MotiusCrm()
            self.fields['crm_id'] = Select2ChoiceField(
                label=_('CRM Account'),
                choices=self.motius_crm.get_client_choices(),
                required=False,
                widget=Select2Widget(select2_options={'width': '220px'})
            )
        except SugarError:
            self.fields['crm_id'] = forms.CharField(label=_('CRM ID'), required=False)