I generally like Firefox, and generally like Google. But having Google suggest enabled when I search for things is annoying, at least on my rather small laptop screen. Mostly it is because my internet right now is pretty average, so I try to cut down on any extra traffic. Anyhow, I didn’t even both Googling how to do this:
Type in ‘about:config’ into your address bar and type ‘search’ in the top filter bar. Look about half-way down for this:
Read Turn Off Google Suggest in FirefoxSo, you’ve seen how to look up thumbnails via python, but wonder how to integrate this with Django? I created a sample app to demonstrate. One thing to note about this app is it is slightly more complex than just using the previously mentioned ThumbnailUtility. For starters, the thumbnail is downloaded from Alexa onto the server. Another part is first searching if the thumbnail exists already, and if it does, serving that instead of querying Alexa. Let’s just start with some code.
Read Alexa Site Thumbnail And DjangoFor one of my sites I needed to get thumbnails, yet Alexa Site Thumbnail didn’t have any code snippets for Python. Well, no they/you do.
import base64 import datetime import hmac import sha import sys import re import urllib import xml.dom.minidom AWS_ACCESS_KEY_ID = 'your-access-key-id' AWS_SECRET_ACCESS_KEY = 'your-super-secret-key' # This one is for an individual thumbnail... def create_thumbnail(site_url, img_size): def generate_timestamp(dtime): return dtime.strftime("%Y-%m-%dT%H:%M:%SZ") def generate_signature(operation, timestamp, secret_access_key): my_sha_hmac = hmac.new(secret_access_key, operation + timestamp, sha) my_b64_hmac_digest = …
Read Alexa Site Thumbnail with PythonThis is how I actually use Alexa Site Thumbnail, and since I’min a sharing mood, I’ll extend the code your way. In short, this takes the url and searches in STORELOC first, then any urls not already in STORELOC are retrieved and named via a slug. You need to pass two variables to either of these: blog_site.url and blot_site.slug – since I’m using Django, this is naturally how sites are returned after I filter a queryset. What I do is place the call to Alexa as high up the page as I can, and because I’ve threaded this, the page can continue to load without waiting for Alexa’s response. For instance, let’s say you have some model with cool sites, and you want to return …
Read Alexa Site Thumbnail with Python IIThis is fairly well documented in the Django docs, so I’ll be brief. This is the the bit of search code I use in almost all of my Django sites, and it works great:
def search(request): from django.db.models import Q q = request.GET.get("q", "") if q and len(q) >= 3: clause = Q(dirtword__icontains=q) \ | Q(description__icontains=q) \ | Q(tags__name__icontains=q) site_search = Dirt.objects.filter(clause).distinct() else: site_search = Dirt.objects.order_by('?')[:100] return list_detail.object_list( request = request, queryset = site_search, template_name = "dirty/search.html", template_object_name = "dirty", paginate_by = 20, extra_context = {"q" : q}, ) …
Read Adding Search to DjangoCreating feeds in Django is freaking simple. I’ll start with an example of just updating a feed with the newest objects (for instace, newest blog posts). Similar to the forms.py way of handling our different forms, I’ve created a feeds.py to handle the feeds.
from django.contrib.syndication.feeds import Feed from colddirt.dirty.models import Dirt class LatestDirts(Feed): title = "Cold Dirt" link = "/" description = "When you have dirt, you've got dirt. Right..." copyright = 'All Rights Unreserved' def items(self): return Dirt.objects.all()[:50] What this will do is query our Dirt DB and return an obj. The fields here are pretty well documented in the Django docs, …
Read Django Syndication with ColddirtSince I’ve already covered a really simple syndication example, I’ll move onto something a little more complex. Let’s say you want to offer syndication that is slightly more custom. The Django syndication docs give an example from Adrian’s Chicagocrime.org syndication of beats. I had to ponder a minute to get “custom” syndication to work, so here’s my example from start to finish.
First, as usual, feeds.py
class PerDirt(Feed): link = "/" copyright = 'Copyright (c) 2007, Blog Mozaic' def get_object(self, bits): from django.shortcuts import get_object_or_404 if len(bits) != 1: raise ObjectDoesNotExist my_dirt = get_object_or_404(Dirt, slug__exact=bits[0]) …
Read Django Syndication with Colddirt III hear many complaints and questions about newforms, but I’ve personally found it rather easy and logical to use. There are numerous ways for you to use do forms in Django, and most likely the best way to see them all is to read the docs. On the Colddirt demo site, this is how I used newforms. I’ll take the index page as an example.
I’ve accessed the newforms module like so:
Read Django Newforms Usage in ColddirtSo, the Django developers, in my opinion, are freaking smart. Instead of bundling Django with a particular library, they have added XML and JSON serialization; us humble users can choose whatever AJAX library we want. Prototype 1.5.1 has been pretty fun to work with, so I’ll kick off this demo with a really simple example.
How simple? The intended goal is to have the total number of ‘dirts’ update without user intervention. Laaaammmeee. If you are a visual type of person, take a look on the Colddirt huh page. That number automatically increases without user intervention. And this is how.
The process (some pseudocode) will go like this:
Read Simple Ajax with DjangoNote: Colddirt’s source code is run from Django SVN, check out on May 10th-ish. If you are using a newer branch, some things have changed. i.e. clean_data has been renamed cleaned_data. Remember to check the BackwardsIncompatible page in the wiki.
Part 1: Simple AJAX with Django
Part 2: Django Newforms Usage
Part 3: Search with Django
Part 4: Django’s Syndication Framework (simple)
Part 5: Django’s Syndication Framework (little more complex)
Feel free to see the source code from Github.
Read Colddirt Information