Ejemplo n.º 1
0
        def pre_save(self, obj):
            obj = super(OrgExtCRUDL.Edit, self).pre_save(obj)
            obj.set_banner_text(self.form.cleaned_data["banner_text"])

            field_ids = self.form.cleaned_data["contact_fields"]

            Field.get_all(self.request.org).filter(pk__in=field_ids).update(
                is_visible=True)
            Field.get_all(self.request.org).exclude(pk__in=field_ids).update(
                is_visible=False)

            group_ids = self.form.cleaned_data["suspend_groups"]

            Group.get_all(self.request.org).filter(pk__in=group_ids).update(
                suspend_from=True)
            Group.get_all(self.request.org).exclude(pk__in=group_ids).update(
                suspend_from=False)

            followup_flow_uuid = self.form.cleaned_data["followup_flow"]

            if followup_flow_uuid:
                for flow in obj.get_backend().fetch_flows(obj):
                    if flow.uuid == followup_flow_uuid:
                        obj.set_followup_flow(flow)
                        break
            else:
                obj.set_followup_flow(None)

            return obj
Ejemplo n.º 2
0
    def __init__(self, *args, **kwargs):
        org = kwargs.pop("org")
        super(OrgEditForm, self).__init__(*args, **kwargs)

        self.fields["banner_text"].initial = org.get_banner_text()

        field_choices = []
        for field in Field.objects.filter(org=org, is_active=True).order_by("label"):
            field_choices.append((field.pk, "%s (%s)" % (field.label, field.key)))

        self.fields["contact_fields"].choices = field_choices
        self.fields["contact_fields"].initial = [f.pk for f in Field.get_all(org, visible=True)]

        group_choices = []
        for group in Group.get_all(org, dynamic=False).order_by("name"):
            group_choices.append((group.pk, group.name))

        self.fields["suspend_groups"].choices = group_choices
        self.fields["suspend_groups"].initial = [g.pk for g in Group.get_suspend_from(org)]

        flow_choices = [("", "----")]
        for flow in org.get_backend().fetch_flows(org):
            flow_choices.append((flow.uuid, flow.name))

        flow_initial = org.get_followup_flow()

        self.fields["followup_flow"].choices = flow_choices
        if flow_initial:
            self.fields["followup_flow"].initial = flow_initial.uuid
Ejemplo n.º 3
0
    def __init__(self, *args, **kwargs):
        org = kwargs.pop('org')
        super(OrgEditForm, self).__init__(*args, **kwargs)

        self.fields['banner_text'].initial = org.get_banner_text()

        field_choices = []
        for field in Field.objects.filter(org=org,
                                          is_active=True).order_by('label'):
            field_choices.append(
                (field.pk, "%s (%s)" % (field.label, field.key)))

        self.fields['contact_fields'].choices = field_choices
        self.fields['contact_fields'].initial = [
            f.pk for f in Field.get_all(org, visible=True)
        ]

        group_choices = []
        for group in Group.get_all(org, dynamic=False).order_by('name'):
            group_choices.append((group.pk, group.name))

        self.fields['suspend_groups'].choices = group_choices
        self.fields['suspend_groups'].initial = [
            g.pk for g in Group.get_suspend_from(org)
        ]
Ejemplo n.º 4
0
        def pre_save(self, obj):
            obj = super(OrgExtCRUDL.Edit, self).pre_save(obj)
            obj.set_banner_text(self.form.cleaned_data['banner_text'])

            field_ids = self.form.cleaned_data['contact_fields']

            Field.get_all(self.request.org).filter(pk__in=field_ids).update(is_visible=True)
            Field.get_all(self.request.org).exclude(pk__in=field_ids).update(is_visible=False)

            group_ids = self.form.cleaned_data['suspend_groups']

            Group.get_all(self.request.org).filter(pk__in=group_ids).update(suspend_from=True)
            Group.get_all(self.request.org).exclude(pk__in=group_ids).update(suspend_from=False)

            return obj
