REST APIs disguised as Django ORM Models
With django-wham you can query REST APIs in exactly the same way you query Django Models:
```
artist = SpotifyArtist.objects.get(id='5Z1XZyEFY0dewG8faEIiEx') fetching https://api.spotify.com/v1/artists/5Z1XZyEFY0dewG8faEIiEx print artist.name Django Reinhardt ```
```
twitter_user = TwitterUser.objects.get(screen_name='djangoproject') fetching https://api.twitter.com/1.1/users/show.json?screen_name=djangoproject for tweet in twitter_user.tweets.all(): print tweet.text fetching https://api.twitter.com/1.1/statuses/user_timeline.json?user_id=191225303 Django 1.7 release candidate 2 - It's almost here! Today we're pleased to announce the second release-candidate pa... Django 1.7 release candidate 1 - It's almost here! Tonight we're pleased to announce the first release-candidate p... ... ```
In order to query a REST API (eg: Twitter, Flickr, Youtube, etc), you need a Wham Model. A Wham Model corresponds to an API Endpoint and has a special inner WhamMeta class that describes how the REST API should map to the Django Model.
This simple Wham Model that enables you too look up a Spotify Artist by its Spotify ID and retrieve some information about that artist. In this case the names of the fields match the property names of the retrieved JSON data, so very little configuration is required.
``` from wham.models import WhamModel
class SpotifyArtist(WhamModel):
id = CharField(max_length=100, primary_key=True)
# the primary key field should be of the same name and type as the API Endpoint ID
# rather than the default auto-incrementing key Django provides. For example,
# '5Z1XZyEFY0dewG8faEIiEx' is the Spotify ID for Django Reinhardt.
name = TextField()
href = TextField()
popularity = IntegerField()
uri = UriField()
class WhamMeta(SpotifyMeta):
base_url = 'https://api.spotify.com/v1/'
endpoint = 'artists'
```
Now we add another Spotify Endpoint, this time both of the Models' WhamMeta class inherit from a single class, to share commonalities between endpoints such as the base url ``` class SpotifyWhamMeta: base_url = 'https://api.spotify.com/v1/'
class SpotifyArtist(WhamModel): ...
class WhamMeta(SpotifyWhamMeta):
endpoint = 'artists'
class SpotifyAlbum(WhamModel):
id = WhamCharField(max_length=255, primary_key=True)
name = WhamTextField()
release_date = WhamTextField(null=True)
popularity = WhamIntegerField(null=True, wham_detailed=True)
class WhamMeta(SpotifyWhamMeta):
endpoint = 'albums'
```
Django Wham supports many to many endpoints such as https://api.spotify.com/v1/artists/{id}/albums
.
This is possible by specifying a WhamManyToMany field and doing a little configuration.
To create a many to many field between Artists and Albums:
``` class SpotifyArtist(WhamModel): ...
albums = WhamManyToManyField(
'SpotifyAlbum',
wham_endpoint='artists/{{id}}/albums', #django template syntax is used to substitue the value of thd id
wham_results_path=['items']
# This is the 'path' where to find the list of results in the retreived JSON data.
# The list or tuple represents a path in the JSON tree. A hypothetical example might be ['photos', 'results']
)
...
```
Have a look at https://github.com/mbylstra/django-wham/tree/master/wham/apis for some more examples of Wham Models. We're hoping this will become an ever growing repository of Wham Models for the multitude of public REST APIs on the web, so you won't have to write your own. If you have ever wanted to contribute to open source, but have been too daunted, creating a Wham Model is a fun and easy way to get started. Only basic Django experience is required.
django-wham can be installed via PyPI: ``` pip install django-wham
```
Add some entries to your INSTALLED_APPS in your settings.py file. See https://github.com/mbylstra/django-wham/tree/master/wham/apis for a list of example apis. ``` INSTALLED_APPS = ( ... 'wham', #required 'wham.apis.spotify', #an optional provided API 'wham.apis.twitter', #optional 'your_custom_wham_app' #this should be a Django app with one or more Wham Models in its models.py file ... )
You must then run syncdb before you can start using any wham models
python manage.py syncdb
Django wham models can be used just like regular Django Models
from wham.apis.spotify import SpotifyArtist
SpotifyArtist.objects.filter(name__icontains="green")
```
This git repository also comes with a demo django project and some Ipython notebook examples that can be run with django manage.py shell_plus --notebook
. Your environment must have the packages in ipython-requirements.txt installed for this to work.
Django Wham is currently in an Alpha/Proof-of-Concept stage. While I am using it to great success in my project http://kaleidoscope.fm, there are probably many edge cases that it does not handle very well. I would not recommend using this in production just yet. If you are interested in using this, please contact me and I won't hestitate to help out. Any kind of feedback would also be highly appreciated (bug reports, suggestions for improvements, requests for more APIs to be supported) - please submit a Git issue!
Bumps ipython from 2.1.0 to 7.16.3.
Sourced from ipython's releases.
7.9.0
No release notes provided.
7.8.0
No release notes provided.
7.7.0
No release notes provided.
7.6.1
No release notes provided.
7.6.0
No release notes provided.
7.5.0
No release notes provided.
7.4.0
No release notes provided.
7.3.0
No release notes provided.
7.2.0
No release notes provided.
7.1.1
No release notes provided.
7.1.0
No release notes provided.
7.0.1
No release notes provided.
7.0.0
No release notes provided.
7.0.0-doc
No release notes provided.
7.0.0rc1
No release notes provided.
7.0.0b1
No release notes provided.
6.2.1
No release notes provided.
... (truncated)
d43c7c7
release 7.16.35fa1e40
Merge pull request from GHSA-pq7m-3gw7-gq5x8df8971
back to dev9f477b7
release 7.16.2138f266
bring back release helper from master branch5aa3634
Merge pull request #13341 from meeseeksmachine/auto-backport-of-pr-13335-on-7...bcae8e0
Backport PR #13335: What's new 7.16.28fcdcd3
Pin Jedi to <0.17.2.2486838
release 7.16.120bdc6f
fix conda buildDependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase
.
Bumps django from 1.6.5 to 2.2.24.
2da029d
[2.2.x] Bumped version for 2.2.24 release.f27c38a
[2.2.x] Fixed CVE-2021-33571 -- Prevented leading zeros in IPv4 addresses.053cc95
[2.2.x] Fixed CVE-2021-33203 -- Fixed potential path-traversal via admindocs'...6229d87
[2.2.x] Confirmed release date for Django 2.2.24.f163ad5
[2.2.x] Added stub release notes and date for Django 2.2.24.bed1755
[2.2.x] Changed IRC references to Libera.Chat.63f0d7a
[2.2.x] Refs #32718 -- Fixed file_storage.test_generate_filename and model_fi...5fe4970
[2.2.x] Post-release version bump.61f814f
[2.2.x] Bumped version for 2.2.23 release.b8ecb06
[2.2.x] Fixed #32718 -- Relaxed file name validation in FileField.Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase
.
Bumps jinja2 from 2.7.3 to 2.11.3.
Sourced from jinja2's releases.
2.11.3
This contains a fix for a speed issue with the
urlize
filter.urlize
is likely to be called on untrusted user input. For certain inputs some of the regular expressions used to parse the text could take a very long time due to backtracking. As part of the fix, the email matching became slightly stricter. The various speedups apply tourlize
in general, not just the specific input cases.
- PyPI: https://pypi.org/project/Jinja2/2.11.3/
- Changes: https://jinja.palletsprojects.com/en/2.11.x/changelog/#version-2-11-3
2.11.2
2.11.1
This fixes an issue in async environment when indexing the result of an attribute lookup, like
{{ data.items[1:] }}
.2.11.0
- Changes: https://jinja.palletsprojects.com/en/2.11.x/changelog/#version-2-11-0
- Blog: https://palletsprojects.com/blog/jinja-2-11-0-released/
- Twitter: https://twitter.com/PalletsTeam/status/1221883554537230336
This is the last version to support Python 2.7 and 3.5. The next version will be Jinja 3.0 and will support Python 3.6 and newer.
2.10.3
2.10.2
2.10.1
- Changes: https://jinja.palletsprojects.com/en/2.10.x/changelog/#version-2-10-1
- Blog: https://palletsprojects.com/blog/jinja-2-10-1-released/
- Twitter: https://twitter.com/PalletsTeam/status/1114605127308992513
2.10
Primary changes
- A
NativeEnvironment
that renders Python types instead of strings. http://jinja.pocoo.org/docs/2.10/nativetypes/- A
namespace
object that works with{% set %}
. This replaces previous hacks for storing state across iterations or scopes. http://jinja.pocoo.org/docs/2.10/templates/#assignments- The
loop
object now hasnextitem
andprevitem
attributes, as well as achanged
method, for the common case of outputting something as a value in the loop changes. More complicated cases can use thenamespace
object. http://jinja.pocoo.org/docs/2.10/templates/#forInstall or upgrade
Install from PyPI with pip:
... (truncated)
Sourced from jinja2's changelog.
Version 2.11.3
Released 2021-01-31
- Improve the speed of the
urlize
filter by reducing regex backtracking. Email matching requires a word character at the start of the domain part, and only word characters in the TLD. :pr:1343
Version 2.11.2
Released 2020-04-13
- Fix a bug that caused callable objects with
__getattr__
, like :class:~unittest.mock.Mock
to be treated as a :func:contextfunction
. :issue:1145
- Update
wordcount
filter to trigger :class:Undefined
methods by wrapping the input in :func:soft_str
. :pr:1160
- Fix a hang when displaying tracebacks on Python 32-bit. :issue:
1162
- Showing an undefined error for an object that raises
AttributeError
on access doesn't cause a recursion error. :issue:1177
- Revert changes to :class:
~loaders.PackageLoader
from 2.10 which removed the dependency on setuptools and pkg_resources, and added limited support for namespace packages. The changes caused issues when using Pytest. Due to the difficulty in supporting Python 2 and :pep:451
simultaneously, the changes are reverted until 3.0. :pr:1182
- Fix line numbers in error messages when newlines are stripped. :pr:
1178
- The special
namespace()
assignment object in templates works in async environments. :issue:1180
- Fix whitespace being removed before tags in the middle of lines when
lstrip_blocks
is enabled. :issue:1138
- :class:
~nativetypes.NativeEnvironment
doesn't evaluate intermediate strings during rendering. This prevents early evaluation which could change the value of an expression. :issue:1186
Version 2.11.1
Released 2020-01-30
- Fix a bug that prevented looking up a key after an attribute (
{{ data.items[1:] }}
) in an async template. :issue:1141
... (truncated)
cf21539
release version 2.11.315ef8f0
Merge pull request #1343 from pallets/urlize-speedupef658dc
speed up urlize matchingeeca0fe
Merge pull request #1207 from mhansen/patch-12dd7691
Merge pull request #1209 from mhansen/patch-34892940
do_dictsort: update example ready to copy/paste7db7d33
api.rst: bugfix in docs, import PackageLoader9ec465b
fix changelog header737a4cd
release version 2.11.2179df6b
Merge pull request #1190 from pallets/native-evalDependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase
.
How to auth with username:password and without token? I can't find how auth. without fast authenficate with twitter, facebook, google, and etc.... ?
I thought django-wham was a great idea. I am about to integrate with justgiving and I wondered if the project was still active?