Ejemplo n.º 1
0
def upload(request):
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            if request.POST['url_file']:
                temp_doc = ContentFile(requests.get(request.POST['url_file']).content)
                newdoc = Document(
                    docfile=ContentFile(temp_doc.read(), hashlib.md5(temp_doc.read()).hexdigest() + '.png'))
                newdoc.save()
                return HttpResponseRedirect(reverse('list'))
            elif request.FILES['docfile']:
                temp_doc = request.FILES['docfile']
                _, extension = str(temp_doc).split('.')
                newdoc = Document(
                    docfile=ContentFile(temp_doc.read(),
                                        hashlib.md5(temp_doc.read()).hexdigest() + f'.{extension}'))
                newdoc.save()
                return HttpResponseRedirect(reverse('list'))
            else:
                form = DocumentForm()
                return render(request, 'upload_image.html', {'form': form})
        else:
            form = DocumentForm()
            return render(request, 'upload_image.html', {'form': form})

    else:
        form = DocumentForm()
        return render(request, 'upload_image.html', {'form': form})
Ejemplo n.º 2
0
    def save(self, *args, **kwargs):
        # Update the Project's URIs
        docutils_settings = getattr(settings, "RESTRUCTUREDTEXT_FILTER_SETTINGS", {})

        docutils_settings.update({"warning_stream": os.devnull})

        try:
            html_string = publish_string(source=smart_str(self.description), writer_name="html4css1", settings_overrides=docutils_settings)
            if html_string.strip():
                html = lxml.html.fromstring(html_string)

                for link in html.xpath("//a/@href"):
                    try:
                        if any(urlparse.urlparse(link)[:5]):
                            PackageURI.objects.get_or_create(package=self.package, uri=link)
                    except ValueError:
                        pass
        except Exception:
            # @@@ We Swallow Exceptions here, but it's the best way that I can think of atm.
            pass

        # @@@ Temporary Code to  Migrate Files to The New Location
        for f in self.files.all():
            dirs = f.file.name.split("/")
            if len(dirs) > 1:
                if dirs[0] == "packages":
                    cf = ContentFile(f.file.read())
                    if "sha256$" + hashlib.sha256(cf.read()).hexdigest().lower() == f.digest:
                        f.file.delete()
                        f.file.save(f.filename, cf)
                    else:
                        print "sha256$" + hashlib.sha256(cf.read()).hexdigest().lower(), "did not match", f.digest

        return super(Release, self).save(*args, **kwargs)
Ejemplo n.º 3
0
def create_base64_closing(report: Report):
    if report.pdf_report_base64 is None or report.pdf_report_base64 == "":
        pdf = generate_finish_pdf(report)
        file = ContentFile(pdf)
        encode_string = base64.b64encode(file.read())
        report.pdf_report_base64 = encode_string.decode('ascii')
        report.save()
Ejemplo n.º 4
0
	def create_metadata_file_from_output(self, incoming_obj):
		# here we need to handle all the possible returned file types that the scan workers could kick out
		# there is only one right now, the picklable file sample
		f = None
		if isinstance(incoming_obj, PickleableFileSample):
			f = ContentFile(incoming_obj.all_content)

		# todo what else would it be? should we raise an exception..?

		if not f:
			raise CantConvertMetadataFileException

		# important or hashing will be all whacked up
		f.seek(0)
		filehash = self.model.filename_hasher(f.read())

		try:
			a = self.get(sha256=filehash)
		except ObjectDoesNotExist:
			a = self.model(sha256=filehash)
			a.filename = incoming_obj.original_filename
			f.seek(0)
			a.file.save(filehash, f)
			a.save()

		return a
Ejemplo n.º 5
0
 def test_compress_content_len(self):
     """
     Test that file returned by _compress_content() is readable.
     """
     content = ContentFile("I should be gzip'd")
     content = self.storage._compress_content(content)
     self.assertTrue(len(content.read()) > 0)
Ejemplo n.º 6
0
def generate_pdf():
    image_data = "R0lGODlhAQABAIABAP8AAP///yH5BAEAAAEALAAAAAABAAEAAAICRAEAOw=="
    image_data_b64 = b64decode(image_data)
    image_file = ContentFile(image_data_b64, "one.PDF")
    return SimpleUploadedFile(image_file.name,
                              image_file.read(),
                              content_type="image/pdf")
Ejemplo n.º 7
0
def uploadProductImage(request):


    try:
        if request.FILES.has_key("photo") and request.POST.has_key("productId"):
            product = Products.objects.get(pk=request.POST.get("productId"))
            print product.id

            _file = ContentFile(request.FILES['photo'].read())
            image = ProductImage()
            image.image.save(request.FILES['photo'].name, _file)
            thumb = importImg()
            x = thumb.handleImage(request.FILES['photo'])
            x = ContentFile(x.read())
            image.thumb.save(request.FILES['photo'].name, x)


            product.image = image
            product.save()
        jsonObj = simplejson.dumps({'success': True, 'image': product.image.thumb.url})
        return HttpResponse(jsonObj)

    except Exception, err:
        print err
        return HttpResponse(simplejson.dumps({'success': False}))
Ejemplo n.º 8
0
    def create_metadata_file_from_output(self, incoming_obj):
        # here we need to handle all the possible returned file types that the scan workers could kick out
        # there is only one right now, the picklable file sample
        f = None
        if isinstance(incoming_obj, PickleableFileSample):
            f = ContentFile(incoming_obj.all_content)

        # todo what else would it be? should we raise an exception..?

        if not f:
            raise CantConvertMetadataFileException

        # important or hashing will be all whacked up
        f.seek(0)
        filehash = self.model.filename_hasher(f.read())

        try:
            a = self.get(sha256=filehash)
        except ObjectDoesNotExist:
            a = self.model(sha256=filehash)
            a.filename = incoming_obj.original_filename
            f.seek(0)
            a.file.save(filehash, f)
            a.save()

        return a
Ejemplo n.º 9
0
    def to_internal_value(self, base64_data):
        if base64_data is None:
            data = base64_data
        # Check if this is a base64 string
        elif isinstance(base64_data, basestring):
            # Try to decode the file. Return validation error if it fails.
            try:
                decoded_file = base64.b64decode(base64_data)
            except TypeError:
                raise serializers.ValidationError(
                    _(u"Please upload a valid image."))

            # Generate file name:
            file_name = str(
                uuid.uuid4())[:12]  # 12 characters are more than enough.
            # Get the file name extension:
            file_extension = self.get_file_extension(file_name, decoded_file)
            self.check_file_extension(file_extension)
            complete_file_name = file_name + "." + file_extension
            data = ContentFile(decoded_file, name=complete_file_name)
        else:
            data = base64_data
            file_extension = self.get_file_extension(data.name, data.read())
            self.check_file_extension(file_extension)
            data.seek(0)

        return super(Base64ImageField, self).to_internal_value(data)
