<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals

import os, json

from django.conf import settings
from django.core.urlresolvers import reverse
from django.core import signing
from django.core.files.uploadedfile import InMemoryUploadedFile, TemporaryUploadedFile
from django.core.files.storage import FileSystemStorage
from django.forms import forms
from django.test import override_settings, TestCase
from django.test.client import Client

from pyquery.pyquery import PyQuery

from djng import app_settings
from djng.forms import NgModelFormMixin
from djng.forms.fields import ImageField


class TestUploadForm(NgModelFormMixin, forms.Form):
    scope_prefix = 'my_data'
    form_name = 'my_form'

    avatar = ImageField()



class FileUploadTest(TestCase):
    signer = signing.Signer()
    storage = app_settings.upload_storage

    def tearDown(self):
        try:
            os.remove(os.path.join(settings.MEDIA_ROOT, 'upload_temp/sample-image.jpg'))
        except:
            pass

    def upload_image(self):
        client = Client()
        upload_filename = os.path.join(os.path.dirname(__file__), 'sample-image.jpg')
        with open(upload_filename) as fp:
            upload_url = reverse('fileupload')
            response = client.post(upload_url, {'file:0': fp})
        self.assertEquals(response.status_code, 200)
        return json.loads(response.content)

    def test_upload(self):
        content = self.upload_image()
        self.assertTrue('file:0' in content)
        self.assertTrue(content['file:0']['url'].startswith('url(data:application/octet-stream;base64,/9j/4AAQSkZJRgABA'))
        self.assertEquals(content['file:0']['image_name'], 'sample-image.jpg')
        self.assertEquals(content['file:0']['content_type'], 'image/jpeg')
        self.assertEquals(self.signer.unsign(content['file:0']['temp_name']), 'sample-image.jpg')

    def test_render_widget(self):
        form = TestUploadForm()
        htmlsource = form.as_p()
        dom = PyQuery(htmlsource)
        textarea = dom('div.drop-box textarea')
        self.assertEquals(textarea.attr('fileupload-url'), reverse('fileupload'))
        self.assertEquals(textarea.attr('ngf-drop'), 'uploadFiles($files)')
        self.assertEquals(textarea.attr('ngf-select'), 'uploadFiles($files)')

        delete_button = dom('div.drop-box span')
        self.assertEquals(delete_button.attr('djng-fileupload-button'), "my_data['avatar']")<commit_msg>open image file as binary<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals

import os, json

from django.conf import settings
from django.core.urlresolvers import reverse
from django.core import signing
from django.core.files.uploadedfile import InMemoryUploadedFile, TemporaryUploadedFile
from django.forms import forms
from django.test import override_settings, TestCase
from django.test.client import Client

from pyquery.pyquery import PyQuery

from djng import app_settings
from djng.forms import NgModelFormMixin
from djng.forms.fields import ImageField


class TestUploadForm(NgModelFormMixin, forms.Form):
    scope_prefix = 'my_data'
    form_name = 'my_form'

    avatar = ImageField()



class FileUploadTest(TestCase):
    signer = signing.Signer()
    storage = app_settings.upload_storage

    def tearDown(self):
        try:
            os.remove(os.path.join(settings.MEDIA_ROOT, 'upload_temp/sample-image.jpg'))
        except:
            pass

    def upload_image(self):
        client = Client()
        upload_filename = os.path.join(os.path.dirname(__file__), 'sample-image.jpg')
        with open(upload_filename, 'rb') as fp:
            upload_url = reverse('fileupload')
            response = client.post(upload_url, {'file:0': fp})
        self.assertEquals(response.status_code, 200)
        return json.loads(response.content)

    def test_upload(self):
        content = self.upload_image()
        self.assertTrue('file:0' in content)
        self.assertTrue(content['file:0']['url'].startswith('url(data:application/octet-stream;base64,/9j/4AAQSkZJRgABA'))
        self.assertEquals(content['file:0']['image_name'], 'sample-image.jpg')
        self.assertEquals(content['file:0']['content_type'], 'image/jpeg')
        self.assertEquals(self.signer.unsign(content['file:0']['temp_name']), 'sample-image.jpg')

    def test_render_widget(self):
        form = TestUploadForm()
        htmlsource = form.as_p()
        dom = PyQuery(htmlsource)
        textarea = dom('div.drop-box textarea')
        self.assertEquals(textarea.attr('fileupload-url'), reverse('fileupload'))
        self.assertEquals(textarea.attr('ngf-drop'), 'uploadFiles($files)')
        self.assertEquals(textarea.attr('ngf-select'), 'uploadFiles($files)')

        delete_button = dom('div.drop-box span')
        self.assertEquals(delete_button.attr('djng-fileupload-button'), "my_data['avatar']")
{'fraction_non_alphanumeric': 0.12357259380097879, 'fraction_numerical': 0.005709624796084829, 'mean_word_length': 3.813542688910697, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2544344', 'n_tokens_mistral': 1481, 'n_tokens_neox': 1442, 'n_words': 278}