def newsitem_detail(request, schema_slug, newsitem_id): ni = get_object_or_404(NewsItem.objects.select_related(), id=newsitem_id, schema__slug=schema_slug) if not ni.schema.is_public and not has_staff_cookie(request): raise Http404('Not public') if not ni.schema.has_newsitem_detail: # Don't show detail pages. if ni.url: return HttpResponsePermanentRedirect(ni.url) else: # We have nothing to show the user; ticket #110. raise Http404("This news item needs an external URL and doesn't have one") atts = ni.attributes_for_template() has_location = ni.location is not None if has_location: locations_within = Location.objects.select_related().filter( newsitemlocation__news_item__id=ni.id) center_x = ni.location.centroid.x center_y = ni.location.centroid.y else: locations_within = () center_x = settings.DEFAULT_MAP_CENTER_LON center_y = settings.DEFAULT_MAP_CENTER_LAT hide_ads = (request.COOKIES.get(HIDE_ADS_COOKIE_NAME) == 't') templates_to_try = ('db/newsitem_detail/%s.html' % ni.schema.slug, 'db/newsitem_detail.html') # Try to find a usable URL to link to from the location name. # TODO: move this logic to NewsItem.location_url() location_url = ni.location_url() if not location_url: # There might be any number of intersecting locations_within, # and we don't have any criteria for deciding which if any # best corresponds to ni.location_name; but we can try # a few other fallbacks. if ni.block: location_url = ni.block.url() elif ni.location: # Try reverse-geocoding and see if we get a block. try: block, distance = geocoder.reverse.reverse_geocode(ni.location) # TODO: if this happens, we should really update # ni.block, but this seems like a terrible place to do # that. logger.warn( "%r (id %d) can be reverse-geocoded to %r (id %d) but" " self.block isn't set" % (ni, ni.id, block, block.id)) location_url = block.url() except geocoder.reverse.ReverseGeocodeError: logger.error( "%r (id %d) has neither a location_url, nor a block," " nor a reverse-geocodable location" % (ni, ni.id)) pass context = { 'newsitem': ni, 'attribute_list': [att for att in atts if att.sf.display], 'attribute_dict': dict((att.sf.name, att) for att in atts), 'has_location': has_location, 'locations_within': locations_within, 'location_url': location_url, 'hide_ads': hide_ads, 'map_center_x': center_x, 'map_center_y': center_y, 'bodyclass': 'newsitem-detail', 'bodyid': schema_slug, } context['breadcrumbs'] = breadcrumbs.newsitem_detail(context) context['map_configuration'] = _preconfigured_map(context) return eb_render(request, templates_to_try, context)
def newsitem_detail(request, schema_slug, newsitem_id): ni = get_object_or_404(NewsItem.objects.by_request(request).select_related(), id=newsitem_id, schema__slug=schema_slug) if not ni.schema.has_newsitem_detail: # Don't show detail pages. if ni.url: return HttpResponsePermanentRedirect(ni.url) else: # We have nothing to show the user; ticket #110. raise Http404("This news item needs an external URL and doesn't have one") atts = ni.attributes_for_template() has_location = ni.location is not None if has_location: locations_within = Location.objects.select_related().filter( newsitemlocation__news_item__id=ni.id) center_x = ni.location.centroid.x center_y = ni.location.centroid.y else: locations_within = () center_x = settings.DEFAULT_MAP_CENTER_LON center_y = settings.DEFAULT_MAP_CENTER_LAT hide_ads = (request.COOKIES.get(HIDE_ADS_COOKIE_NAME) == 't') templates_to_try = ('db/newsitem_detail/%s.html' % ni.schema.slug, 'db/newsitem_detail.html') # Try to find a usable URL to link to from the location name. # TODO: move this logic to NewsItem.location_url() location_url = ni.location_url() if not location_url: # There might be any number of intersecting locations_within, # and we don't have any criteria for deciding which if any # best corresponds to ni.location_name; but we can try # a few other fallbacks. if ni.location: # Try reverse-geocoding and see if we get a block. try: block, distance = geocoder.reverse.reverse_geocode(ni.location) location_url = block.url() except geocoder.reverse.ReverseGeocodeError: logger.error( "%r (id %d) has neither a location_url, nor a block," " nor a reverse-geocodable location" % (ni, ni.id)) pass from ebpub.neighbornews.utils import user_can_edit from easy_thumbnails.files import get_thumbnailer size = getattr(settings, 'UPLOADED_IMAGE_DIMENSIONS', (640, 480)) images = [get_thumbnailer(i.image).get_thumbnail({'size': size}) for i in ni.newsitemimage_set.all()] if 'ebpub.moderation' in settings.INSTALLED_APPS: allow_flagging = ni.schema.allow_flagging else: allow_flagging = False context = { 'newsitem': ni, 'attribute_list': [att for att in atts if att.sf.display], 'attribute_dict': dict((att.sf.name, att) for att in atts), 'has_location': has_location, 'locations_within': locations_within, 'location_url': location_url, 'hide_ads': hide_ads, 'map_center_x': center_x, 'map_center_y': center_y, 'bodyclass': 'newsitem-detail', 'bodyid': schema_slug, 'can_edit': user_can_edit(request, ni), 'allow_flagging': allow_flagging, 'images': images, } context['breadcrumbs'] = breadcrumbs.newsitem_detail(context) context['map_configuration'] = _preconfigured_map(context) return eb_render(request, templates_to_try, context)