attachments.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. from __future__ import unicode_literals
  2. import os
  3. from django.contrib.auth import get_user_model
  4. from django.core.files import File
  5. from misago.threads.models import Attachment, AttachmentType, Post
  6. from misago.threads.serializers import AttachmentSerializer
  7. from . import OLD_FORUM, fetch_assoc, localise_datetime, movedids
  8. UserModel = get_user_model()
  9. IMAGE_TYPES = ('image/gif', 'image/jpeg', 'image/png', )
  10. def move_attachments(stdout, style):
  11. query = '''
  12. SELECT *
  13. FROM
  14. misago_attachment
  15. ORDER BY
  16. id
  17. '''
  18. posts = []
  19. attachment_types = {}
  20. for attachment_type in AttachmentType.objects.all():
  21. for mimetype in attachment_type.mimetypes_list:
  22. attachment_types[mimetype] = attachment_type
  23. for attachment in fetch_assoc(query):
  24. if attachment['content_type'] not in attachment_types:
  25. stdout.write(
  26. style.WARNING("Skipping attachment: %s (invalid type)" % attachment['name'])
  27. )
  28. continue
  29. if not attachment['post_id']:
  30. stdout.write(style.WARNING("Skipping attachment: %s (orphaned)" % attachment['name']))
  31. continue
  32. filetype = attachment_types[attachment['content_type']]
  33. post_pk = movedids.get('post', attachment['post_id'])
  34. post = Post.objects.get(pk=post_pk)
  35. if post_pk not in posts:
  36. posts.append(post_pk)
  37. uploader = None
  38. if attachment['user_id']:
  39. uploader_pk = movedids.get('user', attachment['user_id'])
  40. uploader = UserModel.objects.get(pk=uploader_pk)
  41. file_path = os.path.join(OLD_FORUM['ATTACHMENTS'], attachment['path'])
  42. upload = OldAttachmentFile(open(file_path, 'rb'), attachment)
  43. new_attachment = Attachment(
  44. secret=Attachment.generate_new_secret(),
  45. filetype=filetype,
  46. post=post,
  47. uploaded_on=localise_datetime(attachment['date']),
  48. uploader=uploader,
  49. uploader_name=attachment['user_name'],
  50. uploader_slug=attachment['user_name_slug'],
  51. uploader_ip=attachment['ip'],
  52. filename=attachment['name'],
  53. size=attachment['size'],
  54. )
  55. if attachment['content_type'] in IMAGE_TYPES:
  56. new_attachment.set_image(upload)
  57. else:
  58. new_attachment.set_file(upload)
  59. new_attachment.save()
  60. movedids.set('attachment', attachment['id'], new_attachment.pk)
  61. update_attachments_caches(posts)
  62. def update_attachments_caches(posts):
  63. for post in Post.objects.filter(pk__in=posts).iterator():
  64. attachments = post.attachment_set.order_by('id')
  65. post.attachments_cache = AttachmentSerializer(attachments, many=True).data
  66. for attachment in post.attachments_cache:
  67. del attachment['acl']
  68. del attachment['post']
  69. del attachment['uploader_ip']
  70. post.save()
  71. class OldAttachmentFile(File):
  72. def __init__(self, file, attachment):
  73. self._attachment = attachment
  74. self.name = attachment['name']
  75. self.file = file
  76. if hasattr(file, 'mode'):
  77. self.mode = file.mode