コード例 #1
0
 def get_write_only_fields(self, obj):
     url = obj.proxy_url
     proxy_password = None
     if url is not None:
         proxy_password = strip_auth_from_url(url)[2]
     return get_write_only_fields(
         self, obj, extra_data={'proxy_password': proxy_password})
コード例 #2
0
    def to_representation(self, instance):
        """
        Splits proxy_url field from DB in 3 fields for representation.
        proxy_url, proxy_username, proxy_password (write-only)
        """

        data = super().to_representation(instance)

        if instance.proxy_url is not None:
            url, username, _ = strip_auth_from_url(instance.proxy_url)
            data['proxy_url'] = url
            data['proxy_username'] = username

        return data
コード例 #3
0
    def save(self):
        """
        Rejoins 3 fields url, username, password back to proxy_url on DB.

        If proxy_url is null or blank, the field is cleaned up in DB
        regardless of username, password values.

        If proxy, url, password or username is missing from the payload then
        should be the same as the existing value in the DB or None.

        If proxy username/password is set to null or blank, the fields are
        cleaned up in DB.
        """

        empty = object()
        proxy_url = self.validated_data.get('proxy_url', empty)
        proxy_username = self.validated_data.get('proxy_username', empty)
        proxy_password = self.validated_data.get('proxy_password', empty)

        url_in_db, username_in_db, password_in_db = strip_auth_from_url(
            self.instance.proxy_url) if self.instance.proxy_url else (None,
                                                                      None,
                                                                      None)

        if proxy_url is empty:
            proxy_url = url_in_db

        if proxy_username is empty:
            proxy_username = username_in_db

        if proxy_password is empty:
            proxy_password = password_in_db

        self.validated_data['proxy_url'] = join_proxy_url(
            proxy_url,
            proxy_username,
            proxy_password,
        ) if proxy_url else None

        super().save()