Ejemplo n.º 5
0
    def __init__(self, *args, **kwargs):
        org = kwargs.pop('org')
        super(OrgEditForm, self).__init__(*args, **kwargs)

        self.fields['banner_text'].initial = org.get_banner_text()

        field_choices = []
        for field in Field.objects.filter(org=org, is_active=True).order_by('label'):
            field_choices.append((field.pk, "%s (%s)" % (field.label, field.key)))

        self.fields['contact_fields'].choices = field_choices
        self.fields['contact_fields'].initial = [f.pk for f in Field.get_all(org, visible=True)]

        group_choices = []
        for group in Group.get_all(org, dynamic=False).order_by('name'):
            group_choices.append((group.pk, group.name))

        self.fields['suspend_groups'].choices = group_choices
        self.fields['suspend_groups'].initial = [g.pk for g in Group.get_suspend_from(org)]
Ejemplo n.º 6
0
    def test_edit(self):
        url = reverse('orgs_ext.org_edit')

        self.login(self.admin)

        response = self.url_get('unicef', url)
        self.assertEqual(response.status_code, 200)

        form = response.context['form']

        self.assertEqual(form.initial['name'], "UNICEF")
        self.assertEqual(form.initial['timezone'],
                         pytz.timezone("Africa/Kampala"))

        self.assertEqual(form.fields['banner_text'].initial,
                         "Howdy (U)Partner!")
        self.assertEqual(form.fields['contact_fields'].choices,
                         [(self.age.pk, "Age (age)"),
                          (self.nickname.pk, "Nickname (nickname)"),
                          (self.state.pk, "State (state)")])
        self.assertEqual(set(form.fields['contact_fields'].initial),
                         {self.age.pk, self.nickname.pk})
        self.assertEqual(form.fields['suspend_groups'].choices,
                         [(self.females.pk, "Females"),
                          (self.males.pk, "Males"),
                          (self.reporters.pk, "Reporters")])
        self.assertEqual(form.fields['suspend_groups'].initial,
                         [self.reporters.pk])

        # test updating
        response = self.url_post(
            'unicef', url, {
                'name': "UNIZEFF",
                'timezone': "Africa/Kigali",
                'banner_text': "Chill",
                'contact_fields': [self.state.pk],
                'suspend_groups': [self.males.pk]
            })

        self.assertEqual(response.status_code, 302)

        self.unicef.refresh_from_db()
        self.unicef._config = None

        self.assertEqual(self.unicef.name, "UNIZEFF")
        self.assertEqual(self.unicef.timezone, pytz.timezone("Africa/Kigali"))
        self.assertEqual(self.unicef.get_banner_text(), "Chill")

        self.assertEqual(set(Group.get_suspend_from(self.unicef)),
                         {self.males})
        self.assertEqual(set(Field.get_all(self.unicef, visible=True)),
                         {self.state})
Ejemplo n.º 7
0
    def __init__(self, *args, **kwargs):
        org = kwargs.pop('org')
        is_create = kwargs.pop('is_create')

        super(LabelForm, self).__init__(*args, **kwargs)

        # don't let users change names of existing labels
        if not is_create:
            self.fields['name'].widget = forms.TextInput(attrs={'readonly': 'readonly'})

        self.fields['groups'].queryset = Group.get_all(org).order_by('name')

        self.fields['field_test'] = FieldTestField(
            org=org, label=_("Field criteria"), required=False,
            help_text=_("Match messages where contact field value is equal to any of these values (comma separated)")
        )
Ejemplo n.º 8
0
    def __init__(self, *args, **kwargs):
        org = kwargs.pop('org')
        is_create = kwargs.pop('is_create')

        super(LabelForm, self).__init__(*args, **kwargs)

        # don't let users change names of existing labels
        if not is_create:
            self.fields['name'].widget = forms.TextInput(
                attrs={'readonly': 'readonly'})

        self.fields['groups'].queryset = Group.get_all(org).order_by('name')

        self.fields['field_test'] = FieldTestField(
            org=org,
            label=_("Field criteria"),
            required=False,
            help_text=
            _("Match messages where contact field value is equal to any of these values (comma separated)"
              ))
