def test_post_add_form(self):
        name = 'test_article'
        text = 'test_article text'
        tag = self.tag

        with open(os.path.join(settings.MEDIA_ROOT, r'test/images/test1.jpg'),
                  'rb') as image:
            image = SimpleUploadedFile('test1.jpg',
                                       image.read(),
                                       content_type='image/jpeg')
            response = self.client.post(
                reverse('blog:my_page'), {
                    'add_form': ['Save'],
                    'name': name,
                    'text': text,
                    'tag': tag.name,
                    'image': image,
                })

        self.assertEqual(response.status_code, 302)
        self.assertTrue(
            Article.objects.get(
                author=self.writer,
                name=name,
                text=text,
                tag=tag,
            ).image.path.startswith(
                os.path.join(settings.MEDIA_ROOT,
                             'articles/images/test_writer_test_article')))
Ejemplo n.º 2
0
    def test_resize_image(self):
        """ Testing image save """
        person = Person.objects.get(pk=1)
        self.assertEqual(person.get_img_url(), '/static/img/no_image.png')
        photo = open('assets/img/no_image_test.png', 'rb')
        data = {
            'first_name': 'firstname',
            'last_name': 'lastname',
            'date_of_birth': '1991-01-01',
            'contacts': 'contacts',
            'bio': 'bio',
            'email': '*****@*****.**',
            'jabber': '*****@*****.**',
            'skype': 'skypeid'
        }
        photo = SimpleUploadedFile(photo.name,
                                   photo.read())
        form = ProfileForm(data, dict(photo=photo), instance=person)
        self.assertTrue(form.is_valid())
        form.save()
        person = Person.objects.get(pk=1)

        self.assertNotEqual(person.get_img_url(),
                            '/static/img/no_image.png')
        image_resized = Image.open(person.photo)
        self.assertLessEqual(image_resized.height, 200)
        self.assertLessEqual(image_resized.width, 200)
Ejemplo n.º 3
0
def image_create(request, **kwargs):
    copy_from = kwargs.pop('copy_from', None)
    data = kwargs.pop('data', None)
    location = kwargs.pop('location', None)

    image = glanceclient(request).images.create(**kwargs)

    if data:
        if isinstance(data, TemporaryUploadedFile):
            # Hack to fool Django, so we can keep file open in the new thread.
            data.file.close_called = True
        if isinstance(data, InMemoryUploadedFile):
            # Clone a new file for InMemeoryUploadedFile.
            # Because the old one will be closed by Django.
            data = SimpleUploadedFile(data.name,
                                      data.read(),
                                      data.content_type)
        thread.start_new_thread(image_update,
                                (request, image.id),
                                {'data': data,
                                 'purge_props': False})
    elif copy_from:
        thread.start_new_thread(image_update,
                                (request, image.id),
                                {'copy_from': copy_from,
                                 'purge_props': False})
    elif location:
        thread.start_new_thread(image_update,
                                (request, image.id),
                                {'location': location,
                                 'purge_props': False})

    return image
Ejemplo n.º 4
0
    def test_format_package(self):
        files = []
        documents = []
        for i in range(0, 2):
            document = PACKAGE_DOCUMENT_FORMAT.copy()
            filename = 'form_{}.pdf'.format(i)
            document['name'] = filename
            file = SimpleUploadedFile(filename, b'test content')
            files.append(('files', (file.name, file.read())))
            documents.append(document)

        responses = {
            'last_name_you': 'Test',
            'given_name_1_you': 'Party 0',
            'last_name_spouse': 'Test',
            'given_name_1_spouse': 'Party 1'
        }

        package = self.packaging.format_package(self.request, responses, documents)

        self.assertTrue(package)
        self.assertEqual(package['filingPackage']['documents'][0]['name'], 'form_0.pdf')
        self.assertEqual(package['filingPackage']['documents'][1]['name'], 'form_1.pdf')
        self.assertEqual(package['filingPackage']['parties'][0]['firstName'], 'Party 0')
        self.assertEqual(package['filingPackage']['parties'][1]['firstName'], 'Party 1')
Ejemplo n.º 5
0
def image_create(request, **kwargs):
    """Create image.

    :param kwargs:
        * copy_from: URL from which Glance server should immediately copy
            the data and store it in its configured image store.
        * data: Form data posted from client.
        * location: URL where the data for this image already resides.

    In the case of 'copy_from' and 'location', the Glance server
    will give us a immediate response from create and handle the data
    asynchronously.

    In the case of 'data' the process of uploading the data may take
    some time and is handed off to a separate thread.
    """
    data = kwargs.pop('data', None)
    location = kwargs.pop('location', None)

    image = glanceclient(request).images.create(**kwargs)
    if location is not None:
        glanceclient(request).images.add_location(image.id, location, {})

    if data:
        if isinstance(data, str):
            # The image data is meant to be uploaded externally, return a
            # special wrapper to bypass the web server in a subsequent upload
            return ExternallyUploadedImage(image, request)

        if isinstance(data, TemporaryUploadedFile):
            # Hack to fool Django, so we can keep file open in the new thread.
            data.file._closer.close_called = True
        elif isinstance(data, InMemoryUploadedFile):
            # Clone a new file for InMemeoryUploadedFile.
            # Because the old one will be closed by Django.
            data = SimpleUploadedFile(data.name, data.read(),
                                      data.content_type)

        def upload():
            try:
                return glanceclient(request).images.upload(image.id, data)
            finally:
                try:
                    filename = str(data.file.name)
                except AttributeError:
                    pass
                else:
                    try:
                        os.remove(filename)
                    except OSError as e:
                        LOG.warning(
                            'Failed to remove temporary image file '
                            '%(file)s (%(e)s)', {
                                'file': filename,
                                'e': e
                            })

        thread.start_new_thread(upload, ())

    return Image(image)
Ejemplo n.º 6
0
def image_create(request, **kwargs):
    """Create image.

    :param kwargs:
        * copy_from: URL from which Glance server should immediately copy
            the data and store it in its configured image store.
        * data: Form data posted from client.
        * location: URL where the data for this image already resides.

    In the case of 'copy_from' and 'location', the Glance server
    will give us a immediate response from create and handle the data
    asynchronously.

    In the case of 'data' the process of uploading the data may take
    some time and is handed off to a separate thread.
    """
    data = kwargs.pop('data', None)

    image = glanceclient(request).images.create(**kwargs)

    if data:
        if isinstance(data, TemporaryUploadedFile):
            # Hack to fool Django, so we can keep file open in the new thread.
            data.file.close_called = True
        if isinstance(data, InMemoryUploadedFile):
            # Clone a new file for InMemeoryUploadedFile.
            # Because the old one will be closed by Django.
            data = SimpleUploadedFile(data.name, data.read(),
                                      data.content_type)
        thread.start_new_thread(image_update, (request, image.id), {
            'data': data,
            'purge_props': False
        })

    return image
Ejemplo n.º 7
0
def image_create(request, **kwargs):
    copy_from = kwargs.pop('copy_from', None)
    data = kwargs.pop('data', None)
    location = kwargs.pop('location', None)

    image = glanceclient(request).images.create(**kwargs)

    if data:
        if isinstance(data, TemporaryUploadedFile):
            # Hack to fool Django, so we can keep file open in the new thread.
            data.file.close_called = True
        if isinstance(data, InMemoryUploadedFile):
            # Clone a new file for InMemeoryUploadedFile.
            # Because the old one will be closed by Django.
            data = SimpleUploadedFile(data.name, data.read(),
                                      data.content_type)
        thread.start_new_thread(image_update, (request, image.id), {
            'data': data,
            'purge_props': False
        })
    elif copy_from:
        thread.start_new_thread(image_update, (request, image.id), {
            'copy_from': copy_from,
            'purge_props': False
        })
    elif location:
        thread.start_new_thread(image_update, (request, image.id), {
            'location': location,
            'purge_props': False
        })

    return image
