remove unnecessary imports
[mygpo.git] / mygpo / api / advanced / settings.py
blobc772fc7b03a19a874b6ae0cc2a16f7b6667a0b50
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
20 from mygpo.api.httpresponse import JsonResponse
21 from django.shortcuts import get_object_or_404
22 from mygpo.api.models import Device, SubscriptionMeta, EpisodeSettings
23 from django.views.decorators.csrf import csrf_exempt
24 from mygpo.decorators import allowed_methods
25 import json
28 @csrf_exempt
29 @require_valid_user
30 @check_username
31 @allowed_methods(['GET', 'POST'])
32 def main(request, username, scope):
34 models = dict(
35 account = lambda: request.get_profile(),
36 device = lambda: get_object_or_404(Device, user=request.user, uid=request.GET.get('device', '')),
37 podcast = lambda: SubscriptionMeta.objects.get_or_create(user=request.user,
38 podcast__url=request.GET.get('podcast', ''))[0],
39 episode = lambda: EpisodeSettings.objects.get_or_create(user=request.user,
40 episode__url=request.GET.get('episode', ''), episode__podcast__url=request.GET.get('podcast', ''))[0]
44 if scope not in models.keys():
45 return HttpResponseBadRequest()
47 obj = models[scope]()
49 if request.method == 'GET':
50 return JsonResponse( obj.settings )
51 elif request.method == 'POST':
52 actions = json.loads(request.raw_post_data)
53 return JsonResponse( update_settings(obj, actions) )
56 def update_settings(obj, actions):
57 for key, value in actions.get('set', {}).iteritems():
58 obj.settings[key] = value
60 for key in actions.get('remove', []):
61 if key in obj.settings:
62 del obj.settings[key]
64 return obj.settings