|
| 1 | +import flask |
| 2 | + |
| 3 | +from flask.ext.restful.fields import Url |
| 4 | +from flask.ext.restful.fields import Raw |
| 5 | +from flask.ext.restful.fields import MarshallingException |
| 6 | + |
| 7 | +try: |
| 8 | + from urlparse import urlparse |
| 9 | + from urlparse import urlunparse |
| 10 | + from urllib import urlencode |
| 11 | +except ImportError: |
| 12 | + from urllib.parse import urlparse |
| 13 | + from urllib.parse import urlunparse |
| 14 | + from urllib.parse import urlencode |
| 15 | + |
| 16 | +class Date(Raw): |
| 17 | + """ Renders a Date obj as a string """ |
| 18 | + |
| 19 | + def __init__(self, date_format, default=None, attribute=None): |
| 20 | + super(Date, self).__init__(default, attribute) |
| 21 | + self.date_format = date_format |
| 22 | + |
| 23 | + def format(self, x): |
| 24 | + try: |
| 25 | + return x.strftime(self.date_format) |
| 26 | + except AttributeError: |
| 27 | + raise MarshallingException("Must be a valid date") |
| 28 | + |
| 29 | +class DateTime(Raw): |
| 30 | + """ Renders a DateTime obj as a string """ |
| 31 | + |
| 32 | + def __init__(self, datetime_format, default=None, attribute=None): |
| 33 | + super(DateTime, self).__init__(default, attribute) |
| 34 | + self.datetime_format = datetime_format |
| 35 | + |
| 36 | + def format(self, x): |
| 37 | + try: |
| 38 | + return x.strftime(self.datetime_format) |
| 39 | + except AttributeError: |
| 40 | + raise MarshallingException("Must be a valid date and time") |
| 41 | + |
| 42 | +class UrlUsage(Raw): |
| 43 | + """ Renders all the usages for a endpoint """ |
| 44 | + |
| 45 | + def __init__(self, endpoint, absolute=False, scheme=None): |
| 46 | + self.endpoint = endpoint |
| 47 | + self.absolute = absolute |
| 48 | + self.scheme = scheme |
| 49 | + |
| 50 | + def output(self, obj, key): |
| 51 | + urls = [] |
| 52 | + for rule in flask.current_app.url_map.iter_rules(): |
| 53 | + if rule.endpoint == self.endpoint: |
| 54 | + o = urlparse(flask.url_for(self.endpoint, _external = self.absolute)) |
| 55 | + scheme = self.scheme if self.scheme is not None else o.scheme |
| 56 | + urls.append(urlunparse((scheme, o.netloc, rule.rule, "", "", ""))) |
| 57 | + return urls |
| 58 | + |
| 59 | +class UrlWithParams(Url): |
| 60 | + """ Will render the a url with specified params |
| 61 | + |
| 62 | + params can be either a list or a dict. If params is a list then |
| 63 | + the url will contain a param key contained in the list |
| 64 | + and the value will be looked up in the obj by the same name. Using |
| 65 | + a dict allow you to specify a different key to look up in the obj. |
| 66 | +
|
| 67 | + ## Example using a list |
| 68 | + |
| 69 | + ```python |
| 70 | + fields = { |
| 71 | + foo: Integer, |
| 72 | + url: UrlWithParams('root', params=['foo']) |
| 73 | + } |
| 74 | +
|
| 75 | + marshal({foo: 1}, fields) |
| 76 | + ``` |
| 77 | +
|
| 78 | + Will produce: |
| 79 | +
|
| 80 | + http://example.com/?foo=1 |
| 81 | +
|
| 82 | + ## Example using a dict |
| 83 | +
|
| 84 | + |
| 85 | + ```python |
| 86 | + fields = { |
| 87 | + bar: Integer, |
| 88 | + url: UrlWithParams('root', params={'foo': 'bar'}) |
| 89 | + } |
| 90 | +
|
| 91 | + marshal({foo: 1}, fields) |
| 92 | + ``` |
| 93 | +
|
| 94 | + Will produce: |
| 95 | +
|
| 96 | + http://example.com/?foo=1 |
| 97 | + """ |
| 98 | + def __init__(self, endpoint, params=None, absolute=False, scheme=None): |
| 99 | + assert(isinstance(params, (type(None), list, dict))) |
| 100 | + super(UrlWithParams, self).__init__( |
| 101 | + endpoint, absolute, scheme) |
| 102 | + self.params = params |
| 103 | + |
| 104 | + def output(self, key, obj): |
| 105 | + url = super(UrlWithParams, self).output(key,obj) |
| 106 | + o = urlparse(url) |
| 107 | + if self.params is None: |
| 108 | + return url |
| 109 | + elif isinstance(self.params, list): |
| 110 | + return urlunparse((o.scheme, o.netloc, o.path, "", |
| 111 | + urlencode({ k:obj[k] for k in self.params}), "")) |
| 112 | + elif isinstance(self.params, dict): |
| 113 | + return urlunparse((o.scheme, o.netloc, o.path, "", |
| 114 | + urlencode({ k:obj[v] for k,v in self.params.items()}), "")) |
| 115 | + else: |
| 116 | + raise TypeError("params must be of type None, list, or dict") |
0 commit comments