Ejemplo n.º 10
0
    def from_native(self, base64_data):
        if base64_data is None:
            data = base64_data
        # Check if this is a base64 string
        elif isinstance(base64_data, basestring):
            # Try to decode the file. Return validation error if it fails.
            try:
                decoded_file = base64.b64decode(base64_data)
            except TypeError:
                raise serializers.ValidationError(_(u"Please upload a valid image."))

            # Generate file name:
            file_name = str(uuid.uuid4())[:12] # 12 characters are more than enough.
            # Get the file name extension:
            file_extension = self.get_file_extension(file_name, decoded_file)
            self.check_file_extension(file_extension)
            complete_file_name = file_name + "." + file_extension
            data = ContentFile(decoded_file, name=complete_file_name)
        else:
            data = base64_data
            file_extension = self.get_file_extension(data.name, data.read())
            self.check_file_extension(file_extension)
            data.seek(0)

        return super(Base64ImageField, self).from_native(data)
def test_put_twice():
    f = ContentFile(b'This is test content')
    filename1 = default_storage.save('twice.txt', f)
    filename2 = default_storage.save('twice.txt', f)
    assert filename1 == filename2
    with default_storage.open(filename1) as f:
        assert f.read() == b'This is test content'
Ejemplo n.º 12
0
def change_headshot(request, attr):
    if request.POST.get("action") == "delete":
        user = get_object_or_404(get_user_model(), pk=request.POST['user_id'])
        setattr(user, attr, "")
        user.save()
        return JsonResponse({})
    user = get_object_or_404(get_user_model(), pk=request.POST['user_id'])
    f = request.FILES.get(
        attr, None)  # if somehow they upload the uncompressed image
    fname = "%s-%s.jpg" % (attr, user.id)
    if request.POST.get('blob', None):
        f = ContentFile(request.POST['blob'].split(",")[1].decode('base64'))
    if attr == 'headshot':
        user.headshot.save(fname, f)
        user.save()
        attr = 'headshot_url'
    elif attr == 'id_photo':
        admin_url = "https://txrxlabs.org/admin/user/user/%s" % user.id
        msg = EmailMessage(
            user.username, "\n".join([
                str(s) for s in [
                    user.get_full_name(), user.email, user.paypal_email,
                    admin_url
                ]
            ]), settings.DEFAULT_FROM_EMAIL, [settings.ID_PHOTO_EMAIL])
        msg.attach(fname, f.read(), 'image/jpg')
        msg.send()
        user.id_photo_date = datetime.date.today()
        user.save()
        attr = 'id_photo_date'
    return JsonResponse({'done': str(getattr(user, attr))})
Ejemplo n.º 13
0
    def test_basic_actions(self):
        storage = CloudStorage()
        name = u'tmp.ąćęłńóśźż.马铃薯.zip'

        f = ContentFile('content', name='my_file')
        filename = storage.save(name, f)
        self.assertIsInstance(filename, six.string_types)
        self.assertTrue(filename.endswith(name))

        self.assertTrue(storage.exists(filename))
        self.assertEqual(storage.size(filename), len('content'))
        url = storage.url(filename)
        self.assertIsInstance(url, six.string_types)
        self.assertNotEqual(url, '')

        abs_url = six.moves.urllib.parse.urlunparse(
            ('http', os.environ['HTTP_HOST'], url, None, None, None)
        )
        response = urlfetch.fetch(abs_url)
        self.assertEqual(response.status_code, six.moves.http_client.OK)
        self.assertEqual(response.content, 'content')

        f = storage.open(filename)
        self.assertIsInstance(f, File)
        self.assertEqual(f.read(), 'content')

        # Delete it
        storage.delete(filename)
        self.assertFalse(storage.exists(filename))
Ejemplo n.º 14
0
    def put(self):
        agent = self.__get_agent(create=True)
        try:
            state = ContentFile(self.state.read())
        except:
            try:
                state = ContentFile(self.state)
            except:
                state = ContentFile(str(self.state))

        if self.registrationId:
            p, created = models.activity_state.objects.get_or_create(
                state_id=self.stateId,
                agent=agent,
                activity=self.activity,
                registration_id=self.registrationId)
        else:
            p, created = models.activity_state.objects.get_or_create(
                state_id=self.stateId, agent=agent, activity=self.activity)
        if not created:
            etag.check_preconditions(self.req_dict, p)
            p.state.delete()  # remove old state file
        p.content_type = self.content_type
        p.etag = etag.create_tag(state.read())
        if self.updated:
            p.updated = self.updated
        state.seek(0)
        if created:
            p.save()

        fn = "%s_%s_%s" % (p.agent_id, p.activity_id,
                           self.req_dict.get('filename', p.id))
        p.state.save(fn, state)
Ejemplo n.º 15
0
    def test_basic_actions(self):
        storage = CloudStorage()

        f = ContentFile('content', name='my_file')
        filename = storage.save('tmp', f)
        self.assertIsInstance(filename, basestring)
        self.assertTrue(filename.endswith('tmp'))

        self.assertTrue(storage.exists(filename))
        self.assertEqual(storage.size(filename), len('content'))
        url = storage.url(filename)
        self.assertIsInstance(url, basestring)
        self.assertNotEqual(url, '')

        abs_url = urlparse.urlunparse(
            ('http', os.environ['HTTP_HOST'], url, None, None, None)
        )
        response = urlfetch.fetch(abs_url)
        self.assertEqual(response.status_code, httplib.OK)
        self.assertEqual(response.content, 'content')

        f = storage.open(filename)
        self.assertIsInstance(f, File)
        self.assertEqual(f.read(), 'content')

        # Delete it
        storage.delete(filename)
        self.assertFalse(storage.exists(filename))
Ejemplo n.º 16
0
    def test_basic_actions(self):

        storage = BlobstoreStorage()

        # Save a new file
        f = ContentFile('content', name='my_file')
        filename = storage.save('tmp', f)

        self.assertIsInstance(filename, basestring)
        self.assertTrue(filename.endswith('tmp'))

        # Check .exists(), .size() and .url()
        self.assertTrue(storage.exists(filename))
        self.assertEqual(storage.size(filename), len('content'))
        url = storage.url(filename)
        self.assertIsInstance(url, basestring)
        self.assertNotEqual(url, '')

        # Check URL can be fetched
        abs_url = urlparse.urlunparse(
            ('http', os.environ['HTTP_HOST'], url, None, None, None))
        response = urlfetch.fetch(abs_url)
        self.assertEqual(response.status_code, httplib.OK)
        self.assertEqual(response.content, 'content')

        # Open it, read it
        # NOTE: Blobstore doesn’t support updating existing files.
        f = storage.open(filename)
        self.assertIsInstance(f, File)
        self.assertEqual(f.read(), 'content')

        # Delete it
        storage.delete(filename)
        self.assertFalse(storage.exists(filename))
