def sidebar_broadcast(user): """Displays a broadcast to a given usertype""" def load_broadcast(user_class): """Return a function to load the correct broadcast""" def inner(): """Argument-less function to load correct broadcast""" try: # exclude stale broadcasts from displaying last_week = datetime.now() - timedelta(7) broadcast = Broadcast.objects.get(updated__gte=last_week, context=user_class).text except Broadcast.DoesNotExist: broadcast = '' return broadcast return inner try: user_class = user.profile.acct_type if user.is_authenticated( ) else 'anonymous' except Profile.DoesNotExist: user_class = 'anonymous' return cache_get_or_set('sb:%s:broadcast' % user_class, load_broadcast(user_class), settings.DEFAULT_CACHE_TIMEOUT)
def generate_crowdfund_context(the_crowdfund, the_url_name, the_form, the_context): """Generates context in a way that's agnostic towards the object being crowdfunded.""" endpoint = reverse(the_url_name, kwargs={'pk': the_crowdfund.pk}) payment_form = crowdfund_form(the_crowdfund, the_form) logged_in, user_email = crowdfund_user(the_context) the_request = the_context.request named, contrib_count, anon_count = (cache_get_or_set( 'cf:%s:crowdfund_widget_data' % the_crowdfund.pk, lambda: ( list(the_crowdfund.named_contributors()), the_crowdfund.contributors_count(), the_crowdfund.anonymous_contributors_count(), ), settings.DEFAULT_CACHE_TIMEOUT)) contrib_sum = contributor_summary(named, contrib_count, anon_count) obj_url = the_crowdfund.get_crowdfund_object().get_absolute_url() return { 'crowdfund': the_crowdfund, 'named_contributors': named, 'contributors_count': contrib_count, 'anon_contributors_count': anon_count, 'contributor_summary': contrib_sum, 'endpoint': endpoint, 'login_form': AuthenticationForm(), 'logged_in': logged_in, 'user_email': user_email, 'payment_form': payment_form, 'request': the_request, 'stripe_pk': settings.STRIPE_PUB_KEY, 'obj_url': obj_url, 'domain': the_context['domain'], }
def get_context_data(self, **kwargs): """Adds interesting articles to the explore page.""" context = super(NewsExploreView, self).get_context_data(**kwargs) recent_articles = cache_get_or_set( 'hp:articles', lambda: (Article.objects.get_published().prefetch_related( 'authors', 'authors__profile', 'projects', )[:5]), 600) context['featured_projects'] = (Project.objects.get_visible( self.request.user).filter(featured=True).prefetch_related( Prefetch('articles__authors', queryset=User.objects.select_related( 'profile'))).optimize()) context['recent_articles'] = recent_articles context['top_tags'] = Article.tags.most_common()[:15] return context
def get_organization(user): """Gets organization, if it exists""" def load_organization(user): """Return a function to load the user's organization""" def inner(): """Argument-less function to load user's organization""" org = None if user.profile.organization: org = user.profile.organization owned_org = Organization.objects.filter(owner=user) if owned_org.exists(): # there should only ever be one. if there is more than one, just get the first. org = owned_org.first() return org return inner return cache_get_or_set('sb:%s:user_org' % user.username, load_organization(user), settings.DEFAULT_CACHE_TIMEOUT)