Exemplo n.º 1
0
 def hashed_name(self, name, content=None, filename=None):
     parsed_name = urlsplit(unquote(name))
     clean_name = parsed_name.path.strip()
     opened = False
     if content is None:
         absolute_path = finders.find(clean_name)
         try:
             content = open(absolute_path, 'rb')
         except (IOError, OSError) as e:
             if e.errno == errno.ENOENT:
                 raise ValueError(
                     "The file '%s' could not be found with %r." %
                     (clean_name, self))
             else:
                 raise
         content = File(content)
         opened = True
     try:
         file_hash = self.file_hash(clean_name, content)
     finally:
         if opened:
             content.close()
     path, filename = os.path.split(clean_name)
     root, ext = os.path.splitext(filename)
     if file_hash is not None:
         file_hash = ".%s" % file_hash
     hashed_name = os.path.join(path, "%s%s%s" % (root, file_hash, ext))
     unparsed_name = list(parsed_name)
     unparsed_name[2] = hashed_name
     # Special casing for a @font-face hack, like url(myfont.eot?#iefix")
     # http://www.fontspring.com/blog/the-new-bulletproof-font-face-syntax
     if '?#' in name and not unparsed_name[3]:
         unparsed_name[2] += '?'
     return urlunsplit(unparsed_name)
Exemplo n.º 2
0
 def hashed_name(self, name, content=None, filename=None):
     parsed_name = urlsplit(unquote(name))
     clean_name = parsed_name.path.strip()
     opened = False
     if content is None:
         absolute_path = finders.find(clean_name)
         try:
             content = open(absolute_path, 'rb')
         except (IOError, OSError) as e:
             if e.errno == errno.ENOENT:
                 raise ValueError("The file '%s' could not be found with %r." % (clean_name, self))
             else:
                 raise
         content = File(content)
         opened = True
     try:
         file_hash = self.file_hash(clean_name, content)
     finally:
         if opened:
             content.close()
     path, filename = os.path.split(clean_name)
     root, ext = os.path.splitext(filename)
     if file_hash is not None:
         file_hash = ".%s" % file_hash
     hashed_name = os.path.join(path, "%s%s%s" % (root, file_hash, ext))
     unparsed_name = list(parsed_name)
     unparsed_name[2] = hashed_name
     # Special casing for a @font-face hack, like url(myfont.eot?#iefix")
     # http://www.fontspring.com/blog/the-new-bulletproof-font-face-syntax
     if '?#' in name and not unparsed_name[3]:
         unparsed_name[2] += '?'
     return urlunsplit(unparsed_name)
Exemplo n.º 3
0
 def test_forward_dependency(self):
     instance = DependencyTesting.objects.create()
     lenna_rect = File(open(add_base("media/static/images/lenna_rect.jpg"), 'rb'))
     instance.image_3 = lenna_rect
     instance.image_4 = lenna_rect
     instance.save()
     image_3_path = instance.image_3.path
     image_4_path = instance.image_4.path
     self.assertEqual(instance.image_3.width, 400)
     self.assertEqual(instance.image_4.width, 400)
     self.assertEqual(
         instance.image_3.url,
         "/media/test_app/dependencytesting/%s/image_3.jpg" % instance.pk)
     self.assertEqual(
         instance.image_4.url,
         "/media/test_app/dependencytesting/%s/image_4.jpg" % instance.pk)
     instance.image_2 = lenna_rect
     self.assertTrue(os.path.isfile(image_3_path))
     self.assertTrue(os.path.isfile(image_4_path))
     instance.save()
     lenna_rect.close()
     self.assertEqual(instance.image_3.width, 100)
     self.assertEqual(instance.image_4.width, 150)
     # forward dependencies on django's FileFields will also do the cleanup
     self.assertFalse(os.path.isfile(image_3_path))
     self.assertFalse(os.path.isfile(image_4_path))
     instance.delete()
Exemplo n.º 4
0
 def test_image_processor(self):
     instance = ImageTesting.objects.create()
     lenna_rect = File(open(add_base("media/static/images/lenna_rect.jpg"), 'rb'))
     instance.image_3 = lenna_rect
     instance.save()
     # make sure conversion went through properly
     self.assertEquals(instance.image_3_png.width, 200)
     self.assertEquals(instance.image_3_png.height, 112)
     # save instance, so files get commited to storage
     path = instance.image_3.path
     path_png = instance.image_3_png.path
     # check to see that files got commited
     self.assertTrue(os.path.isfile(path))
     self.assertTrue(os.path.isfile(path_png))
     # make sure dependency gets reattached as expected
     instance = ImageTesting.objects.get(pk=instance.pk)
     self.assertEquals(instance.image_3_png.width, 200)
     self.assertEquals(instance.image_3_png.height, 112)
     self.assertTrue(os.path.isfile(path))
     self.assertTrue(os.path.isfile(path_png))
     # test problematic processor (JPEG2000 is missing a required library)
     instance.image_4 = lenna_rect
     instance.save()
     # check to see that files got commited
     # It is possible than `libjpeg` isn't installed which will cause the test to fail
     self.assertEquals(instance.image_4_jpeg2000.width, 400)
     self.assertEquals(instance.image_4_jpeg2000.height, 225)
     lenna_rect.close()
     # delete instance and check if everything is cleaned up
     instance.delete()
     self.assertFalse(os.path.isfile(path))
     self.assertFalse(os.path.isfile(path_png))
Exemplo n.º 5
0
    def test_manage_update_with_existing_version(self):
        """Check if creation of a new document succeeds even if there is a version to update """
        from django.core.files.base import File

        document = Document()
        document.title = "Wachtmeister Studer"
        document.author = "Friedrich Glauser"
        document.source_publisher = "Diogenes"
        document.save()

        user = User.objects.get(pk=1)

        versionFile = File(open(os.path.join(TEST_DATA_DIR, 'test.xml')))
        version = Version.objects.create(
            comment = "testing 123",
            document = document,
            created_by = user)
        version.content.save("updated_version.xml", versionFile)
        versionFile.close()

        self.client.login(username='******', password='******')
        response = self.client.post(reverse('manage_update', args=[document.pk]), {
                'title': 'testing 456',
                'language': 'de'
                })
        self.assertTemplateNotUsed(response, 'documents/manage_update.html')
        self.assertRedirects(response, reverse('manage_index'))
Exemplo n.º 6
0
 def test_code_body(self):
     """
     Tests when codefile was uploaded, read file and set as body.
     """
     category = Category.objects.create(label=u"ソースコード")
     f = File(tempfile.NamedTemporaryFile(
         mode="r+w+t", suffix=".py", 
         dir=settings.TEST_TEMPORARY_FILE_DIR
     ))
     
     body = """
         import this
         print "this is a sample code file"
         hoge = 1 + 1
     """
     f.write(body)
     material_file = MaterialFile.objects.create(file=f)
     code = Material.objects.create(
             _file=material_file, 
             description="description", 
             category=category
     )
     print code.body
     f.close()
     eq_(code.body, body)
     code.delete()
Exemplo n.º 7
0
    def setUp(self):
        user_1 = User.objects.create(username='******')
        user_1.set_password('password-1')
        user_1.save()

        file_hash_1 = File(open(MEDIA_ROOT + '/test_file_1', 'w'))
        file_hash_1.write('Хеш-1')
        file_hash_1.close()
        file_hash_1.open('r')

        order_1 = Order.objects.create(title='Название-1',
                                       text='Текст-1',
                                       date=timezone.now(),
                                       author=user_1,
                                       order_hash=file_hash_1,
                                       is_closed=False)
        file_hash_1.close()

        self.order_1 = order_1

        for i in range(2):
            UserOrder.objects.create(user=user_1,
                                     order=order_1,
                                     is_accepted=False,
                                     is_completed=False)

        Signature.objects.create(signer=user_1,
                                 order=order_1,
                                 is_correct=False)
Exemplo n.º 8
0
def student_register_page(request):
    if request.method == "POST":
        form = RegistrationForm(request.POST)
        if form.is_valid():
            user = User.objects.create_user(
                username=form.cleaned_data["username"],
                password=form.cleaned_data["password1"],
                email=form.cleaned_data["email"],
            )
            fileinit = open("%s/%s" % (STATIC_ROOT, "images/student.gif"), "rb")
            filea = File(fileinit)
            filename = filea.name
            name = filename.split(".")
            filea.name = user.username + "_avatar_student" + "." + name[len(name) - 1]
            user_profile = UserProfile.objects.create(
                user=user, fullname=form.cleaned_data["fullname"], typeUser="******", avatar=filea
            )
            filea.close()
            user_profile.save()
            LessonReference.objects.create(user=user_profile)
            return render_to_response("registration/teacher_signup_success.html", RequestContext(request))
    else:
        form = RegistrationForm()
    variables = RequestContext(request, {"form": form})
    return render_to_response("registration/student_signup.html", variables)
Exemplo n.º 9
0
def generateGhostDocumentFile(product, document, locker):
    """
    :param product: :class:`.Product` that represents the arborescense
    :param Doc_controller: :class:`.Document3DController` from which we want to generate the :class:`.DocumentFile`


    For one :class:`.Product` (**product**) and one :class:`.Document3DController` (**Doc_controller**), generates a :class:`.DocumentFile` with a file .stp emptily without indexation

    It updates the attributes **doc_id** and **doc_path** of the :class:`.Product` (**product**) in relation of the generated :class:`.DocumentFile`

    """
    doc_file = pmodels.DocumentFile()
    name = doc_file.file.storage.get_available_name(product.name+".stp")
    path = os.path.join(doc_file.file.storage.location, name)
    f = File(open(path.encode(), 'w'))
    f.close()
    doc_file.no_index = True
    doc_file.filename = "Ghost.stp"
    doc_file.size = f.size
    doc_file.file = name
    doc_file.document = document
    doc_file.locked = True
    doc_file.locker = locker
    doc_file.save()
    product.doc_id = doc_file.id
    product.doc_path = doc_file.file.path
    return doc_file.file.path
Exemplo n.º 10
0
 def test_forward_dependency(self):
     instance = DependencyTesting.objects.create()
     lenna_rect = File(open(add_base("static/images/lenna_rect.jpg"), 'rb'))
     instance.image_3 = lenna_rect
     instance.image_4 = lenna_rect
     instance.save()
     image_3_path = instance.image_3.path
     image_4_path = instance.image_4.path
     self.assertEqual(instance.image_3.width, 400)
     self.assertEqual(instance.image_4.width, 400)
     self.assertEqual(
         instance.image_3.url,
         "/media/tests/dependencytesting/%s/image_3.jpg" % instance.pk)
     self.assertEqual(
         instance.image_4.url,
         "/media/tests/dependencytesting/%s/image_4.jpg" % instance.pk)
     instance.image_2 = lenna_rect
     self.assertTrue(os.path.isfile(image_3_path))
     self.assertTrue(os.path.isfile(image_4_path))
     instance.save()
     lenna_rect.close()
     self.assertEqual(instance.image_3.width, 100)
     self.assertEqual(instance.image_4.width, 150)
     # forward dependencies on django's FileFields will also do the cleanup
     self.assertFalse(os.path.isfile(image_3_path))
     self.assertFalse(os.path.isfile(image_4_path))
     instance.delete()
Exemplo n.º 11
0
    def store_file(self, filename):
        storage = self.get_storage()

        f = File(open(filename))
        try:
            storage.save(os.path.basename(filename), f)
        finally:
            f.close()
Exemplo n.º 12
0
def save_text_to_file(filename=None, path=None, content=None):
    if path is None:
        path = settings.MEDIA_ROOT
    if filename is None:
        raise ValueError('Filename required.')
    full_path = os.path.join(path, filename)
    f = open(full_path, 'w')
    file = File(f)
    file.write(content)
    file.close()
Exemplo n.º 13
0
def export_xml_by_source(request, dataset_id):
    """Call export API with this dataset_id, combine paginated responses"""

    if not dataset_id:
        return

    base_url = request.build_absolute_uri(
            reverse('export:activity-export')
        ) + "?dataset={dataset_id}&format=xml&page_size=100&page={page}".format(  # NOQA: E501
            dataset_id=dataset_id, page="{page}"
        )

    def get_result(xml, page_num):
        print('making request, page: ' + str(page_num))
        rf = RequestFactory()
        req = rf.get(base_url.format(page=page_num))

        view = IATIActivityList.as_view()(req).render()
        xml.extend(etree.fromstring(view.content).getchildren())

        link_header = view.get('link')

        if not link_header:
            return xml

        link = requests.utils.parse_header_links(link_header)
        has_next = reduce(lambda acc, x: acc or (
            x['rel'] == 'next'), link, False)

        if has_next:
            return get_result(xml, page_num + 1)
        else:
            return xml

    xml = E('iati-activities', version="2.02")

    final_xml = get_result(xml, 1)
    final_xml.attrib[
        'generated-datetime'
    ] = datetime.datetime.now().isoformat()

    from django.core.files.base import File
    from django.conf import settings
    import uuid

    file_name = "{}.xml".format(uuid.uuid4())
    path = "{}/{}".format(settings.MEDIA_ROOT, file_name)

    xml_file = File(open(path, mode='w'))
    xml_file.write(etree.tostring(final_xml, pretty_print=True))
    xml_file.close()

    return file_name
Exemplo n.º 14
0
def job_save(request):
    if request.POST:
      form = add_job_form(request.POST)
      Job = form.save(request.POST)
      f = open('%s/%s' %(settings.SCRIPTS_DIR,Job.name) , 'w')
      myfile = File(f)
      content = '#!/bin/sh \nRecips=" %s " \nSubj=" %s " \nBody="%s" \nFromDir="%s" \n source sys/mailer.sh \n ' % ( Job.recips , Job.subj, Job.body ,Job.fromdir )
      myfile.write(content)
      f.close()
      myfile.close()
      os.chmod('%s/%s' %(settings.SCRIPTS_DIR,Job.name),stat.S_IRWXU)
      return redirect ('/mailer/viewjob/')
Exemplo n.º 15
0
def export_xml_by_source(request, dataset_id):
    """Call export API with this dataset_id, combine paginated responses"""

    if not dataset_id:
        return

    base_url = request.build_absolute_uri(
        reverse('export:activity-export')
    ) + "?dataset={dataset_id}&format=xml&page_size=100&page={page}".format(  # NOQA: E501
        dataset_id=dataset_id, page="{page}")

    def get_result(xml, page_num):
        print('making request, page: ' + str(page_num))
        rf = RequestFactory()
        req = rf.get(base_url.format(page=page_num))

        view = IATIActivityList.as_view()(req).render()
        xml.extend(etree.fromstring(view.content).getchildren())

        link_header = view.get('link')

        if not link_header:
            return xml

        link = requests.utils.parse_header_links(link_header)
        has_next = reduce(lambda acc, x: acc or (x['rel'] == 'next'), link,
                          False)

        if has_next:
            return get_result(xml, page_num + 1)
        else:
            return xml

    xml = E('iati-activities', version="2.02")

    final_xml = get_result(xml, 1)
    final_xml.attrib['generated-datetime'] = datetime.datetime.now().isoformat(
    )

    from django.core.files.base import File
    from django.conf import settings
    import uuid

    file_name = "{}.xml".format(uuid.uuid4())
    path = "{}/{}".format(settings.MEDIA_ROOT, file_name)

    xml_file = File(open(path, mode='w'))
    xml_file.write(etree.tostring(final_xml, pretty_print=True))
    xml_file.close()

    return file_name
Exemplo n.º 16
0
def job_save_e(request,job_id):
    if request.POST:
      a = Job.objects.get(id=job_id)
      os.remove('/scripts/%s' %(a.name))
      form = add_job_form(request.POST,instance=a)
      job = form.save(request.POST)
      f = open('%s/%s' %(settings.SCRIPTS_DIR,a.name), 'w')
      myfile = File(f)
      content = '#!/bin/sh \nRecips=" %s " \nSubj=" %s " \nBody="%s" \nFromDir="%s" \n source sys/mailer.sh \n  ' % ( a.recips , a.subj, a.body ,a.fromdir )
      myfile.write(content)
      f.close()
      myfile.close()
      os.chmod('%s/%s' %(settings.SCRIPTS_DIR,a.name),stat.S_IRWXU)
      return redirect ('/mailer/viewjob/')
Exemplo n.º 17
0
 def test_self_dependency(self):
     instance = DependencyTesting.objects.create()
     lenna_rect = File(open(add_base("media/static/images/lenna_rect.jpg"), 'rb'))
     instance.image_1 = lenna_rect
     instance.save()
     lenna_rect.close()
     self.assertEqual(instance.image_1.width, 50)
     self.assertEqual(
         instance.image_1.url,
         "/media/test_app/dependencytesting/%s/image_1.bmp" % instance.pk)
     self.assertEqual(instance.image_1_gif.width, 50)
     self.assertEqual(
         instance.image_1_gif.url,
         "/media/test_app/dependencytesting/%s/image_1_gif.gif" % instance.pk)
     instance.delete()
Exemplo n.º 18
0
 def test_value_restoration_1(self):
     lenna_rect = File(open(add_base("media/static/images/lenna_rect.jpg"), 'rb'))
     text_file = File(open(add_base("media/static/defaults/foo.txt"), 'rb'))
     instance = DependencyTesting.objects.create()
     instance.image_1 = lenna_rect
     instance.save()
     lenna_rect.close()
     image_1 = instance.image_1
     image_1_gif = instance.image_1_gif
     instance.image_1 = text_file
     instance.save()
     text_file.close()
     self.assertIs(instance.image_1, image_1)
     self.assertIs(instance.image_1_gif, image_1_gif)
     instance.delete()
Exemplo n.º 19
0
 def test_value_restoration_1(self):
     lenna_rect = File(open(add_base("static/images/lenna_rect.jpg"), 'rb'))
     text_file = File(open(add_base("static/defaults/foo.txt"), 'rb'))
     instance = DependencyTesting.objects.create()
     instance.image_1 = lenna_rect
     instance.save()
     lenna_rect.close()
     image_1 = instance.image_1
     image_1_gif = instance.image_1_gif
     instance.image_1 = text_file
     instance.save()
     text_file.close()
     self.assertIs(instance.image_1, image_1)
     self.assertIs(instance.image_1_gif, image_1_gif)
     instance.delete()
Exemplo n.º 20
0
 def test_self_dependency(self):
     instance = DependencyTesting.objects.create()
     lenna_rect = File(open(add_base("static/images/lenna_rect.jpg"), 'rb'))
     instance.image_1 = lenna_rect
     instance.save()
     lenna_rect.close()
     self.assertEqual(instance.image_1.width, 50)
     self.assertEqual(
         instance.image_1.url,
         "/media/tests/dependencytesting/%s/image_1.bmp" % instance.pk)
     self.assertEqual(instance.image_1_gif.width, 50)
     self.assertEqual(
         instance.image_1_gif.url,
         "/media/tests/dependencytesting/%s/image_1_gif.gif" % instance.pk)
     instance.delete()
Exemplo n.º 21
0
 def test_file_cleanup_after_delete(self):
     instance = FileTesting.objects.create()
     foo_bar = File(open(add_base("media/static/defaults/foo-bar.txt"), 'r'))
     instance.field_3 = foo_bar
     instance.field_4 = foo_bar
     instance.save()
     foo_bar.close()
     field_3_path = instance.field_3.path
     field_4_path = instance.field_4.path
     self.assertTrue(os.path.isfile(field_3_path))
     self.assertTrue(os.path.isfile(field_4_path))
     instance.delete()
     # testing cleanup without dependencies
     self.assertFalse(os.path.isfile(field_3_path))
     # testing keep_orphans=True
     self.assertTrue(os.path.isfile(field_4_path))
Exemplo n.º 22
0
 def test_file_cleanup_after_delete(self):
     instance = FileTesting.objects.create()
     foo_bar = File(open(add_base("static/defaults/foo-bar.txt"), 'r'))
     instance.field_3 = foo_bar
     instance.field_4 = foo_bar
     instance.save()
     foo_bar.close()
     field_3_path = instance.field_3.path
     field_4_path = instance.field_4.path
     self.assertTrue(os.path.isfile(field_3_path))
     self.assertTrue(os.path.isfile(field_4_path))
     instance.delete()
     # testing cleanup without dependencies
     self.assertFalse(os.path.isfile(field_3_path))
     # testing keep_orphans=True
     self.assertTrue(os.path.isfile(field_4_path))
Exemplo n.º 23
0
 def test_auto_cast_material_type_other(self):
     """
     Tests cast material model to suitable type when other file was uploaded.
     """
     from apps.materials.models import Material
     test_file = File(tempfile.NamedTemporaryFile(
         mode="r+w+t", suffix=".hoge", 
         dir=settings.TEST_TEMPORARY_FILE_DIR
     ))
     test_file.write("hello!hello!")
     material_file = MaterialFile.objects.create(file=test_file)
     material = Material.objects.create(
             _file=material_file, 
             description="description", 
             category=Category.objects.get(pk=1)
     )
     ok_(isinstance(material, Material))
     test_file.close()
Exemplo n.º 24
0
 def test_value_restoration_2(self):
     lenna_rect = File(open(add_base("media/static/images/lenna_rect.jpg"), 'rb'))
     text_file = File(open(add_base("media/static/defaults/foo.txt"), 'rb'))
     instance = DependencyTesting.objects.create()
     instance.image_2 = lenna_rect
     instance.save()
     lenna_rect.close()
     image_3 = instance.image_3
     image_4 = instance.image_4
     # restores values since new file is a text file that cannot be processed
     instance.image_2 = text_file
     instance.save()
     text_file.close()
     self.assertEqual(instance.image_3, image_3)
     self.assertEqual(instance.image_4, image_4)
     self.assertEqual(instance.image_3.path, image_3.path)
     self.assertEqual(instance.image_4.path, image_4.path)
     instance.delete()
Exemplo n.º 25
0
    def rename_file(self, new_file_name,sep='/') :
        # NB: only changes the actual file name
        file = self.resource.file
        file_path = file.name.split(sep)
        old_name = file_path[-1]
        print old_name, ' to ', new_file_name
        
        file_path[-1]=new_file_name
        new_path = sep.join(file_path)

        try :
            f = File(open(file.name,'rb'))
            self.resource = f
            self.resource.name = new_path
            self.save()
            f.close()
            
        except Exception, e :
            print e
Exemplo n.º 26
0
    def rename_file(self, new_file_name, sep='/'):
        # NB: only changes the actual file name
        file = self.resource.file
        file_path = file.name.split(sep)
        old_name = file_path[-1]
        print old_name, ' to ', new_file_name

        file_path[-1] = new_file_name
        new_path = sep.join(file_path)

        try:
            f = File(open(file.name, 'rb'))
            self.resource = f
            self.resource.name = new_path
            self.save()
            f.close()

        except Exception, e:
            print e
Exemplo n.º 27
0
 def test_file_field(self):
     instance = FileTesting.objects.create()
     # test default static
     self.assertEqual(instance.field_1_foo.url, "/static/defaults/foo.txt")
     self.assertEqual(instance.bar.url, "/static/defaults/bar.txt")
     # test default FieldFile set and processed
     self.assertEqual(instance.field_1_foo.read(), force_bytes("FOO\n"))
     self.assertEqual(instance.bar.read(), force_bytes("BAR\n"))
     self.assertEqual(instance.field_2.read(), force_bytes("foo\n"))
     field_2_path = instance.field_2.path
     self.assertTrue(os.path.isfile(field_2_path))
     # test assignment of file
     foo_bar = File(open(add_base("static/defaults/foo-bar.txt"), 'r'))
     instance.field_1 = foo_bar
     instance.save()
     foo_bar.close()
     # make sure default file was not removed
     self.assertTrue(os.path.isfile(field_2_path))
     # check new content
     self.assertEqual(instance.field_1.read(), force_bytes("FOO BAR\n"))
     self.assertEqual(instance.field_1_foo.read(), force_bytes("FOO BAR\n"))
     instance.field_2.seek(0)
     self.assertEqual(instance.field_2.read(), force_bytes("foo\n"))
     # testing setting default value again
     instance.field_2 = None
     instance.save()
     # make sure previous file was removed
     self.assertFalse(os.path.isfile(field_2_path))
     self.assertEqual(instance.field_2.read(), force_bytes("foo bar\n"))
     # test deletion of file together with instance
     field_1_path = instance.field_1.path
     field_1_foo_path = instance.field_1_foo.path
     field_2_path = instance.field_2.path
     self.assertTrue(os.path.isfile(field_1_path))
     self.assertTrue(os.path.isfile(field_1_foo_path))
     self.assertTrue(os.path.isfile(field_2_path))
     instance.delete()
     self.assertFalse(os.path.isfile(field_1_path))
     self.assertFalse(os.path.isfile(field_1_foo_path))
     self.assertFalse(os.path.isfile(field_2_path))
Exemplo n.º 28
0
 def test_file_field(self):
     instance = FileTesting.objects.create()
     # test default static
     self.assertEqual(instance.field_1_foo.url, "/static/defaults/foo.txt")
     self.assertEqual(instance.bar.url, "/static/defaults/bar.txt")
     # test default FieldFile set and processed
     self.assertEqual(instance.field_1_foo.read(), force_bytes("FOO\n"))
     self.assertEqual(instance.bar.read(), force_bytes("BAR\n"))
     self.assertEqual(instance.field_2.read(), force_bytes("foo\n"))
     field_2_path = instance.field_2.path
     self.assertTrue(os.path.isfile(field_2_path))
     # test assignment of file
     foo_bar = File(open(add_base("media/static/defaults/foo-bar.txt"), 'r'))
     instance.field_1 = foo_bar
     instance.save()
     foo_bar.close()
     # make sure default file was not removed
     self.assertTrue(os.path.isfile(field_2_path))
     # check new content
     self.assertEqual(instance.field_1.read(), force_bytes("FOO BAR\n"))
     self.assertEqual(instance.field_1_foo.read(), force_bytes("FOO BAR\n"))
     instance.field_2.seek(0)
     self.assertEqual(instance.field_2.read(), force_bytes("foo\n"))
     # testing setting default value again
     instance.field_2 = None
     instance.save()
     # make sure previous file was removed
     self.assertFalse(os.path.isfile(field_2_path))
     self.assertEqual(instance.field_2.read(), force_bytes("foo bar\n"))
     # test deletion of file together with instance
     field_1_path = instance.field_1.path
     field_1_foo_path = instance.field_1_foo.path
     field_2_path = instance.field_2.path
     self.assertTrue(os.path.isfile(field_1_path))
     self.assertTrue(os.path.isfile(field_1_foo_path))
     self.assertTrue(os.path.isfile(field_2_path))
     instance.delete()
     self.assertFalse(os.path.isfile(field_1_path))
     self.assertFalse(os.path.isfile(field_1_foo_path))
     self.assertFalse(os.path.isfile(field_2_path))
Exemplo n.º 29
0
 def test_image_field_mimic_django(self):
     instance = ImageTesting.objects.create()
     lenna_rect = File(open(add_base("media/static/images/lenna_rect.jpg"), 'rb'),
                       name="lenna_rect.jpg")
     instance.image_1 = lenna_rect
     instance.image_2 = lenna_rect
     instance.save()
     lenna_rect.close()
     # make sure width and heigth values are correct and same as django's
     self.assertEqual(instance.image_1_width, instance.image_2_width)
     self.assertEqual(instance.image_1_height, instance.image_2_height)
     self.assertEqual(instance.image_2_width, 400)
     self.assertEqual(instance.image_2_height, 225)
     # make sure values are saved properly
     instance = ImageTesting.objects.get(pk=instance.pk)
     self.assertEqual(instance.image_2_width, 400)
     self.assertEqual(instance.image_2_height, 225)
     # make sure image is still there and can properly retrieve dims
     self.assertEqual(instance.image_2.width, 400)
     self.assertEqual(instance.image_2.height, 225)
     self.assertEqual(instance.image_1.url, "/media/image_1/lenna_rect.jpg")
     self.assertEqual(instance.image_2.url, "/media/image_2/lenna_rect.jpg")
     # test image replacing
     lenna_square = File(open(add_base("media/static/images/lenna_square.png"), 'rb'))
     instance.image_2 = lenna_square
     self.assertTrue(os.path.isfile(add_base("media/image_2/lenna_rect.jpg")))
     instance.save()
     lenna_square.close()
     self.assertFalse(os.path.isfile(add_base("media/image_2/lenna_rect.jpg")))
     self.assertEqual(instance.image_2.width, 512)
     self.assertEqual(instance.image_2.height, 512)
     instance.image_2 = None
     instance.save()
     self.assertIsNone(instance.image_2_width)
     self.assertIsNone(instance.image_2_height)
     # remove django's ImageFieldFile manually
     instance.image_1.delete()
     instance.delete()
     self.assertFalse(os.path.isfile(add_base("media/image_2/lenna_square.png")))
Exemplo n.º 30
0
 def test_image_processor(self):
     instance = ImageTesting.objects.create()
     lenna_rect = File(open(add_base("static/images/lenna_rect.jpg"), 'rb'))
     instance.image_3 = lenna_rect
     instance.save()
     # make sure conversion went through properly
     self.assertEquals(instance.image_3_png.width, 200)
     self.assertEquals(instance.image_3_png.height, 112)
     # save instance, so files get commited to storage
     path = instance.image_3.path
     path_png = instance.image_3_png.path
     # check to see that files got commited
     self.assertTrue(os.path.isfile(path))
     self.assertTrue(os.path.isfile(path_png))
     # make sure dependency gets reattached as expected
     instance = ImageTesting.objects.get(pk=instance.pk)
     self.assertEquals(instance.image_3_png.width, 200)
     self.assertEquals(instance.image_3_png.height, 112)
     self.assertTrue(os.path.isfile(path))
     self.assertTrue(os.path.isfile(path_png))
     # test problematic processor (JPEG2000 is missing a required library)
     instance.image_4 = lenna_rect
     instance.save()
     self.assertEqual(instance.smartfields_get_field_status('image_4'), {
         'state': 'error', 
         'messages': [
             'ProcessingError: There was a problem with image conversion: encoder '
             'jpeg2k not available'
         ], 
         'app_label': 'tests', 
         'pk': 1, 
         'field_name': 'image_4', 
         'model_name': 'imagetesting'
     })
     lenna_rect.close()
     # delete instance and check if everything is cleaned up
     instance.delete()
     self.assertFalse(os.path.isfile(path))
     self.assertFalse(os.path.isfile(path_png))
Exemplo n.º 31
0
 def test_image_processor(self):
     instance = ImageTesting.objects.create()
     lenna_rect = File(open(add_base("static/images/lenna_rect.jpg"), 'rb'))
     instance.image_3 = lenna_rect
     instance.save()
     # make sure conversion went through properly
     self.assertEquals(instance.image_3_png.width, 200)
     self.assertEquals(instance.image_3_png.height, 112)
     # save instance, so files get commited to storage
     path = instance.image_3.path
     path_png = instance.image_3_png.path
     # check to see that files got commited
     self.assertTrue(os.path.isfile(path))
     self.assertTrue(os.path.isfile(path_png))
     # make sure dependency gets reattached as expected
     instance = ImageTesting.objects.get(pk=instance.pk)
     self.assertEquals(instance.image_3_png.width, 200)
     self.assertEquals(instance.image_3_png.height, 112)
     self.assertTrue(os.path.isfile(path))
     self.assertTrue(os.path.isfile(path_png))
     # test problematic processor (JPEG2000 is missing a required library)
     instance.image_4 = lenna_rect
     instance.save()
     self.assertEqual(instance.smartfields_get_field_status('image_4'), {
         'state': 'error', 
         'messages': [
             'ProcessingError: There was a problem with image conversion: encoder '
             'jpeg2k not available'
         ], 
         'app_label': 'test_app', 
         'pk': 1, 
         'field_name': 'image_4', 
         'model_name': 'imagetesting'
     })
     lenna_rect.close()
     # delete instance and check if everything is cleaned up
     instance.delete()
     self.assertFalse(os.path.isfile(path))
     self.assertFalse(os.path.isfile(path_png))
Exemplo n.º 32
0
def create_resource(top_container, creator, val, f_name, folder) :
    try :
        print "Created by %s" % creator
        title = val.split('.',1)[0]
        name = make_name(title)
        print "Title %s, name %s" % (title,name)
        desc = ''
        license = 'Copyright 2009, Psychosocial Network'
        author = ''
    
        f = File(open('mhpss_export/files/%s'%f_name,'rb'))
    
        resource = get_or_create(creator, top_container,
                             resource=f, title=val, name=name, description=desc,
                             license=license, author=author, stub=False)
        resource.save()
        f.close()
        tag_with_folder_name(resource, creator, folder['title'], 'folder')
        return True
    
    except Exception, e:
        print "******%s",e
        return False
Exemplo n.º 33
0
    def setUpTestData(cls):
        user = User.objects.create(username='******',
                                   first_name='Андрей',
                                   last_name='Петров')
        user.set_password('password')
        user.save()

        user_profile = UserProfile.objects.create(user=user,
                                                  middle_name='Сергеевич')
        user_profile.save()

        file_hash = File(open(MEDIA_ROOT + '/test_file_1', 'w'))
        file_hash.write('Хеш')
        file_hash.close()
        file_hash.open('r')

        order = Order.objects.create(title='Название',
                                     text='Текст',
                                     date=timezone.now(),
                                     author=user,
                                     order_hash=file_hash,
                                     is_closed=False)
        file_hash.close()

        UserOrder.objects.create(user=user,
                                 order=order,
                                 is_accepted=False,
                                 is_completed=False)

        Comment.objects.create(
            user=user,
            order=order,
            date=timezone.now(),
            text=
            'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod'
        )
Exemplo n.º 34
0
    def handle(self, *args, **options):

        Config = ConfigParser.ConfigParser()
        try:
            Config.read('magicsorter.ini')
            port = Config.get('modbus', 'port')
            outbox_num = int(Config.get('sorter', 'outbox_num'))
            video_device = int(Config.get('video', 'device'))
        except:
            cfgfile = open('magicsorter.ini','w')
            # add the settings to the structure of the file, and lets write it out...
            Config.add_section('modbus')
            Config.set('modbus','port','/dev/ttyUSB0')
            Config.add_section('sorter')
            Config.set('sorter','outbox_num','1')
            Config.add_section('video')
            Config.set('video','device','0')
            Config.write(cfgfile)
            cfgfile.close()
            print 'Please check magicsorter.ini and try again!'
            return
                
        ##ModbusServer.startServer()
        #modbus.ModbusServer.init(port)
        #modbus.ModbusServer.startServerAsync()
        
        Sorter.init(outbox_num, port)
        
        Scanner.video_device=video_device
        
        #print('HandleX: Capturing from device %i' % video_device) 
        #cap = cv2.VideoCapture(video_device)
        #print('HandleX: Capturing from device %i done' % video_device) 
    
        
        #if Scan.objects.filter(position__gt=0):
        #    Sorter.sort2()
        #else:
        if True:
            #Card.objects.all().delete()
            Card.objects.all().update(count=0, outbox=0)
            
            Scan.objects.all().delete()
        
            Scanner.start()  
            
            print('Wait for Scanner inicialization ... 3s')
            time.sleep(3)
            #Sorter.start()
            
            #if port == 'None':
            #    port = None
            Sorter.sort(port)
        
            #while Scanner.RUN:
            #    pass
        
            #Sorter.stop()
            #Scanner.stop()
        
        #modbus.ModbusServer.stopServer()
        
        return
    
