attachments.py 3.1 KB

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