class PdfTest(TestCase): def setUp(self): self.pdf = MediaFile(file=File(open('testapp/fixtures/rst-cheatsheet.pdf'))) self.pdf.save() def tearDown(self): pdf_variations = Variation.objects.filter( content_type = ContentType.objects.get_for_model(self.pdf), object_id = self.pdf.pk ) for variation in pdf_variations: variation.delete() self.pdf.delete() def test_page_range(self): from pyPdf import PdfFileReader self.variation = Variation( content_object = self.pdf, spec = 'mediavariations.contrib.pypdf.specs.PageRange', options = simplejson.dumps({ 'start' : 0, 'stop' : 1 }) ) self.variation.save() reader = PdfFileReader(self.variation.file.file) self.assertEqual(reader.getNumPages(), 1)
def import_path(path, parent_category=None): category_name = os.path.basename(path) if not category_name: path = os.path.dirname(path) category_name = os.path.basename(path) if parent_category and parent_category.parent: parent_category = parent_category.parent try: category = Category.objects.filter(title=category_name, parent=parent_category)[0] except IndexError: category = Category(title=category_name, parent=parent_category) category.save() for item in os.listdir(path): subpath = os.path.join(path, item) if os.path.isdir(subpath): import_path(subpath, parent_category=category) elif item.startswith('.'): # ignore . files pass else: content = ContentFile(open(subpath).read()) media_file = MediaFile() media_file.save() media_file.categories.add(category) media_file.file.save(media_file.file.storage.get_valid_name(item), content)
def bulk_upload(request): from django.core.urlresolvers import reverse from os import path if request.method == 'POST' and 'data' in request.FILES: category = None if request.POST.get('category'): category = Category.objects.get(pk=int(request.POST.get('category'))) from django.template.defaultfilters import slugify from django.core.files.base import ContentFile for uploaded_file in request.FILES.getlist('data'): fname, ext = path.splitext(uploaded_file.name) target_fname = slugify(fname) + ext.lower() mf = MediaFile() mf.file.save(target_fname, uploaded_file) mf.save() if category: mf.categories.add(category) mt = MediaFileTranslation() mt.parent = mf mt.caption = fname.replace('_', ' ') mt.save() messages.info(request, _("%d files imported") % len(request.FILES.getlist('data'))) else: messages.error(request, _("No input file given")) return HttpResponseRedirect(reverse('admin:medialibrary_mediafile_changelist'))
class BlitlineTest(TestCase): def setUp(self): self.mediafile = MediaFile(file=File(open('testapp/fixtures/elephant_test_image.jpeg'))) self.mediafile.save() def tearDown(self): """ delete the mediafile """ mediafile_variations = Variation.objects.filter( content_type = ContentType.objects.get_for_model(self.mediafile), object_id = self.mediafile.pk ) for variation in mediafile_variations: variation.delete() self.mediafile.delete() def test_setup(self): self.assertEqual(self.mediafile.type, 'image') self.assertEqual(get_image_dimensions(self.mediafile.file), (404, 346)) def test_blitline(self): self.variation = Variation( content_object=self.mediafile, spec = 'mediavariations.contrib.blitline.specs.Generic', options = ''' {"functions": [{ "name": "crop", "params" : { "x": 5, "y": 5, "width": 40,"height": 40 } }] } ''' ) self.variation.save() # auto medialfield discovery self.assertEqual(self.variation.field, 'file') # wait for image to be processed while (not self.variation.processed): self.variation.get_progress() sleep(1) # test image self.assertEqual(get_image_dimensions(self.variation.file), (40, 40)) def test_templatetag(self): from mediavariations.templatetags.mediavariations import mediavariation self.variated_url = mediavariation(self.mediafile, 'blitline') path, filename = os.path.split(self.mediafile.file.name) basename, ext = os.path.splitext(filename) self.assertTrue(basename in self.variated_url)
def _saveSplash(self, vidFile, thumbURL, mediaPath): thumbMediaPath = os.path.join(mediaPath, '%s.jpg' % vidFile) thumbRealPath = os.path.join(settings.MEDIA_ROOT, thumbMediaPath) subprocess.call(['wget', '-O', thumbRealPath, thumbURL]) try: mf = MediaFile.objects.get(file=thumbMediaPath) except MediaFile.DoesNotExist: mf = MediaFile(file=thumbMediaPath) mf.save()
def test_autocreation(self): from models import MediaVariation MediaVariation.register_preselection( ('cropscale50x50', _('50px Square Thumbnail'), 'image-cropscale', {'height' : 50, 'width' : 50}), ('thumbnail150x99999', _('Max 150px wide image'), 'image-thumbnail', {'height' : 150, 'width' : 99999}), ) from extensions import auto_creation MediaFile.register_extension(auto_creation) MediaFile.register_variation_auto_creation('cropscale50x50', 'thumbnail150x99999') self.assertEqual(MediaFile.variation_auto_creation, ['cropscale50x50', 'thumbnail150x99999']) processed = self.image.get_variation('cropscale50x50') self.assertEqual(get_image_dimensions(processed.file), (50, 50))
def _processDownloadedFile(self, vidFile, title, desc, mediaPath): orig = os.path.join(self.tmp, vidFile) new = os.path.join(settings.MEDIA_ROOT, mediaPath, vidFile) subprocess.call(['yamdi', '-i', orig, '-o', new]) try: mf = MediaFile.objects.get(file=os.path.join(mediaPath, vidFile)) except MediaFile.DoesNotExist: mf = MediaFile(file=os.path.join(mediaPath, vidFile)) mf.save() tr = mf.get_translation() if tr == None: tr = MediaFileTranslation(parent=mf, language_code='cs') tr.caption = mark_safe(title) tr.description = mark_safe(desc) tr.save()
def upload_image(request): """ image upload handler Returns a HttpResponse whose content is JSON. """ result = {"result": False} if request.method == "POST" and request.FILES: if request.FILES["file"]: # TODO: use form (validation) file = request.FILES["file"] mf = MediaFile() mf.file.save(file.name, ContentFile(file.file.read())) mf.save() result = {"result": True} result["location"] = mf.get_absolute_url() return HttpResponse(dumps(result), content_type="application/json")
def bulk_upload(request): from django.core.urlresolvers import reverse from os import path if request.method == 'POST' and 'data' in request.FILES: category = None if request.POST.get('category'): category = Category.objects.get( pk=int(request.POST.get('category'))) from django.template.defaultfilters import slugify from django.core.files.base import ContentFile for uploaded_file in request.FILES.getlist('data'): fname, ext = path.splitext(uploaded_file.name) target_fname = slugify(fname) + ext.lower() mf = MediaFile() mf.file.save(target_fname, uploaded_file) mf.save() if category: mf.categories.add(category) mt = MediaFileTranslation() mt.parent = mf mt.caption = fname.replace('_', ' ') mt.save() messages.info( request, _("%d files imported") % len(request.FILES.getlist('data'))) else: messages.error(request, _("No input file given")) return HttpResponseRedirect( reverse('admin:medialibrary_mediafile_changelist'))
from feincms.module.medialibrary.models import MediaFile from mediavariations.contrib.feincms.extensions import variations MediaFile.register_extension(variations)
def test_10_mediafile_and_imagecontent(self): self.create_default_page_set() page = Page.objects.get(pk=1) self.create_pagecontent(page) path = os.path.join(settings.MEDIA_ROOT, "somefile.jpg") f = open(path, "wb") f.write("blabla") f.close() category = Category.objects.create(title="Category", parent=None) category2 = Category.objects.create(title="Something", parent=category) self.assertEqual(unicode(category2), "Category - Something") self.assertEqual(unicode(category), "Category") mediafile = MediaFile.objects.create(file="somefile.jpg") mediafile.categories = [category] page.mediafilecontent_set.create(mediafile=mediafile, region="main", position="block", ordering=1) self.assertEqual(unicode(mediafile), "somefile.jpg") mediafile.translations.create(caption="something", language_code="%s-ha" % short_language_code()) assert "something" in unicode(mediafile) mf = page.content.main[1].mediafile self.assertEqual(mf.translation.caption, "something") self.assertEqual(mf.translation.short_language_code(), short_language_code()) self.assertNotEqual(mf.get_absolute_url(), "") self.assertEqual(unicode(mf), "something") self.assertEqual(mf.file_type(), "Image") self.assertEqual(MediaFile.objects.only_language("de").count(), 0) self.assertEqual(MediaFile.objects.only_language("en").count(), 0) self.assertEqual(MediaFile.objects.only_language("%s-ha" % short_language_code()).count(), 1) assert "%s-ha" % short_language_code() in mf.available_translations os.unlink(path) # this should not raise self.client.get("/admin/page/page/1/") assert 'alt="something"' in page.content.main[1].render() page.imagecontent_set.create(image="somefile.jpg", region="main", position="default", ordering=2) page.filecontent_set.create(file="somefile.jpg", title="thetitle", region="main", ordering=3) assert "somefile.jpg" in page.content.main[2].render() assert '<a href="/media/somefile.jpg">thetitle</a>' in page.content.main[3].render() page.mediafilecontent_set.update(mediafile=3) # this should not raise self.client.get("/admin/page/page/1/") field = MediaFile._meta.get_field("file") old = (field.upload_to, field.storage) from django.core.files.storage import FileSystemStorage MediaFile.reconfigure( upload_to=lambda: "anywhere", storage=FileSystemStorage(location="/wha/", base_url="/whe/") ) mediafile = MediaFile.objects.get(pk=1) self.assertEqual(mediafile.file.url, "/whe/somefile.jpg") MediaFile.reconfigure(upload_to=old[0], storage=old[1]) mediafile = MediaFile.objects.get(pk=1) self.assertEqual(mediafile.file.url, "/media/somefile.jpg")
def setUp(self): self.mediafile = MediaFile(file=File(open('testapp/fixtures/elephant_test_image.jpeg'))) self.mediafile.save()
def setUp(self): from extensions import media_variation MediaFile.register_extension(media_variation) mediafile = MediaFile(file=File(open('feinheit/media_variations/fixtures/elephant_test_image.jpeg'))) mediafile.save() self.image = mediafile
def setUp(self): self.pdf = MediaFile(file=File(open('testapp/fixtures/rst-cheatsheet.pdf'))) self.pdf.save()
from feincms.module.page.models import Page from feincms.module.medialibrary.models import MediaFile from feincms.content.richtext.models import RichTextContent from feincms.content.medialibrary.v2 import MediaFileContent from feincms_oembed.contents import OembedContent, FeedContent from mediavariations.contrib.feincms.extensions import variations from contents import AnnouncementContent, SimpleGalleryContent, \ TextContent, NewswallContent, FacebookImagePostsContent, WeatherContent from extensions import content_timing_extension, mediafile_cover_extension, \ page_location #MediaFile.register_extension(variations) MediaFile.register_extension(mediafile_cover_extension) Page.register_templates({ 'title': 'Standard Screen', 'path': 'screen.html', 'regions': ( ('main', _('Main content area')), ('announcements', _('Announcements'), 'inherited'), ('inactive', _('Inactive')), ), }) content_timing_extension(RichTextContent, TextContent, MediaFileContent, OembedContent, SimpleGalleryContent, AnnouncementContent, FacebookImagePostsContent, NewswallContent, WeatherContent)
def MediaFileFactory(filename='filename.jpg', url='media_url'): media_file = MediaFile() media_file.file = Mock() media_file.file.name = filename media_file.file.url = url return media_file
def test_10_mediafile_and_imagecontent(self): self.create_default_page_set() page = Page.objects.get(pk=1) self.create_pagecontent(page) path = os.path.join(settings.MEDIA_ROOT, 'somefile.jpg') f = open(path, 'wb') f.write('blabla') f.close() category = Category.objects.create(title='Category', parent=None) category2 = Category.objects.create(title='Something', parent=category) self.assertEqual(unicode(category2), 'Category - Something') self.assertEqual(unicode(category), 'Category') mediafile = MediaFile.objects.create(file='somefile.jpg') mediafile.categories = [category] page.mediafilecontent_set.create(mediafile=mediafile, region='main', position='block', ordering=1) self.assertEqual(unicode(mediafile), 'somefile.jpg') mediafile.translations.create(caption='something', language_code='%s-ha' % short_language_code()) self.assertTrue('something' in unicode(mediafile)) mf = page.content.main[1].mediafile self.assertEqual(mf.translation.caption, 'something') self.assertEqual(mf.translation.short_language_code(), short_language_code()) self.assertNotEqual(mf.get_absolute_url(), '') self.assertEqual(unicode(mf), 'something') self.assertEqual(unicode(mf.file_type()), u'Binary') # Ok, so it's not really an image... self.assertEqual(MediaFile.objects.only_language('de').count(), 0) self.assertEqual(MediaFile.objects.only_language('en').count(), 0) self.assertEqual( MediaFile.objects.only_language('%s-ha' % short_language_code()).count(), 1) self.assertTrue('%s-ha' % short_language_code() in mf.available_translations) os.unlink(path) # this should not raise self.client.get('/admin/page/page/1/') #self.assertTrue('alt="something"' in page.content.main[1].render()) Since it isn't an image page.imagecontent_set.create(image='somefile.jpg', region='main', position='default', ordering=2) page.filecontent_set.create(file='somefile.jpg', title='thetitle', region='main', ordering=3) self.assertTrue('somefile.jpg' in page.content.main[2].render()) self.assertTrue('<a href="/media/somefile.jpg">thetitle</a>' in page.content.main[3].render()) page.mediafilecontent_set.update(mediafile=3) # this should not raise self.client.get('/admin/page/page/1/') field = MediaFile._meta.get_field('file') old = (field.upload_to, field.storage) from django.core.files.storage import FileSystemStorage MediaFile.reconfigure(upload_to=lambda: 'anywhere', storage=FileSystemStorage(location='/wha/', base_url='/whe/')) mediafile = MediaFile.objects.get(pk=1) self.assertEqual(mediafile.file.url, '/whe/somefile.jpg') MediaFile.reconfigure(upload_to=old[0], storage=old[1]) mediafile = MediaFile.objects.get(pk=1) self.assertEqual(mediafile.file.url, '/media/somefile.jpg')
def test_10_mediafile_and_imagecontent(self): self.create_default_page_set() page = Page.objects.get(pk=1) self.create_pagecontent(page) path = os.path.join(settings.MEDIA_ROOT, 'somefile.jpg') f = open(path, 'wb') f.write('blabla') f.close() category = Category.objects.create(title='Category', parent=None) category2 = Category.objects.create(title='Something', parent=category) self.assertEqual(unicode(category2), 'Category - Something') self.assertEqual(unicode(category), 'Category') mediafile = MediaFile.objects.create(file='somefile.jpg') mediafile.categories = [category] page.mediafilecontent_set.create( mediafile=mediafile, region='main', position='block', ordering=1) self.assertEqual(unicode(mediafile), 'somefile.jpg') mediafile.translations.create(caption='something', language_code='%s-ha' % short_language_code()) self.assertTrue('something' in unicode(mediafile)) mf = page.content.main[1].mediafile self.assertEqual(mf.translation.caption, 'something') self.assertEqual(mf.translation.short_language_code(), short_language_code()) self.assertNotEqual(mf.get_absolute_url(), '') self.assertEqual(unicode(mf), 'something') self.assertEqual(unicode(mf.file_type()), u'Binary') # Ok, so it's not really an image... self.assertEqual(MediaFile.objects.only_language('de').count(), 0) self.assertEqual(MediaFile.objects.only_language('en').count(), 0) self.assertEqual(MediaFile.objects.only_language('%s-ha' % short_language_code()).count(), 1) self.assertTrue('%s-ha' % short_language_code() in mf.available_translations) os.unlink(path) # this should not raise self.client.get('/admin/page/page/1/') #self.assertTrue('alt="something"' in page.content.main[1].render()) Since it isn't an image page.imagecontent_set.create(image='somefile.jpg', region='main', position='default', ordering=2) page.filecontent_set.create(file='somefile.jpg', title='thetitle', region='main', ordering=3) self.assertTrue('somefile.jpg' in page.content.main[2].render()) self.assertTrue('<a href="/media/somefile.jpg">thetitle</a>' in page.content.main[3].render()) page.mediafilecontent_set.update(mediafile=3) # this should not raise self.client.get('/admin/page/page/1/') field = MediaFile._meta.get_field('file') old = (field.upload_to, field.storage) from django.core.files.storage import FileSystemStorage MediaFile.reconfigure(upload_to=lambda: 'anywhere', storage=FileSystemStorage(location='/wha/', base_url='/whe/')) mediafile = MediaFile.objects.get(pk=1) self.assertEqual(mediafile.file.url, '/whe/somefile.jpg') MediaFile.reconfigure(upload_to=old[0], storage=old[1]) mediafile = MediaFile.objects.get(pk=1) self.assertEqual(mediafile.file.url, '/media/somefile.jpg')