Exemple #1
0
def _construct_path(search_app, view_type, view_cls, suffix=None):
    prefix = 'public' if view_type == ViewType.public else None

    url_parts = [
        prefix,
        'search',
        search_app.name,
        suffix,
    ]

    url = join_truthy_strings(*url_parts, sep='/')
    url_name = join_truthy_strings(prefix, search_app.name, suffix, sep='-')
    view = view_cls.as_view(search_app=search_app)

    return path(url, view, name=url_name)
Exemple #2
0
    def request(self, method, path, params=None, json_=unset, content_type=''):
        """Make a request with a specified HTTP method."""
        params = urlencode(params) if params else ''
        url = join_truthy_strings(f'http://testserver{path}', params, sep='?')

        if json_ is not self.unset:
            content_type = 'application/json'
            body = json.dumps(json_, cls=DjangoJSONEncoder).encode('utf-8')
        else:
            body = b''

        sender = mohawk.Sender(
            self.credentials,
            url,
            method,
            content=body,
            content_type=content_type,
        )

        return self.api_client.generic(
            method,
            url,
            HTTP_AUTHORIZATION=sender.request_header,
            HTTP_X_FORWARDED_FOR=self.http_x_forwarded_for,
            data=body,
            content_type=content_type,
        )
Exemple #3
0
    def _add_serializer_error(self, field, errors):
        mapped_field = self.SERIALIZER_FIELD_MAPPING.get(field, field)

        if mapped_field in self.fields:
            self.add_error(mapped_field, errors)
        else:
            mapped_errors = [
                join_truthy_strings(field, error, sep=': ') for error in errors
            ]
            self.add_error(None, mapped_errors)
Exemple #4
0
    def get_contact_names(self, obj):
        """Returns contact names for the interaction as a formatted string."""
        contact_queryset = obj.contacts.order_by('pk')

        # Deliberate use of len() to force the query set to be evaluated (so that contact_count
        # and first_contact are consistent)
        contact_count = len(contact_queryset)
        first_contact = contact_queryset[0] if contact_count else None

        return join_truthy_strings(
            first_contact.name if first_contact else '',
            f'and {contact_count - 1} more' if contact_count > 1 else '',
        )
 def __str__(self):
     """Human-readable representation."""
     return join_truthy_strings(
         self.segment,
         '(disabled)' if self.disabled_on else None,
     )
Exemple #6
0
 def name(self):
     """Full name shorthand."""
     return join_truthy_strings(self.first_name, self.last_name)
Exemple #7
0
 def __str__(self):
     """Admin displayed human readable name."""
     company_desc = f'({self.company})' if self.company and self.company.name else ''
     return join_truthy_strings(self.name or '(no name)', company_desc)
Exemple #8
0
def test_join_truthy_strings(args, sep, res):
    """Tests joining turthy strings."""
    assert join_truthy_strings(*args, sep=sep) == res
Exemple #9
0
def _format_expected_adviser_name(dit_participant):
    adviser_name = dit_participant.adviser.name if dit_participant.adviser else ''
    team_name = f'({dit_participant.team.name})' if dit_participant.team else ''
    return join_truthy_strings(adviser_name, team_name)
Exemple #10
0
 def name_with_title(self):
     """Full name with title."""
     return join_truthy_strings(getattr(self.title, 'name', None), self.name)
 def __str__(self):
     """Human readable admin name."""
     return join_truthy_strings(
         self.name,
         '(disabled)' if self.disabled_on else None,
     )