Permalink
Browse files

v0.2 on PyPi, with better readme

  • Loading branch information...
1 parent 44e1158 commit eeabe26a50141dbee5c7d6d66902a9ab1a5825ea @va1en0k va1en0k committed Nov 9, 2011
Showing with 86 additions and 7 deletions.
  1. +2 −0 .gitignore
  2. +51 −5 README.md
  3. +31 −0 pyuploadcare/file.py
  4. +2 −2 setup.py
View
@@ -1,2 +1,4 @@
pyuploadcare.egg-info/
temp.db
+build/
+dist/
View
@@ -1,24 +1,70 @@
-# How to use it with Django?
+# PyUploadCare: a Python module for UploadCare
-## settings.py
+This consists in API interface for [UploadCare][1], and a couple of Django goodies.
+
+To install it, just run
+
+ pip install pyuploadcare
+
+(or `easy_install pyuploadcare` if you're into vintage)
+
+## How to use it with Django?
+
+First of all, you'll need API keys: public and private. You can get them at [the UploadCare website][1]. Currently it requires an invitation code to register, please request an access to [uploadcare-testing user group][2] and find it there.
+
+As soon as you get your API keys, add them to your Django settings file:
+
+### settings.py
UPLOADCARE = {
'pub_key': '***',
'secret': '***'
}
+You're all set now!
+
+Adding a UploadCare file field to your model is really simple. Like that:
-## models.py
+### models.py
from django.db import models
from pyuploadcare.dj import FileField
- # Create your models here.
class Photo(models.Model):
title = models.CharField(max_length=255)
photo = FileField()
+`FileField` doesn't require any arguments, file paths or whatever. It **just works**. That's the point of it all.
+
+It look nice in the admin interface
+
![Admin page](http://f.cl.ly/items/1v120O3h2W462o3T323F/Screen%20Shot%202011-11-04%20at%202.03.32%20PM.png)
-See `test_project/gallery/` for an example.
+### Using it
+
+It's really simple, just use your UploadCare-enabled models as any other models:
+
+ for p in Photo.objects.all():
+
+ # p.photo contains pyuploadcare.file.File object
+
+ print p.photo.url()
+
+ print p.photo.resized(200, 400) # returns the url of resized version of the image
+ print p.photo.resized(height=400)
+ print p.photo.resized(150, 150, crop=True)
+
+### Using it in templates
+
+To make your life easier, UploadCare file objects understand some 'magic' properties.
+
+ {{ p.photo.resized_120x200_crop }}
+ {{ p.photo.resized_120 }}
+ {{ p.photo.resized_x120 }}
+ {{ p.photo.resized_600x120 }}
+
+These are most useful in Django templates, which are somewhat limited in calling functions with arguments.
+
+[1]: http://uploadcare.com/
+[2]: https://groups.google.com/group/uploadcare-testing
View
@@ -1,5 +1,8 @@
import time
+RESIZER_BASE = 'http://services.uploadcare.com/resizer/%(file_id)s/%(cmd_line)s/'
+
+
class File(object):
_info = None
_cached_url = None
@@ -11,6 +14,12 @@ def __init__(self, file_id, ucare):
def __repr__(self):
return '<uploadcare.File %s>' % self.file_id
+ def __getattr__(self, name):
+ if name.startswith('resized_'):
+ return self.string_resized(name[8:])
+
+ return super(File, self).__getattr__(name)
+
def keep(self, wait=False):
self._info = self.ucare.make_request('POST', self.api_uri(), {'keep': 1})
@@ -52,3 +61,25 @@ def url(self):
def filename(self):
return self.url().split('/')[-1]
+ def resized(self, width=None, height=None, crop=False):
+ dimensions = str(width or '')
+
+ if height:
+ dimensions += 'x%i' % height
+
+ chunks = [dimensions]
+
+ if crop:
+ chunks.append('crop')
+
+ cmd_line = '/'.join(chunks)
+
+ return RESIZER_BASE % {'file_id': self.file_id, 'cmd_line': cmd_line}
+
+ def string_resized(self, cmd_line):
+ cmd_line = cmd_line.replace('_', '/')
+
+ return RESIZER_BASE % {'file_id': self.file_id, 'cmd_line': cmd_line}
+
+
+
View
@@ -1,10 +1,10 @@
from setuptools import setup
setup(name='pyuploadcare',
- version='0.1',
+ version='0.2',
description="UploadCare.com API library",
author='Valentin Golev',
author_email='v.golev@gmail.com',
url='https://github.com/uploadcare/pyuploadcare',
- packages=['pyuploadcare'],
+ packages=['pyuploadcare', 'pyuploadcare.dj'],
)

0 comments on commit eeabe26

Please sign in to comment.