Jun 29, 2011

Django: how to generate json response.

It's a common practice to generate content different from standart HTML output by HttpResponce. Lets consider Example on how to generate JSON response by Django.

First of all! Django has built in "other response generators". Most common task is to generate JSON. In general it's extremely easy. Operation called "seriliazation". Official Django docs say:

Django’s serialization framework provides a mechanism for “translating” Django objects into other formats. Usually these other formats will be text-based and used for sending Django objects over a wire, but it’s possible for a serializer to handle any format (text-based or not). "


Anyway an example, helped me a lot.
from django.utils import simplejson
def some_view(request):
    result = []
    result.append({"user":request.user})
    result
.append({"key":request.session.key})     return HttpResponse(simplejson.dumps(result), mimetype='application/json')
In response you will get a nice JSON.
 Hope this will help someone to save some time.  Please leave me a comment if you have something to add/correct...

Django: Rotate Image with PIL usage.

Let's assume we have this simple model:

from django.db import models

class Image(models.Model):
    image = models.FileField(upload_to="images/")
 
Task is to take this simple image, rotate it and save instead of original.
Let's discover this way to do so:

#views.py
from PIL import Image as PilImage
from models import Image


def rotate(request):
    #getting instance of the model
    item=Image.objects.get(pk=1)

    #opening image for PIL to access
    im = PilImage.open(image.image)

    #rotating it by built in PIL command
    rotated_image = im.rotate(270)

    #saving rotated image instead of original. Overwriting is on. 
    rotated_image.save(item.image.file.name, overwrite=True)

    return HttpResponse(str(image.image))

This pattern will work in most cases. Instead of rotate you can use any other PIL editing method, as for e.g. flip or sharpen the image... Have fun coding and please leave me a comment if you'll find this info useful. Thanks.

Jun 24, 2011

SSH: Mount remote file system on your Mac over SSH to access in Finder

    Maybe you're already know about how to connect to remote servers through Local network on your Mac, but how about SSH server connections?
    Lets say you may open a terminal window and run "ssh username@server.com" it will ask you for a password and "Ta-da" we're in. But what about usability. If you're console geek, this is enough for you. As for the others let's talk about adding some GUI to it. Some time ago Google engineers released a package for Mac's to mount remote filesystems, using lots of methods, including SSH.
    This software is called MacFuse. It connects remote filesystems to your Mac's finder. You will see it in your computer as a regular Mac's network drive.
    MacFuse has developed to the point, where it's extremely easy to "make it work". This is the way:

    1. Install MacFuse and SSHFS. at the time of writing this article version is MacFUSE-2.0.3,2.dmg and you can download it here.  SSHFS is currently deprecated and you can download it there too. For accessing deprecated software you must press in a scroll down menu "Search" is set to "Current downloads". Select All downloads there and press search. Latest version I've found is sshfs-1.0.0.dmg. Download and install them, as usual.
    2. Configure SSH server connection. Run sshfs.app and enter your server params:
    3. Open Finder folder. Your My Computer will have another folder of your ssh server in case no errors occur:

It will work like a normal regular folder. You will be able to Drag'n'Drop files there or access it through terminal or any other commander like application in your Mac.

Issues:

    1. Major issue is that you can't auto-mount ssh filesystem at login, like Windows drives. Also you can say to an app to run at Startup but yo'll have to enter password everytime.
    2. It looses connection upon sleep/wake sequence. you have to enter password again.

    Thats all, as for me.

Conclusion:

    Macfuse+sshfs is nice software for using time to time. It's not good to use on everyday basis because of disability to store password and reconnect at logins. But you can easily use it in case you need to connect to SSH servers time to time and use them with some sort of gui and not just console.

If you used it/have better way to do it please drop me a comment...
Thanks for reading.

Jun 21, 2011

Django: Installing Eclipse and PyDev for django

In most common cases of programming Django with Eclipse are that Django is installed in system, like Python.
I dislike this method, because it makes confusions for beginners. What Django version I'm in. Why did I download an app "X" and added an Eclipse project. Everything seems to be fine, but I cant install proper dependencies... So it's not a secret. Managing Django distributions is a hard work for junior developer. Let's try to make their work simpler, by this article.

I'll try to show you the right way to install Django environment on Eclipse.

1. First of all let's install Eclipse from official site. I chose "Eclipse IDE for JavaScript Web Developers". Mac 32bit version, because I'm using this OS type. Any other distributive will work just fine, I think.