Ejemplo n.º 17
0
    def test_basic_actions(self):
        storage = CloudStorage()
        name = u"tmp.ąćęłńóśźż.马铃薯.zip"

        f = ContentFile("content", name="my_file")
        filename = storage.save(name, f)
        self.assertIsInstance(filename, basestring)
        self.assertTrue(filename.endswith(name))

        self.assertTrue(storage.exists(filename))
        self.assertEqual(storage.size(filename), len("content"))
        url = storage.url(filename)
        self.assertIsInstance(url, basestring)
        self.assertNotEqual(url, "")

        abs_url = urlparse.urlunparse(("http", os.environ["HTTP_HOST"], url, None, None, None))
        response = urlfetch.fetch(abs_url)
        self.assertEqual(response.status_code, httplib.OK)
        self.assertEqual(response.content, "content")

        f = storage.open(filename)
        self.assertIsInstance(f, File)
        self.assertEqual(f.read(), "content")

        # Delete it
        storage.delete(filename)
        self.assertFalse(storage.exists(filename))
Ejemplo n.º 18
0
    def partial_update(self, request, path, pk=None):
        """
        Process a chunk that will be appended to an existing upload.
        """
        _, repository = self.get_dr_push(request, path)
        chunk = request.META["wsgi.input"]
        if "Content-Range" in request.headers or "digest" not in request.query_params:
            whole = False
        else:
            whole = True

        if whole:
            start = 0
        else:
            content_range = request.META.get("HTTP_CONTENT_RANGE", "")
            match = self.content_range_pattern.match(content_range)
            start = 0 if not match else int(match.group("start"))

        upload = get_object_or_404(models.Upload, repository=repository, pk=pk)

        chunk = ContentFile(chunk.read())
        with transaction.atomic():
            if upload.size != start:
                raise Exception

            upload.append(chunk, upload.size)
            upload.size += chunk.size
            upload.save()

        return UploadResponse(upload=upload, path=path, content_length=chunk.size, request=request)
Ejemplo n.º 19
0
    def test_basic_actions(self):

        storage = BlobstoreStorage()

        # Save a new file
        f = ContentFile("content", name="my_file")
        filename = storage.save("tmp", f)

        self.assertIsInstance(filename, basestring)
        self.assertTrue(filename.endswith("tmp"))

        # Check .exists(), .size() and .url()
        self.assertTrue(storage.exists(filename))
        self.assertEqual(storage.size(filename), len("content"))
        url = storage.url(filename)
        self.assertIsInstance(url, basestring)
        self.assertNotEqual(url, "")

        # Check URL can be fetched
        abs_url = urlparse.urlunparse(("http", os.environ["HTTP_HOST"], url, None, None, None))
        response = urlfetch.fetch(abs_url)
        self.assertEqual(response.status_code, httplib.OK)
        self.assertEqual(response.content, "content")

        # Open it, read it
        # NOTE: Blobstore doesn’t support updating existing files.
        f = storage.open(filename)
        self.assertIsInstance(f, File)
        self.assertEqual(f.read(), "content")

        # Delete it
        storage.delete(filename)
        self.assertFalse(storage.exists(filename))
Ejemplo n.º 20
0
    def to_internal_value(self, data):
        if isinstance(data, UploadedFile):
            data = ContentFile(data.read(), name=data.name)
        if data == 'undefined':
            return None

        return super(UploadedImageField, self).to_internal_value(data)
Ejemplo n.º 21
0
    def test_basic_actions(self):
        content = b'content'
        storage = CloudStorage()
        name = u'tmp.ąćęłńóśźż.马铃薯.zip'

        f = ContentFile(content, name='my_file')
        filename = storage.save(name, f)
        self.assertIsInstance(filename, six.string_types)
        self.assertTrue(filename.endswith(name))

        self.assertTrue(storage.exists(filename))
        self.assertEqual(storage.size(filename), len(content))
        url = storage.url(filename)
        self.assertIsInstance(url, six.string_types)
        self.assertNotEqual(url, '')

        response = requests.get(url)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content, content)

        f = storage.open(filename)
        self.assertIsInstance(f, File)
        self.assertEqual(f.read(), content)

        # Delete it
        storage.delete(filename)
        self.assertFalse(storage.exists(filename))
Ejemplo n.º 22
0
 def test_compress_content_len(self):
     """
     Test that file returned by _compress_content() is readable.
     """
     content = ContentFile("I should be gzip'd")
     content = self.storage._compress_content(content)
     self.assertTrue(len(content.read()) > 0)
Ejemplo n.º 23
0
def download(request):
    doc = dom.Document()
    root = doc.createElement("diarys")
    doc.appendChild(root)

    diaries = request.user.diary_set.all()

    for diary in diaries:
        blognode = doc.createElement("diary")
        blognode.setAttribute("diaryWeather", diary.weather)
        tools.debug("download diary", diary.weather,
                    weekday(diary.timestamp.weekday()))
        blognode.setAttribute("diaryWeekDay",
                              weekday(diary.timestamp.weekday()))
        blognode.setAttribute("diaryDate", diary.timestamp.strftime("%Y%m%d"))
        bv = doc.createTextNode(diary.body)
        blognode.appendChild(bv)

        root.appendChild(blognode)
    xmlstr = doc.toprettyxml("\t", "\n", 'utf-8')
    f = ContentFile(xmlstr)

    response = HttpResponse(f.read(), content_type='xml')
    response['Content-Disposition'] = \
        'attachment; filename=%s' % "diary"+datetime.now().strftime("%Y%m%d")+".xml"
    return response
Ejemplo n.º 24
0
    def test_basic_actions(self):

        storage = BlobstoreStorage()

        # Save a new file
        f = ContentFile('content', name='my_file')
        filename = storage.save('tmp', f)

        self.assertIsInstance(filename, six.string_types)
        self.assertTrue(filename.endswith('tmp'))

        # Check .exists(), .size() and .url()
        self.assertTrue(storage.exists(filename))
        self.assertEqual(storage.size(filename), len('content'))
        url = storage.url(filename)
        self.assertIsInstance(url, six.string_types)
        self.assertNotEqual(url, '')

        # Check URL can be fetched
        abs_url = six.moves.urllib.parse.urlunparse(
            ('http', os.environ['HTTP_HOST'], url, None, None, None)
        )
        response = urlfetch.fetch(abs_url)
        self.assertEqual(response.status_code, six.moves.http_client.OK)
        self.assertEqual(response.content, 'content')

        # Open it, read it
        # NOTE: Blobstore doesn’t support updating existing files.
        f = storage.open(filename)
        self.assertIsInstance(f, File)
        self.assertEqual(f.read(), 'content')

        # Delete it
        storage.delete(filename)
        self.assertFalse(storage.exists(filename))
