946571e1347e97ea96642a1b209ae7e428b19583
2 # This file is part of my.gpodder.org.
4 # my.gpodder.org is free software: you can redistribute it and/or modify it
5 # under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or (at your
7 # option) any later version.
9 # my.gpodder.org is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
12 # License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with my.gpodder.org. If not, see <http://www.gnu.org/licenses/>.
18 from mygpo
.api
.basic_auth
import require_valid_user
, check_username
19 from django
.http
import HttpResponseBadRequest
, HttpResponseNotAllowed
20 from mygpo
.api
.httpresponse
import JsonResponse
21 from django
.shortcuts
import get_object_or_404
22 from mygpo
.api
.models
import Device
, UserProfile
, SubscriptionMeta
, EpisodeSettings
23 from django
.views
.decorators
.csrf
import csrf_exempt
30 def main(request
, username
, scope
):
33 account
= lambda: request
.get_profile(),
34 device
= lambda: get_object_or_404(Device
, user
=request
.user
, uid
=request
.GET
.get('device', '')),
35 podcast
= lambda: SubscriptionMeta
.objects
.get_or_create(user
=request
.user
,
36 podcast__url
=request
.GET
.get('podcast', ''))[0],
37 episode
= lambda: EpisodeSettings
.objects
.get_or_create(user
=request
.user
,
38 episode__url
=request
.GET
.get('episode', ''), episode__podcast__url
=request
.GET
.get('podcast', ''))[0]
42 if scope
not in models
.keys():
43 return HttpResponseBadRequest()
47 if request
.method
== 'GET':
48 return JsonResponse( obj
.settings
)
49 elif request
.method
== 'POST':
50 actions
= json
.loads(request
.raw_post_data
)
51 return JsonResponse( update_settings(obj
, actions
) )
54 return HttpResponseNotAllowed(['GET', 'POST'])
57 def update_settings(obj
, actions
):
58 for key
, value
in actions
.get('set', {}).iteritems():
59 obj
.settings
[key
] = value
61 for key
in actions
.get('remove', []):
62 if key
in obj
.settings
: