lib.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 CollectionItem, Report, TextComment, \
  19. MediaEntry
  20. from mediagoblin.tools.mail import send_email
  21. from mediagoblin.tools.pluginapi import hook_runall
  22. from mediagoblin.tools.template import render_template
  23. from mediagoblin.tools.translate import pass_to_ugettext as _
  24. def send_comment_email(user, comment, media, request):
  25. """
  26. Sends comment email to user when a comment is made on their media.
  27. Args:
  28. - user: the user object to whom the email is sent
  29. - comment: the comment object referencing user's media
  30. - media: the media object the comment is about
  31. - request: the request
  32. """
  33. comment_url = request.urlgen(
  34. 'mediagoblin.user_pages.media_home.view_comment',
  35. comment=comment.id,
  36. user=media.get_actor.username,
  37. media=media.slug_or_id,
  38. qualified=True) + '#comment'
  39. comment_author = comment.get_actor.username
  40. rendered_email = render_template(
  41. request, 'mediagoblin/user_pages/comment_email.txt',
  42. {'username': user.username,
  43. 'comment_author': comment_author,
  44. 'comment_content': comment.content,
  45. 'comment_url': comment_url})
  46. send_email(
  47. mg_globals.app_config['email_sender_address'],
  48. [user.email],
  49. '{instance_title} - {comment_author} '.format(
  50. comment_author=comment_author,
  51. instance_title=mg_globals.app_config['html_title']) \
  52. + _('commented on your post'),
  53. rendered_email)
  54. def add_media_to_collection(collection, media, note=None, commit=True):
  55. collection_item = CollectionItem()
  56. collection_item.collection = collection.id
  57. collection_item.get_object = media
  58. if note:
  59. collection_item.note = note
  60. Session.add(collection_item)
  61. collection.num_items = collection.num_items + 1
  62. Session.add(collection)
  63. Session.add(media)
  64. hook_runall('collection_add_media', collection_item=collection_item)
  65. if commit:
  66. Session.commit()
  67. def build_report_object(report_form, media_entry=None, comment=None):
  68. """
  69. This function is used to convert a form object (from a User filing a
  70. report) into a Report.
  71. :param report_form A MediaReportForm or a CommentReportForm object
  72. with valid information from a POST request.
  73. :param media_entry A MediaEntry object. The MediaEntry being repo-
  74. -rted by a Report.
  75. :param comment A Comment object. The Comment being
  76. reported by a Report.
  77. :returns A Report object if a valid MediaReportForm is
  78. passed as kwarg media_entry. This Report has
  79. not been saved.
  80. :returns None if the form_dict is invalid.
  81. """
  82. report_object = Report()
  83. if report_form.validate() and comment is not None:
  84. report_object.obj = comment.comment()
  85. report_object.reported_user_id = TextComment.query.get(
  86. comment.id).get_actor.id
  87. elif report_form.validate() and media_entry is not None:
  88. report_object.obj = media_entry
  89. report_object.reported_user_id = MediaEntry.query.get(
  90. media_entry.id).get_actor.id
  91. else:
  92. return None
  93. report_object.report_content = report_form.report_reason.data
  94. report_object.reporter_id = report_form.reporter_id.data
  95. return report_object