Ejemplo n.º 8
0
def image_create(request, **kwargs):
    """Create image.

    :param kwargs:
        * copy_from: URL from which Glance server should immediately copy
            the data and store it in its configured image store.
        * data: Form data posted from client.
        * location: URL where the data for this image already resides.

    In the case of 'copy_from' and 'location', the Glance server
    will give us a immediate response from create and handle the data
    asynchronously.

    In the case of 'data' the process of uploading the data may take
    some time and is handed off to a seperate thread.
    """
    data = kwargs.pop("data", None)

    image = glanceclient(request).images.create(**kwargs)

    if data:
        if isinstance(data, TemporaryUploadedFile):
            # Hack to fool Django, so we can keep file open in the new thread.
            data.file.close_called = True
        if isinstance(data, InMemoryUploadedFile):
            # Clone a new file for InMemeoryUploadedFile.
            # Because the old one will be closed by Django.
            data = SimpleUploadedFile(data.name, data.read(), data.content_type)
        thread.start_new_thread(image_update, (request, image.id), {"data": data, "purge_props": False})

    return image
Ejemplo n.º 9
0
    def _file_upload_csv( string, args, file_name,account):

        base_path = os.path.dirname(os.path.realpath(__file__))
        base_path += '/accounting/testcases/sample_data/'

        data = open(base_path + file_name, 'rb')

        data = SimpleUploadedFile(content = data.read(),name = data.name,content_type='multipart/form-data')

        factory = RequestFactory()
        user = User.objects.get(username=UserConstant.ADMIN_USERNAME)

        if account is AccountingConstant.CHART_OF_ACCOUNTS:
            view = ChartOfAccounts.as_view()
        else :
            view = TrialBalanceView.as_view()

        content_type = 'multipart/form-data'
        headers= {
            'HTTP_HOST': TestConstants.HOST_URL,
            'HTTP_CONTENT_TYPE': content_type,
            'HTTP_CONTENT_DISPOSITION': 'attachment; filename='+file_name}

        request = factory.post(reverse(string, args=[args]),{'file': data},secure = True,
                               **headers)

        force_authenticate(request, user=user)
        response = view(request, args)

        return response.status_code, response.data
Ejemplo n.º 10
0
class FileProxyTest(TestCase):
    def setUp(self):
        self.dummy_file = SimpleUploadedFile(name="dummy.txt", content=b"I am content.\n")

    def test_create_file_proxy(self):
        """Test creation of `FileProxy` object."""
        fp = FileProxy.objects.create(name=self.dummy_file.name, file=self.dummy_file)

        # Now refresh it and make sure it was saved and retrieved correctly.
        fp.refresh_from_db()
        self.dummy_file.seek(0)  # Reset cursor since it was previously read
        self.assertEqual(fp.name, self.dummy_file.name)
        self.assertEqual(fp.file.read(), self.dummy_file.read())

    def test_delete_file_proxy(self):
        """Test deletion of `FileProxy` object."""
        fp = FileProxy.objects.create(name=self.dummy_file.name, file=self.dummy_file)

        # Assert counts before delete
        self.assertEqual(FileProxy.objects.count(), 1)
        self.assertEqual(FileAttachment.objects.count(), 1)

        # Assert counts after delete
        fp.delete()
        self.assertEqual(FileProxy.objects.count(), 0)
        self.assertEqual(FileAttachment.objects.count(), 0)
Ejemplo n.º 11
0
class Test(unittest.TestCase):
    def setUp(self):
        self.password = '******'
        self.wrong_password = '******'
        self.file = SimpleUploadedFile('test.pdf', b'these are the file contents!', content_type="application/pdf")
        self.invalid_file = 'output.pdf'

    def test_encryption_decryption_roundup(self):
        file_name = copy.deepcopy(self.file)
        encrypted_file = EncryptionService().encrypt_file(file_name, self.password)
        self.assertNotEqual(file_name.read(), encrypted_file.read())
        decrypted_file = EncryptionService().decrypt_file(encrypted_file, self.password)
        self.assertEqual(self.file.read(), decrypted_file.read())

    def test_encrypt_decrypt_with_extension_success(self):
        file_name = copy.deepcopy(self.file)
        encrypted_file = EncryptionService().encrypt_file(file_name, self.password, extension='enc')
        self.assertNotEqual(file_name.read(), encrypted_file.read())
        decrypted_file = EncryptionService().decrypt_file(encrypted_file, self.password, extension='enc')
        self.assertEqual(self.file.read(), decrypted_file.read())

    def test_decrypt_with_wrong_password_fails(self):
        file_name = copy.deepcopy(self.file)
        encrypted_file = EncryptionService().encrypt_file(file_name, self.password, extension='enc')
        self.assertNotEqual(file_name.read(), encrypted_file.read())
        decrypted_file = EncryptionService().decrypt_file(encrypted_file, self.wrong_password, extension='enc')
        self.assertNotEquals(self.file.read(), decrypted_file.read())

    def test_encrypt_decrypt_fail_without_password(self):
        file_name = copy.deepcopy(self.file)
        self.assertRaises(ValidationError, EncryptionService().encrypt_file, file_name, None)
        self.assertRaises(ValidationError, EncryptionService().decrypt_file, file_name, None)

    def test_encrypt_decrypt_fail_without_filename(self):
        self.assertRaises(ValidationError, EncryptionService().encrypt_file, None, self.password)
        self.assertRaises(ValidationError, EncryptionService().decrypt_file, None, self.password)

    def test_enrypt_decrypt_fail_for_invalid_file_type(self):
        self.assertRaises(ValidationError, EncryptionService().encrypt_file, self.invalid_file, self.password)
        self.assertRaises(ValidationError, EncryptionService().decrypt_file, self.invalid_file, self.password)

    def test_encrypt_decrypt_return_false_for_raise_exception_false_for_invalid_input(self):
        self.assertEqual(False, EncryptionService(raise_exception=False).encrypt_file(
                          self.invalid_file, self.password))
        self.assertEqual(False, EncryptionService(raise_exception=False).decrypt_file(
                          self.invalid_file, self.password))
Ejemplo n.º 12
0
def image_create(request, **kwargs):
    """Create image.

    :param kwargs:
        * copy_from: URL from which Glance server should immediately copy
            the data and store it in its configured image store.
        * data: Form data posted from client.
        * location: URL where the data for this image already resides.

    In the case of 'copy_from' and 'location', the Glance server
    will give us a immediate response from create and handle the data
    asynchronously.

    In the case of 'data' the process of uploading the data may take
    some time and is handed off to a separate thread.
    """
    data = kwargs.pop('data', None)
    location = None
    if VERSIONS.active >= 2:
        location = kwargs.pop('location', None)

    image = glanceclient(request).images.create(**kwargs)
    if location is not None:
        glanceclient(request).images.add_location(image.id, location, {})

    if data:
        if isinstance(data, six.string_types):
            # The image data is meant to be uploaded externally, return a
            # special wrapper to bypass the web server in a subsequent upload
            return ExternallyUploadedImage(image, request)
        elif isinstance(data, TemporaryUploadedFile):
            # Hack to fool Django, so we can keep file open in the new thread.
            data.file.close_called = True
        elif isinstance(data, InMemoryUploadedFile):
            # Clone a new file for InMemeoryUploadedFile.
            # Because the old one will be closed by Django.
            data = SimpleUploadedFile(data.name,
                                      data.read(),
                                      data.content_type)
        if VERSIONS.active < 2:
            thread.start_new_thread(image_update,
                                    (request, image.id),
                                    {'data': data})
        else:
            def upload():
                try:
                    return glanceclient(request).images.upload(image.id, data)
                finally:
                    filename = str(data.file.name)
                    try:
                        os.remove(filename)
                    except OSError as e:
                        LOG.warning('Failed to remove temporary image file '
                                    '%(file)s (%(e)s)',
                                    {'file': filename, 'e': e})
            thread.start_new_thread(upload, ())

    return Image(image)
