prevent episode playmark events without total time (bug 1136)
[mygpo.git] / mygpo / api / advanced / __init__.py
blobb55707263640b98ea3b1ae13cb07f1d01e8478fa
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 HttpResponse, HttpResponseBadRequest, HttpResponseForbidden, Http404, HttpResponseNotAllowed
20 from mygpo.api.models import Device, Podcast, SubscriptionAction, Episode, EpisodeAction, SUBSCRIBE_ACTION, UNSUBSCRIBE_ACTION, EPISODE_ACTION_TYPES, DEVICE_TYPES, Subscription
21 from mygpo.api.httpresponse import JsonResponse
22 from mygpo.api.sanitizing import sanitize_url
23 from mygpo.api.advanced.directory import episode_data, podcast_data
24 from mygpo.api.backend import get_all_subscriptions
25 from django.core import serializers
26 from django.shortcuts import get_object_or_404
27 from time import mktime, gmtime, strftime
28 from datetime import datetime, timedelta
29 import dateutil.parser
30 from mygpo.log import log
31 from mygpo.utils import parse_time, parse_bool
32 from django.db import IntegrityError
33 import re
34 from django.views.decorators.csrf import csrf_exempt
36 try:
37 #try to import the JSON module (if we are on Python 2.6)
38 import json
40 # Python 2.5 seems to have a different json module
41 if not 'dumps' in dir(json):
42 raise ImportError
44 except ImportError:
45 # No JSON module available - fallback to simplejson (Python < 2.6)
46 print "No JSON module available - fallback to simplejson (Python < 2.6)"
47 import simplejson as json
50 @csrf_exempt
51 @require_valid_user
52 @check_username
53 def subscriptions(request, username, device_uid):
55 now = datetime.now()
56 now_ = int(mktime(now.timetuple()))
58 if request.method == 'GET':
59 try:
60 d = Device.objects.get(user=request.user, uid=device_uid, deleted=False)
61 except Device.DoesNotExist:
62 raise Http404('device %s does not exist' % device_uid)
64 try:
65 since_ = request.GET['since']
66 except KeyError:
67 return HttpResponseBadRequest('parameter since missing')
69 since = datetime.fromtimestamp(float(since_))
71 changes = get_subscription_changes(request.user, d, since, now)
73 return JsonResponse(changes)
75 elif request.method == 'POST':
76 d, created = Device.objects.get_or_create(user=request.user, uid=device_uid, defaults = {'type': 'other', 'name': 'New Device'})
78 if d.deleted:
79 d.deleted = False
80 d.save()
82 actions = json.loads(request.raw_post_data)
83 add = actions['add'] if 'add' in actions else []
84 rem = actions['remove'] if 'remove' in actions else []
86 try:
87 update_urls = update_subscriptions(request.user, d, add, rem)
88 except IntegrityError, e:
89 return HttpResponseBadRequest(e)
91 return JsonResponse({
92 'timestamp': now_,
93 'update_urls': update_urls,
96 else:
97 return HttpResponseNotAllowed(['GET', 'POST'])
100 def update_subscriptions(user, device, add, remove):
101 updated_urls = []
102 add_sanitized = []
103 rem_sanitized = []
105 for a in add:
106 if a in remove:
107 raise IntegrityError('can not add and remove %s at the same time' % a)
109 for u in add:
110 us = sanitize_url(u)
111 if u != us: updated_urls.append( (u, us) )
112 if us != '': add_sanitized.append(us)
114 for u in remove:
115 us = sanitize_url(u)
116 if u != us: updated_urls.append( (u, us) )
117 if us != '' and us not in add_sanitized:
118 rem_sanitized.append(us)
120 for a in add_sanitized:
121 p, p_created = Podcast.objects.get_or_create(url=a)
122 try:
123 s = SubscriptionAction.objects.create(podcast=p,device=device,action=SUBSCRIBE_ACTION)
124 except IntegrityError, e:
125 log('can\'t add subscription %s for user %s: %s' % (a, user, e))
127 for r in rem_sanitized:
128 p, p_created = Podcast.objects.get_or_create(url=r)
129 try:
130 s = SubscriptionAction.objects.create(podcast=p,device=device,action=UNSUBSCRIBE_ACTION)
131 except IntegrityError, e:
132 log('can\'t remove subscription %s for user %s: %s' % (r, user, e))
134 return updated_urls
136 def get_subscription_changes(user, device, since, until):
137 actions = {}
138 for a in SubscriptionAction.objects.filter(device=device, timestamp__gt=since, timestamp__lte=until).order_by('timestamp'):
139 #ordered by ascending date; newer entries overwriter older ones
140 actions[a.podcast] = a
142 add = []
143 remove = []
145 for a in actions.values():
146 if a.action == SUBSCRIBE_ACTION:
147 add.append(a.podcast.url)
148 elif a.action == UNSUBSCRIBE_ACTION:
149 remove.append(a.podcast.url)
151 until_ = int(mktime(until.timetuple()))
152 return {'add': add, 'remove': remove, 'timestamp': until_}
155 @csrf_exempt
156 @require_valid_user
157 @check_username
158 def episodes(request, username, version=1):
160 version = int(version)
161 now = datetime.now()
162 now_ = int(mktime(now.timetuple()))
164 if request.method == 'POST':
165 try:
166 actions = json.loads(request.raw_post_data)
167 except KeyError, e:
168 log('could not parse episode update info for user %s: %s' % (username, e))
169 return HttpResponseBadRequest()
171 try:
172 update_urls = update_episodes(request.user, actions)
173 except Exception, e:
174 log('could not update episodes for user %s: %s' % (username, e))
175 return HttpResponseBadRequest(e)
177 return JsonResponse({'timestamp': now_, 'update_urls': update_urls})
179 elif request.method == 'GET':
180 podcast_url= request.GET.get('podcast', None)
181 device_uid = request.GET.get('device', None)
182 since_ = request.GET.get('since', None)
183 aggregated = parse_bool(request.GET.get('aggregated', False))
185 since = datetime.fromtimestamp(float(since_)) if since_ else None
187 try:
188 podcast = Podcast.objects.get(url=podcast_url) if podcast_url else None
189 device = Device.objects.get(user=request.user,uid=device_uid, deleted=False) if device_uid else None
190 except:
191 raise Http404
193 return JsonResponse(get_episode_changes(request.user, podcast, device, since, now, aggregated, version))
195 else:
196 return HttpResponseNotAllowed(['POST', 'GET'])
199 def get_episode_changes(user, podcast, device, since, until, aggregated, version):
200 if aggregated:
201 actions = {}
202 else:
203 actions = []
204 eactions = EpisodeAction.objects.filter(user=user, timestamp__lte=until)
206 if podcast:
207 eactions = eactions.filter(episode__podcast=podcast)
209 if device:
210 eactions = eactions.filter(device=device)
212 if since: # we can't use None with __gt
213 eactions = eactions.filter(timestamp__gt=since)
215 if aggregated:
216 eactions = eactions.order_by('timestamp')
218 for a in eactions:
219 action = {
220 'podcast': a.episode.podcast.url,
221 'episode': a.episode.url,
222 'action': a.action,
223 'timestamp': a.timestamp.strftime('%Y-%m-%dT%H:%M:%S') #2009-12-12T09:00:00
226 if a.action == 'play' and a.playmark:
227 if version == 1:
228 t = gmtime(a.playmark)
229 action['position'] = strftime('%H:%M:%S', t)
230 elif None in (a.playmark, a.started, a.total):
231 log('Ignoring broken episode action in DB: %r' % (a,))
232 continue
233 else:
234 action['position'] = int(a.playmark)
235 action['started'] = int(a.started)
236 action['total'] = int(a.total)
238 if aggregated:
239 actions[a.episode] = action
240 else:
241 actions.append(action)
243 until_ = int(mktime(until.timetuple()))
245 if aggregated:
246 actions = list(actions.itervalues())
248 return {'actions': actions, 'timestamp': until_}
251 def update_episodes(user, actions):
252 update_urls = []
254 for e in actions:
255 u = e['podcast']
256 us = sanitize_url(u)
257 if u != us: update_urls.append( (u, us) )
258 if us == '': continue
260 podcast, p_created = Podcast.objects.get_or_create(url=us)
262 eu = e['episode']
263 eus = sanitize_url(eu, podcast=False, episode=True)
264 if eu != eus: update_urls.append( (eu, eus) )
265 if eus == '': continue
267 episode, e_created = Episode.objects.get_or_create(podcast=podcast, url=eus)
268 action = e['action']
269 if not valid_episodeaction(action):
270 raise Exception('invalid action %s' % action)
272 if 'device' in e:
273 device, created = Device.objects.get_or_create(user=user, uid=e['device'], defaults={'name': 'Unknown', 'type': 'other'})
275 # undelete a previously deleted device
276 if device.deleted:
277 device.deleted = False
278 device.save()
280 else:
281 device, created = None, False
283 timestamp = dateutil.parser.parse(e['timestamp']) if 'timestamp' in e and e['timestamp'] else datetime.now()
285 # Time values for play actions and their keys in JSON
286 PLAY_ACTION_KEYS = ('position', 'started', 'total')
287 time_values = {}
289 for key in PLAY_ACTION_KEYS:
290 if key in e:
291 if action != 'play':
292 # Key found, but must not be supplied (no play action!)
293 return HttpResponseBadRequest('%s only allowed in play actions' % key)
295 try:
296 time_values[key] = parse_time(e[key])
297 except ValueError:
298 log('could not parse %s parameter (value: %s) for user %s' % (key, repr(e[key]), user))
299 return HttpResponseBadRequest('Wrong format for %s: %s' % (key, repr(e[key])))
300 else:
301 # Value not supplied by client
302 time_values[key] = None
304 # Sanity check: If started or total are given, require position
305 if (time_values['started'] is not None or \
306 time_values['total'] is not None) and \
307 time_values['position'] is None:
308 return HttpResponseBadRequest('started and total require position')
310 # Sanity check: total and position can only appear together
311 if (time_values['total'] or time_values['started']) and \
312 not (time_values['total'] and time_values['started']):
313 return HttpResponseBadRequest('total and started parameters can only appear together')
315 try:
316 EpisodeAction.objects.create(user=user, episode=episode, device=device, action=action, timestamp=timestamp,
317 playmark=time_values['position'], started=time_values['started'], total=time_values['total'])
318 except Exception, e:
319 log('error while adding episode action (user %s, episode %s, device %s, action %s, timestamp %s): %s' % (user, episode, device, action, timestamp, e))
321 return update_urls
324 @csrf_exempt
325 @require_valid_user
326 @check_username
327 def device(request, username, device_uid):
329 # Workaround for mygpoclient 1.0: It uses "PUT" requests
330 # instead of "POST" requests for uploading device settings
331 if request.method in ('POST', 'PUT'):
332 d, created = Device.objects.get_or_create(user=request.user, uid=device_uid)
334 #undelete a previously deleted device
335 if d.deleted:
336 d.deleted = False
337 d.save()
339 data = json.loads(request.raw_post_data)
341 if 'caption' in data:
342 d.name = data['caption']
344 if 'type' in data:
345 if not valid_devicetype(data['type']):
346 return HttpResponseBadRequest('invalid device type %s' % data['type'])
347 d.type = data['type']
349 d.save()
351 return HttpResponse()
353 else:
354 return HttpResponseNotAllowed(['POST'])
356 def valid_devicetype(type):
357 for t in DEVICE_TYPES:
358 if t[0] == type:
359 return True
360 return False
362 def valid_episodeaction(type):
363 for t in EPISODE_ACTION_TYPES:
364 if t[0] == type:
365 return True
366 return False
369 @csrf_exempt
370 @require_valid_user
371 @check_username
372 def devices(request, username):
374 if request.method == 'GET':
375 devices = []
376 for d in Device.objects.filter(user=request.user, deleted=False):
377 devices.append({
378 'id': d.uid,
379 'caption': d.name,
380 'type': d.type,
381 'subscriptions': Subscription.objects.filter(device=d).count()
384 return JsonResponse(devices)
386 else:
387 return HttpResponseNotAllowed(['GET'])
390 @csrf_exempt
391 @require_valid_user
392 @check_username
393 def updates(request, username, device_uid):
394 now = datetime.now()
395 now_ = int(mktime(now.timetuple()))
397 device = get_object_or_404(Device, user=request.user, uid=device_uid)
399 try:
400 since_ = request.GET['since']
401 except KeyError:
402 return HttpResponseBadRequest('parameter since missing')
404 since = datetime.fromtimestamp(float(since_))
406 ret = get_subscription_changes(request.user, device, since, now)
408 # replace added urls with details
409 podcast_details = []
410 for url in ret['add']:
411 podcast = Podcast.objects.get(url=url)
412 podcast_details.append(podcast_data(podcast))
414 ret['add'] = podcast_details
417 # add episode details
418 subscriptions = get_all_subscriptions(request.user)
419 episode_status = {}
420 for e in Episode.objects.filter(podcast__in=subscriptions, timestamp__gte=since).order_by('timestamp'):
421 episode_status[e] = 'new'
422 for a in EpisodeAction.objects.filter(user=request.user, episode__podcast__in=subscriptions, timestamp__gte=since).order_by('timestamp'):
423 episode_status[a.episode] = a.action
425 updates = []
426 for episode, status in episode_status.iteritems():
427 t = episode_data(episode)
428 t['released'] = e.timestamp.strftime('%Y-%m-%dT%H:%M:%S')
429 t['status'] = status
430 updates.append(t)
432 ret['updates'] = updates
434 return JsonResponse(ret)