Ejemplo n.º 9
0
    def test_edit(self):
        url = reverse('orgs_ext.org_edit')

        self.login(self.admin)

        response = self.url_get('unicef', url)
        self.assertEqual(response.status_code, 200)

        form = response.context['form']

        self.assertEqual(form.initial['name'], "UNICEF")
        self.assertEqual(form.initial['timezone'], "Africa/Kampala")

        self.assertEqual(form.fields['banner_text'].initial, "Howdy (U)Partner!")
        self.assertEqual(form.fields['contact_fields'].choices, [(self.age.pk, "Age (age)"),
                                                                 (self.nickname.pk, "Nickname (nickname)"),
                                                                 (self.state.pk, "State (state)")])
        self.assertEqual(set(form.fields['contact_fields'].initial), {self.age.pk, self.nickname.pk})
        self.assertEqual(form.fields['suspend_groups'].choices, [(self.females.pk, "Females"),
                                                                 (self.males.pk, "Males"),
                                                                 (self.reporters.pk, "Reporters")])
        self.assertEqual(form.fields['suspend_groups'].initial, [self.reporters.pk])

        # test updating
        response = self.url_post('unicef', url, {
            'name': "UNIZEFF", 'timezone': "Africa/Kigali", 'banner_text': "Chill",
            'contact_fields': [self.state.pk], 'suspend_groups': [self.males.pk]
        })

        self.assertEqual(response.status_code, 302)

        self.unicef.refresh_from_db()
        self.unicef._config = None

        self.assertEqual(self.unicef.name, "UNIZEFF")
        self.assertEqual(self.unicef.timezone, "Africa/Kigali")
        self.assertEqual(self.unicef.get_banner_text(), "Chill")

        self.assertEqual(set(Group.get_suspend_from(self.unicef)), {self.males})
        self.assertEqual(set(Field.get_all(self.unicef, visible=True)), {self.state})
Ejemplo n.º 10
0
    def get_context_data(self, **kwargs):
        context = super(BaseHomeView, self).get_context_data(**kwargs)
        org = self.request.org
        user = self.request.user
        partner = user.get_partner(org)

        labels = Label.get_all(org, user).order_by('name')
        groups = Group.get_all(org, visible=True).order_by('name')

        # angular app requires context data in JSON format
        context['context_data_json'] = json_encode({
            'user': {'id': user.pk, 'partner': partner.as_json() if partner else None},
            'labels': [l.as_json() for l in labels],
            'groups': [g.as_json() for g in groups],
        })

        context['banner_text'] = org.get_banner_text()
        context['folder'] = self.folder.name
        context['folder_icon'] = self.folder_icon
        context['open_case_count'] = Case.get_open(org, user).count()
        context['closed_case_count'] = Case.get_closed(org, user).count()
        return context
Ejemplo n.º 11
0
    def get_context_data(self, **kwargs):
        context = super(BaseInboxView, self).get_context_data(**kwargs)
        org = self.request.org
        user = self.request.user
        partner = user.get_partner(org)

        labels = list(Label.get_all(org, user).order_by('name'))
        Label.bulk_cache_initialize(labels)

        groups = Group.get_all(org, visible=True).order_by('name')
        fields = Field.get_all(org, visible=True).order_by('label')

        # angular app requires context data in JSON format
        context['context_data_json'] = json_encode({
            'user': {
                'id': user.pk,
                'partner': partner.as_json() if partner else None
            },
            'labels': [l.as_json() for l in labels],
            'groups': [g.as_json() for g in groups],
            'fields': [f.as_json() for f in fields],
        })

        context['banner_text'] = org.get_banner_text()
        context['folder'] = self.folder.name
        context['folder_icon'] = self.folder_icon
        context['open_case_count'] = Case.get_open(org, user).count()
        context['closed_case_count'] = Case.get_closed(org, user).count()
        context['allow_case_without_message'] = getattr(
            settings, 'SITE_ALLOW_CASE_WITHOUT_MESSAGE', False)
        context['user_must_reply_with_faq'] = org and not user.is_anonymous(
        ) and user.must_use_faq()
        context['site_contact_display'] = getattr(settings,
                                                  'SITE_CONTACT_DISPLAY',
                                                  "name")
        return context
