urlman 2.0.0__tar.gz → 2.0.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {urlman-2.0.0/urlman.egg-info → urlman-2.0.2}/PKG-INFO +1 -5
- {urlman-2.0.0 → urlman-2.0.2}/setup.py +2 -2
- urlman-2.0.2/urlman/__init__.py +149 -0
- urlman-2.0.2/urlman/serializers.py +29 -0
- {urlman-2.0.0 → urlman-2.0.2/urlman.egg-info}/PKG-INFO +1 -5
- {urlman-2.0.0 → urlman-2.0.2}/urlman.egg-info/SOURCES.txt +2 -0
- {urlman-2.0.0 → urlman-2.0.2}/LICENSE +0 -0
- {urlman-2.0.0 → urlman-2.0.2}/README.rst +0 -0
- {urlman-2.0.0 → urlman-2.0.2}/setup.cfg +0 -0
- {urlman-2.0.0 → urlman-2.0.2}/urlman.egg-info/dependency_links.txt +0 -0
- {urlman-2.0.0 → urlman-2.0.2}/urlman.egg-info/top_level.txt +0 -0
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: urlman
|
|
3
|
-
Version: 2.0.
|
|
3
|
+
Version: 2.0.2
|
|
4
4
|
Summary: Django URL pattern helpers
|
|
5
5
|
Home-page: https://github.com/andrewgodwin/urlman
|
|
6
6
|
Author: Andrew Godwin
|
|
7
7
|
Author-email: andrew@aeracode.org
|
|
8
|
-
License: UNKNOWN
|
|
9
|
-
Platform: UNKNOWN
|
|
10
8
|
Classifier: Development Status :: 5 - Production/Stable
|
|
11
9
|
Classifier: Framework :: Django
|
|
12
10
|
Classifier: License :: OSI Approved :: Apache Software License
|
|
@@ -100,5 +98,3 @@ is required):
|
|
|
100
98
|
|
|
101
99
|
class MySerializer(ModelSerializer):
|
|
102
100
|
urls = UrlManField(urls=['view', 'edit'], attribute='urls', full=True)
|
|
103
|
-
|
|
104
|
-
|
|
@@ -6,11 +6,11 @@ with open(os.path.join(os.path.dirname(__file__), "README.rst")) as fh:
|
|
|
6
6
|
|
|
7
7
|
setup(
|
|
8
8
|
name="urlman",
|
|
9
|
-
version="2.0.
|
|
9
|
+
version="2.0.2",
|
|
10
10
|
description="Django URL pattern helpers",
|
|
11
11
|
long_description=readme_content,
|
|
12
12
|
url="https://github.com/andrewgodwin/urlman",
|
|
13
|
-
|
|
13
|
+
packages=["urlman"],
|
|
14
14
|
author="Andrew Godwin",
|
|
15
15
|
author_email="andrew@aeracode.org",
|
|
16
16
|
classifiers=[
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import string
|
|
2
|
+
|
|
3
|
+
try:
|
|
4
|
+
from urlparse import urlunparse
|
|
5
|
+
except ImportError: # pragma: no cover
|
|
6
|
+
from urllib.parse import urlunparse
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
__version__ = "2.0.2"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class UrlsMetaclass(type):
|
|
13
|
+
"""
|
|
14
|
+
Metaclass which makes attribute access instantiate the class with
|
|
15
|
+
the instance.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
callables = ["urls", "get_url", "get_example_url", "get_scheme", "get_hostname"]
|
|
19
|
+
|
|
20
|
+
def __new__(self, name, bases, attrs):
|
|
21
|
+
# Collect patterns off of the attrs
|
|
22
|
+
attrs["urls"] = {}
|
|
23
|
+
for name, item in list(attrs.items()):
|
|
24
|
+
if name not in self.callables and not name.startswith("__"):
|
|
25
|
+
attrs["urls"][name] = item
|
|
26
|
+
del attrs[name]
|
|
27
|
+
# Initialise
|
|
28
|
+
return type.__new__(self, name, bases, attrs)
|
|
29
|
+
|
|
30
|
+
def __get__(self, instance, klass):
|
|
31
|
+
return self(klass, instance, self.__name__)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class Urls(metaclass=UrlsMetaclass):
|
|
35
|
+
"""
|
|
36
|
+
Special object which lets you specify URL strings for objects.
|
|
37
|
+
|
|
38
|
+
Declare urls as string attributes on the object in _python 3_ string
|
|
39
|
+
format. If you need to you can also specify a handler function for a url.
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
def __init__(self, klass, instance, name):
|
|
43
|
+
self.klass = klass
|
|
44
|
+
self.instance = instance
|
|
45
|
+
self.context = {"self": self.instance}
|
|
46
|
+
self.context.update(self.urls)
|
|
47
|
+
self.__qualname__ = ".".join((klass.__qualname__, name))
|
|
48
|
+
|
|
49
|
+
def __getattr__(self, attr):
|
|
50
|
+
return self.get_url(attr)
|
|
51
|
+
|
|
52
|
+
def get_url(self, attr: str) -> "UrlString":
|
|
53
|
+
# Get the URL value
|
|
54
|
+
try:
|
|
55
|
+
url = self.urls[attr]
|
|
56
|
+
except KeyError:
|
|
57
|
+
raise ValueError(
|
|
58
|
+
"No URL called %r on %r" % (attr, self.instance.__class__.__name__)
|
|
59
|
+
)
|
|
60
|
+
else:
|
|
61
|
+
if callable(url):
|
|
62
|
+
url = url(self.instance)
|
|
63
|
+
value = UrlFormatter(self).vformat(url, [], {})
|
|
64
|
+
url = UrlString(value)
|
|
65
|
+
url.parent = self
|
|
66
|
+
return url
|
|
67
|
+
|
|
68
|
+
def get_example_url(self, attr):
|
|
69
|
+
# Get the URL value
|
|
70
|
+
try:
|
|
71
|
+
url = self.urls[attr]
|
|
72
|
+
except KeyError:
|
|
73
|
+
raise ValueError("No URL called %r" % attr)
|
|
74
|
+
else:
|
|
75
|
+
if callable(url):
|
|
76
|
+
url = url(self.instance)
|
|
77
|
+
value = UrlFormatter(self, example=True).vformat(url, [], {})
|
|
78
|
+
return value
|
|
79
|
+
|
|
80
|
+
def get_scheme(self, url: "UrlString"):
|
|
81
|
+
return "http"
|
|
82
|
+
|
|
83
|
+
def get_hostname(self, url: "UrlString"):
|
|
84
|
+
return "localhost"
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
class UrlString(str):
|
|
88
|
+
"""
|
|
89
|
+
Special string subclass for URLs (for future with/without host modes)
|
|
90
|
+
"""
|
|
91
|
+
|
|
92
|
+
def full(self, scheme="", hostname="", port="", params="", query="", fragment=""):
|
|
93
|
+
|
|
94
|
+
if hasattr(self, "parent") and self.parent is not None:
|
|
95
|
+
scheme = scheme or self.parent.get_scheme(self)
|
|
96
|
+
hostname = hostname or self.parent.get_hostname(self)
|
|
97
|
+
|
|
98
|
+
netloc = hostname
|
|
99
|
+
if port:
|
|
100
|
+
netloc = "%s:%s" % (netloc, port)
|
|
101
|
+
return urlunparse((scheme, netloc, self, params, query, fragment))
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
class UrlFormatter(string.Formatter):
|
|
105
|
+
"""
|
|
106
|
+
Special string formatter that calls methods.
|
|
107
|
+
"""
|
|
108
|
+
|
|
109
|
+
def __init__(self, urls, example=False):
|
|
110
|
+
self.example = example
|
|
111
|
+
self.urls = urls
|
|
112
|
+
|
|
113
|
+
def get_value(self, key, args, kwargs):
|
|
114
|
+
# First, try looking up the key in our own URLs
|
|
115
|
+
try:
|
|
116
|
+
if self.example:
|
|
117
|
+
value = self.urls.get_example_url(key)
|
|
118
|
+
else:
|
|
119
|
+
value = self.urls.get_url(key)
|
|
120
|
+
except ValueError:
|
|
121
|
+
pass
|
|
122
|
+
else:
|
|
123
|
+
return value
|
|
124
|
+
# Now, fall back to looking in the context (just self)
|
|
125
|
+
if key == "self":
|
|
126
|
+
if self.example:
|
|
127
|
+
return PrintMe("self")
|
|
128
|
+
else:
|
|
129
|
+
return self.urls.instance
|
|
130
|
+
# Uh oh.
|
|
131
|
+
raise KeyError("No other URL called %r for use in URL format" % key)
|
|
132
|
+
|
|
133
|
+
def format_field(self, value, format_spec):
|
|
134
|
+
if callable(value):
|
|
135
|
+
value = value()
|
|
136
|
+
return string.Formatter.format_field(self, value, format_spec)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
class PrintMe(object):
|
|
140
|
+
"""Object which prints itself, essentially."""
|
|
141
|
+
|
|
142
|
+
def __init__(self, obj):
|
|
143
|
+
self.obj = obj
|
|
144
|
+
|
|
145
|
+
def __getattr__(self, attr):
|
|
146
|
+
return PrintMe(self.obj + "." + attr)
|
|
147
|
+
|
|
148
|
+
def __str__(self):
|
|
149
|
+
return "{%s}" % self.obj
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
try:
|
|
2
|
+
from rest_framework.serializers import Field
|
|
3
|
+
except ImportError: # pragma: no cover
|
|
4
|
+
|
|
5
|
+
class Field:
|
|
6
|
+
pass
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class UrlManField(Field):
|
|
10
|
+
"""Serializer class for Django Restframework."""
|
|
11
|
+
|
|
12
|
+
def __init__(self, urls, attribute="urls", full=True, **kwargs):
|
|
13
|
+
kwargs.setdefault("read_only", True)
|
|
14
|
+
kwargs.setdefault("write_only", False)
|
|
15
|
+
kwargs.setdefault("label", None)
|
|
16
|
+
kwargs.setdefault("source", "*")
|
|
17
|
+
super().__init__(**kwargs)
|
|
18
|
+
self.urls = urls
|
|
19
|
+
self.url_attribute = attribute
|
|
20
|
+
self.full = full
|
|
21
|
+
|
|
22
|
+
def to_representation(self, value):
|
|
23
|
+
url_class = getattr(value, self.url_attribute)
|
|
24
|
+
return {
|
|
25
|
+
url: getattr(url_class, url).full()
|
|
26
|
+
if self.full
|
|
27
|
+
else getattr(url_class, url)
|
|
28
|
+
for url in self.urls
|
|
29
|
+
}
|
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: urlman
|
|
3
|
-
Version: 2.0.
|
|
3
|
+
Version: 2.0.2
|
|
4
4
|
Summary: Django URL pattern helpers
|
|
5
5
|
Home-page: https://github.com/andrewgodwin/urlman
|
|
6
6
|
Author: Andrew Godwin
|
|
7
7
|
Author-email: andrew@aeracode.org
|
|
8
|
-
License: UNKNOWN
|
|
9
|
-
Platform: UNKNOWN
|
|
10
8
|
Classifier: Development Status :: 5 - Production/Stable
|
|
11
9
|
Classifier: Framework :: Django
|
|
12
10
|
Classifier: License :: OSI Approved :: Apache Software License
|
|
@@ -100,5 +98,3 @@ is required):
|
|
|
100
98
|
|
|
101
99
|
class MySerializer(ModelSerializer):
|
|
102
100
|
urls = UrlManField(urls=['view', 'edit'], attribute='urls', full=True)
|
|
103
|
-
|
|
104
|
-
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|