Ejemplo n.º 13
0
    def create_posts(self):
        """ create admin user and few post """
        password = 12345678
        email = "*****@*****.**"
        photo = open(f"{settings.BASE_DIR}/staticfiles/img/No-photo-m.png",
                     'rb')
        photo = SimpleUploadedFile(photo.name,
                                   photo.read(),
                                   content_type='image/png')
        user = UserModel.objects.create_superuser(email=email,
                                                  password=password,
                                                  photo=photo)

        data = {'email': email, 'password': password}
        self.admin_creds = data
        response = self.client.post(self.login_url, data=data)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.admin_token = response.data['access']
        self.client.credentials(
            HTTP_AUTHORIZATION=f"Bearer {self.admin_token}")

        titles = [
            "Cars", "onion", "People", "Dresses", "Food", "Job", "Vacancies"
        ]

        for title in titles:
            photo = open(f"{settings.BASE_DIR}/staticfiles/img/No-photo-m.png",
                         'rb')
            photo = SimpleUploadedFile(photo.name,
                                       photo.read(),
                                       content_type='image/png')

            data = {
                "title": title,
                "content":
                """Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus aliquam""",
                "image": photo,
            }

            with self.assertNumQueries(2):
                response = self.client.post(self.post_create, data=data)

            self.assertEqual(response.status_code, status.HTTP_201_CREATED)
Ejemplo n.º 14
0
def _update_from_upload_file(tutorial, update_file,
                             content_type="text/x-gettext-translation",
                             user=None, submission_type=None):
    with open(update_file, "r") as f:
        upload = SimpleUploadedFile(os.path.basename(update_file),
                                    f.read(),
                                    content_type)
    test_store = getclass(upload)(upload.read())
    tutorial.update(overwrite=False, only_newer=False)
    tutorial.update(overwrite=True, store=test_store,
                    user=user, submission_type=submission_type)
Ejemplo n.º 15
0
 def make_pq(self, filename='azd.html', upload_type=UPLOAD_TYPE.DOCKET):
     """Make a simple pq object for processing"""
     path = self.make_path(filename)
     with open(path, 'r') as f:
         f = SimpleUploadedFile(filename, f.read())
     return ProcessingQueue.objects.create(
         court_id='scotus',
         uploader=self.user,
         pacer_case_id='asdf',
         filepath_local=f,
         upload_type=upload_type,
     )
Ejemplo n.º 16
0
    def test_post_image_form(self):
        with open(os.path.join(settings.MEDIA_ROOT, r'test/images/test2.jpg'), 'rb') as image:
            image = SimpleUploadedFile('test2.jpg', image.read(), content_type='image/jpeg')

        response = self.client.post(reverse('blog:my_page'), {
            'image_form': ['Submit'],
            'image': image,
        })

        writer = Writer.objects.get(name=self.writer.name)
        self.assertEquals(response.status_code, 302)
        self.assertTrue(writer.image.path.startswith(os.path.join(settings.MEDIA_ROOT, r'writers/images/test_writer')))
Ejemplo n.º 17
0
 def test_download_comment_attachment_without_accel_redirect(self):
     tmp_file = SimpleUploadedFile('filename.txt', b'File content')
     comment = CommentFactory(userrequest=self.request, attachment=tmp_file)
     self._set_permissions([
         'can_comment_requests',
     ])
     response = self._get_comment_attachment(pk=comment.pk)
     self.assertEqual(status.HTTP_200_OK, response.status_code)
     self.assertEqual(f'attachment; filename={tmp_file.name}',
                      response.get('Content-Disposition'))
     tmp_file.seek(0)
     self.assertEqual(response.content, tmp_file.read())
