def update(fs, content, image_type, save_to_db=True): from drawquest.apps.content_metadata.models import save_content_metadata_from_legacy_dict filename = content.details()['original']['name'] # Prevent issues with unicode filenames. filename = filename.encode('ascii') data = fs.read(filename) thumbnailer = Thumbnailer(fs) meta = util.loads(redis.get(content.details_key)) meta.update(thumbnailer.store(data, image_type)) if save_to_db: content.save() save_content_metadata_from_legacy_dict(content, meta) content.details.force()
def update(fs, content, image_type, save_to_db=True): from drawquest.apps.content_metadata.models import save_content_metadata_from_legacy_dict filename = content.details()['original']['name'] # Prevent issues with unicode filenames. filename = filename.encode('ascii') data = fs.read(filename) thumbnailer = Thumbnailer(fs) meta = util.loads(redis.get(content.details_key)) meta.update(thumbnailer.store(data, image_type)) if save_to_db: content.save() save_content_metadata_from_legacy_dict(content, meta) content.details.force()
def create_content(ip, fs, data, remix_of, stamps_used, is_quest=False): from drawquest.apps.content_metadata.models import save_content_metadata_from_legacy_dict util.papertrail.debug('UPLOADS: create_content, is_quest={}'.format(is_quest)) exclude_types = [] if not is_quest: exclude_types = ['homepage_featured'] meta = generate_thumbnails(data, fs=fs, exclude_types=exclude_types) util.papertrail.debug('UPLOADS: thumbnails generated for ID {}'.format(meta['id'])) if remix_of: remix_of = Content.all_objects.get(id=remix_of) remix_of.hide_if_unpublished() else: remix_of = None if stamps_used: stamps_used = [Content.all_objects.get_or_none(id=stamp_id) for stamp_id in stamps_used if stamp_id] stamps_used = [stamp for stamp in stamps_used if stamp] try: util.papertrail.debug('UPLOADS: trying to get existing content with ID {}'.format(meta['id'])) content = Content.all_objects.get(id=meta['id']) util.papertrail.debug('UPLOADS: got existing content for ID {}'.format(meta['id'])) # Don't allow uploading content that has been disabled. if content.visibility == Visibility.DISABLED: return { 'success': False, 'reason': 'This image has been banned.', } except Content.DoesNotExist: util.papertrail.debug('UPLOADS: creating content with ID {}'.format(meta['id'])) content = Content( id = meta['id'], visibility = Visibility.UNPUBLISHED, ip = ip, timestamp = time(), remix_of = remix_of, ) util.papertrail.debug('UPLOADS: instantiated content with ID {}'.format(meta['id'])) try: content.save(force_insert=True) util.papertrail.debug('UPLOADS: saved content with ID {}'.format(meta['id'])) util.papertrail.debug('UPLOADS: saved content with ID {}, has pk {}'.format(meta['id'], content.pk)) util.papertrail.debug('UPLOADS: actual content object for ID {} exists: {}'.format(meta['id'], Content.all_objects.filter(id=meta['id']).exists())) util.papertrail.debug('UPLOADS: actual content object for ID {} exists using pk: {}'.format(meta['id'], Content.all_objects.filter(id=content.pk).exists())) util.papertrail.debug('UPLOADS: actual content object for ID {} exists using pk2: {}'.format(meta['id'], Content.all_objects.filter(pk=content.pk).exists())) except IntegrityError: util.papertrail.debug('UPLOADS: integrity error when trying to create content with ID {}'.format(meta['id'])) # Race condition, retry return create_content(ip, fs, data, remix_of, stamps_used, is_quest=is_quest) content.stamps_used.add(*stamps_used) util.papertrail.debug('UPLOADS: creating metadata for ID {}'.format(meta['id'])) save_content_metadata_from_legacy_dict(content, meta) util.papertrail.debug('UPLOADS: created metadata for ID {}'.format(meta['id'])) util.papertrail.debug('UPLOADS: returning metadata for ID {}'.format(meta['id'])) return {'success': True, 'content': content.details.force()}
def create_content(ip, fs, data, remix_of, stamps_used, is_quest=False): from drawquest.apps.content_metadata.models import save_content_metadata_from_legacy_dict util.papertrail.debug( 'UPLOADS: create_content, is_quest={}'.format(is_quest)) exclude_types = [] if not is_quest: exclude_types = ['homepage_featured'] meta = generate_thumbnails(data, fs=fs, exclude_types=exclude_types) util.papertrail.debug('UPLOADS: thumbnails generated for ID {}'.format( meta['id'])) if remix_of: remix_of = Content.all_objects.get(id=remix_of) remix_of.hide_if_unpublished() else: remix_of = None if stamps_used: stamps_used = [ Content.all_objects.get_or_none(id=stamp_id) for stamp_id in stamps_used if stamp_id ] stamps_used = [stamp for stamp in stamps_used if stamp] try: util.papertrail.debug( 'UPLOADS: trying to get existing content with ID {}'.format( meta['id'])) content = Content.all_objects.get(id=meta['id']) util.papertrail.debug('UPLOADS: got existing content for ID {}'.format( meta['id'])) # Don't allow uploading content that has been disabled. if content.visibility == Visibility.DISABLED: return { 'success': False, 'reason': 'This image has been banned.', } except Content.DoesNotExist: util.papertrail.debug('UPLOADS: creating content with ID {}'.format( meta['id'])) content = Content( id=meta['id'], visibility=Visibility.UNPUBLISHED, ip=ip, timestamp=time(), remix_of=remix_of, ) util.papertrail.debug( 'UPLOADS: instantiated content with ID {}'.format(meta['id'])) try: content.save(force_insert=True) util.papertrail.debug('UPLOADS: saved content with ID {}'.format( meta['id'])) util.papertrail.debug( 'UPLOADS: saved content with ID {}, has pk {}'.format( meta['id'], content.pk)) util.papertrail.debug( 'UPLOADS: actual content object for ID {} exists: {}'.format( meta['id'], Content.all_objects.filter(id=meta['id']).exists())) util.papertrail.debug( 'UPLOADS: actual content object for ID {} exists using pk: {}'. format(meta['id'], Content.all_objects.filter(id=content.pk).exists())) util.papertrail.debug( 'UPLOADS: actual content object for ID {} exists using pk2: {}' .format(meta['id'], Content.all_objects.filter(pk=content.pk).exists())) except IntegrityError: util.papertrail.debug( 'UPLOADS: integrity error when trying to create content with ID {}' .format(meta['id'])) # Race condition, retry return create_content(ip, fs, data, remix_of, stamps_used, is_quest=is_quest) content.stamps_used.add(*stamps_used) util.papertrail.debug('UPLOADS: creating metadata for ID {}'.format( meta['id'])) save_content_metadata_from_legacy_dict(content, meta) util.papertrail.debug('UPLOADS: created metadata for ID {}'.format( meta['id'])) util.papertrail.debug('UPLOADS: returning metadata for ID {}'.format( meta['id'])) return {'success': True, 'content': content.details.force()}