############################################################################     
        
        #thr = threading.Thread(target= sorter, args=(), kwargs={})
        #thr.start()
        
        
        
        
        
        
        position = 0
        outbox = 0
                
        cap = cv2.VideoCapture(1)
        ret = cap.set(3, 1280)#320) 
        ret = cap.set(4, 720)#240)
        
        ret, frame = cap.read()
        #img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        
        title = 'esc for scan, q for quit'
        cv2.namedWindow(title)
        #cv2.imshow("image", img)
        cv2.setMouseCallback(title, mouseclick)
        
        text = ''
        img1 = None
        
        while True:  
         ret, frame = cap.read()
         img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
         
         
         #print img1
         
         img0 = copy.copy(img)
         
         if img1 is not None:
             height, width = img1.shape 
             img0[0:height, 0:width]=img1
             
             if img2 is not None:
                img0[height:height*2, 0:width]=img2
         #cv2.putText(img0 , text, (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 2, 255)
         cv2.imshow(title, img0)
         k = cv2.waitKey(20) & 0xFF
         #print k
         if k == 113 or SCAN== -1:
             # q
             break
         
         
         
         if k == 27 or SCAN == 1:
             
             
             # esc
           
            #inp = raw_input('Press Enter for next, q for quit: ')
            #if inp == 'q':
             #   break;
            #print 'vfrv'
            #while SCAN == 0:
            #    img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            #    cv2.imshow("image", img)
            #    if cv2.waitKey(20) & 0xFF == 27:
            #        break
            #    pass
            #print 'pass'
            OUTBOX = 0
            SCAN = 0
                #if event == cv2.EVENT_LBUTTONDOWN:
                #    refPt = [(x, y)]
                #    cropping = True
            #TODO for each card in stock
            position = position + 1
        
        
        
            #get image
            #filename = '1.jpg'
            #img = cv2.imread(filename, 1)

            
        
            #ret, frame = cap.read()
            #img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            #img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            #cv2.imshow("image", img)
            
            img_hist = cv2.calcHist([img],[0],None,[256],[0,256])
        
        
        
    
            #match template
            card_id = 0
            cards = Card.objects.order_by('pk')
            #if False:
            for card in cards:
                template = cv2.imread(card.image.name, 1)
            
                #TODO get histogram from from model
                if card.hist:
                    tempalte_hist = pickle.loads(card.hist)
                else:
                    tempalte_hist = cv2.calcHist([template],[0],None,[256],[0,256])
                    card.hist = pickle.dumps(tempalte_hist)
                    card.save()
            
                #TODO compare images
                #method = eval('CV_COMP_CORREL')
                result = cv2.compareHist(img_hist, tempalte_hist, 0)
                #print result
                if result > 0.99:
                    card_id = card.id
                    card.count = card.count + 1
                    card.save()
                    
                    #print card.image.path
                    img1 = cv2.imread(card.image.path, 0)
                    img1 = cv2.resize(img1, (0,0), fx=THUMB_RATIO2, fy=THUMB_RATIO2)   
                    img2 = cv2.resize(img, (0,0), fx=THUMB_RATIO2, fy=THUMB_RATIO2)   
                    
                    
                    text = 'Scanner: match ' + str(card.id)
                    print text
                    break
        
            #print card_id
            if card_id == 0:
                   
                
                card = Card.objects.create()
                
                img1 = cv2.resize(img, (0,0), fx=THUMB_RATIO2, fy=THUMB_RATIO2)
                img2 = None
                
                
                text = 'Scanner: new ' + str(card.id)
                print text
                
                card.count = 1
                outbox = outbox + 1
                card.outbox = outbox
            
                card_id = card.id
                card.hist = pickle.dumps(img_hist)
                        
                tmpfilename = '..jpg'             
                cv2.imwrite(tmpfilename, img)
                f = open(tmpfilename, 'rb')
                content = File(f)
                card.image.save(str(card.id) + '.jpg', content)#, save=True)
                #card.image.save(str(card.id) + '.jpg', cv2.imwrite(img), save=True)
                
                img = cv2.resize(img, (0,0), fx=THUMB_RATIO, fy=THUMB_RATIO)   
                cv2.imwrite(tmpfilename, img)
                f = open(tmpfilename, 'rb')
                content = File(f)
                card.thumb.save(str(card.id) + '.jpg', content)
            
            
                card.save()
                content.close()
                f.close()
            
            
            
            #s = Scan.objects.raw('SELECT * FROM ms_scan WHERE fk_card_id=' + str(card_id) + ' LIMIT 1'):
            #if s:
            #    scanoutbox = s.outbox
            #else:
            #    outbox = outbox + 1
            #    scanoutbox = outbox  
                
                  
            # Data retrieval operation - no commit required
            #from django.db import connection, transaction
            #cursor = connection.cursor()
            #cursor.execute('SELECT * FROM ms_scan WHERE fk_card_id=1')# [self.baz])
            #row = cursor.fetchone()
            #print row
                
            scan = Scan.objects.create(position=position, fk_card=card)
            
            scan.outbox = card.outbox
                
            tmpfilename = '..jpg'   
            img = cv2.resize(img, (0,0), fx=THUMB_RATIO, fy=THUMB_RATIO)   
            cv2.imwrite(tmpfilename, img)
            f = open(tmpfilename, 'rb')
            content = File(f)
            scan.image.save(str(card.id) + '-'+ str(position) + '.jpg', content)
            content.close()
            f.close()
            
            #print '{} / {}'.format(scan.outbox, OUTBOX_NUM)
            if scan.outbox < OUTBOX_NUM:
                scan.position = 0
                
                OUTBOX = scan.outbox + 1
                #put card to outbox
            else:
                #1st outbox for notsorted cards
                OUTBOX = 1
                
            scan.save()

            OUTBOX = scan.outbox + 1
            
        
        cap.release()
        cv2.destroyAllWindows()    
        
       
        thr.join()
        modbus.ModbusServer.stopServer()
          
        return       
        
        hist = cv2.calcHist([img],[0],None,[256],[0,256])
        #print pickle.dumps(hist)
        
        hist = cv2.calcHist([template],[0],None,[256],[0,256])
        #print pickle.dumps(hist)
        

        
        
        plt.subplot(221)
        plt.hist(img.ravel(),256,[0,256]);
        #plt.imshow(img, cmap = 'CMRmap')
        plt.title('ImageH')
        plt.xticks([])
        plt.yticks([])
        
        plt.subplot(222)
        #plt.imshow(template, cmap = 'gray')
        plt.hist(template.ravel(),256,[0,256]);
        plt.title('TemplateH')
        plt.xticks([])
        plt.yticks([])
  
        plt.subplot(223)
        plt.imshow(img, cmap = 'CMRmap')
        plt.title('Image')
        plt.xticks([])
        plt.yticks([])  
        
        plt.subplot(224)
        plt.imshow(template, cmap = 'CMRmap')
        plt.title('Template')
        plt.xticks([])
        plt.yticks([])     
        
        #plt.suptitle(meth)

        plt.show()
Exemplo n.º 35
0
            filename = result_filename

        fp = open(filename)

        print >> logger, "Storing PDF file and updating database..."
        logger.flush()

        pdf = SavedPdf(author=request.user, 
                       configuration=config,
                       comment=comment,
                       data_source=data_source,
                       group=key,
                       cover_letter=request.POST.get('coverletter', ''))
        file = File(fp)
        pdf.file.save("%s-%s.pdf" % (config.pk, request.user), file)
        file.close()
        os.unlink(filename)
        del(fd)
        saved_pdfs.append(pdf)
    
    print >> logger, "PDF generation complete."
    logger.flush()
    logger.close()
    os.unlink(log_filename)

    if request.is_ajax():
        return pdf_table_snippet(request, saved_pdfs) 
    return pdf_table_results(request, saved_pdfs)

@rendered_with("pdfbuilder/pdf_table.html")
def pdf_table_results(request, saved_pdfs):
Exemplo n.º 36
0
 def scan():
     #OUTBOX = 0
     #SCAN = 0
 
     Scanner.position = Scanner.position + 1
             
     img_hist = cv2.calcHist([Scanner.img],[0],None,[256],[0,256])
     
     card_id = 0
     cards = Card.objects.order_by('pk')
 
     for card in cards:
         template = cv2.imread(card.image.name, 1)
         
         if card.hist:
             tempalte_hist = pickle.loads(card.hist)
         else:
             tempalte_hist = cv2.calcHist([template],[0],None,[256],[0,256])
             card.hist = pickle.dumps(tempalte_hist)
             card.save()
         
         #TODO compare images
         #method = eval('CV_COMP_CORREL')
         result = cv2.compareHist(img_hist, tempalte_hist, 0)
         #print result
     
         if result > 0.99:
             card_id = card.id
             card.count = card.count + 1
             card.save()
                 
             Scanner.img1 = cv2.imread(card.image.path, 0)
             Scanner.img1 = cv2.resize(Scanner.img1, (0,0), fx=THUMB_RATIO2, fy=THUMB_RATIO2)   
             Scanner.img2 = cv2.resize(Scanner.img, (0,0), fx=THUMB_RATIO2, fy=THUMB_RATIO2)   
                 
             Scanner.text = 'Scanner: match ' + str(card.id)
             print Scanner.text
             break
     
     #print card_id
     if card_id == 0:
         card = Card.objects.create()
             
         Scanner.img1 = cv2.resize(Scanner.img, (0,0), fx=THUMB_RATIO2, fy=THUMB_RATIO2)
         Scanner.img2 = None
             
         Scanner.text = 'Scanner: new ' + str(card.id)
         print Scanner.text
             
         card.count = 1
         Scanner.outbox = Scanner.outbox + 1
         card.outbox = Scanner.outbox
         
         card_id = card.id
         card.hist = pickle.dumps(img_hist)
                     
         tmpfilename = '..jpg'             
         cv2.imwrite(tmpfilename, Scanner.img)
         f = open(tmpfilename, 'rb')
         content = File(f)
         card.image.save(str(card.id) + '.jpg', content)#, save=True)
     
         img = cv2.resize(Scanner.img, (0,0), fx=THUMB_RATIO, fy=THUMB_RATIO)   
         cv2.imwrite(tmpfilename, img)
         f = open(tmpfilename, 'rb')
         content = File(f)
         card.thumb.save(str(card.id) + '.jpg', content)
         
         card.save()
         content.close()
         f.close()
         
     scan = Scan.objects.create(position=Scanner.position, fk_card=card)
     scan.outbox = card.outbox
             
     tmpfilename = '..jpg'   
     img = cv2.resize(Scanner.img, (0,0), fx=THUMB_RATIO, fy=THUMB_RATIO)   
     cv2.imwrite(tmpfilename, img)
     f = open(tmpfilename, 'rb')
     content = File(f)
     scan.image.save(str(card.id) + '-'+ str(Scanner.position) + '.jpg', content)
     content.close()
     f.close()
         
     #print '{} / {}'.format(scan.outbox, OUTBOX_NUM)
     #if scan.outbox < OUTBOX_NUM:
     #    scan.position = 0
     #        
     #    OUTBOX = scan.outbox + 1
     #else:
         #1st outbox for notsorted cards
     #    OUTBOX = 1
     #OUTBOX = scan.outbox + 1
     
     scan.save()
     return scan