2. Unpack downloaded archive into some dir. It will be installation directory of the eclipse. Usually it doesn't mater where it is stored. So I used "/Users/garmoncheg/Developer/tutorial/eclipse" for this purposes.I would not recommend storing it into default "Applications" folder because of many files besides main app.  Eclipse wasn't designed under Mac OS X. So it's better to store it in a "Linux" way.
3. Run and create initial setup steps, like create a workspace:
And close the annoying welcome window:
You may also prefer to check "Keep in dock" option, like I did:
Thats pretty much all about initial Eclipse setup.
Maybe Windows setup and initial steps differs a bit. But I dont seem to meet any trouble here.
Let us now talk about THE Environment! :)
Eclipse for JavaScript development is ready now. But we started to talk about Django!.
First things forst. We need python. 
If you're using Mac OS X 10.6, like I do, you probably have python installed. You can check this by starting console and simply typing "python" like so:
If you're seeing something similar to this, like "Python 2.7.1 (r271:86882M, Nov 30 2010, 10:35:34)", you have python installed already.
If you don't - worry not. There are plenty of articles on how to install python under MAC OS X.
But in general you just may download (in my case Latest STABLE production version 2.7.2) from up here: http://www.python.org/download/.
Also Python Mac OS X install page says:
"Python comes pre-installed on Mac OS X, but due to Apple's release cycle, it's often one or even two years old. The overwhelming recommendation of the "MacPython" community is to upgrade your Python by downloading and installing a newer version from the Python standard release page."
So in general it's a good idea to install a bit later version of python than you have already. But do not go high in the sky to 3.2. The latest version numbers are usually supported later by 3-rd party software and you'll probably need to downgrade python in future.
For now I'm comfortable with version 2.7.1, as you could have notice from previous screenshot...
Nevertheless even preinstalled python would be sufficient for you now and let's go ahead.
Eclipse is a monstrous IDE (Interactive Development Environment, if i'm not mistaken). It often is a subject to such called "Eclipse shock". I had such one while studying it. If you're experiencing something similar afraid not! It's a common practice. Books helped me a lot. I started to read them. (Haven't finished them even now. :) ) I used: Eclipse For Dummies (For Dummies (Computer/Tech))  (Because I like "for Dummies" series) and Eclipse Web Tools Platform: Developing Java™ Web Applications. But torrent versions of them will suit just fine, I suppose... :)

I dont believe anyone could use ALL features of this IDE. But lets try to use ours.
Eclipse is so monstrous, it has even it's own marketplace inside. Let's use it:
It is situated in Help submenu and called as expected "Marketplace" :). Lets tap this and click Next, choosing Eclipse marketplace inside.
Lets search for "django":

We've found one main plugin to work with python source codes. It is called PyDev. It includes IDE enhancements and code completion for python based languages. That's why we need it. Also it is a main known plugin for editing Django source. Select Install on it.
There will be some confirmations, that you'll have to accept. I won't stop on them deeply. In general: you have to confirm full package install, accept licence, wait while install process goes and finally restart Eclipse. I don't think it will cause you much trouble to guess how to do that.
Now that we have PyDev installed, we can see PyDev project in the New Project create wizard:
Ok. Seems to be good so far. Thats pretty much all about Eclipse IDE Plugin setup.

Now lt us create Example Django Project to setup environment.
If you select to create new "Pydev Django Project" now, you wont finish it. Finish button simply disagree to be used. :) That's because of python not properly configured to work with Eclipse IDE. Let's make it work.
Go to "Eclipse -> Preferences", open collapsed submenu "PyDev" there and select "Interpreter - Python", like so:
There are many other ways to configure Python, but we'll choose IMHO the simplest. Hit "Auto Config"! :)
It will do some scanning work and show you main system interpreter of python. Somehow in my case it is 2.6. Nevertheless let's hit ok. As I've mentioned earlier, It's not hardly important what Python interpreter is installed with Eclipse for now... You'll see something like this:
In my case it will show some other packages I have. Don't bother if it will show you only checked Python path variables. It's right! :) I dont have a lot of Mac's to practice with, so I'm using my only one working computer for writing this article.
Let us hit OK. We will see newly Auto-configured Python interpreter and PYTHONPATH variables.
You may hit Apply and check some other options, like editor background color (I prefer light olive.) or so.

 This would be sufficient to write "pure" Python apps. But we're using Django!. If you'll try to create Django project at this point, you would probably see something like this:
That's because django is not configured. It will be a good practice not to install it into system, like official manual says. It is good to have ability to have multiple Django versions installed. No?

