Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
flashingpumpkin committed Jan 9, 2013
0 parents commit 7b79971
Show file tree
Hide file tree
Showing 17 changed files with 203 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
tests/MEDIA/
10 changes: 10 additions & 0 deletions LICENSE
@@ -0,0 +1,10 @@
Copyright (c) 2012, Caffeinehit Ltd.
Copyright (c) 2012, Alen Mujezinovic <alen@caffeinehit.com>

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Empty file added croppy/__init__.py
Empty file.
40 changes: 40 additions & 0 deletions croppy/fields.py
@@ -0,0 +1,40 @@
from django.db import models
from django.core.serializers.json import DjangoJSONEncoder
from django.db.models.fields.files import ImageField, ImageFieldFile

import json
import jsonfield

class Crop(object):
pass

class CropsDescriptor(object):
def __init__(self, data):
self.data = data
self._post_init()

def create(self, name, (x, y, width, height)):
self._create_accessor(name, 'asdf')
self.data[name] = 'asdf'

def _post_init(self):
for (key, value) in self.data.iteritems():
self._create_accessor(key, value)

def _create_accessor(self, name, value):
setattr(self, name, Crop())


class CropsField(jsonfield.JSONField):
__metaclass__ = models.SubfieldBase

def __init__(self, image_field, *args, **kwargs):
self.image_field = image_field
super(CropsField, self).__init__(*args, **kwargs)

def to_python(self, value):
data = super(CropsField, self).to_python(value)
return CropsDescriptor(data)

def get_db_prep_value(self, value, connection, prepared=False):
return super(CropsField, self).get_db_prep_value(value.data, connection, prepared = False)
3 changes: 3 additions & 0 deletions croppy/models.py
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
16 changes: 16 additions & 0 deletions croppy/tests.py
@@ -0,0 +1,16 @@
"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".
Replace this with more appropriate tests for your application.
"""

from django.test import TestCase


class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.assertEqual(1 + 1, 2)
1 change: 1 addition & 0 deletions croppy/views.py
@@ -0,0 +1 @@
# Create your views here.
3 changes: 3 additions & 0 deletions requirements.txt
@@ -0,0 +1,3 @@
nose
sphinx
pillow
27 changes: 27 additions & 0 deletions setup.py
@@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-

from setuptools import setup, find_packages


with open('README.rst') as f:
readme = f.read()

with open('LICENSE') as f:
license = f.read()

with open('requirements.txt') as f:
install_requires = f.read().split('\n')

setup(
name='django-croppy',
version='0.0.1',
description='Image library for creating user-croppable media',
long_description=readme,
author='Alen Mujezinovic',
author_email='me@kennethreitz.com',
url='https://github.com/caffeinehit/django-ilib',
license=license,
install_requires=install_requires,
packages=find_packages(exclude=('tests', 'docs'))
)

Empty file added tests/__init__.py
Empty file.
Empty file added tests/app/__init__.py
Empty file.
Binary file added tests/app/assets/test.tiff
Binary file not shown.
10 changes: 10 additions & 0 deletions tests/app/models.py
@@ -0,0 +1,10 @@
from django.db import models
from croppy.fields import CropsField

# Create your models here.


class Image(models.Model):
image = models.ImageField(upload_to = 'images')
crops = CropsField('image')

58 changes: 58 additions & 0 deletions tests/app/tests.py
@@ -0,0 +1,58 @@
"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".
Replace this with more appropriate tests for your application.
"""

from django.core.files import File
from django.test import TestCase

from .models import *

import os

def get_image(filename):
path = os.path.join(
os.path.dirname(__file__),
'assets',
filename)

image = Image()

with open(path) as f:
image.image.save(filename, File(f))

return image



class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.assertEqual(1 + 1, 2)

class CropsFieldTest(TestCase):
def setUp(self):
self.image = get_image('test.tiff')
self.crop = (0,0,100,100)

def test_save_image(self):
image = Image.objects.get(id = self.image.id)
self.assertTrue(os.path.isfile(self.image.image.path))

def test_create_crop(self):
self.image.crops.create('square', self.crop)
self.assertTrue(self.image.crops.square)
#self.assertTrue(os.path.isfile(self.image.crops.square.path))

# def test_delete_crop(self):
# self.test_create_crop()
# path = self.image.crops.square.path
# self.image.crops.square.delete()
# self.assertRaises(AttributeError, lambda: self.image.crops.square.path)
# self.assertFalse(os.path.isfile(path))


1 change: 1 addition & 0 deletions tests/app/views.py
@@ -0,0 +1 @@
# Create your views here.
28 changes: 28 additions & 0 deletions tests/settings.py
@@ -0,0 +1,28 @@
import os

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
('Tester', 'test@example.com'),
)

MEDIA_ROOT = os.path.join(
os.path.dirname(__file__),
'MEDIA')

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'test.db',
},
}

INSTALLED_APPS = [
'django.contrib.auth',
'django.contrib.contenttypes',
'croppy',
'tests.app',
]


5 changes: 5 additions & 0 deletions tests/test.sh
@@ -0,0 +1,5 @@
#!/bin/bash

export PYTHONPATH=$PWD/../:$PYTHONPATH

django-admin.py test app --settings tests.settings

0 comments on commit 7b79971

Please sign in to comment.