def update_subscriptions(self, user, device, add, remove): conflicts = intersect(add, remove) if conflicts: msg = "can not add and remove '{}' at the same time".format(str(conflicts)) logger.warning(msg) raise RequestException(msg) add_s = list(map(normalize_feed_url, add)) rem_s = list(map(normalize_feed_url, remove)) assert len(add) == len(add_s) and len(remove) == len(rem_s) pairs = zip(add + remove, add_s + rem_s) updated_urls = list(filter(lambda pair: pair[0] != pair[1], pairs)) add_s = filter(None, add_s) rem_s = filter(None, rem_s) # If two different URLs (in add and remove) have # been sanitized to the same, we ignore the removal rem_s = filter(lambda x: x not in add_s, rem_s) for add_url in add_s: podcast = Podcast.objects.get_or_create_for_url(add_url).object subscribe(podcast.pk, user.pk, device.uid, add_url) remove_podcasts = Podcast.objects.filter(urls__url__in=rem_s) for podcast in remove_podcasts: unsubscribe(podcast.pk, user.pk, device.uid) return updated_urls
def get_urls(self, body): """ Parse and normalize the URLs from the request """ podcast_url = body.get('podcast', '') episode_url = body.get('episode', '') if not podcast_url: raise RequestException('Podcast URL missing') if not episode_url: raise RequestException('Episode URL missing') update_urls = [] # podcast sanitizing s_podcast_url = normalize_feed_url(podcast_url) if s_podcast_url != podcast_url: update_urls.append((podcast_url, s_podcast_url or '')) # episode sanitizing s_episode_url = normalize_feed_url(episode_url, 'episode') if s_episode_url != episode_url: update_urls.append((episode_url, s_episode_url or '')) return s_podcast_url, s_episode_url, update_urls
def get_urls(self, body): """ Parse and normalize the URLs from the request """ podcast_url = body.get("podcast", "") episode_url = body.get("episode", "") if not podcast_url: raise RequestException("Podcast URL missing") if not episode_url: raise RequestException("Episode URL missing") update_urls = [] # podcast sanitizing s_podcast_url = normalize_feed_url(podcast_url) if s_podcast_url != podcast_url: update_urls.append((podcast_url, s_podcast_url or "")) # episode sanitizing s_episode_url = normalize_feed_url(episode_url, "episode") if s_episode_url != episode_url: update_urls.append((episode_url, s_episode_url or "")) return s_podcast_url, s_episode_url, update_urls
def post(self, request, username): """ Add / remove Chapters to/from an episode """ user = request.user now_ = get_timestamp(datetime.utcnow()) body = self.parsed_body(request) podcast_url, episode_url, update_urls = self.get_urls(body) body['podcast'] = podcast_url body['episode'] = episode_url if not podcast_url or not episode_url: raise RequestException('Invalid Podcast or Episode URL') self.update_chapters(body, user) return JsonResponse({'update_url': update_urls, 'timestamp': now_})
def get_scope(self, request, scope): """ Get the scope object """ if scope == 'account': return None if scope == 'device': uid = request.GET.get('device', '') return request.user.client_set.get(uid=uid) episode_url = request.GET.get('episode', '') podcast_url = request.GET.get('podcast', '') if scope == 'podcast': return get_object_or_404(Podcast, urls__url=podcast_url) if scope == 'episode': podcast = get_object_or_404(Podcast, urls__url=podcast_url) return get_object_or_404(Episode, podcast=podcast, urls__url=episode_url) raise RequestException('undefined scope %s' % scope)
def get_scope(self, request, scope): """Get the scope object""" if scope == "account": return None if scope == "device": uid = request.GET.get("device", "") return request.user.client_set.get(uid=uid) episode_url = request.GET.get("episode", "") podcast_url = request.GET.get("podcast", "") if scope == "podcast": return get_object_or_404(Podcast, urls__url=podcast_url) if scope == "episode": podcast = get_object_or_404(Podcast, urls__url=podcast_url) return get_object_or_404(Episode, podcast=podcast, urls__url=episode_url) raise RequestException("undefined scope %s" % scope)