views.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # GNU MediaGoblin -- federated, autonomous media hosting
  2. # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. from mediagoblin import mg_globals
  17. from mediagoblin.db.models import MediaEntry
  18. from mediagoblin.db.util import media_entries_for_tag_slug
  19. from mediagoblin.tools.pagination import Pagination
  20. from mediagoblin.tools.response import render_to_response
  21. from mediagoblin.decorators import uses_pagination
  22. from werkzeug.contrib.atom import AtomFeed
  23. def _get_tag_name_from_entries(media_entries, tag_slug):
  24. """
  25. Get a tag name from the first entry by looking it up via its slug.
  26. """
  27. # ... this is slightly hacky looking :\
  28. tag_name = tag_slug
  29. for entry in media_entries:
  30. for tag in entry.tags:
  31. if tag['slug'] == tag_slug:
  32. tag_name = tag['name']
  33. break
  34. break
  35. return tag_name
  36. @uses_pagination
  37. def tag_listing(request, page):
  38. """'Gallery'/listing for this tag slug"""
  39. tag_slug = request.matchdict[u'tag']
  40. cursor = media_entries_for_tag_slug(request.db, tag_slug)
  41. cursor = cursor.order_by(MediaEntry.created.desc())
  42. pagination = Pagination(page, cursor)
  43. media_entries = pagination()
  44. tag_name = _get_tag_name_from_entries(media_entries, tag_slug)
  45. return render_to_response(
  46. request,
  47. 'mediagoblin/listings/tag.html',
  48. {'tag_slug': tag_slug,
  49. 'tag_name': tag_name,
  50. 'media_entries': media_entries,
  51. 'pagination': pagination})
  52. ATOM_DEFAULT_NR_OF_UPDATED_ITEMS = 15
  53. def atom_feed(request):
  54. """
  55. generates the atom feed with the tag images
  56. """
  57. tag_slug = request.matchdict.get(u'tag')
  58. feed_title = "MediaGoblin Feed"
  59. if tag_slug:
  60. cursor = media_entries_for_tag_slug(request.db, tag_slug)
  61. link = request.urlgen('mediagoblin.listings.tags_listing',
  62. qualified=True, tag=tag_slug )
  63. feed_title += "for tag '%s'" % tag_slug
  64. else: # all recent item feed
  65. cursor = MediaEntry.query.filter_by(state=u'processed')
  66. link = request.urlgen('index', qualified=True)
  67. feed_title += "for all recent items"
  68. atomlinks = [
  69. {'href': link,
  70. 'rel': 'alternate',
  71. 'type': 'text/html'}]
  72. if mg_globals.app_config["push_urls"]:
  73. for push_url in mg_globals.app_config["push_urls"]:
  74. atomlinks.append({
  75. 'rel': 'hub',
  76. 'href': push_url})
  77. cursor = cursor.order_by(MediaEntry.created.desc())
  78. cursor = cursor.limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS)
  79. feed = AtomFeed(
  80. feed_title,
  81. feed_url=request.url,
  82. id=link,
  83. links=atomlinks)
  84. for entry in cursor:
  85. feed.add(entry.get('title'),
  86. entry.description_html,
  87. id=entry.url_for_self(request.urlgen,qualified=True),
  88. content_type='html',
  89. author={'name': entry.get_actor.username,
  90. 'uri': request.urlgen(
  91. 'mediagoblin.user_pages.user_home',
  92. qualified=True, user=entry.get_actor.username)},
  93. updated=entry.get('created'),
  94. links=[{
  95. 'href':entry.url_for_self(
  96. request.urlgen,
  97. qualified=True),
  98. 'rel': 'alternate',
  99. 'type': 'text/html'}])
  100. return feed.get_response()