Ejemplo n.º 25
0
    def test_basic_actions(self):
        storage = CloudStorage()
        name = u'tmp.ąćęłńóśźż.马铃薯.zip'

        f = ContentFile('content', name='my_file')
        filename = storage.save(name, f)
        self.assertIsInstance(filename, basestring)
        self.assertTrue(filename.endswith(name))

        self.assertTrue(storage.exists(filename))
        self.assertEqual(storage.size(filename), len('content'))
        url = storage.url(filename)
        self.assertIsInstance(url, basestring)
        self.assertNotEqual(url, '')

        abs_url = urlparse.urlunparse(
            ('http', os.environ['HTTP_HOST'], url, None, None, None))
        response = urlfetch.fetch(abs_url)
        self.assertEqual(response.status_code, httplib.OK)
        self.assertEqual(response.content, 'content')

        f = storage.open(filename)
        self.assertIsInstance(f, File)
        self.assertEqual(f.read(), 'content')

        # Delete it
        storage.delete(filename)
        self.assertFalse(storage.exists(filename))
Ejemplo n.º 26
0
def get_image(url, timeout=10):
    """wrapper for requesting an image"""
    raise_not_valid_url(url)
    try:
        resp = requests.get(
            url,
            headers={
                "User-Agent": settings.USER_AGENT,
            },
            timeout=timeout,
        )
    except RequestException as err:
        logger.info(err)
        return None, None

    if not resp.ok:
        return None, None

    image_content = ContentFile(resp.content)
    extension = imghdr.what(None, image_content.read())
    if not extension:
        logger.info("File requested was not an image: %s", url)
        return None, None

    return image_content, extension
Ejemplo n.º 27
0
def files(request, id):
    """
    Returns file.  Allows us to handle privacy.

    If default storage is remote:
        We can get data from remote location, convert to file
        object and return a file response.
    """
    import os
    import mimetypes
    from django.core.files.base import ContentFile
    from tendenci.apps.forms_builder.forms.models import FieldEntry

    field = get_object_or_404(FieldEntry, pk=id)
    form = field.field.form

    base_name = os.path.basename(field.value)
    mime_type = mimetypes.guess_type(base_name)[0]

    if not has_perm(request.user, 'forms.change_form', form):
        raise Http403

    if not mime_type:
        raise Http404

    if not default_storage.exists(field.value):
        raise Http404

    data = default_storage.open(field.value).read()
    f = ContentFile(data)

    EventLog.objects.log()
    response = HttpResponse(f.read(), content_type=mime_type)
    response['Content-Disposition'] = 'filename="%s"' % base_name
    return response
Ejemplo n.º 28
0
    def put(self):
        agent = self.__get_agent(create=True)
        try:
            state = ContentFile(self.state.read())
        except:
            try:
                state = ContentFile(self.state)
            except:
                state = ContentFile(str(self.state))

        if self.registrationId:
            p,created = models.activity_state.objects.get_or_create(state_id=self.stateId,agent=agent,activity=self.activity,registration_id=self.registrationId)
        else:
            p,created = models.activity_state.objects.get_or_create(state_id=self.stateId,agent=agent,activity=self.activity)
        if not created:
            etag.check_preconditions(self.req_dict,p)
            p.state.delete() # remove old state file
        p.content_type = self.content_type
        p.etag = etag.create_tag(state.read())
        if self.updated:
            p.updated = self.updated
        state.seek(0)
        if created:
            p.save()

        fn = "%s_%s_%s" % (p.agent_id,p.activity_id, self.req_dict.get('filename', p.id))
        p.state.save(fn, state)
Ejemplo n.º 29
0
def generate_photo_file():
    image_data = b64decode(
        "R0lGODlhAQABAIABAP8AAP///yH5BAEAAAEALAAAAAABAAEAAAICRAEAOw==")
    image_file = ContentFile(image_data, 'one.GIF')
    uploaded_image_file = SimpleUploadedFile(image_file.name,
                                             image_file.read(),
                                             content_type="image/png")
    return uploaded_image_file
Ejemplo n.º 30
0
 def test_compress_content_len(self):
     """
     Test that file returned by _compress_content() is readable.
     """
     if not s3boto.S3BotoStorage.gzip:  # Gzip not available.
         return
     content = ContentFile("I should be gzip'd")
     content = self.storage._compress_content(content)
     self.assertTrue(len(content.read()) > 0)
Ejemplo n.º 31
0
def _upload_file(request):
    """
    Upload file to the server.
    """
    if request.method == "POST":
        folder = request.POST.get("folder")
        fb_uploadurl_re = re.compile(r"^.*(%s)" % reverse("fb_upload"))
        folder = fb_uploadurl_re.sub("", folder)
        if "." in folder:
            return HttpResponseBadRequest("")

        if request.FILES:
            filedata = request.FILES["Filedata"]
            directory = get_directory()

            # Validate file against EXTENSIONS setting.
            if not get_file_type(filedata.name):
                return HttpResponseBadRequest("")

            # PRE UPLOAD SIGNAL
            filebrowser_pre_upload.send(
                sender=request, path=request.POST.get("folder"), file=filedata
            )

            # Try and remove both original and normalised thumb names,
            # in case files were added programmatically outside FB.
            file_path = os.path.join(directory, folder, filedata.name)
            remove_thumbnails(file_path)
            filedata.name = convert_filename(filedata.name)
            file_path = os.path.join(directory, folder, filedata.name)
            remove_thumbnails(file_path)

            if (
                "." in file_path
                and file_path.split(".")[-1].lower() in fb_settings.ESCAPED_EXTENSIONS
            ):
                filedata = ContentFile(escape(filedata.read()), name=filedata.name)

            # HANDLE UPLOAD
            uploadedfile = default_storage.save(file_path, filedata)
            if default_storage.exists(file_path) and file_path != uploadedfile:
                default_storage.move(
                    smart_text(uploadedfile),
                    smart_text(file_path),
                    allow_overwrite=True,
                )

            # POST UPLOAD SIGNAL
            filebrowser_post_upload.send(
                sender=request,
                path=request.POST.get("folder"),
                file=FileObject(smart_text(file_path)),
            )
        get_params = request.POST.get("get_params")
        if get_params:
            return HttpResponseRedirect(reverse("fb_browse") + get_params)
    return HttpResponse("True")
def test_put_and_delete():
    f = ContentFile(b'This is test content')
    filename = default_storage.save('deletion.txt', f)
    assert filename
    with default_storage.open(filename, 'r') as f:
        assert f.read() == 'This is test content'
    default_storage.delete(filename)
    with pytest.raises(HTTPError):
        default_storage.open(filename)
Ejemplo n.º 33
0
 def test_compress_content_len(self):
     """
     Test that file returned by _compress_content() is readable.
     """
     if not s3boto.S3BotoStorage.gzip:  # Gzip not available.
         return
     content = ContentFile("I should be gzip'd")
     content = self.storage._compress_content(content)
     self.assertTrue(len(content.read()) > 0)
Ejemplo n.º 34
0
def _upload_file(request):
    """
    Upload file to the server.
    """
    if request.method == 'POST':
        folder = request.POST.get('folder')
        fb_uploadurl_re = re.compile(r'^.*(%s)' % reverse("fb_upload"))
        folder = fb_uploadurl_re.sub('', folder)
        if ".." in folder:
            return HttpResponseBadRequest("")

        if request.FILES:
            filedata = request.FILES['Filedata']
            directory = get_directory()

            # Validate file against EXTENSIONS setting.
            if not get_file_type(filedata.name):
                return HttpResponseBadRequest("")

            # PRE UPLOAD SIGNAL
            filebrowser_pre_upload.send(sender=request,
                                        path=request.POST.get('folder'),
                                        file=filedata)

            # Try and remove both original and normalised thumb names,
            # in case files were added programmatically outside FB.
            file_path = os.path.join(directory, folder, filedata.name)
            remove_thumbnails(file_path)
            filedata.name = convert_filename(filedata.name)
            # this won't work with windows and s3 - replace the "\"
            file_path = os.path.join(directory, folder,
                                     filedata.name).replace("\\", "/")
            remove_thumbnails(file_path)

            if "." in file_path and file_path.split(
                    ".")[-1].lower() in ESCAPED_EXTENSIONS:
                filedata = ContentFile(escape(filedata.read()),
                                       name=filedata.name)

            # HANDLE UPLOAD
            uploadedfile = default_storage.save(file_path, filedata)
            if default_storage.exists(file_path) and file_path != uploadedfile:
                default_storage.move(smart_text(uploadedfile),
                                     smart_text(file_path),
                                     allow_overwrite=True)

            # POST UPLOAD SIGNAL
            filebrowser_post_upload.send(sender=request,
                                         path=request.POST.get('folder'),
                                         file=FileObject(
                                             smart_text(file_path)))
        get_params = request.POST.get('get_params')
        if get_params:
            return HttpResponseRedirect(reverse('fb_browse') + get_params)
    return HttpResponse('True')
Ejemplo n.º 35
0
    def setUpTestData(cls):
        cls.user = UserFactory()

        image_data = b64decode(
            "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg=="
        )
        image_file = ContentFile(image_data, "fake.png")

        image = InMemoryUploadedFile(
            BytesIO(image_data),
            field_name="tempfile",
            name="fake.png",
            content_type="image/png",
            size=len(image_data),
            charset="utf-8",
        )
        cls.in_memory_image = image

        cls.uploaded_image_file = SimpleUploadedFile(
            image_file.name, image_file.read(), content_type="image/png"
        )

        text_data = b64decode("R2hvc3R3cml0ZXIK")
        text_file = ContentFile(text_data, "fake.txt")

        text = InMemoryUploadedFile(
            BytesIO(text_data),
            field_name="tempfile",
            name="fake.txt",
            content_type="text/html",
            size=len(text_data),
            charset="utf-8",
        )
        cls.in_memory_text = text

        cls.uploaded_text_file = SimpleUploadedFile(
            text_file.name, text_file.read(), content_type="text/html"
        )

        cls.uploaded_mismatched_file = SimpleUploadedFile(
            image_file.name, image_file.read(), content_type="text/html"
        )
Ejemplo n.º 36
0
def get_avatar(backend, user, response, *args, **kwargs):
    if backend.name == 'facebook':
        url = PathUtils.get_oauth_avatar_url(response['id'], provider='facebook')
    elif backend.name == 'github':
        url = response['avatar_url']

    if url:
        temp = ContentFile(requests.get(url).content)

        if temp.read() != user.profile.avatar_image.read():
            user.profile.avatar_image.save('{}.jpg'.format(user.username), temp)
Ejemplo n.º 37
0
    def put_profile(self, request_dict):
        #Parse out profile from request_dict
        try:
            profile = ContentFile(request_dict['profile'].read())
        except:
            try:
                profile = ContentFile(request_dict['profile'])
            except:
                profile = ContentFile(str(request_dict['profile']))

        #Check if activity exists
        try:
            # Always want global version
            activity = models.activity.objects.get(activity_id=request_dict['activityId'],
                global_representation=True)
        except models.activity.DoesNotExist:
            err_msg = 'There is no activity associated with the id: %s' % request_dict['activityId']
            log_message(self.log_dict, err_msg, __name__, self.put_profile.__name__, True)
            update_parent_log_status(self.log_dict, 404)
            raise IDNotFoundError(err_msg)

        user = get_user_from_auth(request_dict.get('auth', None))
        #Get the profile, or if not already created, create one
        p,created = models.activity_profile.objects.get_or_create(profileId=request_dict['profileId'],activity=activity, user=user)
        
        if created:
            log_message(self.log_dict, "Created Activity Profile", __name__, self.put_profile.__name__)
        else:
            #If it already exists delete it
            etag.check_preconditions(request_dict,p, required=True)
            p.profile.delete()
            log_message(self.log_dict, "Retrieved Activity Profile", __name__, self.put_profile.__name__)
        
        #Save profile content type based on incoming content type header and create etag
        p.content_type = request_dict['CONTENT_TYPE']
        p.etag = etag.create_tag(profile.read())
        
        #Set updated
        if request_dict['updated']:
            p.updated = request_dict['updated']
        
        #Go to beginning of file
        profile.seek(0)
        
        #If it didn't exist, save it
        if created:
            p.save()

        #Set filename with the activityID and profileID and save
        fn = "%s_%s" % (p.activity_id,request_dict.get('filename', p.id))
        p.profile.save(fn, profile)

        log_message(self.log_dict, "Saved Activity Profile", __name__, self.put_profile.__name__)
Ejemplo n.º 38
0
 def test_file_methods_pathlib_path(self):
     p = Path('test.file')
     self.assertFalse(self.storage.exists(p))
     f = ContentFile('custom contents')
     f_name = self.storage.save(p, f)
     # Storage basic methods.
     self.assertEqual(self.storage.path(p), os.path.join(self.temp_dir, p))
     self.assertEqual(self.storage.size(p), 15)
     self.assertEqual(self.storage.url(p), self.storage.base_url + f_name)
     with self.storage.open(p) as f:
         self.assertEqual(f.read(), b'custom contents')
     self.addCleanup(self.storage.delete, p)
Ejemplo n.º 39
0
    def store(self, course_id, filename, buff):
        """
        Store the contents of `buff` in a directory determined by hashing
        `course_id`, and name the file `filename`. `buff` can be any file-like
        object, ready to be read from the beginning.
        """
        path = self.path_to(course_id, filename)
        # See https://github.com/boto/boto/issues/2868
        # Boto doesn't play nice with unicod in python3
        if not six.PY2:
            buff = ContentFile(buff.read().encode('utf-8'))

        self.storage.save(path, buff)
Ejemplo n.º 40
0
    def test_file_access_options(self):
        self.assertFalse(self.storage.exists("storage_test"))

        f = ContentFile(b"storage contents")
        self.storage.save("storage_test", f)
        self.assertEqual(self.storage.size("storage_test"), 16)
        self.assertNumQueries(1, self.storage.size, "storage_test")

        f = self.storage.open("storage_test")
        self.assertEqual(f.read(), b"storage contents")

        self.storage.delete("storage_test")
        self.assertFalse(self.storage.exists("storage_test"))
Ejemplo n.º 41
0
    def test_file_access_options(self):
        self.assertFalse(self.storage.exists('storage_test'))

        f = ContentFile(b'storage contents')
        self.storage.save('storage_test', f)
        self.assertEqual(self.storage.size('storage_test'), 16)
        self.assertNumQueries(1, self.storage.size, 'storage_test')

        f = self.storage.open('storage_test')
        self.assertEqual(f.read(), b'storage contents')

        self.storage.delete('storage_test')
        self.assertFalse(self.storage.exists('storage_test'))
Ejemplo n.º 42
0
 def test_do_upload(self):
     url = reverse("fb_do_upload")
     test_file = ContentFile(b"Test File content", name="test-file-upload.txt")
     test_file_path = os.path.join(self.upload_dir, test_file.name)
     self.assertFalse(default_storage.exists(test_file_path))
     response = self.client.post(url, data={"folder": "", "Filedata": test_file})
     self.assertEqual(200, response.status_code)
     self.assertTrue(default_storage.exists(test_file_path))
     with default_storage.open(test_file_path) as uploaded_file:
         test_file.seek(0)
         self.assertEqual(uploaded_file.read(), test_file.read())
     # Cleanup uploaded file
     default_storage.delete(test_file_path)
Ejemplo n.º 43
0
    def generate_image(self):
        """
        Generate QR image and get its binary data.

        Returns
        -------
        bytes
            Binary data of the png image file which can directly be returned to the user
        """
        img = self._generate_image()
        f = ContentFile(b'', name='qr.png')
        img.save(f, 'png')
        f.seek(0)
        return f.read()
Ejemplo n.º 44
0
    def generate_image(self):
        """
        Generate QR image and get its binary data.

        Returns
        -------
        bytes
            Binary data of the png image file which can directly be returned to the user
        """
        img = self._generate_image()
        f = ContentFile(b'', name='qr.png')
        img.save(f, 'png')
        f.seek(0)
        return f.read()
Ejemplo n.º 45
0
def avatar(
    faker: Faker,
    jpeg_image: ContentFile,
):
    """
    JPEG file appropriate for API Client POST method with 'multipart'
    format.
    """

    avatar = io.BytesIO()
    avatar.write(jpeg_image.read())
    avatar.name = faker.file_name(extension='jpeg')
    avatar.seek(0)
    return avatar
Ejemplo n.º 46
0
def tmp_image(request):
	if request.method == 'POST':
		tmp_image_name = ''.join(random.choice(string.lowercase) for i in range(10))
		# request file coming from the webcamjs
		files = request.FILES['webcam']
		# Script that convert the inmemoryupload to a file
		files = ContentFile(files.read())
		x = 'photos/tmp/'+tmp_image_name+'.jpg'
		# script saving to a folder
		path = default_storage.save(x, files)
		scheme = request.scheme
		http_host = request.META['HTTP_HOST']
		return HttpResponse(scheme+"://"+http_host+"/media/"+x)
	else:
		return HttpResponse("No data")
Ejemplo n.º 47
0
def exercicio_de_programacao(request, exercicio, ctx):
    usuario = request.user

    # TODO Usar messages framework ao invés de colocar no contexto (na verdade acho que não está sendo utilizado atualmente)
    msg = ''
    if exercicio is None:
        msg = 'Boa tentativa, mas não vai dar certo!'
    if request.method == 'POST' and exercicio:
        codigo_arquivo = request.FILES.get('codigo_arquivo', None)
        codigo_texto = request.POST.get('codigo_texto', None)
        if codigo_texto and not codigo_arquivo:
            codigo_arquivo = ContentFile(codigo_texto.encode('utf-8'))
        if codigo_arquivo:
            codigo_texto = codigo_arquivo.read()
            submissao = RespostaExProgramacao(exercicio=exercicio,
                                              autor=usuario)

            resultado = executa_codigo(exercicio, codigo_texto)
            submissao.lista_de_falhas = resultado.failure_msgs
            submissao.stack_traces = resultado.stack_traces
            submissao.stdouts = resultado.stdouts
            submissao.resultado = Resultado.OK if resultado.success else Resultado.ERRO
            submissao.save()
            submissao.codigo.save(caminho_submissoes_usuario(submissao, ''),
                                  codigo_arquivo)
            messages.success(request,
                             'mostrar ultimo envio',
                             extra_tags='show_last')
            return redirect('exercicio', c_id=exercicio.id)

    ctx[EXERCICIO] = exercicio
    exercicio_programado = None
    if exercicio:
        exercicio_programado = ctx[EXERCICIOS_PROGRAMADOS].get(exercicio.id)
    ctx[EXERCICIOS_PROGRAMADO] = exercicio_programado
    ctx[RESPOSTAS] = RespostaExProgramacao.objects.por(usuario).filter(
        exercicio=ctx[EXERCICIO]).order_by('-data_submissao')
    ctx['msg'] = msg
    ctx[ULTIMA_SUBMISSAO] = RespostaExProgramacao.objects.por(
        usuario).ultima_submissao(ctx[EXERCICIO])
    ctx[ERROR_COUNTER] = Counter()
    if ctx[RESPOSTAS]:
        ctx[RESPOSTA_ULTIMA_SUBMISSAO] = [ctx[RESPOSTAS][0]]
    else:
        ctx[RESPOSTA_ULTIMA_SUBMISSAO] = []

    return render(request, 'core/exercicio.html', context=ctx)
Ejemplo n.º 48
0
def _upload_file(request):
    """
    Upload file to the server.
    """
    if request.method == "POST":
        folder = request.POST.get("folder")
        fb_uploadurl_re = re.compile(r"^.*(%s)" % reverse("fb_upload"))
        folder = fb_uploadurl_re.sub("", folder)
        if "." in folder:
            return HttpResponseBadRequest("")

        if request.FILES:
            filedata = request.FILES["Filedata"]
            directory = get_directory()

            # Validate file against EXTENSIONS setting.
            if not get_file_type(filedata.name):
                return HttpResponseBadRequest("")

            # PRE UPLOAD SIGNAL
            filebrowser_pre_upload.send(sender=request, path=request.POST.get("folder"), file=filedata)

            # Try and remove both original and normalised thumb names,
            # in case files were added programmatically outside FB.
            file_path = os.path.join(directory, folder, filedata.name)
            remove_thumbnails(file_path)
            filedata.name = convert_filename(filedata.name)
            file_path = os.path.join(directory, folder, filedata.name)
            remove_thumbnails(file_path)

            if "." in file_path and file_path.split(".")[-1].lower() in ESCAPED_EXTENSIONS:
                filedata = ContentFile(escape(filedata.read()), name=filedata.name)

            # HANDLE UPLOAD
            uploadedfile = default_storage.save(file_path, filedata)
            if default_storage.exists(file_path) and file_path != uploadedfile:
                default_storage.move(smart_text(uploadedfile), smart_text(file_path), allow_overwrite=True)

            # POST UPLOAD SIGNAL
            filebrowser_post_upload.send(
                sender=request, path=request.POST.get("folder"), file=FileObject(smart_text(file_path))
            )
        get_params = request.POST.get("get_params")
        if get_params:
            return HttpResponseRedirect(reverse("fb_browse") + get_params)
    return HttpResponse("True")
Ejemplo n.º 49
0
def genProductImage():
    img = ProductImage()
    
    _file = ContentFile(open(STATIC_FILE_PATH + "/test.jpg", 'rb').read())
    image = ProductImage()
    image.image.save("generic", _file)
    
    size = 70, 70
    x = StringIO.StringIO()
    
    im = Image.open(open(STATIC_FILE_PATH + "/test.jpg"))
    im.thumbnail(size, Image.ANTIALIAS)
    im.save(x, format= 'JPEG')
    x.seek(0)
    
    x = ContentFile(x.read())
    image.thumb.save("generic", x)
    print "[ ii ] ProductImage generated ..."
    return 
Ejemplo n.º 50
0
    def handle(self, *args, **options):
        path = args[0]

        if path.startswith('http'):
            content = json.load(urllib2.urlopen(path))
        else:
            content = json.load(open(path))

        for entry in content:
            try:
                news = News.objects.get(id=int(entry['id']))
            except News.DoesNotExist:
                news = News(id=int(entry['id']))

            if not 'html' in entry or entry['pub'] == '0':
                continue

            news.title = entry['name']
            news.caption = entry['announce']
            news.is_visible = entry['pub'] == '10'
            news.date_published = datetime.datetime.strptime(entry['time'], '%d.%m.%Y').date()
            news.content = entry['html']
            news.save()

            for url_hash in re.findall('src="/pic/(.*?)/"', news.content):
                url = 'http://tomat-podarky.ru/pic/%s/' % url_hash

                fp = ContentFile(requests.get(url).content)
                today = datetime.date.today()
                target_name = 'uploads/%(year)d/%(month)d/%(name)s%(ext)s' % {
                    'year': today.year,
                    'month': today.month,
                    'name': uuid.uuid4().hex,
                    'ext': '.jpg',
                }
                filepath = os.path.join(settings.MEDIA_ROOT, target_name)
                out = open(filepath, 'w')
                out.write(fp.read())
                out.close()

                news.content = news.content.replace('/pic/%s/' % url_hash, '/media/%s' % target_name)
            news.save()
Ejemplo n.º 51
0
    def put_profile(self, request_dict):
        #Parse out profile from request_dict
        try:
            profile = ContentFile(request_dict['profile'].read())
        except:
            try:
                profile = ContentFile(request_dict['profile'])
            except:
                profile = ContentFile(str(request_dict['profile']))

        #Check if activity exists
        try:
            activity = models.activity.objects.get(activity_id=request_dict['activityId'])
        except models.activity.DoesNotExist:
            raise IDNotFoundError('There is no activity associated with the id: %s' % request_dict['activityId'])

        #Get the profile, or if not already created, create one
        p,created = models.activity_profile.objects.get_or_create(profileId=request_dict['profileId'],activity=activity)
        
        #If it already exists delete it
        if not created:
            etag.check_preconditions(request_dict,p, required=True)
            p.profile.delete()
        
        #Save profile content type based on incoming content type header and create etag
        p.content_type = request_dict['CONTENT_TYPE']
        p.etag = etag.create_tag(profile.read())
        
        #Set updated
        if request_dict['updated']:
            p.updated = request_dict['updated']
        
        #Go to beginning of file
        profile.seek(0)
        
        #If it didn't exist, save it
        if created:
            p.save()

        #Set filename with the activityID and profileID and save
        fn = "%s_%s" % (p.activity_id,request_dict.get('filename', p.id))
        p.profile.save(fn, profile)
Ejemplo n.º 52
0
    def file_view(self, request, field_entry_id):
        """
        Output the file for the requested field entry.
        """
        field = get_object_or_404(FieldEntry, id=field_entry_id)

        base_name = os.path.basename(field.value)
        mime_type = mimetypes.guess_type(base_name)[0]

        if not mime_type:
            raise Http404

        if not default_storage.exists(field.value):
            raise Http404

        data = default_storage.open(field.value).read()
        f = ContentFile(data)

        response = HttpResponse(f.read(), content_type=mime_type)
        response['Content-Disposition'] = 'filename=%s' % base_name
        return response
Ejemplo n.º 53
0
def files(request, id):
    """
    Returns file.  Allows us to handle privacy.

    If default storage is remote:
        We can get data from remote location, convert to file
        object and return a file response.
    """

    import os
    import mimetypes
    from django.http import Http404
    from django.core.files.base import ContentFile
    from django.core.files.storage import default_storage
    from tendenci.core.perms.utils import has_view_perm
    from tendenci.apps.forms_builder.forms.models import FieldEntry

    field = get_object_or_404(FieldEntry, pk=id)
    form = field.field.form

    base_name = os.path.basename(field.value)
    mime_type = mimetypes.guess_type(base_name)[0]

    if not has_view_perm(request.user, 'forms.view_form', form):
        raise Http403

    if not mime_type:
        raise Http404

    if not default_storage.exists(field.value):
        raise Http404

    data = default_storage.open(field.value).read()
    f = ContentFile(data)

    EventLog.objects.log()
    response = HttpResponse(f.read(), mimetype=mime_type)
    response['Content-Disposition'] = 'filename=%s' % base_name
    return response