Ejemplo n.º 12
0
    def test_edit(self, mock_fetch_flows):
        mock_fetch_flows.return_value = [
            Flow("0001-0001", "Registration"),
            Flow("0002-0002", "Follow-Up")
        ]

        url = reverse("orgs_ext.org_edit")

        self.login(self.admin)

        response = self.url_get("unicef", url)
        self.assertEqual(response.status_code, 200)

        form = response.context["form"]

        self.assertEqual(form.initial["name"], "UNICEF")
        self.assertEqual(form.initial["timezone"],
                         pytz.timezone("Africa/Kampala"))

        self.assertEqual(form.fields["banner_text"].initial,
                         "Howdy (U)Partner!")
        self.assertEqual(
            form.fields["contact_fields"].choices,
            [(self.age.pk, "Age (age)"),
             (self.nickname.pk, "Nickname (nickname)"),
             (self.state.pk, "State (state)")],
        )
        self.assertEqual(set(form.fields["contact_fields"].initial),
                         {self.age.pk, self.nickname.pk})
        self.assertEqual(
            form.fields["suspend_groups"].choices,
            [(self.females.pk, "Females"), (self.males.pk, "Males"),
             (self.reporters.pk, "Reporters")],
        )
        self.assertEqual(form.fields["suspend_groups"].initial,
                         [self.reporters.pk])
        self.assertEqual(
            form.fields["followup_flow"].choices,
            [("", "----"), ("0001-0001", "Registration"),
             ("0002-0002", "Follow-Up")],
        )
        self.assertEqual(form.fields["followup_flow"].initial, None)

        # test updating
        response = self.url_post(
            "unicef",
            url,
            {
                "name": "UNIZEFF",
                "timezone": "Africa/Kigali",
                "banner_text": "Chill",
                "contact_fields": [self.state.pk],
                "suspend_groups": [self.males.pk],
                "followup_flow": "0002-0002",
            },
        )

        self.assertEqual(response.status_code, 302)

        self.unicef.refresh_from_db()
        self.unicef._config = None

        self.assertEqual(self.unicef.name, "UNIZEFF")
        self.assertEqual(self.unicef.timezone, pytz.timezone("Africa/Kigali"))
        self.assertEqual(self.unicef.get_banner_text(), "Chill")
        self.assertEqual(self.unicef.get_followup_flow(),
                         Flow("0002-0002", "Follow-Up"))

        self.assertEqual(set(Group.get_suspend_from(self.unicef)),
                         {self.males})
        self.assertEqual(set(Field.get_all(self.unicef, visible=True)),
                         {self.state})

        # open the form again
        response = self.url_get("unicef", url)
        self.assertEqual(response.status_code, 200)

        form = response.context["form"]

        self.assertEqual(form.fields["followup_flow"].initial, "0002-0002")

        # test clearing things
        response = self.url_post(
            "unicef",
            url,
            {
                "name": "UNIZEFF",
                "timezone": "Africa/Kigali",
                "banner_text": "",
                "contact_fields": [],
                "suspend_groups": [],
                "followup_flow": "",
            },
        )

        self.assertEqual(response.status_code, 302)

        self.unicef.refresh_from_db()
        self.unicef._config = None

        self.assertIsNone(self.unicef.get_followup_flow())