Last active
June 23, 2020 17:07
-
-
Save andyreagan/b8109f1f5dd6ccb2b1dd3fc6b29da80d to your computer and use it in GitHub Desktop.
Smart url encoder for tastypie
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.utils.html import smart_urlquote | |
from tastypie import fields | |
class SmartUrlField(fields.ApiField): | |
""" | |
A field for urls in plain text. | |
""" | |
dehydrated_type = 'string' | |
help_text = 'a field for urls in plain text.' | |
def convert(self, value): | |
if value is None: | |
return None | |
return smart_urlquote(value) | |
def hydrate(self, bundle): | |
value = super(SmartUrlField, self).hydrate(bundle) | |
return self.convert(value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment