示例#1
0
文件: views.py 项目: vinodyk/feedjack
def initview(request, response_cache=True):
    '''Retrieves the basic data needed by all feeds (host, feeds, etc)
        Returns a tuple of:
            1. A valid cached response or None
            2. The current site object
            3. The cache key
            4. The subscribers for the site (objects)
            5. The feeds for the site (ids)'''

    http_host, path_info = ( smart_unicode(part.strip('/')) for part in
        [ request.META['HTTP_HOST'],
            request.META.get('REQUEST_URI', request.META.get('PATH_INFO', '/')) ] )
    query_string = request.META['QUERY_STRING']

    url = '{0}/{1}'.format(http_host, path_info)
    cachekey = u'{0}?{1}'.format(*it.imap(smart_unicode, (path_info, query_string)))
    hostdict = fjcache.hostcache_get() or dict()

    site_id = hostdict.get(url, None)
    if site_id is not None:
        if response_cache:
            response = fjcache.cache_get(site_id, cachekey)
            if response:
                return response, None, cachekey
        site = models.Site.objects.get(pk=site_id)
    else: # match site from all of them
        sites = models.Site.objects.all()

        django_site = get_current_site(request)
        if sites.count() == 0: # no sites available, create a default one
            site = models.Site(django_site=django_site)
            site.save()
        else:
            site = get_object_or_404(models.Site, django_site=django_site)
            if urlparse(site.url).netloc != http_host: # redirect to proper site hostname
                response = HttpResponsePermanentRedirect(
                    'http://{0}/{1}{2}'.format( site_url.netloc, path_info,
                        '?{0}'.format(query_string) if query_string.strip() else '') )
                return (response, timezone.now()), None, cachekey

        hostdict[url] = site_id = site.id
        fjcache.hostcache_set(hostdict)

    if response_cache:
        response = fjcache.cache_get(site_id, cachekey)
        if response:
            return response, None, cachekey

    return None, site, cachekey
示例#2
0
def initview(request):
    """ Retrieves the basic data needed by all feeds (host, feeds, etc)

    Returns a tuple of:
    1. A valid cached response or None
    2. The current site object
    3. The cache key
    4. The subscribers for the site (objects)
    5. The feeds for the site (ids)
    """

    host = '/'
    if 'HTTP_HOST' in request.META:
        host = request.META['HTTP_HOST']

    site_id, cachekey = fjlib.getcurrentsite(host, \
      request.META.get('REQUEST_URI', request.META.get('PATH_INFO', '/')), \
      request.META['QUERY_STRING'])
    response = fjcache.cache_get(site_id, cachekey)
    if response:
        return response, None, cachekey, [], []

    site = models.Site.objects.get(pk=site_id)
    sfeeds_obj = fjlib.sitefeeds(site)
    sfeeds_ids = [subscriber.feed.id for subscriber in sfeeds_obj]

    return None, site, cachekey, sfeeds_obj, sfeeds_ids
示例#3
0
def getcloud(site, feed_id=None):
	""" Returns the tag cloud for a site or a site's subscriber.
	"""

	cloudict = fjcache.cache_get(site.id, 'tagclouds')
	if not cloudict:
		cloudict = cloudata(site)
		fjcache.cache_set(site, 'tagclouds', cloudict)

	# A subscriber's tag cloud has been requested.
	if feed_id:
		feed_id = int(feed_id)
		if feed_id in cloudict:
			return cloudict[feed_id]
		return []
	# The site tagcloud has been requested.
	return cloudict[0]
示例#4
0
def initview(request):
    """ Retrieves the basic data needed by all feeds (host, feeds, etc)

    Returns a tuple of:
    1. A valid cached response or None
    2. The current site object
    3. The cache key
    4. The subscribers for the site (objects)
    5. The feeds for the site (ids)
    """

    site_id, cachekey = fjlib.getcurrentsite(request.META['HTTP_HOST'], \
      request.META.get('REQUEST_URI', request.META.get('PATH_INFO', '/')), \
      request.META['QUERY_STRING'])
    response = fjcache.cache_get(site_id, cachekey)
    if response:
        return response, None, cachekey, [], []

    site = models.Site.objects.get(pk=site_id)
    sfeeds_obj = fjlib.sitefeeds(site)
    sfeeds_ids = [subscriber.feed.id for subscriber in sfeeds_obj]

    return None, site, cachekey, sfeeds_obj, sfeeds_ids
示例#5
0
def initview(request, response_cache=True):
    '''Retrieves the basic data needed by all feeds (host, feeds, etc)
		Returns a tuple of:
			1. A valid cached response or None
			2. The current site object
			3. The cache key
			4. The subscribers for the site (objects)
			5. The feeds for the site (ids)'''

    http_host, path_info = (smart_unicode(part.strip('/')) for part in [
        request.META['HTTP_HOST'],
        request.META.get('REQUEST_URI', request.META.get('PATH_INFO', '/'))
    ])
    query_string = request.META['QUERY_STRING']

    url = '{0}/{1}'.format(http_host, path_info)
    cachekey = u'{0}?{1}'.format(
        *it.imap(smart_unicode, (path_info, query_string)))
    hostdict = fjcache.hostcache_get() or dict()

    site_id = hostdict[url] if url in hostdict else None
    if site_id and response_cache:
        response = fjcache.cache_get(site_id, cachekey)
        if response: return response, None, cachekey

    if site_id: site = models.Site.objects.get(pk=site_id)
    else:  # match site from all of them
        sites = list(models.Site.objects.all())

        if not sites:
            # Somebody is requesting something, but the user
            #  didn't create a site yet. Creating a default one...
            site = models.Site(name='Default Feedjack Site/Planet',
                               url=request.build_absolute_uri(request.path),
                               title='Feedjack Site Title',
                               description='Feedjack Site Description.'
                               ' Please change this in the admin interface.',
                               template='bootstrap')
            site.save()

        else:
            # Select the most matching site possible,
            #  preferring "default" when everything else is equal
            results = defaultdict(list)
            for site in sites:
                relevance, site_url = 0, urlparse(site.url)
                if site_url.netloc == http_host:
                    relevance += 10  # host matches
                if path_info.startswith(site_url.path.strip('/')):
                    relevance += 10  # path matches
                if site.default_site: relevance += 5  # marked as "default"
                results[relevance].append((site_url, site))
            for relevance in sorted(results, reverse=True):
                try:
                    site_url, site = results[relevance][0]
                except IndexError:
                    pass
                else:
                    break
            if site_url.netloc != http_host:  # redirect to proper site hostname
                response = HttpResponsePermanentRedirect(
                    'http://{0}/{1}{2}'.format(
                        site_url.netloc, path_info, '?{0}'.format(query_string)
                        if query_string.strip() else ''))
                return (response, timezone.now()), None, cachekey

        hostdict[url] = site_id = site.id
        fjcache.hostcache_set(hostdict)

    if response_cache:
        response = fjcache.cache_get(site_id, cachekey)
        if response: return response, None, cachekey

    return None, site, cachekey
示例#6
0
文件: views.py 项目: chrisv2/feedjack
def initview(request, response_cache=True):
	'''Retrieves the basic data needed by all feeds (host, feeds, etc)
		Returns a tuple of:
			1. A valid cached response or None
			2. The current site object
			3. The cache key
			4. The subscribers for the site (objects)
			5. The feeds for the site (ids)'''

	http_host, path_info = ( smart_unicode(part.strip('/')) for part in
		[ request.META['HTTP_HOST'],
			request.META.get('REQUEST_URI', request.META.get('PATH_INFO', '/')) ] )
	query_string = request.META['QUERY_STRING']

	url = '{}/{}'.format(http_host, path_info)
	cachekey = u'{}?{}'.format(*it.imap(smart_unicode, (path_info, query_string)))
	hostdict = fjcache.hostcache_get() or dict()

	if url in hostdict:
		site = models.Site.objects.get(pk=hostdict[url])

	else:
		sites = list(models.Site.objects.all())

		if not sites:
			# Somebody is requesting something, but the user
			#  didn't create a site yet. Creating a default one...
			site = models.Site(
				name='Default Feedjack Site/Planet',
				url='www.feedjack.org',
				title='Feedjack Site Title',
				description='Feedjack Site Description.'
					' Please change this in the admin interface.' )
			site.save()

		else:
			# Select the most matching site possible,
			#  preferring "default" when everything else is equal
			results = defaultdict(list)
			for site in sites:
				relevance, site_url = 0, urlparse(site.url)
				if site_url.netloc == http_host: relevance += 10 # host matches
				if path_info.startswith(site_url.path.strip('/')): relevance += 10 # path matches
				if site.default_site: relevance += 5 # marked as "default"
				results[relevance].append((site_url, site))
			for relevance in sorted(results, reverse=True):
				try: site_url, site = results[relevance][0]
				except IndexError: pass
				else: break
			if site_url.netloc != http_host: # redirect to proper site hostname
				# TODO: SERVER_PORT doesn't seem very useful here, but just "http://{}/" is just wrong
				#  ...in a way that it doesn't respect port and protocol
				response = HttpResponsePermanentRedirect(
					'http://{}/{}{}'.format( site_url.netloc, path_info,
						'?{}'.format(query_string) if query_string.strip() else '') )
				return (response, datetime.now()), None, cachekey

		hostdict[url] = site.id
		fjcache.hostcache_set(hostdict)

	if response_cache:
		response = fjcache.cache_get(site.id, cachekey)
		if response: return response, None, cachekey

	return None, site, cachekey