Let's now "install" Django. First thing you need is a fresh, or not, Django distribution. You can download it at their official site here: https://www.djangoproject.com/download/. I'll use old version to meet my purposes. (Install another django project, that requires Django-1.1 environment and not the new one.) But you may easily use newest one, if you're planning to write apps from scratch, or so...
Ok. Wait for download to be finished. Unpack "Django-1.1.4.tar.gz" (in my case) somewhere and copy it to your work folder. I used "/Users/garmoncheg/Developer/tutorial/Django-1.1.4". We'll need this path to be static, to connect it to Eclipse IDE.

Let's add this path to Eclipse "PYTHONPATH" (connect folder in preferences). Go to PyDev interpreter settings in Preferences again. Add a folder "Django-1.1.4" (or any other you've chosen) distribution. Eclipse will identify that it is a Django distribution automatically and index all variables. So let's insert it to our Python environment like so:

Hit "New folder", select "Django-1.1.4" dir and wait while indexing process continues.
Than hit "OK" and try to create a new Django project.

Eclipse now allows us to create a new Django project properly.
Let's somehow call "New Project -> PyDev Django Project" and click next. You'll se project initial setup here:
I gave it a name "django_example" and changed option to "Add project directory to the PYTHONPATH". Because I dislike another "src" dir inside project.
Hit next and you'll se something like this inside:
Here we're using debug mode database "sqlite3" so we're not changing anything. If you prefer to work from any other Django compatible database type, you can specify it's settings here. Fileds in this form are equal to fields in your future setiings.py file. Also I'm changing Django version from "1.2 or later" to "1.1" in order spell checking to work properly.
Thats all with our project. Let's hit finish.
Eclipse will probably ask for adding a new perspective. You should agree with this. You'll find handy bookmark made especially for python development, looking like so:
And let's explore or newly created project. It will look similar to this:

Notice handy "PYTHONPATH" directories attached to the project, where you can explore nearly all source you're using. Even entire Django distribution.

Ok now we need this thing to work. You'll get default (not working) run configuration profile if you simply press "Run" at this point. IMHO there is no good auto-config way to make this, so we'll construct our newly made configuration by hand.
Go to "Run" button, hit the small arrow near it and choose "Run configurations" there like so:
Double-click "PyDev Django" and you'll get something like this:
Let's now create run configuration for our example Django project.
First thing to do is enter a configuration name, which is "django_example" in my case. You may, of corse, select any other one you like. Hit the "Browse" button here on the "Project" line and select eclipse project that will be Run. Run configuration will add all PYTHONPATH variables used to project "PYTHONPATH that will be used in the run" field. Than you should select "Main Module" that will be run upon project startup. Usually it's "manage.py" inside a project dir.
On the next tab "Arguments" i'd recommend to insert "runserver --noreload" in order for Eclipse not to reload server after any code changes. You'll easily do it manually later, if needed. 
On the Interpreter tab you could specify another interpreter. In our case of clean install we'll probably leave Default.
On the next Refresh tab i'd recommend check "Refresh resources upon completion" it will sync project with filesystem every time you'll relaunch it.
The next "desirable one" tab is "Environment". Here we can specify additional interpreters for our project. Let's add one, because it's empty.
Lets hit "New" like so:
And enter "PYTHONPATH" in the "Name" field.
As for value, it's a bit more complicated here. Lt's hit "Variables" button. Notice "Select variable" window appears with many variables we may need, but without any desirable ones. Hit "Edit Variables...". You must see a new window - "Preferences (filtered)" now. It says "Strings substitution".
Let's hit "New" button. A window with adding new variable will appear. It will look like so:
Let's add a name "Django-1.1" and say it a directory to your downloaded Django distribution. You don't have to enter description, like I did.
Now lets hit "Ok", "Ok", select our newly created variable and hit "Ok" and finally "Ok". :)
A-ha. Almost forgot this. Name of your new variable should be "PYTHONPATH".
You'll probably get Environment tab looking like so:
Now finally we're done making run configuration and ready to hit "Run" button below.
Yo'll probably see Django server running in console in case made everything right.
Thats all for now with setting up Django to work with Eclipse.
Any comments/suggestions/better ways to do somthing? Please drop me a comment below.

Jun 19, 2011

Django: Better way to install django apps into apps. (Handling django projects dependencies.)

Let's talk about common programming practice. You love to code yourself. Everyday you write tons of code. Making lots of common tasks is interesting and challenging. No?
A-ha! You love plugins too! :) I'm making a startup. Everyday there are some ideas coming to my head. Some are easy to implement, some are not, but I often download tons of plugins.
There are lots of ways to use plugins without installing them to main system. Python virtualenv is easy and common to use for this purposes. Soon I'll write an article about it too. But the main problem of Python Virtualenv is it's complexity. You basically need to install something, make an environment and so on... What if you just need to implement nice and easy "Django way".
Here direct copy comes in mind. It's the easiest way, as for me.
Basic concept is:
   - download plugin (be it in egg, zip, tar.gz or any other format)
   - unpack it 
   - copy to django app dir (simple drag and copy files to eclipse in my case)
   - settings.py modification (and/or any App post setup procedures)