Ejemplo n.º 54
0
def load_dump(request):
    #Retrieve the content of the file uploaded.
    try:
        if('file' in request.FILES.keys() ):
            logger.info("Load a dump from a sent file.")
            # Write the received file into a file into settings.FIXTURE_DIRS
            file_content = ContentFile(request.FILES['file'].read())
            filename = 'load_dump.json'
            fixture = os.path.join(tempfile.gettempdir(), filename)
            tmp_dump = open(fixture, 'w')
            f = File(tmp_dump)
            f.write(file_content.read())
            f.close()
            logger.info("Dump file was persisted for future loading.")
            receivers_senders = [(receiver_examination, models.Examination), (receiver_newpatient, models.Patient)]

            with block_disconnect_all_signal(
                signal=signals.post_save,
                receivers_senders=receivers_senders
                ):
                logger.info("Signals were disactivated, perform clearing of the database")
                call_command('flush', interactive=False, load_initial_data=False)
                # It means that the settings.FIXTURE_DIRS should be set in settings
                previous = settings.FIXTURE_DIRS
                settings.FIXTURE_DIRS = [tempfile.gettempdir()]
                # And when loading dumps, write the file into this directory with the name : load_dump.json
                logger.info("Load the fixture from path : %s "% (fixture))
                call_command('loaddata', fixture)
                # Delete the fixture
                logger.info("Clearing the fixture")
                os.remove(fixture)
                settings.FIXTURE_DIRS = previous
                logger.info("Could restore signals")
            logger.info("end of reloading.")
            return HttpResponse(content=u'reloaded')
        else :
            return HttpResponse()
    except :
        return HttpResponse(content=_(u'This archive file seems to be incorrect. Impossible to load it.'), status=412)
Ejemplo n.º 55
0
    def put_profile(self, request_dict):
        try:
            profile = ContentFile(request_dict['profile'].read())
        except:
            try:
                profile = ContentFile(request_dict['profile'])
            except:
                profile = ContentFile(str(request_dict['profile']))

        p,created = agent_profile.objects.get_or_create(profileId=request_dict['profileId'],agent=self.agent)
        if not created:
            etag.check_preconditions(request_dict,p, required=True)
            p.profile.delete()
        p.content_type = request_dict['CONTENT_TYPE']
        p.etag = etag.create_tag(profile.read())
        if request_dict['updated']:
            p.updated = request_dict['updated']
        profile.seek(0)
        if created:
            p.save()

        fn = "%s_%s" % (p.agent_id,request_dict.get('filename', p.id))
        p.profile.save(fn, profile)
Ejemplo n.º 56
0
def change_headshot(request,attr):
  user = get_object_or_404(get_user_model(),pk=request.POST['user_id'])
  f = request.FILES.get(attr,None) # if somehow they upload the uncompressed image
  fname = "%s-%s.jpg"%(attr,user.id)
  if request.POST.get('blob',None):
    f = ContentFile(request.POST['blob'].split(",")[1].decode('base64'))
  if attr == 'headshot':
    user.headshot.save(fname,f)
    user.save()
  elif attr == 'id_photo':
    admin_url = "https://txrxlabs.org/admin/user/user/%s"%user.id
    msg = EmailMessage(
      user.username,
      "\n".join([str(s) for s in [user.get_full_name(),user.email,user.paypal_email,admin_url]]),
      settings.DEFAULT_FROM_EMAIL,
      [settings.ID_PHOTO_EMAIL]
    )
    msg.attach(fname,f.read(),'image/jpg')
    msg.send()
    user.id_photo_date = datetime.date.today()
    user.save()
    attr = 'id_photo_date'
  return JsonResponse({'done': str(getattr(user,attr))})
Ejemplo n.º 57
0
    def put(self):
        agent = self.__get_agent(create=True)
        try:
            state = ContentFile(self.state.read())
        except:
            try:
                state = ContentFile(self.state)
            except:
                state = ContentFile(str(self.state))

        if self.registrationId:
            p,created = models.activity_state.objects.get_or_create(state_id=self.stateId,agent=agent,activity=self.activity,registration_id=self.registrationId, user=self.user)
        else:
            p,created = models.activity_state.objects.get_or_create(state_id=self.stateId,agent=agent,activity=self.activity, user=self.user)
        
        if created:
            log_message(self.log_dict, "Created Activity State", __name__, self.put.__name__)
        elif not created:
            etag.check_preconditions(self.req_dict,p)
            p.state.delete() # remove old state file
            log_message(self.log_dict, "Retrieved Activity State", __name__, self.put.__name__)

        # if not created:
        #     etag.check_preconditions(self.req_dict,p)
        #     p.state.delete() # remove old state file
        p.content_type = self.content_type
        p.etag = etag.create_tag(state.read())
        if self.updated:
            p.updated = self.updated
        state.seek(0)
        if created:
            p.save()

        fn = "%s_%s_%s" % (p.agent_id,p.activity_id, self.req_dict.get('filename', p.id))
        p.state.save(fn, state)

        log_message(self.log_dict, "Saved Activity State", __name__, self.put.__name__)
Ejemplo n.º 58
0
def upload_file(request):
    for f in request.FILES:
        create_new_doc = False

        if request.FILES[f].content_type == 'application/json':
            uploaded_file = ContentFile(request.FILES[f].read())
            j = json.loads(uploaded_file.read())
            doc_urn = j['urn']
            doc_title = j['title']
            doc_content = j['content']

            create_new_doc = True
        elif request.FILES[f].content_type == 'text/plain':
            file_name = request.FILES[f].name.encode('utf-8')
            doc_title = str(' '.join(splitext(basename(file_name))[0].split()))
            doc_urn = str('_'.join(splitext(basename(file_name))[0].split()))
            print(doc_title + "   " + doc_urn)
            # read plain text content
            content = []
            for chunk in request.FILES[f].chunks():
                content.append(chunk)
            doc_content = ''.join(content)

            create_new_doc = True

        if create_new_doc:
            if not Document.objects.filter(urn=doc_urn).exists():
                document = Document.objects.create_document(doc_urn, doc_title, doc_content)
            else:
                document = Document.objects.get(urn=doc_urn)

            # import document into workspace
            workspace = Workspace.objects.get_workspace(owner=request.user)
            workspace.documents.add(document)

    return redirect('/')
Ejemplo n.º 59
0
    def _save(self, name, content):
        content = ContentFile(compress(content.read()))

        return super(GzipFileSystemStorage, self)._save(name, content)