def handle(self, *args, **options): docs = set() progress(0, len(docs), '', stream=sys.stderr) for username in options.get('users', []): user = User.get_user(username) self.add_user_recursive(user, docs) if options.get('toplist', False): toplist = PodcastToplist() for n, podcast in toplist[:25]: self.add_podcast_recursive(podcast, docs) for podcast_url in options.get('podcasts'): podcast = podcast_for_url(podcast_url, docs) if not podcast: logger.warn('podcast not found for URL "%s"', podcast_url) else: self.add_podcast_recursive(podcast, docs) db = get_main_database() docs = sorted(docs) self.dump(docs, db)
def get(self, request, username): user = User.get_user(username) month_ago = datetime.today() - timedelta(days=31) site = RequestSite(request) context_funs = { 'lists': partial(self.get_podcast_lists, user), 'subscriptions': partial(self.get_subscriptions, user), 'recent_episodes': partial(self.get_recent_episodes, user), 'seconds_played_total': partial(self.get_seconds_played_total, user), 'seconds_played_month': partial(self.get_seconds_played_since, user, month_ago), 'favorite_episodes': partial(self.get_favorite_episodes, user), 'num_played_episodes_total': partial(self.get_played_episodes_total, user), 'num_played_episodes_month': partial(self.get_played_episodes_since, user, month_ago), } context = { 'page_user': user, 'flattr_username': user.get_wksetting(FLATTR_USERNAME), 'site': site.domain, 'subscriptions_token': user.get_token('subscriptions_token'), 'favorite_feeds_token': user.get_token('favorite_feeds_token'), } context.update(self.get_context(context_funs)) return render(request, 'userpage.html', context)
def post(self, request): username = request.POST.get('username') email = request.POST.get('email') if not (username or email): messages.error(request, _('Provide either username or email address')) return HttpResponseRedirect(reverse('admin-activate-user')) user = None if username: user = User.get_user(username, is_active=None) if email and not user: user = User.get_user_by_email(email, is_active=None) if not user: messages.error(request, _('No user found')) return HttpResponseRedirect(reverse('admin-activate-user')) activate_user(user) messages.success(request, _('User {username} ({email}) activated'.format( username=user.username, email=user.email))) return HttpResponseRedirect(reverse('admin-activate-user'))
def update_published_podcasts(request, username): user = User.get_user(username) if not user: raise Http404 published_podcasts = podcasts_by_id(user.published_objects) update_podcasts.delay([podcast.url for podcast in published_podcasts]) return HttpResponse('Updated:\n' + '\n'.join([p.url for p in published_podcasts]), mimetype='text/plain')
def get_user(username, email, is_active=None): if username: return User.get_user(username, is_active=None) elif email: return User.get_user_by_email(email, is_active=None) return None
def lists_user(request, username): user = User.get_user(username) if not user: raise Http404 lists = PodcastList.for_user(user._id) return render(request, "lists_user.html", {"lists": lists, "user": user})
def lists_user(request, username): user = User.get_user(username) if not user: raise Http404 lists = podcastlists_for_user(user._id) return render(request, 'lists_user.html', { 'lists': lists, 'user': user, })
def for_user(request, username): user = User.get_user(username) if not user: raise Http404 subscriptions = user.get_subscribed_podcasts(public=True) token = user.get_token('subscriptions_token') return render(request, 'user_subscriptions.html', { 'subscriptions': subscriptions, 'other_user': user, 'token': token, })
def _decorator(request, username, listname, *args, **kwargs): user = User.get_user(username) if not user: raise Http404 if must_own and request.user != user: return HttpResponseForbidden() plist = PodcastList.for_user_slug(user._id, listname) if plist is None: raise Http404 return f(request, plist, user, *args, **kwargs)
def favorite_feed(request, username): site = RequestSite(request) user = User.get_user(username) if not user: raise Http404 feed = FavoriteFeed(user) return render(request, 'userfeed.xml', { 'site': site, 'feed_user': user, 'feed': feed, }, content_type='text/xml')
def get_lists(request, username): """ Returns a list of all podcast lists by the given user """ user = User.get_user(username) if not user: raise Http404 lists = PodcastList.for_user(user._id) site = RequestSite(request) get_data = partial(_get_list_data, username=user.username, domain=site.domain) lists_data = map(get_data, lists) return JsonResponse(lists_data)
def for_user_opml(request, username): user = User.get_user(username) if not user: raise Http404 subscriptions = user.get_subscribed_podcasts(public=True) if parse_bool(request.GET.get('symbian', False)): subscriptions = map(symbian_opml_changes, subscriptions) response = render(request, 'user_subscriptions.opml', { 'subscriptions': subscriptions, 'other_user': user }) response['Content-Disposition'] = 'attachment; filename=%s-subscriptions.opml' % username return response
def handle(self, *args, **options): if len(args) < 2: print >>sys.stderr, "Usage: ./manage.py make-publisher <username> <podcast-url-1> [<podcast-url-2> ...]" return username = args[0] user = User.get_user(username) if not user: print >>sys.stderr, "User %s does not exist" % username return urls = args[1:] podcasts = map(podcast_for_url, urls) ids = map(Podcast.get_id, podcasts) add_published_objs(user, ids)
def handle(self, *args, **options): docs = set() for username in options.get('users', []): user = User.get_user(username) # User docs.add(user._id) # Suggestions suggestions = Suggestions.for_user(user) docs.add(suggestions._id) # Podcast States for p_state in PodcastUserState.for_user(user): docs.add(p_state._id) # Categories for tag in p_state.tags: c = Category.for_tag(tag) if c: docs.add(c._id) # Podcast podcast = Podcast.get(p_state.podcast) docs.add(podcast._id) # Categories for s in podcast.tags: for tag in podcast.tags[s]: c = Category.for_tag(tag) if c: docs.add(c._id) # Episodes for episode in podcast.get_episodes(): docs.add(episode._id) # Episode States e_state = episode.get_user_state(user) if e_state._id: docs.add(e_state._id) db = get_main_database() docs = sorted(docs) self.dump(docs, db)
def view_or_basicauth(view, request, username, token_name, realm = "", *args, **kwargs): user = User.get_user(username) if not user: raise Http404 token = getattr(user, token_name, '') # check if a token is required at all if token == '': return view(request, username, *args, **kwargs) # this header format is used when passing auth-headers # from Aapache to fcgi if 'AUTHORIZATION' in request.META: auth = request.META['AUTHORIZATION'] elif 'HTTP_AUTHORIZATION' in request.META: auth = request.META['HTTP_AUTHORIZATION'] else: return auth_request() auth = auth.split(None, 1) if len(auth) == 2: auth_type, credentials = auth # NOTE: We are only support basic authentication for now. if auth_type.lower() == 'basic': credentials = credentials.decode('base64').split(':', 1) if len(credentials) == 2: uname, passwd = credentials if uname != username: return auth_request() if token == passwd: try: return view(request, uname, *args, **kwargs) except Exception, e: log(repr(e)) return HttpResponseBadRequest(e)
def tmp(request, username, *args, **kwargs): from mygpo.users.models import User user = User.get_user(username) if not user: raise Http404 token = user.get_token(token_name) u_token = request.GET.get('token', '') if token == '' or token == u_token: return fn(request, username, *args, **kwargs) else: if denied_template: return render(request, denied_template, { 'other_user': user }) else: return HttpResponseForbidden()
def handle(self, *args, **options): max_suggestions = options.get('max') if options.get('username'): users = [User.get_user(options.get('username'))] else: users = User.all_users() users = filter(lambda u: u.is_active, users) if options.get('outdated'): users = filter(lambda u: not u.suggestions_up_to_date, users) if options.get('max_users'): users = users[:int(options.get('max_users'))] total = len(users) for n, user in enumerate(users): suggestion = Suggestions.for_user(user) subscribed_podcasts = list(set(user.get_subscribed_podcasts())) subscribed_podcasts = filter(None, subscribed_podcasts) subscribed_podcasts = filter(None, subscribed_podcasts) related = chain.from_iterable([p.related_podcasts for p in subscribed_podcasts]) related = filter(lambda pid: not pid in suggestion.blacklist, related) counter = Counter(related) get_podcast_id = itemgetter(0) suggested = map(get_podcast_id, counter.most_common(max_suggestions)) suggestion.podcasts = suggested suggestion.save() _update_user(user=user) progress(n+1, total)
def post(self, request): username = request.POST.get('username') user = User.get_user(username) if user is None: messages.error(request, 'User "{username}" not found'.format(username=username)) return HttpResponseRedirect(reverse('admin-make-publisher-input')) feeds = request.POST.get('feeds') feeds = feeds.split() podcasts = set() for feed in feeds: podcast = podcast_for_url(feed) if podcast is None: messages.warning(request, 'Podcast with URL {feed} not found'.format(feed=feed)) continue podcasts.add(podcast) self.set_publisher(request, user, podcasts) self.send_mail(request, user, podcasts) return HttpResponseRedirect(reverse('admin-make-publisher-result'))
def get_user(self, username): try: return User.get_user(username) except: #TODO: which exception return None
def get_user(self, username): return User.get_user(username)
def get_object(self, request, username): self.site = RequestSite(request) return User.get_user(username)