Skip to content
Newer
Older
100644 128 lines (102 sloc) 3.92 KB
b273e48 @alexkay Set up API URLs
authored Nov 21, 2011
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright © 2011 Alexander Kojevnikov <alexander@kojevnikov.com>
4 #
5 # muspy is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # muspy is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Affero General Public License for more details.
14 #
15 # You should have received a copy of the GNU Affero General Public License
16 # along with muspy. If not, see <http://www.gnu.org/licenses/>.
17
bf5a23d @alexkay api: artist and release handlers
authored Nov 21, 2011
18 from django.core.exceptions import ObjectDoesNotExist
19
b273e48 @alexkay Set up API URLs
authored Nov 21, 2011
20 from piston.handler import AnonymousBaseHandler, BaseHandler
4eff413 @alexkay api: DELETE artists
authored Nov 22, 2011
21 from piston.resource import Resource
bf5a23d @alexkay api: artist and release handlers
authored Nov 21, 2011
22 from piston.utils import rc
b273e48 @alexkay Set up API URLs
authored Nov 21, 2011
23
24 from app.models import *
25
26
4eff413 @alexkay api: DELETE artists
authored Nov 22, 2011
27 class ApiResource(Resource):
28 """TODO: Remove after upgrading to django-piston >= 0.2.3"""
29 def __init__(self, handler, authentication=None):
30 super(ApiResource, self).__init__(handler, authentication)
31 self.csrf_exempt = getattr(self.handler, 'csrf_exempt', True)
32
33
b273e48 @alexkay Set up API URLs
authored Nov 21, 2011
34 class ArtistHandler(AnonymousBaseHandler):
bf5a23d @alexkay api: artist and release handlers
authored Nov 21, 2011
35 allowed_methods = ('GET',)
36
37 def read(self, request, mbid):
38 try:
39 artist = Artist.objects.get(mbid=mbid)
40 except Artist.DoesNotExist:
41 return rc.NOT_HERE
42
43 return {
44 'mbid': artist.mbid,
45 'name': artist.name,
46 'sort_name': artist.sort_name,
47 'disambiguation': artist.disambiguation,
48 }
49
4eff413 @alexkay api: DELETE artists
authored Nov 22, 2011
50
e16077b @alexkay api: GET artists
authored Nov 22, 2011
51 class ArtistsHandler(BaseHandler):
54545fb @alexkay api: PUT artists
authored Nov 23, 2011
52 allowed_methods = ('GET', 'PUT', 'DELETE')
e16077b @alexkay api: GET artists
authored Nov 22, 2011
53
4eff413 @alexkay api: DELETE artists
authored Nov 22, 2011
54 def read(self, request, userid, mbid):
e16077b @alexkay api: GET artists
authored Nov 22, 2011
55 if request.user.username != userid:
56 return rc.FORBIDDEN
57
58 artists = Artist.get_by_user(user=request.user)
59 return [{
60 'mbid': artist.mbid,
61 'name': artist.name,
62 'sort_name': artist.sort_name,
63 'disambiguation': artist.disambiguation,
64 } for artist in artists]
65
54545fb @alexkay api: PUT artists
authored Nov 23, 2011
66 def update(self, request, userid, mbid):
67 if request.user.username != userid:
68 return rc.FORBIDDEN
69
70 mbid = request.POST.get('mbid', mbid)
71
72 if not mbid:
73 return rc.BAD_REQUEST
74
75 try:
76 artist = Artist.get_by_mbid(mbid)
77 except (Artist.Blacklisted, Artist.Unknown):
78 return rc.BAD_REQUEST
79 if not artist:
80 return rc.NOT_FOUND
81
82 UserArtist.add(request.user, artist)
83 response = rc.ALL_OK
84 response.content = {
85 'mbid': artist.mbid,
86 'name': artist.name,
87 'sort_name': artist.sort_name,
88 'disambiguation': artist.disambiguation,
89 }
90 return response
91
4eff413 @alexkay api: DELETE artists
authored Nov 22, 2011
92 def delete(self, request, userid, mbid):
93 if request.user.username != userid:
94 return rc.FORBIDDEN
95
96 if not mbid:
97 return rc.BAD_REQUEST
98
99 UserArtist.remove(user=request.user, mbids=[mbid])
100 return rc.DELETED
101
102
bf5a23d @alexkay api: artist and release handlers
authored Nov 21, 2011
103 class ReleaseHandler(AnonymousBaseHandler):
104 allowed_methods = ('GET',)
105
106 def read(self, request, mbid):
47f2017 @alexkay api: Multiple artists per release
authored Nov 21, 2011
107 q = ReleaseGroup.objects.select_related('artist')
108 q = q.filter(mbid=mbid)
109 q = q.filter(is_deleted=False)
110 releases = list(q)
111 if not releases:
bf5a23d @alexkay api: artist and release handlers
authored Nov 21, 2011
112 return rc.NOT_HERE
b273e48 @alexkay Set up API URLs
authored Nov 21, 2011
113
47f2017 @alexkay api: Multiple artists per release
authored Nov 21, 2011
114 release = releases[0]
115 artists = [release.artist for release in releases]
bf5a23d @alexkay api: artist and release handlers
authored Nov 21, 2011
116 return {
117 'mbid': release.mbid,
118 'name': release.name,
119 'type': release.type,
120 'date': release.date_str(),
47f2017 @alexkay api: Multiple artists per release
authored Nov 21, 2011
121 'artists': [{
122 'mbid': artist.mbid,
123 'name': artist.name,
124 'sort_name': artist.sort_name,
125 'disambiguation': artist.disambiguation,
126 } for artist in artists]
bf5a23d @alexkay api: artist and release handlers
authored Nov 21, 2011
127 }
Something went wrong with that request. Please try again.