Ejemplo n.º 18
0
    def test_create_comment(self):
        self.client.credentials()
        response = self.client.get(self.list_url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        post_id = response.data['results'][0]['id']
        # not authenticated
        comment_url = reverse('post:comment-create', args=[post_id])
        response = self.client.post(comment_url, data={})
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
        # not Visitor user
        self.client.login(**self.admin_creds)
        response = self.client.post(comment_url, data={})
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
        # not valid form
        password = 12345678
        email = "*****@*****.**"
        photo = open(f"{settings.BASE_DIR}/staticfiles/img/No-photo-m.png",
                     'rb')
        photo = SimpleUploadedFile(photo.name,
                                   photo.read(),
                                   content_type='image/png')
        user = VisitorModel.objects.create_user(email=email,
                                                password=password,
                                                photo=photo)
        data = {'email': email, 'password': password}
        self.client.login(**data)
        response = self.client.post(comment_url, data={})
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        # valid form
        with self.assertNumQueries(4):
            response = self.client.post(comment_url,
                                        data={"text": 'some text'})
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

        with self.assertNumQueries(4):
            response = self.client.post(comment_url,
                                        data={"text": 'some new text'})
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

        with self.assertNumQueries(4):
            response = self.client.post(comment_url, data={"text": 'weather'})
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

        # get post
        post_detail_url = reverse('post:detail', args=[post_id])

        with self.assertNumQueries(6):
            response = self.client.get(post_detail_url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        schema = self.schemas.get_post_detail_schema()
        self.check_fields(schema, response.data)
        self.check_fields(schema['comments'][0], response.data['comments'][0])
Ejemplo n.º 19
0
 def test_saving_and_retrieving_files(self):
     user = DummyUser().create_dummy_user()
     file = SimpleUploadedFile("file.txt", b"file_content")
     expected = file.read()
     DummyUser().create_dummy_profile(user, picture=file)
     saved_profile = UserProfile.objects.first()
     # Вмісти збереженого файда і первинного співпадають?
     self.assertEqual(saved_profile.picture.read(), expected)
     # Чи правильний фактичний шлях до файла
     basename = os.path.basename(saved_profile.picture.path)
     self.assertEqual(basename, "1.jpg")
     # Видляємо з диска (бо файл по-чесному записався в /media/profile_images/1.jpg)
     saved_profile.picture.delete()
Ejemplo n.º 20
0
 def test_saving_and_retrieving_files(self):
     user = DummyUser().create_dummy_user()
     file = SimpleUploadedFile("file.txt", b"file_content")
     expected = file.read()
     DummyUser().create_dummy_profile(user, picture=file)
     saved_profile = UserProfile.objects.first()
     # Вмісти збереженого файда і первинного співпадають?
     self.assertEqual(saved_profile.picture.read(), expected)
     # Чи правильний фактичний шлях до файла
     basename = os.path.basename(saved_profile.picture.path)
     self.assertEqual(basename, "1.jpg")
     # Видляємо з диска (бо файл по-чесному записався в /media/profile_images/1.jpg)
     saved_profile.picture.delete()
Ejemplo n.º 21
0
def _update_from_upload_file(store, update_file,
                             content_type="text/x-gettext-translation",
                             user=None, submission_type=None):
    with open(update_file, "r") as f:
        upload = SimpleUploadedFile(os.path.basename(update_file),
                                    f.read(),
                                    content_type)
    if store.state < PARSED:
        store.update(store.file.store)
    test_store = getclass(upload)(upload.read())
    store_revision = parse_pootle_revision(test_store)
    store.update(test_store, store_revision=store_revision,
                 user=user, submission_type=submission_type)
Ejemplo n.º 22
0
def create_test_upload(file, encoding=None, project=None, user=None):
    if project is None:
        project = amcattest.create_test_project()
    if user is None:
        user = project.owner

    if type(file) in (str, bytes):
        file = open(file, mode='rb')
    file = SimpleUploadedFile(os.path.basename(file.name), file.read())
    upload = UploadedFile(file=file, user=user, project=project)
    upload.save()
    upload.encoding_override(encoding)
    return upload
Ejemplo n.º 23
0
def _update_from_upload_file(store, update_file,
                             content_type="text/x-gettext-translation",
                             user=None, submission_type=None):
    with open(update_file, "r") as f:
        upload = SimpleUploadedFile(os.path.basename(update_file),
                                    f.read(),
                                    content_type)
    if store.state < PARSED:
        store.update(store.file.store)
    test_store = getclass(upload)(upload.read())
    store_revision = parse_pootle_revision(test_store)
    store.update(test_store, store_revision=store_revision,
                 user=user, submission_type=submission_type)
Ejemplo n.º 24
0
 def setUp(self):
     self.user = User.objects.get(username='******')
     self.filename = 'cand.html'
     path = os.path.join(settings.INSTALL_ROOT, 'cl', 'recap',
                         'test_assets', self.filename)
     with open(path, 'r') as f:
         f = SimpleUploadedFile(self.filename, f.read())
     self.pq = ProcessingQueue.objects.create(
         court_id='scotus',
         uploader=self.user,
         pacer_case_id='asdf',
         filepath_local=f,
         upload_type=UPLOAD_TYPE.DOCKET,
     )
Ejemplo n.º 25
0
def _update_from_upload_file(tutorial,
                             update_file,
                             content_type="text/x-gettext-translation",
                             user=None,
                             submission_type=None):
    with open(update_file, "r") as f:
        upload = SimpleUploadedFile(os.path.basename(update_file), f.read(),
                                    content_type)
    test_store = getclass(upload)(upload.read())
    tutorial.update(overwrite=False, only_newer=False)
    tutorial.update(overwrite=True,
                    store=test_store,
                    user=user,
                    submission_type=submission_type)
Ejemplo n.º 26
0
def insert_issue(request, client_path, project_path, jira_key):
    try:
        client = Client.objects.get(path=client_path)
        project = client.projects.get(path=project_path)

        soap = SOAPpy.WSDL.Proxy(JIRA_WSDL)
        auth = request.session.get('jira_auth')

        jira_project = JiraProject.objects.get(key=jira_key)

        issue_url = None
        if request.method == 'POST':
            form = JiraTicketForm(request.POST, request.FILES)
            if form.is_valid():
                submitted = True

                newissue = soap.createIssue(auth, {'project': jira_project.key, 
                                                   'type': '1', 
                                                   'description': form.cleaned_data['description'],
                                                   'summary': form.cleaned_data['summary'],
                                                   'customFieldValues': [{'customfieldId': 'customfield_10001',
                                                                          'values': [form.cleaned_data['steps_to_reproduce']]}],
                                                   'assignee': JIRA_USER
                                                   })
                if form.cleaned_data['attachment']:
                    filename="%s" % form.cleaned_data['attachment']
    
                    uploaded = SimpleUploadedFile(filename,
                                             form.cleaned_data['attachment'].read())
    
                    b64t = base64.encodestring(uploaded.read())
                    soap.addBase64EncodedAttachmentsToIssue(auth,
                                               newissue['key'],
                                               [filename],
                                               [b64t])
                issue_url = newissue['key']
        else:
            form = JiraTicketForm()
        
        return render_to_response('jira_create_issue.html', RequestContext(request, {'project':project, 
                                                                   'client':client,
                                                                   'client_path': client_path,
                                                                   'project_path': project_path,
                                                                   'form': form,
                                                                   'jira_project': jira_project,
                                                                   'user':request.user,
                                                                   'issue_url': issue_url,
                                                                   'jira_key': jira_key}))
    except Client.DoesNotExist, Project.DoesNotExist:
        raise Http404
Ejemplo n.º 27
0
def image_create(request, **kwargs):
    """Create image.

    :param kwargs:
        * copy_from: URL from which Glance server should immediately copy
            the data and store it in its configured image store.
        * data: Form data posted from client.
        * location: URL where the data for this image already resides.

    In the case of 'copy_from' and 'location', the Glance server
    will give us a immediate response from create and handle the data
    asynchronously.

    In the case of 'data' the process of uploading the data may take
    some time and is handed off to a separate thread.
    """
    data = kwargs.pop('data', None)
    location = None
    if VERSIONS.active >= 2:
        location = kwargs.pop('location', None)

    image = glanceclient(request).images.create(**kwargs)
    if location is not None:
        glanceclient(request).images.add_location(image.id, location, {})

    if data:
        if isinstance(data, six.string_types):
            # The image data is meant to be uploaded externally, return a
            # special wrapper to bypass the web server in a subsequent upload
            return ExternallyUploadedImage(image, request)
        elif isinstance(data, TemporaryUploadedFile):
            # Hack to fool Django, so we can keep file open in the new thread.
            data.file.close_called = True
        elif isinstance(data, InMemoryUploadedFile):
            # Clone a new file for InMemeoryUploadedFile.
            # Because the old one will be closed by Django.
            data = SimpleUploadedFile(data.name, data.read(),
                                      data.content_type)
        if VERSIONS.active < 2:
            thread.start_new_thread(image_update, (request, image.id),
                                    {'data': data})
        else:

            def upload():
                return glanceclient(request).images.upload(image.id, data)

            thread.start_new_thread(upload, ())

    return Image(image)
Ejemplo n.º 28
0
    def clean_upload_file_minidump(self):
        file = self.cleaned_data["upload_file_minidump"]

        if file.name.endswith('.tar'):
            t_file = BytesIO(file.read())
            t_file = tarfile.open(fileobj=t_file, mode='r')
            self.cleaned_data['archive_file'] = file
            dump_name = filter(lambda i: i.endswith('.dmp'), t_file.getnames())
            try:
                file_name = next(dump_name)
                file = t_file.extractfile(file_name)
                file = SimpleUploadedFile(file_name, file.read())
            except StopIteration:
                return None
        return file
Ejemplo n.º 29
0
 def setUp(self):
     self.user = User.objects.get(username="******")
     self.filename = "cand.html"
     path = os.path.join(
         settings.INSTALL_ROOT, "cl", "recap", "test_assets", self.filename
     )
     with open(path, "r") as f:
         f = SimpleUploadedFile(self.filename, f.read())
     self.pq = ProcessingQueue.objects.create(
         court_id="scotus",
         uploader=self.user,
         pacer_case_id="asdf",
         filepath_local=f,
         upload_type=UPLOAD_TYPE.DOCKET,
     )
Ejemplo n.º 30
0
 def setUp(self):
     user = User.objects.get(username='******')
     self.filename = 'cand.html'
     path = os.path.join(settings.INSTALL_ROOT, 'cl', 'recap', 'test_assets',
                         self.filename)
     with open(path, 'r') as f:
         f = SimpleUploadedFile(self.filename, f.read())
     self.pq = ProcessingQueue.objects.create(
         court_id='scotus',
         uploader=user,
         pacer_case_id='asdf',
         filepath_local=f,
         status=ProcessingQueue.AWAITING_PROCESSING,
         upload_type=ProcessingQueue.DOCKET,
     )
Ejemplo n.º 31
0
    def clean_upload_file_minidump(self):
        file = self.cleaned_data["upload_file_minidump"]

        if file.name.endswith('.tar'):
            t_file = BytesIO(file.read())
            t_file = tarfile.open(fileobj=t_file, mode='r')
            self.cleaned_data['archive_file'] = file
            dump_name = filter(lambda i: i.endswith('.dmp'), t_file.getnames())
            try:
                file_name = next(dump_name)
                file = t_file.extractfile(file_name)
                file = SimpleUploadedFile(file_name, file.read())
            except StopIteration:
                return None
        return file
Ejemplo n.º 32
0
class GeometryFileFieldTestCase(SimpleTestCase):
    def setUp(self):
        self.field = GeometryFileField()
        self.fp = SimpleUploadedFile('geom.json',
                                     json.dumps(_geom).encode('ascii'))
        self.fp.seek(0)

    def test_to_python(self):
        self.assertIsInstance(self.field.to_python(self.fp), OGRGeometry)
        fp = SimpleUploadedFile('empty.json', b'{}')
        self.assertRaises(forms.ValidationError, self.field.to_python, fp)

    def test_feature_to_python(self):
        feature = Feature(geometry=_geom)
        self.fp.write(str(feature).encode('ascii'))
        self.fp.seek(0)
        v = self.field.to_python(self.fp)
        self.assertIsInstance(v, OGRGeometry)

    def test_shapefile(self):
        base = 'dir/geofield.shp'
        path = default_storage.path(base)
        os.mkdir(os.path.dirname(path))
        write_shp(path, _geom)
        b = io.BytesIO()
        with zipfile.ZipFile(b, 'w') as zf:
            for ext in ('dbf', 'prj', 'shp', 'shx'):
                fname = base.replace('shp', ext)
                with default_storage.open(fname) as fp:
                    zf.writestr(fname, fp.read())
        shutil.rmtree(os.path.dirname(path))
        upfile = SimpleUploadedFile('geofield.zip', b.getvalue())
        b.close()
        result = self.field.to_python(upfile)
        self.assertIsInstance(result, OGRGeometry)
        self.assertIsNotNone(result.srs)

    def test_zipfile(self):
        zfile = io.BytesIO()
        with zipfile.ZipFile(zfile, 'w') as zf:
            zf.writestr(self.fp.name, self.fp.read())
        zfile.seek(0)
        upfile = SimpleUploadedFile('geofield.zip', zfile.read())
        zfile.close()
        self.assertIsInstance(self.field.to_python(upfile), OGRGeometry)

    def tearDown(self):
        self.fp.close()
Ejemplo n.º 33
0
class GeometryFileFieldTestCase(SimpleTestCase):
    def setUp(self):
        self.field = GeometryFileField()
        self.fp = SimpleUploadedFile(
            'geom.json', json.dumps(_geom).encode('ascii'))
        self.fp.seek(0)

    def test_to_python(self):
        self.assertIsInstance(self.field.to_python(self.fp), OGRGeometry)
        fp = SimpleUploadedFile('empty.json', b'{}')
        self.assertRaises(forms.ValidationError, self.field.to_python, fp)

    def test_feature_to_python(self):
        feature = Feature(geometry=_geom)
        self.fp.write(str(feature).encode('ascii'))
        self.fp.seek(0)
        v = self.field.to_python(self.fp)
        self.assertIsInstance(v, OGRGeometry)

    def test_shapefile(self):
        base = 'dir/geofield.shp'
        path = default_storage.path(base)
        os.mkdir(os.path.dirname(path))
        write_shp(path, _geom)
        b = io.BytesIO()
        with zipfile.ZipFile(b, 'w') as zf:
            for ext in ('dbf', 'prj', 'shp', 'shx'):
                fname = base.replace('shp', ext)
                with default_storage.open(fname) as fp:
                    zf.writestr(fname, fp.read())
        shutil.rmtree(os.path.dirname(path))
        upfile = SimpleUploadedFile('geofield.zip', b.getvalue())
        b.close()
        result = self.field.to_python(upfile)
        self.assertIsInstance(result, OGRGeometry)
        self.assertIsNotNone(result.srs)

    def test_zipfile(self):
        zfile = io.BytesIO()
        with zipfile.ZipFile(zfile, 'w') as zf:
            zf.writestr(self.fp.name, self.fp.read())
        zfile.seek(0)
        upfile = SimpleUploadedFile('geofield.zip', zfile.read())
        zfile.close()
        self.assertIsInstance(self.field.to_python(upfile), OGRGeometry)

    def tearDown(self):
        self.fp.close()
Ejemplo n.º 34
0
    def test_changing_size(self, mock_is_testing_mode):
        mock_is_testing_mode.return_value = False
        TESTFILE = "small_kitten.jpeg"

        data = SimpleUploadedFile(name=TESTFILE,
                                  content=open(TESTFILE, "rb").read())

        initial_size = data.size
        optimize_from_buffer(data)
        resized_data_size = data.size
        data.open()
        actual_final_size = len(data.read())
        self.assertEqual(actual_final_size, resized_data_size)
        self.assertLess(actual_final_size,
                        initial_size,
                        msg="Test not valid - image was not reduced")
Ejemplo n.º 35
0
class Image(models.Model):
    name = models.CharField(max_length=50)
    image = models.ImageField(upload_to="bigpath/", null=True, blank=True)
    thumbnail = models.ImageField(upload_to="bigpath/thumbnails", null=True, blank=True)
    is_deleted = models.BooleanField(default=False)
    shelter = models.ForeignKey(Shelter)
    user = models.ForeignKey(User)

    def __unicode__(self):
        return u'%s => %s' % (self.name, self.id)

    def save(self, *args, **kwargs):
        #self.image = SimpleUploadedFile(self.system_name, self.image_file, getattr(self.image, "content-type", "application/octet-stream"))
        if self.content_type is None:
            self.content_type = "image/jpg"
        self.image = SimpleUploadedFile(self.system_name, self.image_file, self.content_type)
        self.thumbnail = self.create_thumbnail()
        super(Image, self).save(*args, **kwargs)

    def create_thumbnail(self):
        if not self.image:
            return

        import PIL, os
        from cStringIO import StringIO

        THUMBNAIL_SIZE = (200,200)
        DEFAULT_TYPE = 'JPEG'
        image = PIL.Image.open(StringIO(self.image.read()))
        image.thumbnail(THUMBNAIL_SIZE, PIL.Image.ANTIALIAS)

        thumbnail_type = self.content_type.split('/')[1]
        thumbnail_type = thumbnail_type.encode('ascii', 'ignore')

        #set default type
        if thumbnail_type is None:
            thumbnail_type = DEFAULT_TYPE
        elif thumbnail_type == 'jpg' or thumbnail_type == 'JPG':
            thumbnail_type = DEFAULT_TYPE

        temp_handle = StringIO()
        image.save(temp_handle, format=thumbnail_type)
        temp_handle.seek(0)

        return SimpleUploadedFile(self.system_name, temp_handle.read(), content_type=self.content_type)
Ejemplo n.º 36
0
    def clean_upload_file_minidump(self):
        file = self.cleaned_data["upload_file_minidump"]

        if file and file.name.endswith('.tar'):
            try:
                t_file = BytesIO(file.read())
                t_file = tarfile.open(fileobj=t_file, mode='r')
                self.cleaned_data['archive_file'] = file
                dump_name = filter(lambda i: i.endswith('.dmp'), t_file.getnames())
                try:
                    file_name = next(dump_name)
                    file = t_file.extractfile(file_name)
                    file = SimpleUploadedFile(file_name, file.read())
                except StopIteration:
                    return None
            except tarfile.TarError as err:
                raise forms.ValidationError('The tar file is broken, error: {0}'.format(err.message))
        return file
Ejemplo n.º 37
0
    def test_format_package(self):
        files = []
        for i in range(0, 2):
            file = SimpleUploadedFile('form_{}.pdf'.format(i), b'test content')
            files.append(('files', (file.name, file.read())))
        parties = []
        for i in range(0, 2):
            party = PACKAGE_PARTY_FORMAT.copy()
            party['firstName'] = 'Party {}'.format(i)
            party['lastName'] = 'Test'
            parties.append(party)
        package = self.hub._format_package(self.request, files, parties=parties)

        self.assertTrue(package)
        self.assertEqual(package['filingPackage']['documents'][0]['name'], 'form_0.pdf')
        self.assertEqual(package['filingPackage']['documents'][1]['name'], 'form_1.pdf')
        self.assertEqual(package['filingPackage']['parties'][0]['firstName'], 'Party 0')
        self.assertEqual(package['filingPackage']['parties'][1]['firstName'], 'Party 1')
Ejemplo n.º 38
0
    def clean_upload_file_minidump(self):
        file = self.cleaned_data["upload_file_minidump"]

        if file and file.name.endswith('.tar'):
            try:
                t_file = BytesIO(file.read())
                t_file = tarfile.open(fileobj=t_file, mode='r')
                self.cleaned_data['archive_file'] = file
                dump_name = filter(lambda i: i.endswith('.dmp'),
                                   t_file.getnames())
                try:
                    file_name = next(dump_name)
                    file = t_file.extractfile(file_name)
                    file = SimpleUploadedFile(file_name, file.read())
                except StopIteration:
                    return None
            except tarfile.TarError as err:
                raise forms.ValidationError(
                    'The tar file is broken, error: {0}'.format(err.message))
        return file
Ejemplo n.º 39
0
    def clean(self,data,initial=None):
        if self.required and not (data.get('file') or (data.get('tmp_file_path') and data.get('tmp_file_name'))):
            raise ValidationError(self.error_messages['required'])
        if data['file']:
            file = data['file']
            super(TempImageField,self).__init__(file,initial)
            tmp_file_path = file.temporary_file_path()
            tmp_file_name = os.path.split(tmp_file_path)[-1]
            tmp_file_url = os.path.join(settings.FILE_UPLOAD_TEMP_URL_DIR, tmp_file_name)
            self.widget.tmp_file_path = tmp_file_path 
            self.widget.tmp_file_url = tmp_file_url 
            self.widget.tmp_file_name = file.name

        elif data['tmp_file_path'] and os.path.exists(data['tmp_file_path']):
            file = open(data['tmp_file_path'],'rb')
            file = SimpleUploadedFile(data['tmp_file_name'],file.read())
        else:
            raise ValidationError('Sorry you took too long to submit. Please upload picture again.')

        return file
Ejemplo n.º 40
0
    def image_create(self, **kwargs):
        data = kwargs.pop('data', None)
        location = None
        if int(VERSIONS) >= 2:
            location = kwargs.pop('location', None)

        image = self.glance_client.images.create(**kwargs)
        if location is not None:
            self.glance_client.images.add_location(image.id, location, {})

        if data:
            if isinstance(data, TemporaryUploadedFile):
                data.file.close_called = True
            elif isinstance(data, InMemoryUploadedFile):
                data = SimpleUploadedFile(data.name, data.read(),
                                          data.content_type)
            if int(VERSIONS) < 2:
                t = threading.Thread(target=self.image_update,
                                     args=(image.id, ),
                                     kwargs={'data': data})
                t.start()
            else:

                def upload():
                    try:
                        return self.glance_client.images.upload(image.id, data)
                    finally:
                        filename = str(data.file.name)
                        try:
                            os.remove(filename)
                        except OSError as e:
                            LOG.warning(
                                'Failed to remove temporary image file '
                                '%(file)s (%(e)s)', {
                                    'file': filename,
                                    'e': e
                                })

                t = threading.Thread(target=upload)
                t.start()
        return image
Ejemplo n.º 41
0
 def test_resize_image(self):
     """
     is image save, resize
     """
     person = Person.objects.first()
     photo = create_img()
     data = {'first_name': 'first_name',
             'last_name': 'last_name',
             'date_of_birth': '1990-10-09',
             'other': 'other',
             'bio': 'bio',
             'email': '*****@*****.**',
             'jabber': '*****@*****.**',
             'skype': 'skype'}
     photo = SimpleUploadedFile(photo.name, photo.read())
     form = PersonEditForm(data, dict(photo=photo), instance=person)
     self.assertTrue(form.is_valid())
     form.save()
     person = Person.objects.first()
     self.assertLessEqual(person.photo.width/person.photo.height, 500/250)
     self.assertTrue(person.photo.width and person.photo.height <= 200)
Ejemplo n.º 42
0
    def test_resize_image(self):
        """ Testing image save """
        person = Person.objects.get(pk=1)
        photo = open('assets/images/default.png', 'rb')
        data = {
            'name': 'Victoria',
            'last_name': 'Prokophuck',
            'date': '1992-08-29',
            'other_contacts': 'contacts',
            'bio': 'bio',
            'email': '*****@*****.**',
            'jubber': '*****@*****.**',
            'skype': 'vviicckkyy',
        }
        photo = SimpleUploadedFile(photo.name, photo.read())
        form = PersonForm(data, dict(photo=photo), instance=person)
        self.assertTrue(form.is_valid())
        form.save()
        person = Person.objects.get(pk=1)

        self.assertNotEqual(person.photo.url, '/static/images/default.png')
        image_resized = Image.open(person.photo)
        self.assertLessEqual(image_resized.height, 200)
        self.assertLessEqual(image_resized.width, 200)
Ejemplo n.º 43
0
 def get_source_code(self, pk=1):
     source_code = open(self._get_source_file(), 'rb')
     source_code = SimpleUploadedFile(source_code.name,
                                      source_code.read())
     return source_code
Ejemplo n.º 44
0
class SimpleTest(TestCase):
    def setUp(self):
        self.u1 = User.objects.create(username='******')
        self.upload = SimpleUploadedFile('fake.pdf', 'This is a fake pdf')
        self.upload2 = SimpleUploadedFile('fake2.pdf', 'This is a second fake pdf')

    def test_document_upload(self):
        """
        Tests that we can upload a file and get it back
        """
        doc = uploaded_new_document(self.upload)
        doc.title = 'File uploaded'
        doc.author = self.u1
        doc.file.read()
        doc.save()

        doc = Document.objects.get(title='File uploaded')
        self.upload.seek(0)
        self.assertEqual(doc.file.read(), self.upload.read())

    def test_derived_document_upload(self):
        """
        Test derived file upload
        """
        doc = uploaded_new_document(self.upload)
        doc.title = 'File uploaded'
        doc.author = self.u1
        doc.save()

        derived = uploaded_new_derived_document(self.upload2)
        derived.derived_from = doc._blob
        derived.index = 0
        derived.save()

        derived2 = doc.get_derived_documents_of_type('pdf')[0]
        self.assertEqual(derived, derived2)
        self.upload2.seek(0)
        self.assertEqual(derived2.file.read(), self.upload2.read())

    def test_png_container(self):

        doc = uploaded_new_document(self.upload)
        doc.title = 'File uploaded'
        doc.author = self.u1
        doc.save()

        self.upload.name = 'fake.png'
        derived = uploaded_new_derived_document(self.upload)
        derived.derived_from = doc._blob
        derived.index = 0
        derived.save()

    def test_orphaned_blobs(self):
        """Make sure that deleting a Document does not result in orphanded blobs"""
        doc = uploaded_new_document(self.upload)
        doc.title = 'File uploaded'
        doc.author = self.u1
        doc.save()

        self.upload.name = 'fake.png'
        derived = uploaded_new_derived_document(self.upload)
        derived.derived_from = doc._blob
        derived.index = 0
        derived.save()

        DerivedDocument.objects.all().delete()
        self.assertEqual(DerivedBlob.objects.count(), 0)

        Document.objects.all().delete()
        self.assertEqual(ParentBlob.objects.count(), 0)

    def test_deleted_derived(self):
        """
        Make sure that derived documents are deleted when parent document and
        blob are deleted.
        """
        doc = uploaded_new_document(self.upload)
        doc.title = 'File uploaded'
        doc.author = self.u1
        doc.save()

        self.upload.name = 'fake.png'
        derived = uploaded_new_derived_document(self.upload)
        derived.derived_from = doc._blob
        derived.index = 0
        derived.save()

        Document.objects.all().delete()
        self.assertEqual(DerivedBlob.objects.count(), 0)

    def test_files_are_deleted(self):
        """Make sure file is deleted when blob is"""
        doc = uploaded_new_document(self.upload)
        doc.title = 'File uploaded'
        doc.author = self.u1
        doc.save()
        name = doc._blob.file.name

        from django.core.files.storage import get_storage_class
        storage = get_storage_class()()

        doc.delete()
        self.assertFalse(storage.exists(name))

    def test_auto_blob_creation(self):
        """
        Test that a blob is created when using file attriube on a document.
        """
        doc = Document(title='New Doc', file_name='A File', file=self.upload,
                                                                author=self.u1)
        doc.save()
        doc.file.seek(0)
        self.upload.seek(0)

        self.assertEqual(doc.file.read(), self.upload.read())
        self.assertEqual(ParentBlob.objects.count(), 1)

        doc.file.seek(0)
        self.upload.seek(0)

        self.assertEqual(ParentBlob.objects.all()[0].file.read(),
                                                        self.upload.read())

    def test_auto_derived_blob_creation(self):
        """
        Test that a blob is created when using the file attribue on a derived
        document.
        """
        doc = Document(title='New Doc', file_name='A File', file=self.upload,
                                                                author=self.u1)
        doc.save()
        derived = DerivedDocument(derived_from=doc._blob, index=0,
                                                        file=self.upload2)
        derived.save()

        self.upload2.seek(0)
        self.upload.seek(0)

        self.assertEqual(derived.file.read(), self.upload2.read())
        self.assertEqual(DerivedBlob.objects.count(), 1)

        self.upload2.seek(0)
        derived.file.seek(0)

        self.assertEqual(DerivedBlob.objects.all()[0].file.read(),
                                                        self.upload2.read())

    def test_read_only_file(self):

        doc = Document(title='New Doc', file_name='A File', file=self.upload,
                                                                author=self.u1)
        doc.save()

        self.assertRaises(ReadOnlyFileError, doc.file.delete)
        self.assertRaises(ReadOnlyFileError, doc.file.write, 'test')
        self.assertRaises(ReadOnlyFileError, doc.file.writelines, 'test')

    def tearDown(self):
        self.u1.delete()
        Document.objects.all().delete()
Ejemplo n.º 45
0
def image_create(request, **kwargs):
    copy_from = kwargs.pop('copy_from', None)
    data = kwargs.pop('data', None)
    location = kwargs.pop('location', None)
    
    
    file_extension_list = ["png", "jpg", "jpeg","bmp","dib","gif","tif","jfif"]
    fileNotMatched = False
    if data:
        file_name = str(data).split(".")
        file_name_ext = file_name[len(file_name)-1]
        for ext in file_extension_list:
            if( file_name_ext.lower()  == ext):
                fileNotMatched = True
                break
                
    if copy_from:
        file_name = str(copy_from).split(".")
        file_name_ext = file_name[len(file_name)-1]
        for ext in file_extension_list:
            if( file_name_ext.lower()  == ext):
                fileNotMatched = True
                break

    
    if(fileNotMatched):
        print ("\n**INVALID IMAGE ============ CHECK IMAGE ext.")
        error_message = "You must provide valid image type (.png, .jpeg, .bmp are not valid)"
        messages.error(request, error_message)
        return False
    
    

    image = glanceclient(request).images.create(**kwargs)

    if data:
        if isinstance(data, TemporaryUploadedFile):
            # Hack to fool Django, so we can keep file open in the new thread.
            data.file.close_called = True
        if isinstance(data, InMemoryUploadedFile):
            # Clone a new file for InMemeoryUploadedFile.
            # Because the old one will be closed by Django.
            data = SimpleUploadedFile(data.name,
                                      data.read(),
                                      data.content_type)
        thread.start_new_thread(image_update,
                                (request, image.id),
                                {'data': data,
                                 'purge_props': False})
    elif copy_from:
        thread.start_new_thread(image_update,
                                (request, image.id),
                                {'copy_from': copy_from,
                                 'purge_props': False})
    elif location:
        thread.start_new_thread(image_update,
                                (request, image.id),
                                {'location': location,
                                 'purge_props': False})

    return image
Ejemplo n.º 46
0
class CVResumeTests(APITestCase):
	"""
	Tests CVResume model.
	"""

	def setUp(self):
		self.user = create_user()

		# Create a file like objects representing uploaded data
		self.cv = SimpleUploadedFile('my_cv.pdf', b'Impresive CV')
		self.resume = SimpleUploadedFile('my_resume.pdf', b'Impresive Resume')

	def tearDown(self):
		"""
		Close open files.
		"""
		self.cv.close()
		self.resume.close()

	def test_cv_created(self):
		"""
		Test whether a cv is saved correctly.
		"""

		# Pass a cv file object to CVResume to save to db/disk
		CVResume.objects.create(
			user=self.user,
			cv=self.cv
		)
		self.cv.open() # Reopen the cv object - set seek to 0
		cv_resume = CVResume.objects.get(pk=1)
		self.assertEqual(
			self.cv.read(), cv_resume.cv.read(), # Compare file like objects
			'Created cv does not match the provided one.'
		)

	def test_resume_created(self):
		"""
		Test whether a resume is saved correctly.
		"""

		# Pass a resume file object to CVResume to save to db/disk
		CVResume.objects.create(
			user=self.user,
			resume=self.resume
		)
		self.resume.open() # Reopen the resume object - set seek to 0
		cv_resume = CVResume.objects.get(pk=1)
		self.assertEqual(
			self.resume.read(), cv_resume.resume.read(), # Compare file like objects
			'Created reusme does not match the provided one.'
		)

	def test_cv_and_resume_created(self):
		"""
		Test whether both cv and resume are saved correctly.
		"""
		CVResume.objects.create(
			user=self.user,
			cv=self.cv,
			resume=self.resume
		)
		# Set file positions to the begining
		self.cv.open()
		self.resume.open()

		# Retrieve the saved object
		cv_resume = CVResume.objects.get(pk=1)

		# Check if cv match
		self.assertEqual(
			self.cv.read(), cv_resume.cv.read(), # Compare file like objects
			'Created cv does not match the provided one.'
		)

		# Check if resume match
		self.assertEqual(
			self.resume.read(), cv_resume.resume.read(), # Compare file like objects
			'Created reusme does not match the provided one.'
		)
Ejemplo n.º 47
0
 def save_image(self):
     filename = self.get_filename()
     self.change_image()
     res = self.store_image()
     res = SimpleUploadedFile(filename, res.read())
     return (filename, res)
Ejemplo n.º 48
0
class CVN(models.Model):

    cvn_file = models.FileField(_(u'PDF'), upload_to=get_cvn_path)

    xml_file = models.FileField(_(u'XML'), upload_to=get_cvn_path)

    fecha = models.DateField(_("Date of CVN"))

    created_at = models.DateTimeField(_("Created"), auto_now_add=True)

    updated_at = models.DateTimeField(_("Updated"), auto_now=True)

    uploaded_at = models.DateTimeField(_("Uploaded PDF"),
                                       default=datetime.datetime(2014, 10, 18))

    user_profile = models.OneToOneField(UserProfile)

    status = models.IntegerField(_("Status"), choices=st_cvn.CVN_STATUS)

    is_inserted = models.BooleanField(_("Inserted"), default=False)

    def __init__(self, *args, **kwargs):
        user = kwargs.pop('user', None)
        pdf_path = kwargs.pop('pdf_path', None)
        pdf = kwargs.pop('pdf', None)
        super(CVN, self).__init__(*args, **kwargs)
        if user:
            self.user_profile = user.profile
        if pdf_path:
            pdf_file = open(pdf_path)
            pdf = pdf_file.read()
        if pdf and user:
            self.update_from_pdf(pdf, commit=False)

    def update_from_pdf(self, pdf, commit=True):
        name = 'CVN-' + self.user_profile.documento
        (xml, error) = fecyt.pdf2xml(pdf, name)
        if not error:
            CVN.remove_cvn_by_userprofile(self.user_profile)
            self.cvn_file = SimpleUploadedFile(name,
                                               pdf,
                                               content_type="application/pdf")
            self.initialize_fields(xml, commit)

    def update_from_xml(self, xml, commit=True):
        pdf = fecyt.xml2pdf(xml)
        if pdf:
            self.update_from_pdf(pdf, commit)

    def initialize_fields(self, xml, commit=True):
        # Warning: The filename is ignored by your extension is needed
        self.xml_file.save(u'fake-filename.xml', ContentFile(xml), save=False)
        tree_xml = etree.XML(xml)
        self.fecha = parse_date(tree_xml.find('Version/VersionID/Date'))
        self.is_inserted = False
        self.uploaded_at = datetime.datetime.now()
        self.update_status(commit)
        self.xml_file.close()
        if commit:
            self.save()

    def update_status(self, commit=True):
        status = self.status
        if self.fecha <= config.EXPIRY_DATE:
            self.status = st_cvn.CVNStatus.EXPIRED
        elif not self._is_valid_identity():
            self.status = st_cvn.CVNStatus.INVALID_IDENTITY
        else:
            self.status = st_cvn.CVNStatus.UPDATED
        if self.status != status and commit:
            self.save()

    def _is_valid_identity(self):
        try:
            if self.xml_file.closed:
                self.xml_file.open()
        except IOError:
            return False
        xml_tree = etree.parse(self.xml_file)
        self.xml_file.seek(0)
        nif = parse_nif(xml_tree)
        self.xml_file.close()
        for character in [' ', '-']:
            if character in nif:
                nif = nif.replace(character, '')
        if nif.upper() == self.user_profile.documento.upper():
            return True
        # NIF/NIE without final letter
        if len(nif) == 8 and nif == self.user_profile.documento[:-1]:
            return True
        return False

    @classmethod
    def get_user_pdf_ull(cls, user, start_date=None, end_date=None):
        if user.profile.rrhh_code is None:
            return None
        parser = CvnXmlWriter(user=user)

        learning = cls._insert_learning(user, parser, start_date, end_date)

        cargos = cls._insert_profession(st.WS_ULL_CARGOS, user, parser,
                                        start_date, end_date)

        contratos = cls._insert_profession(st.WS_ULL_CONTRATOS, user, parser,
                                           start_date, end_date)

        docencia = cls._insert_teaching(user, parser, start_date, end_date)

        if not learning and not cargos and not contratos and not docencia:
            return None

        xml = parser.tostring()
        return fecyt.xml2pdf(xml)

    @classmethod
    def _insert_learning(cls, user, parser, start_date, end_date):
        items = ws.get(url=st.WS_ULL_LEARNING %
                       user.profile.id_without_control_digit,
                       use_redis=True)
        counter = 0
        if items is None:
            return counter
        for item in items:
            values = item.copy()
            cls._clean_data_learning(values)
            item_date_range = DateRange(values[u'f_expedicion'],
                                        values[u'f_expedicion'])
            if not item_date_range.intersect(DateRange(start_date, end_date)):
                continue
            if (u'des1_grado_titulacion' in item
                    and item[u'des1_grado_titulacion'].upper() == u'DOCTOR'):
                del values[u'des1_grado_titulacion']
                parser.add_learning_phd(**values)
            else:
                parser.add_learning(**values)
            counter += 1
        return counter

    @staticmethod
    def _clean_data_learning(item):
        if u'f_expedicion' in item and item[u'f_expedicion'] is not None:
            item[u'f_expedicion'] = dateutil.parser.parse(
                item[u'f_expedicion']).date()
        else:
            item[u'f_expedicion'] = None

    @classmethod
    def _insert_profession(cls, ws_url, user, parser, start_date, end_date):
        items = ws.get(url=ws_url % user.profile.id_without_control_digit,
                       use_redis=True)
        counter = 0
        if items is None:
            return counter
        for item in items:
            values = item.copy()
            cls._clean_data_profession(values)
            if u'f_toma_posesion' in item:
                initial_date = values[u'f_toma_posesion']
            else:
                initial_date = values[u'f_desde']
            item_date_range = DateRange(initial_date, values[u'f_hasta'])
            if not item_date_range.intersect(DateRange(start_date, end_date)):
                continue
            parser.add_profession(**values)
            counter += 1
        return counter

    @staticmethod
    def _clean_data_profession(item):
        if u'f_toma_posesion' in item and item[u'f_toma_posesion'] is not None:
            item[u'f_toma_posesion'] = dateutil.parser.parse(
                item[u'f_toma_posesion']).date()

        if u'f_desde' in item and item[u'f_desde'] is not None:
            item[u'f_desde'] = dateutil.parser.parse(item[u'f_desde']).date()

        if u'f_hasta' in item and item[u'f_hasta'] is not None:
            item[u'f_hasta'] = dateutil.parser.parse(item[u'f_hasta']).date()
        else:
            item[u'f_hasta'] = None

        if u'des1_dedicacion' in item:
            dedicacion = item[u'des1_dedicacion'].upper()
            item[u'des1_dedicacion'] = dedicacion == u'TIEMPO COMPLETO'

    @classmethod
    def _insert_teaching(cls, user, parser, start_date, end_date):
        items = ws.get(url=st.WS_ULL_TEACHING % user.profile.rrhh_code,
                       use_redis=True)
        counter = 0
        if items is None:
            return counter
        for item in items:
            values = item.copy()
            date = datetime.date(int(values[u'curso_inicio']), 1, 1)
            item_date_range = DateRange(date, date)
            if not item_date_range.intersect(DateRange(start_date, end_date)):
                continue
            parser.add_teaching(**values)
            counter += 1
        return counter

    @staticmethod
    def create(user, xml=None):
        if not xml:
            parser = CvnXmlWriter(user=user)
            xml = parser.tostring()
        pdf = fecyt.xml2pdf(xml)
        if pdf is None:
            return None
        cvn = CVN(user=user, pdf=pdf)
        cvn.save()
        return cvn

    @classmethod
    def remove_cvn_by_userprofile(cls, user_profile):
        try:
            cvn_old = cls.objects.get(user_profile=user_profile)
            cvn_old.remove()
        except ObjectDoesNotExist:
            pass

    def remove(self):
        # Removes data related to CVN that is not on the CVN class.
        self._backup_pdf()
        try:
            if self.cvn_file:
                self.cvn_file.delete(False)  # Remove PDF file
        except IOError as e:
            logger.error(str(e))
        try:
            if self.xml_file:
                self.xml_file.delete(False)  # Remove XML file
        except IOError as e:
            logger.error(str(e))

    def _backup_pdf(self):
        filename = OldCvnPdf.create_filename(self.cvn_file.name,
                                             self.uploaded_at)
        try:
            old_cvn_file = SimpleUploadedFile(filename,
                                              self.cvn_file.read(),
                                              content_type="application/pdf")
        except IOError as e:
            logger.error(e.message)
        else:
            cvn_old = OldCvnPdf(user_profile=self.user_profile,
                                cvn_file=old_cvn_file,
                                uploaded_at=self.uploaded_at)
            cvn_old.save()

    def remove_producciones(self):
        Articulo.remove_by_userprofile(self.user_profile)
        Libro.remove_by_userprofile(self.user_profile)
        Capitulo.remove_by_userprofile(self.user_profile)
        Congreso.remove_by_userprofile(self.user_profile)
        Proyecto.remove_by_userprofile(self.user_profile)
        Convenio.remove_by_userprofile(self.user_profile)
        TesisDoctoral.remove_by_userprofile(self.user_profile)
        Patente.remove_by_userprofile(self.user_profile)

    def insert_xml(self):
        try:
            if self.xml_file.closed:
                self.xml_file.open()
            self.xml_file.seek(0)
            cvn_items = etree.parse(self.xml_file).findall('CvnItem')
            self._parse_cvn_items(cvn_items)
            self.is_inserted = True
            self.save()
        except IOError:
            if self.xml_file:
                logger.error(
                    (u'No existe el fichero' + u' %s') % (self.xml_file.name))
            else:
                logger.warning(u'Se requiere de un fichero CVN-XML')

    def _parse_cvn_items(self, cvn_items):
        for cvnitem in cvn_items:
            produccion = parse_cvnitem_to_class(cvnitem)
            if produccion is None:
                continue
            produccion.objects.create(cvnitem, self.user_profile)

    def update_document_in_path(self):
        # Latest CVN
        relative_pdf_path = get_cvn_path(self, u'fake.pdf')
        full_pdf_path = os_path_join(st.MEDIA_ROOT, relative_pdf_path)
        xml_path = get_cvn_path(self, u'fake.xml')
        full_xml_path = os_path_join(st.MEDIA_ROOT, xml_path)
        root_path = '/'.join(full_pdf_path.split('/')[:-1])
        if not os_path_isdir(root_path):
            makedirs(root_path)
        if self.cvn_file.path != full_pdf_path:
            file_move_safe(self.cvn_file.path,
                           full_pdf_path,
                           allow_overwrite=True)
            self.cvn_file.name = relative_pdf_path
        if self.xml_file.path != full_xml_path:
            file_move_safe(self.xml_file.path,
                           full_xml_path,
                           allow_overwrite=True)
            self.xml_file.name = xml_path
        self.save()

    def __unicode__(self):
        return '%s ' % self.cvn_file.name.split('/')[-1]

    class Meta:
        verbose_name_plural = _("Normalized Curriculum Vitae")
        ordering = ['-uploaded_at', '-updated_at']
Ejemplo n.º 49
-16
 def test_saving_and_retrieving_files(self):
     root = DummyFolder().create_dummy_root_folder()
     file = SimpleUploadedFile("file.txt", b"file_content")
     expected = file.read()
     DummyFolder().create_dummy_report(root, file=file)
     saved_report = Report.objects.first()
     # Перевіряємо, чи збереглася первинна назва файла
     self.assertEqual(saved_report.filename, "file.txt")
     # Чи правильний фактичний шлях до файла
     basename = os.path.basename(saved_report.file.path)
     self.assertEqual(basename, "1.data")
     # Час створення (до секунди) співпадає з поточним?
     self.assertAlmostEqual(saved_report.uploaded_on, now(), delta=timedelta(minutes=1))
     # Вмісти збереженого файда і первинного співпадають?
     self.assertEqual(saved_report.file.read(), expected)
     # Видляємо з диска (бо файл по-чесному записався в /uploads/folders/0/1.data)
     saved_report.file.delete()