示例#1
0
    def test_should_auto_close_polls(self, mock_send_bulk_sms):
        self.poll_to_post['question'] = 'Will you comply, yes or no?'
        self.poll_to_post['ptype'] = 'yesno'
        second_poll = self.poll_to_post.copy()
        second_poll['question'] = 'Please say, yes or no?'
        second_poll['keyword'] = 'anotherkey'
        # self.poll_to_send['text'] = 'Will you comply, yes or no? Reply With: NECOCPoll YES/NO'

        response = self.client.post(self.POLL_ENDPOINT,
                                    data=json.dumps(self.poll_to_post),
                                    content_type="application/json")
        self.assertEqual(201, response.status_code)
        retrieved_poll = Poll.objects(**self.poll_to_post)
        self.assertEqual(1, retrieved_poll.count())
        self.assertEqual(True, retrieved_poll[0].open)

        response = self.client.post(self.POLL_ENDPOINT,
                                    data=json.dumps(second_poll),
                                    content_type="application/json")
        self.assertEqual(201, response.status_code)
        retrieved_poll = Poll.objects(**self.poll_to_post)
        self.assertEqual(1, retrieved_poll.count())
        self.assertEqual(False, retrieved_poll[0].open)

        retrieved_poll = Poll.objects(**second_poll)
        self.assertEqual(1, retrieved_poll.count())
        self.assertEqual(True, retrieved_poll[0].open)
示例#2
0
    def test_should_auto_close_polls(self, mock_send_bulk_sms):
        self.poll_to_post['question'] = 'Will you comply, yes or no?'
        self.poll_to_post['ptype'] = 'yesno'
        second_poll = self.poll_to_post.copy()
        second_poll['question'] = 'Please say, yes or no?'
        second_poll['keyword'] = 'anotherkey'
        # self.poll_to_send['text'] = 'Will you comply, yes or no? Reply With: NECOCPoll YES/NO'

        response = self.client.post(self.POLL_ENDPOINT, data=json.dumps(self.poll_to_post),
                                    content_type="application/json")
        self.assertEqual(201, response.status_code)
        retrieved_poll = Poll.objects(**self.poll_to_post)
        self.assertEqual(1, retrieved_poll.count())
        self.assertEqual(True, retrieved_poll[0].open)

        response = self.client.post(self.POLL_ENDPOINT, data=json.dumps(second_poll),
                                    content_type="application/json")
        self.assertEqual(201, response.status_code)
        retrieved_poll = Poll.objects(**self.poll_to_post)
        self.assertEqual(1, retrieved_poll.count())
        self.assertEqual(False, retrieved_poll[0].open)

        retrieved_poll = Poll.objects(**second_poll)
        self.assertEqual(1, retrieved_poll.count())
        self.assertEqual(True, retrieved_poll[0].open)
示例#3
0
    def get_queryset(self):
        query_params = {key: value or None for key, value in self.request.GET.items()}
        user_group = self.request.user.group.name
        if user_group in getattr(settings, 'DISTRICT_GROUPS', []):
            target_locations = get_user_district_locations(self.request.user)
            query_params.update({'target_locations__in': target_locations})

        if 'ordering' in query_params:
            ordering_params = query_params['ordering']
            del query_params['ordering']
            query_set = Poll.objects(**query_params).order_by('%s' % ordering_params)
        else:
            query_set = Poll.objects(**query_params).order_by('-created_at')

        return query_set
示例#4
0
    def get_queryset(self):
        query_params = {
            key: value or None
            for key, value in self.request.GET.items()
        }
        user_group = self.request.user.group.name
        if user_group in getattr(settings, 'DISTRICT_GROUPS', []):
            target_locations = get_user_district_locations(self.request.user)
            query_params.update({'target_locations__in': target_locations})

        if 'ordering' in query_params:
            ordering_params = query_params['ordering']
            del query_params['ordering']
            query_set = Poll.objects(**query_params).order_by('%s' %
                                                              ordering_params)
        else:
            query_set = Poll.objects(**query_params).order_by('-created_at')

        return query_set
示例#5
0
    def test_should_post_a_yesno_poll_and_resend_appropriate_outgoing_message(self, mock_send_bulk_sms):
        self.poll_to_post['question'] = 'Will you comply, yes or no?'
        self.poll_to_post['ptype'] = 'yesno'
        self.poll_to_send['text'] = 'Will you comply, yes or no? Reply With: NECOCPoll YES/NO'

        response = self.client.post(self.POLL_ENDPOINT, data=json.dumps(self.poll_to_post),
                                    content_type="application/json")
        self.assertEqual(201, response.status_code)
        # mock_send_bulk_sms.delay.assert_called_once_with(ANY, ANY, self.poll_to_send['text'])
        self.assertTrue(mock_send_bulk_sms.delay.called_once_with(ANY, ANY, self.poll_to_send['text']))

        retrieved_poll = Poll.objects(**self.poll_to_post)
        self.assertEqual(1, retrieved_poll.count())
        self.assertTrue(retrieved_poll[0].is_yesno_poll())