Thats ALL!

Lets go into this way more deeply. Lets install django-pagination into my app.
First wee need to check plugin manual. Let's download it from google code HERE.
Unpack It. If you've done it in a period near to writing this article you'll probably see something like this.

We see a list of folders, containing one, like "docs" and a bunch of setup scripts like "setup.py", "setup.cfg" and so on. "Docs/install.txt" says about different install methods.
But you don't have to install it in order things to work properly!
The simplest way is to copy this main app "pagination" (it is usually a first dir in a tree containing "__init__.py" file) into your project or app dir, containing "settings.py".  This way you can see apps or plugins, like they where installed on a python path. 
Setup script usually does this for you, but installing this directory to your system (or virtualenv) python path. And what if you'd like any other version of this django app installed? 
There some other thoughts here also. You may create dir, like "plugins" and copy installed plugins there. But i would not recommend this. You usually need up to 10 plugins per project. Why bother separating them? Bad side here is that we need to do some inside plugin modifications because you're renaming main plugin directory path. So it's possible, but unlikely simpler...

Now we must do plugin setup inside our App. You unfortunately can not escape this. :)
So let's make steps 1 and 2 from "docs/usage.txt" in our plugin dir. Let's add 'pagination', app to INSTALLED_APPS in our settings.py and add a middleware 'pagination.middleware.PaginationMiddleware', string to our MIDDLEWARE_CLASSES.

Let's add request to our TEMPLATE_CONTEXT_PROCESSORS too (if it isn't already there).
It will look something like so:

And thats pretty much all. Now we can add {% load pagination_tags %} somewhere in our template and use  {% autopaginate object_list %} and then {% paginate %} father. (see "docs/usage.txt" of this app for more info)

Now your app is really installed. You can use common from my_app import my_function like in "standard" installation way. Nothing more to do. 

We worked out most common situation with django plugins or redistributable apps. Maybe to some complex python apps, like lets say PIL, this method would be unsuitable, BUT for most common "django app installation" cases it will work like a charm.
Main plus of this way, that you can have all code related to you App/project etc. stored in one place.
This code would not affect main system also. So if you decide to use older version of plugin (for older ver of django for e.g.), maybe you'll remember yourself reading this article :)...

Bad thing here, that if your app has some more dependencies, you have to install them somehow too. Maybe you have to change your new app code, if you choose to copy them to your main dir way. But it's in not so common "complex" cases.

Comments? Suggestions? Please drop me a comment about what you think here...

Jun 15, 2011

Django: add image in an ImageField from image url

Today I had experience with django file downloads from an specified url into Django model FileField.
I'm writing content grabber from Flickr. I didn't find information about it and decided to write what I've discovered so far in example.

Stackoverflow helped me a bit. But answer proposed there had not worked.
So this is code example how to workout this issue:
because code worth a thousand words :)

We have a model with one field:
#models.py

class Photo(models.Model):
    image = models.ImageField('Label', upload_to='path/')

We need to create a photo from image url and save it to the model's FileField.

#somewhere in views.py

# usually in header imports
from urlparse import urlparse 
import urllib2 
from django.core.files import File
  #add imprt of content file wrapper
from django.core.files.base import ContentFile
# somewhere: for e.g. in view handling the file operations
photo
= Photo() img_url = 'http://i.ytimg.com/vi/GPpN5YUNDeI/default.jpg' name = urlparse(img_url).path.split('/')[-1]   #wrap your file content content = ContentFile(urllib2.urlopen(img_url).read())) photo.image.save(name, content, save=True)

In my project I used this in a bit another way.
First a user (site admin, for now) synchronizes with Flickr. He downloads into django model all photos data for flickr "username".
Than all this synched photos appear in a view, where user can choose what photos to download into main app model.
Than it passes chosen list to next view. It parses synched model and actually downloads photos to main App.
Finally it cleans up temporary Flickr sync model.

Maybe that is an ugly and bad way to do this. My excuses are to code less, because of using almost untouched plugin code. This feature, also, must be used 1 or 2 times in site production stage only.
So it is made for this purposes.

Maybe later I'll write an article on making Flickr content grabber for your Django site.
For now, thanks for reading and may The Code be with you!

Ideas, Suggestions? Please drop me a comment!