def edit_document(self, **params): # Build a fake file fake_file = ContentFile(b("A boring example document")) fake_file.name = 'test.txt' # Create a document without tags to edit document = models.Document.objects.create(title="Test document", file=fake_file) # Build another fake file another_fake_file = ContentFile(b("A boring example document")) another_fake_file.name = 'test.txt' # Submit post_data = { 'title': "Test document changed!", 'file': another_fake_file, } post_data.update(params) response = self.client.post(reverse('wagtaildocs:edit', args=(document.id,)), post_data) # User should be redirected back to the index self.assertRedirects(response, reverse('wagtaildocs:index')) # Document should be changed doc = models.Document.objects.filter(title=post_data['title']) self.assertTrue(doc.exists()) return doc.first()
def get_app(self, version): plist_str = sample_plist % (reverse('deploy-latest-ipa', args=['someapp']), version) plist = ContentFile(plist_str.encode('UTF-8')) plist.name = 'someapp.plist' ipa = ContentFile(b'Some Data') ipa.name = 'someapp.ipa' plist.name = 'someapp.plist' app = App(name='someapp', version=version, plist=plist, ipa=ipa, is_active=True) return app
def _upload_file(self, request): """ Upload file to the server. """ if request.method == "POST": folder = request.GET.get('folder', '') if request.is_ajax(): # Advanced (AJAX) submission filedata = ContentFile(request.raw_post_data) else: # Basic (iframe) submission if len(request.FILES) != 1: raise Http404('Invalid request! Multiple files included.') filedata = request.FILES.values()[0] try: filedata.name = convert_filename(request.GET['qqfile']) except KeyError: return HttpResponseBadRequest('Invalid request! No filename given.') else: # Basic (iframe) submission # TODO: This needs some attention, do we use this at all? folder = request.POST.get('folder') if len(request.FILES) == 1: filedata = request.FILES.values()[0] else: raise Http404('Invalid request! Multiple files included.') # filedata.name = convert_filename(upload.name) filedata.name = convert_filename(request.POST.get('file_name')) fb_uploadurl_re = re.compile(r'^.*(%s)' % reverse("filebrowser:fb_upload", current_app=self.name)) folder = fb_uploadurl_re.sub('', folder) path = os.path.join(self.directory, folder) file_name = os.path.join(path, filedata.name) file_already_exists = self.storage.exists(file_name) # Check for name collision with a directory if file_already_exists and self.storage.isdir(file_name): ret_json = {'success': False, 'filename': filedata.name} return HttpResponse(json.dumps(ret_json)) signals.filebrowser_pre_upload.send(sender=request, path=request.POST.get('folder'), file=filedata, site=self) uploadedfile = handle_file_upload(path, filedata, site=self) if file_already_exists and OVERWRITE_EXISTING: old_file = smart_unicode(file_name) new_file = smart_unicode(uploadedfile) self.storage.move(new_file, old_file, allow_overwrite=True) else: file_name = smart_unicode(uploadedfile) signals.filebrowser_post_upload.send(sender=request, path=request.POST.get('folder'), file=FileObject(smart_unicode(file_name), site=self), site=self) # let Ajax Upload know whether we saved it or not ret_json = {'success': True, 'filename': filedata.name} return HttpResponse(json.dumps(ret_json))
def test_snapshot_get3(self): file = ContentFile(b"123") file.name = "file.jpg" retention_hourly = models.RetentionPolicy.objects.create(name="hourly", duration=timedelta(hours=1)) collection1 = models.Collection.objects.create(name="collection1", default_retention_policy=retention_hourly) collection2 = models.Collection.objects.create(name="collection2", default_retention_policy=retention_hourly) snapshots1 = [] snapshots2 = [] for i in range(0,3): snapshots1.append(models.Snapshot.objects.create(collection = collection1, file = file)) snapshots2.append(models.Snapshot.objects.create(collection = collection2, file = file)) # first collection self.assertEqual(snapshots1[0].get_next(), snapshots1[1]) self.assertEqual(snapshots1[1].get_next(), snapshots1[2]) self.assertEqual(snapshots1[1].get_previous(), snapshots1[0]) self.assertEqual(snapshots1[2].get_previous(), snapshots1[1]) self.assertEqual(collection1.get_earliest(), snapshots1[0]) self.assertEqual(collection1.get_latest(), snapshots1[2]) # second collection self.assertEqual(snapshots2[0].get_next(), snapshots2[1]) self.assertEqual(snapshots2[1].get_next(), snapshots2[2]) self.assertEqual(snapshots2[1].get_previous(), snapshots2[0]) self.assertEqual(snapshots2[2].get_previous(), snapshots2[1]) self.assertEqual(collection2.get_earliest(), snapshots2[0]) self.assertEqual(collection2.get_latest(), snapshots2[2])
def get_natives(*names): r = [] for name in names: f = ContentFile(name) f.name = name r.append(f) return r
def process_item(self, item): """ For the demo we also want to copy the revisions and associated documents """ revisions = item.revision_set.all() item = super(DemoMatterCloneService, self).process_item(item=item) # # Now clone the revisions and copy their documents # for rev in revisions: rev.pk = None # invalidate its pk rev.item = item # the new created item # give the new file a name prefixed with the matter.pk # NEW_EXECUTED_PATH = '%s/%s-%s' % (os.path.dirname(rev.executed_file.name), item.matter.pk, os.path.basename(rev.executed_file.name)) try: # # @TODO must be a simpler way # # with open(NEW_EXECUTED_PATH, 'w') as executed_file: # # read the contents of the file into the new local_file # executed_file.write(rev.executed_file.read()) new_executed_file = ContentFile(rev.executed_file.read()) new_executed_file.name = rev.executed_file.name rev.executed_file = new_executed_file except IOError: logger.critical('DemoMatterCloneService: executed_file: %s does not exist' % rev.executed_file.name) rev.save()
def test_add_thumbnail(self): thumbnail = ContentFile(file("datatests/thumbnail.png").read()) thumbnail.name = "Thumbnail.png" self.controller.add_file(self.get_file()) f2 = self.controller.files.all()[0] self.controller.add_thumbnail(f2, thumbnail) self.assertNotEquals(None, f2.thumbnail)
def docente_edit(req, docente_id): docente = get_object_or_404(CursoDocente, id=docente_id) if req.method == "POST": nombre = req.POST.get("nombre") twitter = req.POST.get("twitter") perfil = req.POST.get("perfil") imagen = req.POST.get("imagen") imagen_n = req.POST.get("imagen_filename") if nombre and twitter and perfil: docente.nombre = nombre docente.twitter = twitter docente.perfil = perfil if imagen and imagen_n: # carga de imagen uploaded_file = ContentFile(base64.b64decode(imagen.split(",")[1])) uploaded_file.name = imagen_n docente.imagen = uploaded_file docente.save() return HttpResponse(jstatus_ok(serialize_docente(docente))) else: return HttpResponse(jstatus_err()) return render_to_response("edmin/docente/admin.html", {"docente": docente})
def add_profile_photo(request, user_id, title): uploaded_image = ContentFile(request.body) uploaded_image.name = "%s.png" % title add_user_photo(user_id, uploaded_image) return {"status": "success"}
def get_db_prep_value(self, value, connection, prepared=False): """Convert pickle object to a string""" fname = uuid.uuid4().hex content_file = ContentFile(value) content_file.name = fname name = self.fs.save(fname, content_file) return change_format(name)
def curso_add(req): if req.method == 'POST': nombre = req.POST.get('nombre') slug = req.POST.get('slug') pais = req.POST.get('pais') precio = req.POST.get('precio') descripcion = req.POST.get('descripcion') direccion = req.POST.get('direccion') mapa = req.POST.get('mapa') imagen = req.POST.get('imagen') imagen_n = req.POST.get('imagen_filename') info_pago = req.POST.get('info_pago') if nombre and slug and pais and precio and descripcion and direccion and mapa and info_pago and imagen and imagen_n: # carga de imagen uploaded_file = ContentFile(base64.b64decode(imagen.split(',')[1])) uploaded_file.name = imagen_n curso = Curso(nombre=nombre, slug=slug, pais=pais, precio=precio, descripcion=descripcion, direccion=direccion, info_pago=info_pago, mapa=mapa, imagen=uploaded_file) curso.save() return HttpResponse('OK') else: return HttpResponse('ERR') return render_to_response('edmin/curso/admin.html')
def add_poster(request): """Process upload of poster file.""" if request.method == 'POST': # Load the data into the form form = PosterUpload(request.POST) # Process the image data submitted as base64 encoded. img = form.data['posterfile'] # Get rid of the preceding info before the filedata. format, imgstr = img.split(';base64,') # Get the file extension ext = format.split('/')[-1] # Load the base64 encoded data to a ContentFile. data = ContentFile(b64decode(imgstr), name='temp.' + ext) # Get glossvideos pk from the submitted form data glossvideo_pk = form.data['pk'] glossvideo = GlossVideo.objects.get(pk=glossvideo_pk) # Delete the existing file (we do not want to keep copies of them). glossvideo.posterfile.delete() # Create a desired filename for the posterfile. data.name = glossvideo.create_poster_filename(ext) glossvideo.posterfile = data glossvideo.save() if 'HTTP_REFERER' in request.META: url = request.META['HTTP_REFERER'] else: url = '/' return redirect(url)
def createSVGView(request, filename): """ This view receives the svg information from the workspace and saves the file """ if request.is_ajax(): filenameRegex = re.search(r'(?P<filename>[a-zA-Z]+[\d\.]*)\.(?P<extension>[a-zA-Z]{1,4}$)', filename) cleanFileName = filenameRegex.group('filename') cleanFileExtension = filenameRegex.group('extension') newFile = ContentFile(cleanFileName+'.svg', 'w') newFile.name = cleanFileName+'.svg' fileContent = base64.b64decode(request.POST['svg']).decode('utf-8') newFile.write(fileContent) newFileDB = UploadSVGFile(file=newFile) newFileDB.save() response_data = { 'success': 1, 'url': newFileDB.file.url, 'filename': filename, 'extension': cleanFileExtension } return HttpResponse(json.dumps(response_data), content_type="application/json")
def approve_entry(self): """Creating a JournalEntry Transactions and Receipts from the Entry. This does not delete the entry, as should be done when an Entry is approved. You **must manually delete** the CreditCardEntry. Returns the created JournalEntry. """ journal_entry = JournalEntry.objects.create( date=self.date, memo=self.generate_memo(), comments=self.comments) transactions = self.transaction_set.all() if transactions.count() == 1: creditcard_detail = transactions[0].detail else: creditcard_detail = 'Purchases by {}'.format(self.name) for transaction in transactions: Transaction.objects.create( journal_entry=journal_entry, account=transaction.account, detail=transaction.detail, balance_delta=(-1 * transaction.amount) ) Transaction.objects.create( journal_entry=journal_entry, account=self.card.account, balance_delta=self.amount, detail=creditcard_detail, ) for receipt in self.receipt_set.all(): new_receipt = ContentFile(receipt.receipt_file.file.read()) new_receipt.name = receipt.receipt_file.name Receipt.objects.create( journal_entry=journal_entry, receipt_file=new_receipt) return journal_entry
def load_field(self, fieldname, val, zf): if val is None: return val, False try: field = self.model._meta.get_field(fieldname) except models.fields.FieldDoesNotExist: field = None deferr = False if field: if isinstance(field, models.DateTimeField): val = datetime.datetime.strptime(val, DATETIME_FORMAT) elif isinstance(field, models.DateField): val = datetime.date.strptime(val, DATE_FORMAT) elif isinstance(field, models.ManyToManyField): val = self.load_many(field.related.parent_model, val, zf) deferr = True elif isinstance(field, models.FileField): f = ContentFile(zf.read(val)) f.name = val # we hack in a name, to force django to automatically save the ContentFile on Model.save() val = f elif isinstance(field, models.ForeignKey): val = load_model_instance(field.rel.to, val, zf) elif isinstance(field, generic.GenericRelation): val = self.load_many(field.rel.to, val, zf, commit=False) deferr = True elif isinstance(val, list): rel_model = getattr(self.model, fieldname).related.model val = self.load_many(rel_model, val, zf, commit=False) deferr = True return val, deferr
def up_view(request): content = request.raw_post_data response = False if not content: response = {'success': False, 'message': 'Empty file!'} return HttpResponse(simplejson.dumps(response)) try: contentFile = ContentFile(content) contentFile.name = request.META['HTTP_UP_FILENAME'] contentFile.type = request.META['HTTP_UP_TYPE'] if int(contentFile.size) > MAX_UPLOAD_SIZE: response = {'success' : False, 'error' : 'File is too big!'} raise f = File(pub_date=datetime.now(), secret=random()*100, name=random()*100, type=contentFile.type) f.file.save(contentFile.name, contentFile) f.save() f = File.objects.get(pk=f.pk) response = {'success': True, 'name': f.name, 'delete': f.secret} except: if not response: response = {'success': False, 'error': 'Upload failed!'} return HttpResponse(simplejson.dumps(response))
def generate(self): obj = self.content_object field = getattr(obj, self.field) image = ContentFile(field.file.read()) # Get extension if "." in field.file.name: split_file_name = field.file.name.split('.') split_file_name.reverse() extension = split_file_name[0] else: extension = "jpg" # Make hash filename to ensure uniqueness filename_hash = hashlib.md5(field.file.name) filename_hash = filename_hash.hexdigest() # Build filename image.name = "{model}.{obj}.{field}.{filename}.{ext}".format( model=obj._meta.db_table, obj=self.object_id, field=self.field, ext=extension, filename=filename_hash) # Save to model self.image = image self.save() # if self.coordinates: self._crop_image()
def setUp(self): # Build a fake file fake_file = ContentFile(b("A boring example song")) fake_file.name = 'song.mp3' self.root_collection = Collection.get_first_root_node() self.evil_plans_collection = self.root_collection.add_child(name="Evil plans") self.nice_plans_collection = self.root_collection.add_child(name="Nice plans") # Create a media to edit self.media = models.Media.objects.create( title="Test media", file=fake_file, collection=self.nice_plans_collection, duration=100 ) # Create a user with change_media permission but not add_media user = get_user_model().objects.create_user( username='******', email='*****@*****.**', password='******' ) change_permission = Permission.objects.get( content_type__app_label='wagtailmedia', codename='change_media' ) admin_permission = Permission.objects.get( content_type__app_label='wagtailadmin', codename='access_admin' ) self.changers_group = Group.objects.create(name='Media changers') GroupCollectionPermission.objects.create( group=self.changers_group, collection=self.root_collection, permission=change_permission ) user.groups.add(self.changers_group) user.user_permissions.add(admin_permission) self.assertTrue(self.client.login(username='******', password='******'))
def setUp(self): # Build a fake file fake_file = ContentFile(b("A boring example document")) fake_file.name = 'test.txt' self.root_collection = Collection.get_first_root_node() self.evil_plans_collection = self.root_collection.add_child(name="Evil plans") self.nice_plans_collection = self.root_collection.add_child(name="Nice plans") # Create a document to edit self.document = models.Document.objects.create( title="Test document", file=fake_file, collection=self.nice_plans_collection ) # Create a user with change_document permission but not add_document user = get_user_model().objects.create_user( username='******', email='*****@*****.**', password='******' ) change_permission = Permission.objects.get( content_type__app_label='wagtaildocs', codename='change_document' ) admin_permission = Permission.objects.get( content_type__app_label='wagtailadmin', codename='access_admin' ) self.changers_group = Group.objects.create(name='Document changers') GroupCollectionPermission.objects.create( group=self.changers_group, collection=self.root_collection, permission=change_permission ) user.groups.add(self.changers_group) user.user_permissions.add(admin_permission) self.assertTrue(self.client.login(username='******', password='******'))
def docente_edit(req, docente_id): docente = get_object_or_404(CursoDocente, id=docente_id) if req.method == 'POST': nombre = req.POST.get('nombre') twitter = req.POST.get('twitter') perfil = req.POST.get('perfil') imagen = req.POST.get('imagen') imagen_n = req.POST.get('imagen_filename') if nombre and twitter and perfil: docente.nombre = nombre docente.twitter = twitter docente.perfil = perfil if imagen and imagen_n: # carga de imagen uploaded_file = ContentFile(base64.b64decode(imagen.split(',')[1])) uploaded_file.name = imagen_n docente.imagen = uploaded_file docente.save() return HttpResponse(jstatus_ok(serialize_docente(docente))) else: return HttpResponse(jstatus_err()) return render_to_response('edmin/docente/admin.html', { 'docente': docente})
def test_valid_dump_import_with_logo(client, settings): settings.CELERY_ENABLED = False user = f.UserFactory.create() client.login(user) url = reverse("importer-load-dump") data = ContentFile(bytes(json.dumps({ "slug": "valid-project", "name": "Valid project", "description": "Valid project desc", "is_private": False, "logo": { "name": "logo.bmp", "data": base64.b64encode(DUMMY_BMP_DATA).decode("utf-8") } }), "utf-8")) data.name = "test" response = client.post(url, {'dump': data}) assert response.status_code == 201 response_data = response.data assert "id" in response_data assert response_data["name"] == "Valid project" assert "logo_small_url" in response_data assert response_data["logo_small_url"] != None assert "logo_big_url" in response_data assert response_data["logo_big_url"] != None
def package_exe(game, lovefile, prefix, name, slug, version): love_exe = open(p(prefix + "/windows/love.exe"), "rb").read() love_archive = open(lovefile, "rb").read() blob = ContentFile(love_exe + love_archive) blob.name = u"{}.exe".format(slug) return blob
def test_post_with_collections(self): root_collection = Collection.get_first_root_node() evil_plans_collection = root_collection.add_child(name="Evil plans") # Build a fake file fake_file = ContentFile(b("A boring example document")) fake_file.name = 'test.txt' # Submit post_data = { 'title': "Test document", 'file': fake_file, 'collection': evil_plans_collection.id, } response = self.client.post(reverse('wagtaildocs:add'), post_data) # User should be redirected back to the index self.assertRedirects(response, reverse('wagtaildocs:index')) # Document should be created, and be placed in the Evil Plans collection self.assertTrue(models.Document.objects.filter(title="Test document").exists()) root_collection = Collection.get_first_root_node() self.assertEqual( models.Document.objects.get(title="Test document").collection, evil_plans_collection )
def test_storage(self): """ Storage testing. """ text = '' storage = S3Storage(host='s3.amazonaws.com') file_length = random.randrange(300, 1300) text = get_string(file_length) filename_length = random.randrange(5, 12) filename = get_string(filename_length) self.assertFalse(storage.exists(filename)) test_file = ContentFile(text) test_file.name = filename uploaded_url = upload(test_file, host='s3.amazonaws.com') self.assertTrue(storage.exists(filename)) url = 'http://' + BOTO_S3_BUCKET + '.s3.amazonaws.com/' + filename self.assertEqual(uploaded_url, url) page = urllib2.urlopen(uploaded_url) self.assertEqual(text, page.read()) self.assertEqual(len(text), storage.size(filename)) self.assertEqual(url, storage.url(filename)) storage.delete(filename) self.assertFalse(storage.exists(filename))
def test_post_video_with_collections(self): root_collection = Collection.get_first_root_node() evil_plans_collection = root_collection.add_child(name="Evil plans") # Build a fake file fake_file = ContentFile(b("A boring example movie")) fake_file.name = 'movie.mp3' # Submit post_data = { 'title': "Test media", 'file': fake_file, 'duration': 100, 'collection': evil_plans_collection.id, } response = self.client.post(reverse('wagtailmedia:add', args=('video', )), post_data) # User should be redirected back to the index self.assertRedirects(response, reverse('wagtailmedia:index')) # Media should be created, and be placed in the Evil Plans collection self.assertTrue(models.Media.objects.filter(title="Test media").exists()) media = models.Media.objects.get(title="Test media") self.assertEqual(media.collection, evil_plans_collection) self.assertEqual(media.type, 'video')
def test_post(self): # Build a fake file fake_file = ContentFile(b("A boring example document")) fake_file.name = 'test.txt' # Submit post_data = { 'title': "Test document", 'file': fake_file, } response = self.client.post(reverse('wagtaildocs:add'), post_data) # User should be redirected back to the index self.assertRedirects(response, reverse('wagtaildocs:index')) # Document should be created, and be placed in the root collection document = models.Document.objects.get(title="Test document") root_collection = Collection.get_first_root_node() self.assertEqual( document.collection, root_collection ) # Check that the file_size field was set self.assertTrue(document.file_size)
def test_filecolumn_text_custom_value(self): name = 'foobar.html' file_ = ContentFile('') file_.name = name root = parse(tables.FileColumn(text='Download').render(value=file_, record=None)) assert root.tag == 'span' assert root.attrib == {'title': name, 'class': ''} assert root.text == 'Download'
def _create_test_image(self, size=(128, 128)): image = Image.new('RGB', size, 'black') png = StringIO.StringIO() image.save(png, "png") png.seek(0) f = ContentFile(content=png.read()) f.name = "test.png" return f
def defaults(): if icon_url: try: icon = ContentFile(urllib2.urlopen(icon_url).read()) icon.name = icon_url.split('/')[-1] yield 'icon', icon except urllib2.HTTPError, e: pass
def filecolumn_supports_contentfile(column): name = "foobar.html" file_ = ContentFile('') file_.name = name # Django <1.4 compatible root = parse(column.render(value=file_)) assert root.tag == "span" assert root.attrib == {"title": name, "class": "span"} assert root.text == "foobar.html"
def _open(self, name, mode='rb'): url = self._get_url(name) response = requests.get(url) if response.status_code == 404: raise IOError response.raise_for_status() file = ContentFile(response.content) file.name = name file.mode = mode return file
def test_count(self): fake_file = ContentFile(b("Example document")) fake_file.name = 'test.txt' document = Document.objects.create(title="Test document", file=fake_file) counter = DownloadCount.objects.create(file=document) assert counter.count is 0 document_served.send(sender=Document, instance=document) counter.refresh_from_db() assert counter.count is 1
def setUp(self): self.login() # Build a fake file fake_file = ContentFile(b("A boring example document")) fake_file.name = 'test.txt' # Create a document to edit self.document = models.Document.objects.create(title="Test document", file=fake_file)
def save_image_course_seeder(encoded_image, image_id): image_format, base64_payload = encoded_image.split(';base64,') ext = image_format.split('/')[-1] data = ContentFile(b64decode(base64_payload), name="") if imghdr.what(data) is not None: data.name = "u" + str(image_id) + "." + imghdr.what(data) else: return None return data
def set_image_from_url(self, url: str): """ Set the image from a URL. """ response = httpx.get(url) if response.status_code == 200: file = ContentFile(response.content) file.name = "url-" + shortuuid.uuid() self.image = file self.save()
def test_revise_with_thumbnail(self): thumbnail = ContentFile(file("datatests/thumbnail.png").read()) thumbnail.name = "Thumbnail.png" self.controller.add_file(self.get_file()) f2 = self.controller.files.all()[0] self.controller.add_thumbnail(f2, thumbnail) revb = self.controller.revise("b") f3 = revb.files.all()[0] self.assertNotEquals(f2.thumbnail.path, f3.thumbnail.path)
def _open(self, name, mode="rb"): url = self.mapper.get(name) response = self._session.get(url, timeout=10) if response.status_code == 404: raise IOError(f"File {name} isn't uploaded in Uplyfile") response.raise_for_status() file = ContentFile(response.content) file.name = name file.mode = mode return file
def test_filecolumn_text_custom_value(self): name = 'foobar.html' file_ = ContentFile('') file_.name = name root = parse( tables.FileColumn(text='Download').render(value=file_, record=None)) assert root.tag == 'span' assert root.attrib == {'title': name, 'class': ''} assert root.text == 'Download'
def test_filecolumn_text_custom_value(self): name = "foobar.html" file_ = ContentFile("") file_.name = name root = parse( tables.FileColumn(text="Download").render(value=file_, record=None)) assert root.tag == "span" assert root.attrib == {"title": name, "class": ""} assert root.text == "Download"
def setUpTestData(cls): cls.request = RequestFactory().get("/") cls.request.user = ( AnonymousUser() ) # technically, Anonymous users cannot access the admin fake_file = ContentFile("Test") fake_file.name = "test.mp3" cls.audio = Media.objects.create( title="Test audio", duration=1000, file=fake_file, type="audio" ) fake_file = ContentFile("Test") fake_file.name = "test.mp4" cls.video = Media.objects.create( title="Test video", duration=1024, file=fake_file, type="video" ) # a MediaChooserPanel class that works on BlogStreamPage's 'video' field cls.edit_handler = ObjectList([MediaChooserPanel("featured_media")]).bind_to( model=BlogStreamPage, request=cls.request ) cls.my_media_chooser_panel = cls.edit_handler.children[0] # build a form class containing the fields that MyPageChooserPanel wants cls.MediaChooserForm = cls.edit_handler.get_form_class() root_page = Page.objects.first() cls.test_instance = BlogStreamPage( title="Post", slug="post", author="Joe Bloggs", date="1984-01-01", featured_media=cls.video, ) root_page.add_child(instance=cls.test_instance) cls.form = cls.MediaChooserForm(instance=cls.test_instance) cls.media_chooser_panel = cls.my_media_chooser_panel.bind_to( instance=cls.test_instance, form=cls.form )
def mark_as_approved(self): speakermodel = apps.get_model('program', 'speaker') speakerproposalmodel = apps.get_model('program', 'speakerproposal') speaker = speakermodel() speaker.camp = self.camp speaker.name = self.name speaker.biography = self.biography if self.picture_small and self.picture_large: temp = ContentFile(self.picture_small.read()) temp.name = os.path.basename(self.picture_small.name) speaker.picture_small = temp temp = ContentFile(self.picture_large.read()) temp.name = os.path.basename(self.picture_large.name) speaker.picture_large = temp speaker.proposal = self speaker.save() self.proposal_status = speakerproposalmodel.PROPOSAL_APPROVED self.save()
def save(self, *args, **kwargs): grant_id = self.validated_data['grant_id'] path = 'grant_id-{}.jpg'.format(grant_id) full_path = os.path.join(MEDIA_ROOT, path) raw_image = self.validated_data['image'] image_file = ContentFile(base64.b64decode(raw_image)) image_file.name = path grant_image = models.GrantImage.objects.create(grant_id=grant_id, image=image_file) client.upload_file(full_path, 'trevi-grants', path) return grant_image
def test_folders_and_files_paths_changing_then_replace(self): # Create nested folders n0 = self.root_folder.addNewFolder('n0') # /n0 self.assertEqual(n0.full_name, '/n0') n1 = n0.addNewFolder('n1') # /n0/n1 self.assertEqual(n1.full_name, '/n0/n1') n2 = n1.addNewFolder('n2') # /n0/n1/n2 self.assertEqual(n2.full_name, '/n0/n1/n2') n3 = n2.addNewFolder('n3') # /n0/n1/n2/n3 self.assertEqual(n3.full_name, '/n0/n1/n2/n3') f3 = ContentFile(b"dummy string f3") f3.name = 'f3' file_3 = File.objects.createFile(f3, n3, self.user) self.assertEqual(file_3.full_name, '/n0/n1/n2/n3/f3') n3_1 = n2.addNewFolder('n3_1') # /n0/n1/n2/n3_1 self.assertEqual(n3_1.full_name, '/n0/n1/n2/n3_1') n3_2 = n2.addNewFolder('n3_2') # /n0/n1/n2/n3_2 self.assertEqual(n3_2.full_name, '/n0/n1/n2/n3_2') f3_2 = ContentFile(b"dummy string f3_2") f3_2.name = 'f3_2' file_3_2 = File.objects.createFile(f3_2, n3_2, self.user) self.assertEqual(file_3_2.full_name, '/n0/n1/n2/n3_2/f3_2') n1.replace(self.root_folder, folders=[n2]) n2 = Folder.objects.get(pk=n2.pk) self.assertEqual(n2.full_name, '/n2') n3 = Folder.objects.get(pk=n3.pk) self.assertEqual(n3.full_name, '/n2/n3') file_3 = File.objects.get(pk=file_3.pk) self.assertEqual(file_3.full_name, '/n2/n3/f3') n3_1 = Folder.objects.get(pk=n3_1.pk) self.assertEqual(n3_1.full_name, '/n2/n3_1') n3_2 = Folder.objects.get(pk=n3_2.pk) self.assertEqual(n3_2.full_name, '/n2/n3_2') file_3_2 = File.objects.get(pk=file_3_2.pk) self.assertEqual(file_3_2.full_name, '/n2/n3_2/f3_2')
def setUp(self): self.login() # Build a fake file fake_file = ContentFile(b("A boring example song")) fake_file.name = 'song.mp3' # Create a media to edit self.media = models.Media.objects.create(title="Test media", file=fake_file, duration=100)
def _checkin_decomposed_step(self, doc, node): # just add a new revision with the same content df = self._get_step_docfile(node) fake_file = ContentFile(df.file.read()) fake_file.name = df.filename # no thumbnail doc.checkin(df, fake_file, True, False) self.added_files.append(df) self._files.append(df.file.path) if self._lock: doc.lock(df)
def test_valid_dump_import_with_enough_membership_public_project_slots_multiple_projects(client, settings): settings.CELERY_ENABLED = False user = f.UserFactory.create(max_members_public_projects=10) project = f.ProjectFactory.create(owner=user) f.MembershipFactory.create(project=project) f.MembershipFactory.create(project=project) f.MembershipFactory.create(project=project) f.MembershipFactory.create(project=project) f.MembershipFactory.create(project=project) client.login(user) url = reverse("importer-load-dump") data = ContentFile(bytes(json.dumps({ "slug": "project-without-memberships-slots", "name": "Valid project", "description": "Valid project desc", "is_private": False, "roles": [{"name": "Role"}], "memberships": [ { "email": "*****@*****.**", "role": "Role", }, { "email": "*****@*****.**", "role": "Role", }, { "email": "*****@*****.**", "role": "Role", }, { "email": "*****@*****.**", "role": "Role", }, { "email": "*****@*****.**", "role": "Role", }, { "email": "*****@*****.**", "role": "Role", } ] }), "utf-8")) data.name = "test" response = client.post(url, {'dump': data}) assert response.status_code == 201 response_data = response.data assert "id" in response_data assert response_data["name"] == "Valid project"
def get_plugin_descriptor_file(args): """ Get the plugin descriptor file from the arguments. """ if args.descriptorfile: f = args.descriptorfile else: app_repr = json.loads(args.descriptorstring) f = ContentFile(json.dumps(app_repr).encode()) f.name = args.name + '.json' return f
def fetch_youtube_audio(source_file, artist, title, link): """ Task that uses youtubedl to extract the audio from a YouTube link. :param source_file: SourceFile model :param artist: Track artist :param title: Track title :param link: YouTube link """ fetch_task = source_file.youtube_fetch_task # Mark as in progress fetch_task.status = YTAudioDownloadTask.Status.IN_PROGRESS fetch_task.save() try: # Get paths directory = os.path.join(settings.MEDIA_ROOT, settings.UPLOAD_DIR, str(source_file.id)) filename = slugify(artist + ' - ' + title) + get_file_ext(link) rel_media_path = os.path.join(settings.UPLOAD_DIR, str(fetch_task.id), filename) rel_path = os.path.join(settings.MEDIA_ROOT, rel_media_path) pathlib.Path(directory).mkdir(parents=True, exist_ok=True) # Start download download_audio(link, rel_path) is_local = settings.DEFAULT_FILE_STORAGE == 'django.core.files.storage.FileSystemStorage' # Check file exists if os.path.exists(rel_path): fetch_task.status = YTAudioDownloadTask.Status.DONE if is_local: # File is already on local filesystem source_file.file.name = rel_media_path else: # Need to copy local file to S3/Azure Blob/etc. raw_file = open(rel_path, 'rb') content_file = ContentFile(raw_file.read()) content_file.name = filename source_file.file = content_file rel_dir_path = os.path.join(settings.MEDIA_ROOT, settings.UPLOAD_DIR, str(source_file.id)) # Remove local file os.remove(rel_path) # Remove empty directory os.rmdir(rel_dir_path) fetch_task.save() source_file.save() else: raise Exception('Error writing to file') except Exception as error: print(error) fetch_task.status = YTAudioDownloadTask.Status.ERROR fetch_task.error = str(error) fetch_task.save() raise error
def test_snapshot_delete1(self): file = ContentFile(b"123") file.name = "file.jpg" snapshot = models.Snapshot.objects.create(collection=self.collection, file=file) storage = snapshot.file.storage filepath = snapshot.file.name url = reverse('snapshot-detail', args=[snapshot.id]) response = self.client.delete(url) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertFalse(storage.exists(filepath))
def test_readerxml_upload_nonxmlfile_post(self): """ 'upload' POST request of the readerxml page, with an invalid XML file should get an error message """ url = reverse('readerxml') faked_file = ContentFile('hello world') faked_file.name = 'test.xml' response = self.client.post(url, {'upload': 'Upload', 'xmlfile': faked_file}) self.assertEqual(response.status_code, 200) self.assertEqual(response.context['message'], 'The uploaded file is not valid XML')
def create_custom_cover_file(ascii_str: str, name: str): """ Creates a file containing uploaded cover art for a custom game. @param ascii_str: A string representing the ASCII decoding of the image file. @param name: The name to give the file. @return: A ContentFile object containing the uploaded cover art. """ file = ContentFile(base64.b64decode(ascii_str)) file.name = name return file
def save(self, *args, **kwargs): saved_object = self.get_saved_object() if saved_object is not None: if self.generated_name != saved_object.generated_name: if not self.file_deleted: old_file = saved_object.saved_file new_file = ContentFile(old_file.read()) new_file.name = old_file.name old_file.delete(False) self.saved_file = new_file super().save(*args, **kwargs)
def save_dataproduct(obj, filepath, filetype, filename=None, content=None, force=False): if not filename: filename = Path(filepath).name try: dp = DataProduct.objects.get(filetype=filetype, product__endswith=filename) except DataProduct.DoesNotExist: dp = DataProduct() if dp.update is False and force is False: return dp.content_object = obj dp.filetype = filetype if force is True: dp.update = False mode = 'rb' if not content and not filepath: return predicted_path = os.path.join('products', filename) if default_storage.exists(predicted_path): default_storage.delete(predicted_path) if content: file_obj = ContentFile(content.encode('utf-8')) file_obj.name = filename dp.product = file_obj dp.created = datetime.utcnow() dp.save() return try: with default_storage.open(filepath, mode) as f: file_obj = File(f) file_obj.name = filename dp.product = file_obj dp.created = datetime.utcnow() dp.save() except (SuspiciousFileOperation, SuspiciousOperation): with open(filepath, mode) as f: file_obj = File(f) file_obj.name = filename dp.product = file_obj dp.created = datetime.utcnow() dp.save() return
def test_sources_unknown_type(self): fake_file = ContentFile(b("A boring example movie")) fake_file.name = "movie" media = Media() media.file = File(fake_file) self.assertEqual( media.sources, [{ "src": "/media/movie", "type": "application/octet-stream", }], )
def make_media(): fake_file = ContentFile(b("A boring example song")) fake_file.name = 'song.mp3' for i in range(50): media = models.Media( title="Test " + str(i), duration=100 + i, file=fake_file, type='audio', ) media.save()
def post(self, request): certificate = Certifier().sign(request.POST['subject'], request.FILES['public_key']) cert_file = ContentFile( certificate.public_bytes(encoding=serialization.Encoding.PEM)) public_key_file = ContentFile(certificate.public_key().public_bytes( encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.PKCS1)) cert_file.name = f'certificate_{str(uuid.uuid4())}.pem' public_key_file.name = f'public_key_{request.POST["subject"]}_{str(uuid.uuid4())}.pem' cert = Certificate.objects.create( serial_number=certificate.serial_number, issuer=certificate.issuer.get_attributes_for_oid( NameOID.COMMON_NAME)[0].value, subject=certificate.subject.get_attributes_for_oid( NameOID.COMMON_NAME)[0].value, subject_public_key=public_key_file, certificate=cert_file) return HttpResponse(json.dumps(CertificateSerializer(cert).data), content_type='application/json')
def test_file_save_without_name(self): """ File storage extracts the filename from the content object if no name is given explicitly. """ self.assertFalse(self.storage.exists('test.file')) f = ContentFile('custom contents') f.name = 'test.file' storage_f_name = self.storage.save(None, f) self.assertTrue(self.storage.exists(f.name)) self.storage.delete(storage_f_name)
def test_create_photo(self): """Test for photo creation.""" os.system(f'mkdir {os.path.join(settings.BASE_DIR, "test_stuff")}') user = User.objects.first() self.client.force_login(user) with open(os.path.join(settings.BASE_DIR, 'imagersite/static/camera.png'), 'rb') as f: image = ContentFile(f.read()) image.name = 'test.png' response = self.client.post('/images/photos/add/', {'albums': [Album.objects.first().id], 'title': 'stuff', 'description': 'stuff', 'image': image, 'published': 'PRIVATE'}) self.assertEqual(response.status_code, 302) os.system(f'rm -rf {os.path.join(settings.BASE_DIR, "test_stuff")}')
def test_readerxml_upload_xmlfile_post(self): """ 'upload' POST request of the readerxml page, with a valid XML file should upload the file and report success """ url = reverse('readerxml') faked_file = ContentFile('<a><b></b></a>') faked_file.name = 'test.xml' response = self.client.post(url, {'upload': 'Upload', 'xmlfile': faked_file}) self.assertEqual(response.status_code, 200) self.assertEqual(response.context['message'], 'File uploaded') os.remove(os.path.join(settings.ESTORIA_LOCATION, 'readerXML/test.xml'))
def create_export_file(name, ext, description, content): xlsx_file = ContentFile(content) xlsx_file.name = f"{name}.{ext}" existing_file = ExportedFile.objects.filter(name=name).first() if existing_file is not None: existing_file.exported_file.delete() existing_file.delete() ExportedFile.objects.create(name=name, description=description, exported_file=xlsx_file)