示例#6
0
class PollListCreateView(ListCreateAPIView):
    model = Poll
    serializer_class = PollSerializer
    queryset = Poll.objects()
    permission_classes = [
        Or(build_permission_class('dms.can_manage_polls'),
           And(build_permission_class('dms.can_view_polls'), IsGetRequest))
    ]

    def get_queryset(self):
        query_params = {
            key: value or None
            for key, value in self.request.GET.items()
        }
        user_group = self.request.user.group.name
        if user_group in getattr(settings, 'DISTRICT_GROUPS', []):
            target_locations = get_user_district_locations(self.request.user)
            query_params.update({'target_locations__in': target_locations})

        if 'ordering' in query_params:
            ordering_params = query_params['ordering']
            del query_params['ordering']
            query_set = Poll.objects(**query_params).order_by('%s' %
                                                              ordering_params)
        else:
            query_set = Poll.objects(**query_params).order_by('-created_at')

        return query_set

    def post_save(self, obj, created=True):
        locations = self.get_location(obj)
        phone_numbers = list(
            UserProfile.objects(location__in=locations).values_list('phone'))
        if obj.ptype == 'yesno':
            text = '%s Reply With: NECOCPoll YES/NO' % obj.question
        else:
            text = '%s Reply With: NECOCPoll %s ...' % (obj.question,
                                                        obj.keyword)
        send_bulk_sms.delay(obj, phone_numbers, text)

    def get_location(self, obj):
        locations = Location.objects(id__in=obj.target_locations)
        if locations.filter(type='subcounty'):
            return locations
        districts_children = [district.children() for district in locations]
        return flatten(districts_children)

    def get_location_id(self, user):
        profile = UserProfile.objects(user=user).first()
        return profile.location.id if profile else ''
示例#7
0
    def test_should_post_a_poll_and_save_logs(self, mock_requests):
        some_id = 1234
        mock_response = MagicMock()
        mock_response.status_code = 201
        mock_response.json.return_value = {"messages": [some_id], "sms": [some_id]}

        mock_requests.post.return_value = mock_response

        response = self.client.post(self.POLL_ENDPOINT, data=json.dumps(self.poll_to_post),
                                    content_type="application/json")
        self.assertEqual(201, response.status_code)
        self.assertTrue(mock_requests.post.called_once_with(API_URL, json.dumps(self.poll_to_send), self.headers))

        retrieved_poll = Poll.objects(**self.poll_to_post)
        self.assertEqual(1, retrieved_poll.count())
示例#8
0
    def test_should_post_a_yesno_poll_and_resend_appropriate_outgoing_message(
            self, mock_send_bulk_sms):
        self.poll_to_post['question'] = 'Will you comply, yes or no?'
        self.poll_to_post['ptype'] = 'yesno'
        self.poll_to_send[
            'text'] = 'Will you comply, yes or no? Reply With: NECOCPoll YES/NO'

        response = self.client.post(self.POLL_ENDPOINT,
                                    data=json.dumps(self.poll_to_post),
                                    content_type="application/json")
        self.assertEqual(201, response.status_code)
        # mock_send_bulk_sms.delay.assert_called_once_with(ANY, ANY, self.poll_to_send['text'])
        self.assertTrue(
            mock_send_bulk_sms.delay.called_once_with(
                ANY, ANY, self.poll_to_send['text']))

        retrieved_poll = Poll.objects(**self.poll_to_post)
        self.assertEqual(1, retrieved_poll.count())
        self.assertTrue(retrieved_poll[0].is_yesno_poll())
示例#9
0
    def test_should_post_a_poll_and_save_logs(self, mock_requests):
        some_id = 1234
        mock_response = MagicMock()
        mock_response.status_code = 201
        mock_response.json.return_value = {
            "messages": [some_id],
            "sms": [some_id]
        }

        mock_requests.post.return_value = mock_response

        response = self.client.post(self.POLL_ENDPOINT,
                                    data=json.dumps(self.poll_to_post),
                                    content_type="application/json")
        self.assertEqual(201, response.status_code)
        self.assertTrue(
            mock_requests.post.called_once_with(API_URL,
                                                json.dumps(self.poll_to_send),
                                                self.headers))

        retrieved_poll = Poll.objects(**self.poll_to_post)
        self.assertEqual(1, retrieved_poll.count())
示例#10
0
 def validate_keyword(self, attrs, source):
     if len(Poll.objects(keyword=attrs[source])):
         raise rest_serializers.ValidationError('Keyword must be unique')
     return attrs
示例#11
0
 def validate_keyword(self, attrs, source):
     if len(Poll.objects(keyword=attrs[source])):
         raise rest_serializers.ValidationError('Keyword must be unique')
     return attrs