Exemplo n.º 37
0
    def scan():
        #OUTBOX = 0
        #SCAN = 0
    
        Scanner.position = Scanner.position + 1
                
        #img_hist = cv2.calcHist([Scanner.img],[0],None,[256],[0,256])

        
        #sev
        
        
        #global mtg    
        cv2.imwrite('frameCam.jpg', Scanner.img)
        try:
            id, name, code, frame, framecard, rarity = mtg.test()
            
            height, width, __ = frame.shape
            ratio = height / width
            height, width, __ = framecard.shape
            ratiocard = height / width
            if abs(ratiocard - ratio) > 0.1:
                Scanner.text = 'Invalid height / witdth ratio!'
                #print Scanner.text
                raise Exception(Scanner.text)
            
            '''
            img_hist = cv2.calcHist([cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)],[0],None,[256],[0,256])
            tempalte_hist = cv2.calcHist([cv2.cvtColor(framecard, cv2.COLOR_BGR2GRAY)],[0],None,[256],[0,256])
            #TODO not calculate everytime
            #tempalte_hist = pickle.loads(card.hist)
            
            #TODO compare images
            #method = eval('CV_COMP_CORREL')
            result = cv2.compareHist(img_hist, tempalte_hist, 0)
            print result
            
            if abs(result) > 0.01:
                Scanner.text = 'Invalid historgram!'
                #print Scanner.text
                raise Exception(Scanner.text)
            '''
            
            
            print(id, name, code, rarity)
        
        except Exception as err:
            
            #print('Trying scan again...')
            print(err) 
            Scanner.img1 = None
            Scanner.img2 = None
            return None
        
        card_id = 0
        cards = Card.objects.order_by('pk')
    
        for card in cards:
            '''
            #template = cv2.imread(card.image.name, 1)
            
            if card.hist:
                tempalte_hist = pickle.loads(card.hist)
            else:
                tempalte_hist = cv2.calcHist([template],[0],None,[256],[0,256])
                card.hist = pickle.dumps(tempalte_hist)
                card.save()
            
            #TODO compare images
            #method = eval('CV_COMP_CORREL')
            result = cv2.compareHist(img_hist, tempalte_hist, 0)
            #print result
        
            if result > 0.99:
            '''
            #print('%s %s' % (card.price, id,))
            if int(card.mtgcode) == id:
                card_id = card.id
                card.count = card.count + 1
                card.save()
                    
                #Scanner.img1 = cv2.imread(card.image.path, 0)
                #Scanner.img1 = cv2.resize(Scanner.img1, (0,0), fx=THUMB_RATIO2, fy=THUMB_RATIO2)   
                Scanner.img1 = frame
                #Scanner.img2 = cv2.resize(Scanner.img, (0,0), fx=THUMB_RATIO2, fy=THUMB_RATIO2)   
                Scanner.img2 = framecard
                    
                Scanner.text = 'Scanner: match ' + str(card.id)
                print Scanner.text
                break
        
        #print card_id
        
        if card_id == 0:
            card = Card.objects.create()
                
            #Scanner.img1 = cv2.resize(Scanner.img, (0,0), fx=THUMB_RATIO2, fy=THUMB_RATIO2)
            Scanner.img1 = frame
            #Scanner.img2 = None
            Scanner.img2 = framecard
            
            Scanner.text = 'Scanner: new ' + str(card.id)
            print Scanner.text
                
            card.count = 1
            Scanner.outbox = Scanner.outbox + 1
            card.outbox = Scanner.outbox
            
            card.name = name + ' [' + code + ']'
            card_id = card.id
            #card.hist = pickle.dumps(img_hist)
            card.mtgcode = id
            card.rarity = rarity
                        
            tmpfilename = '..jpg'             
            cv2.imwrite(tmpfilename, Scanner.img2)
            f = open(tmpfilename, 'rb')
            content = File(f)
            card.image.save(str(card.id) + '.jpg', content)#, save=True)
        
            #img = cv2.resize(Scanner.img2, (0,0), fx=THUMB_RATIO, fy=THUMB_RATIO)   
            img = Scanner.img2
            cv2.imwrite(tmpfilename, img)
            f = open(tmpfilename, 'rb')
            content = File(f)
            card.thumb.save(str(card.id) + '.jpg', content)
            
            card.save()
            content.close()
            f.close()
            
        scan = Scan.objects.create(position=Scanner.position, fk_card=card)
        scan.outbox = card.outbox
                
        tmpfilename = '..jpg'   
        #img = cv2.resize(Scanner.img1, (0,0), fx=THUMB_RATIO, fy=THUMB_RATIO)   
        img = Scanner.img1
        cv2.imwrite(tmpfilename, img)
        f = open(tmpfilename, 'rb')
        content = File(f)
        scan.image.save(str(card.id) + '-'+ str(Scanner.position) + '.jpg', content)
        content.close()
        f.close()
            
        #print '{} / {}'.format(scan.outbox, OUTBOX_NUM)
        #if scan.outbox < OUTBOX_NUM:
        #    scan.position = 0
        #        
        #    OUTBOX = scan.outbox + 1
        #else:
            #1st outbox for notsorted cards
        #    OUTBOX = 1
        #OUTBOX = scan.outbox + 1
        
        scan.save()
        return scan