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 django
.http
import HttpResponseBadRequest
, Http404
, HttpResponseNotFound
19 from django
.views
.decorators
.csrf
import csrf_exempt
20 from django
.views
.decorators
.cache
import never_cache
21 from django
.shortcuts
import get_object_or_404
23 from mygpo
.decorators
import allowed_methods
, cors_origin
24 from mygpo
.podcasts
.models
import Podcast
, Episode
25 from mygpo
.utils
import parse_request_body
26 from mygpo
.api
.basic_auth
import require_valid_user
, check_username
27 from mygpo
.api
.httpresponse
import JsonResponse
28 from mygpo
.users
.models
import PodcastUserState
, DeviceDoesNotExist
29 from mygpo
.db
.couchdb
import get_userdata_database
30 from mygpo
.db
.couchdb
.podcast_state
import podcast_state_for_user_podcast
31 from mygpo
.db
.couchdb
.episode_state
import episode_state_for_user_episode
38 @allowed_methods(['GET', 'POST'])
40 def main(request
, username
, scope
):
42 udb
= get_userdata_database()
44 def user_settings(user
):
45 return user
, user
, None
47 def device_settings(user
, uid
):
48 device
= user
.get_device_by_uid(uid
)
50 # get it from the user directly so that changes
51 # to settings_obj are reflected in user (bug 1344)
52 settings_obj
= user
.get_device_by_uid(uid
)
54 return user
, settings_obj
, None
56 def podcast_settings(user
, url
):
57 podcast
= get_object_or_404(Podcast
, urls__url
=url
)
58 obj
= podcast_state_for_user_podcast(user
, podcast
)
61 def episode_settings(user
, url
, podcast_url
):
63 episode
= Episode
.objects
.filter(podcast__urls__url
=podcast_url
,
65 except Episode
.DoesNotExist
:
68 episode_state
= episode_state_for_user_episode(user
, episode
)
69 return episode_state
, episode_state
, udb
72 account
= lambda: user_settings(request
.user
),
73 device
= lambda: device_settings(request
.user
, request
.GET
.get('device', '')),
74 podcast
= lambda: podcast_settings(request
.user
, request
.GET
.get('podcast', '')),
75 episode
= lambda: episode_settings(request
.user
, request
.GET
.get('episode', ''), request
.GET
.get('podcast', ''))
79 if scope
not in models
.keys():
80 return HttpResponseBadRequest('undefined scope %s' % scope
)
83 base_obj
, settings_obj
, db
= models
[scope
]()
84 except DeviceDoesNotExist
as e
:
85 return HttpResponseNotFound(str(e
))
87 if request
.method
== 'GET':
88 return JsonResponse( settings_obj
.settings
)
90 elif request
.method
== 'POST':
91 actions
= parse_request_body(request
)
92 ret
= update_settings(settings_obj
, actions
)
94 return JsonResponse(ret
)
97 def update_settings(obj
, actions
):
98 for key
, value
in actions
.get('set', {}).iteritems():
99 obj
.settings
[key
] = value
101 for key
in actions
.get('remove', []):
102 if key
in obj
.settings
:
103 del obj
.settings
[key
]