attachments.py 3.2 KB

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