views.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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.base import Session
  18. from mediagoblin.db.models import MediaEntry
  19. from mediagoblin.decorators import uses_pagination, user_not_banned,\
  20. user_has_privilege, get_user_media_entry
  21. from mediagoblin.tools.response import render_to_response, redirect
  22. from mediagoblin.tools.pagination import Pagination
  23. from mediagoblin.plugins.archivalook.tools import (
  24. split_featured_media_list,
  25. create_featured_media_textbox,
  26. automatically_add_new_feature,
  27. automatically_remove_feature)
  28. from mediagoblin.plugins.archivalook import forms as archivalook_forms
  29. from mediagoblin.plugins.archivalook.models import FeaturedMedia
  30. from mediagoblin.plugins.archivalook.utils import feature_template
  31. from mediagoblin.plugins.archivalook.tools import (promote_feature,
  32. demote_feature)
  33. from mediagoblin.tools.translate import lazy_pass_to_ugettext as _
  34. @user_not_banned
  35. def root_view(request):
  36. """
  37. This is an alternative to the typical root view. This display centers around
  38. displaying featured media.
  39. """
  40. featured_media = {
  41. u'primary':FeaturedMedia.query.order_by(
  42. FeaturedMedia.order.asc()).filter(
  43. FeaturedMedia.display_type==u'primary').all(),
  44. u'secondary':FeaturedMedia.query.order_by(
  45. FeaturedMedia.order.asc()).filter(
  46. FeaturedMedia.display_type==u'secondary').all(),
  47. u'tertiary':FeaturedMedia.query.order_by(
  48. FeaturedMedia.order.asc()).filter(
  49. FeaturedMedia.display_type==u'tertiary').all()}
  50. return render_to_response(
  51. request, 'archivalook/root.html',
  52. {'featured_media': featured_media,
  53. 'allow_registration': mg_globals.app_config["allow_registration"],
  54. 'feature_template': feature_template})
  55. @user_has_privilege(u'featurer')
  56. def featured_media_panel(request):
  57. """
  58. This is a new administrator panel to manage featured media. This is an
  59. entirely optional panel, as there are other ways to manage it, but this way
  60. gives the admin more control.
  61. """
  62. form = archivalook_forms.FeaturedMediaList(request.form)
  63. if request.method == 'POST' and form.validate():
  64. featured_media = split_featured_media_list(form.box_content.data)
  65. previous_features = FeaturedMedia.query.all()
  66. for index, (media_entry, display_type) in enumerate(featured_media):
  67. target = FeaturedMedia.query.filter(
  68. FeaturedMedia.media_entry == media_entry).first()
  69. # If this media was already featured, we don't have to create a new
  70. # feature, we just have to edit the old one's values
  71. if target is not None:
  72. target.order = index
  73. target.display_type = display_type
  74. previous_features.remove(target)
  75. Session.add(target)
  76. else:
  77. new_feature = FeaturedMedia(
  78. media_entry=media_entry,
  79. display_type=display_type,
  80. order=index)
  81. Session.add(new_feature)
  82. [Session.delete(feature) for feature in previous_features]
  83. Session.commit()
  84. form.box_content.data = create_featured_media_textbox()
  85. return render_to_response(
  86. request, 'archivalook/feature.html',
  87. {'form' : form})
  88. @uses_pagination
  89. @user_not_banned
  90. def recent_media_gallery_view(request, page):
  91. """
  92. The replaced homepage is available through this view.
  93. """
  94. cursor = MediaEntry.query.filter_by(state=u'processed').\
  95. order_by(MediaEntry.created.desc())
  96. pagination = Pagination(page, cursor)
  97. media_entries = pagination()
  98. return render_to_response(
  99. request, 'archivalook/recent_media.html',
  100. {'media_entries': media_entries,
  101. 'pagination': pagination})
  102. def add_featured_media_to_media_home(context):
  103. """
  104. A context hook which allows the media home page to know whether the media
  105. has been featured or not.
  106. """
  107. context['featured_media'] = FeaturedMedia.query
  108. return context
  109. @user_has_privilege(u'featurer')
  110. @get_user_media_entry
  111. def feature_media(request, media, **kwargs):
  112. """
  113. A view to feature a new piece of media
  114. """
  115. already_featured_media_ids = [f.media_entry.id
  116. for f in FeaturedMedia.query.all()]
  117. if not media.id in already_featured_media_ids:
  118. new_feature = automatically_add_new_feature(media)
  119. return redirect(
  120. request, 'index')
  121. @user_has_privilege(u'featurer')
  122. @get_user_media_entry
  123. def unfeature_media(request, media, **kwargs):
  124. """
  125. A view to unfeature a piece of media which has previously been featured.
  126. """
  127. already_featured_media_ids = [f.media_entry.id
  128. for f in FeaturedMedia.query.all()]
  129. if media.id in already_featured_media_ids:
  130. automatically_remove_feature(media)
  131. return redirect(
  132. request, 'index')
  133. @user_has_privilege(u'featurer')
  134. @get_user_media_entry
  135. def promote_featured_media(request, media, **kwargs):
  136. """
  137. A view to move a piece of media up the featured stack
  138. """
  139. featured_media = FeaturedMedia.query.filter(
  140. FeaturedMedia.media_entry_id == media.id).first()
  141. if featured_media is not None:
  142. promote_feature(media)
  143. return redirect(
  144. request, 'index')
  145. @user_has_privilege(u'featurer')
  146. @get_user_media_entry
  147. def demote_featured_media(request, media, **kwargs):
  148. """
  149. A view to move a piece of media down the featured stack
  150. """
  151. featured_media = FeaturedMedia.query.filter(
  152. FeaturedMedia.media_entry_id == media.id).first()
  153. if featured_media is not None:
  154. demote_feature(media)
  155. return redirect(
  156. request, 'index')
  157. def get_root_view():
  158. return root_view