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
22 from mygpo
.decorators
import allowed_methods
, cors_origin
23 from mygpo
.core
.models
import Podcast
24 from mygpo
.utils
import parse_request_body
25 from mygpo
.api
.basic_auth
import require_valid_user
, check_username
26 from mygpo
.api
.httpresponse
import JsonResponse
27 from mygpo
.users
.models
import PodcastUserState
, DeviceDoesNotExist
28 from mygpo
.db
.couchdb
.episode
import episode_for_podcast_url
29 from mygpo
.db
.couchdb
.podcast
import podcast_for_url
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 db
= get_main_database()
43 udb
= get_userdata_database()
45 def user_settings(user
):
48 def device_settings(user
, uid
):
49 device
= user
.get_device_by_uid(uid
)
51 # get it from the user directly so that changes
52 # to settings_obj are reflected in user (bug 1344)
53 settings_obj
= user
.get_device_by_uid(uid
)
55 return user
, settings_obj
, db
57 def podcast_settings(user
, url
):
58 podcast
= podcast_for_url(url
)
61 obj
= podcast_state_for_user_podcast(user
, podcast
)
64 def episode_settings(user
, url
, podcast_url
):
65 episode
= episode_for_podcast_url(podcast_url
, url
)
69 episode_state
= episode_state_for_user_episode(user
, episode
)
70 return episode_state
, episode_state
, udb
73 account
= lambda: user_settings(request
.user
),
74 device
= lambda: device_settings(request
.user
, request
.GET
.get('device', '')),
75 podcast
= lambda: podcast_settings(request
.user
, request
.GET
.get('podcast', '')),
76 episode
= lambda: episode_settings(request
.user
, request
.GET
.get('episode', ''), request
.GET
.get('podcast', ''))
80 if scope
not in models
.keys():
81 return HttpResponseBadRequest('undefined scope %s' % scope
)
84 base_obj
, settings_obj
, db
= models
[scope
]()
85 except DeviceDoesNotExist
as e
:
86 return HttpResponseNotFound(str(e
))
88 if request
.method
== 'GET':
89 return JsonResponse( settings_obj
.settings
)
91 elif request
.method
== 'POST':
92 actions
= parse_request_body(request
)
93 ret
= update_settings(settings_obj
, actions
)
95 return JsonResponse(ret
)
98 def update_settings(obj
, actions
):
99 for key
, value
in actions
.get('set', {}).iteritems():
100 obj
.settings
[key
] = value
102 for key
in actions
.get('remove', []):
103 if key
in obj
.settings
:
104 del obj
.settings
[key
]