Tuesday, April 28, 2009

Halve your site's page sizes in 5 minutes

This reduces our page sizes by up to 50%+

from django.utils.html import strip_spaces_between_tags
from django.conf import settings

class SpacelessMiddleware(object):
def process_response(self, request, response):
if not settings.DEBUG:
if 'text/html' in response['Content-Type']:
response.content = strip_spaces_between_tags(response.content)
return response

Tuesday, March 03, 2009

I havent had to worry about SQL recently


I'm happy django does all the database work for me so i can spend my time building features and writing the same snippets of code all over the application.

Monday, January 05, 2009

howto: group by in django

Django doesnt officially yet support group by, however the code to acomplish this is actually hidden inside.  If you had a model that tracked registrations, which contained a FK to the user that refered the new user such as:

class Registration(models.Model):
        ...
        referer = models.ForeignKey(User,null=True,blank=True)


>>> from registration.models import Registration

>>> query_set = Registration.objects.extra(select={'count': 'count(1)'}, order_by=['-count'],where=['referer_id is not null']).values('count', 'referer')  
>>> query_set.query.group_by = ['referer_id']

>>> for row in query_set:
...  print row                                                                                                                                  
... 
{'count': 26L, 'referer': 6384}
{'count': 4L, 'referer': 6871}
{'count': 3L, 'referer': 9993}
{'count': 2L, 'referer': 7804}
{'count': 2L, 'referer': 7892}
{'count': 2L, 'referer': 9699}
{'count': 1L, 'referer': 6111}
{'count': 1L, 'referer': 7821}
{'count': 1L, 'referer': 5025}


this is as of 
Last Changed Rev: 9037
Last Changed Date: 2008-09-15 14:06:02 -0400 (Mon, 15 Sep 2008)


Thursday, December 04, 2008

GeoDjango Error when using python2.4

Anyone getting this error?


  File "/usr/lib/python2.4/site-packages/django/contrib/gis/db/models/__init__.py", line 5, in ?
    from django.contrib.gis.db.models.manager import GeoManager
  File "/usr/lib/python2.4/site-packages/django/contrib/gis/db/models/manager.py", line 2, in ?
    from django.contrib.gis.db.models.query import GeoQuerySet
  File "/usr/lib/python2.4/site-packages/django/contrib/gis/db/models/query.py", line 5, in ?
    from django.contrib.gis.db.backend import SpatialBackend
  File "/usr/lib/python2.4/site-packages/django/contrib/gis/db/backend/__init__.py", line 12, in ?
    from django.contrib.gis.db.backend.postgis import create_spatial_db, get_geo_where_clause, SpatialBackend
  File "/usr/lib/python2.4/site-packages/django/contrib/gis/db/backend/postgis/__init__.py", line 3, in ?
    from django.contrib.gis.db.backend.base import BaseSpatialBackend
  File "/usr/lib/python2.4/site-packages/django/contrib/gis/db/backend/base.py", line 7, in ?
    from django.contrib.gis.geos import GEOSGeometry, GEOSException
  File "/usr/lib/python2.4/site-packages/django/contrib/gis/geos/__init__.py", line 31, in ?
    from django.contrib.gis.geos.base import GEOSGeometry, wkt_regex, hex_regex
  File "/usr/lib/python2.4/site-packages/django/contrib/gis/geos/base.py", line 11, in ?
    from django.contrib.gis.geos.coordseq import GEOSCoordSeq
  File "/usr/lib/python2.4/site-packages/django/contrib/gis/geos/coordseq.py", line 10, in ?
    from django.contrib.gis.geos.prototypes import cs_clone, cs_getdims, cs_getordinate, cs_getsize, cs_setordinate
  File "/usr/lib/python2.4/site-packages/django/contrib/gis/geos/prototypes/__init__.py", line 8, in ?
    from django.contrib.gis.geos.prototypes.coordseq import create_cs, get_cs, \
  File "/usr/lib/python2.4/site-packages/django/contrib/gis/geos/prototypes/coordseq.py", line 67, in ?
    cs_getordinate = cs_operation(lgeos.GEOSCoordSeq_getOrdinate, ordinate=True, get=True)
  File "/usr/lib/python2.4/site-packages/ctypes/__init__.py", line 328, in __getattr__
    func = self.__getitem__(name)
  File "/usr/lib/python2.4/site-packages/ctypes/__init__.py", line 333, in __getitem__
    func = self._FuncPtr((name_or_ordinal, self))
AttributeError: /usr/lib/libgeos_c.so.1: undefined symbol: GEOSCoordSeq_getOrdinate
I found this on our server which was running  python 2.4, I've heard from some other people that upgrading to python 2.5 fixed this problem. 

Sunday, November 30, 2008

New Friend Requests

The New Friend Requests Page now shows additional information about your potential friend, including your mutual friends

Saturday, August 16, 2008

ImportError: cannot import name ImageField

I came across this error when updating django to the lastest SVN version for gamernook.com.

The fix was to replace

from django.db.models.fields import ImageField

with

from django.db.models.fields.files import ImageField

Easy enough, but took a while to figure out

Thursday, February 07, 2008

Building a YouTube Clone (Day7)

Today i changed the way videos are stored in the database and abstracted the storage of the actual media. Now videos can either be uploaded by users or can be submitted from other video sites such as YouTube. This allows users to discuss and share content even if the video isn't physically on their computer.

Tuesday, February 05, 2008

Building a YouTube Clone (Day 6)

Today I finished the user profile pages. User profile pages show basic info about the user. User's can set a featured video to show off on their profile page. User's can now subscribe to various channels/users. The profile page also shows all of the user's videos, as well as their channel subscribers and subscriptions. In the future this page will also showcase the user's playlists, their interests, and their videolog/blog.



User's can also now edit their profiles, and upload a user picture/avatar. Right now the user profile has the same fields as YouTube but this can and will change in the future.

Monday, February 04, 2008

Building a YouTube Clone (Day 5)

Today I rewrote the video browsing section to allow the user to change the sorting of results, and to change the time period of the search; additionally the user will be able to narrow down their search by category as well. This will affect all result type pages where multiple videos are displayed, such as browsing for videos or viewing which video's you have favorited.

Videos also now keep track of how many times they were commented on and how many times they were marked as favorites.




You can also now search and browse by video tags:



Also whats not shown here is that you can filter down the current videos to a certain video category type as well.

Thursday, January 31, 2008

Building a YouTube Clone (Day 4)

Today I expanding the functionality surrounding videos on the site. Videos can now be rated, favorited, flagged, and commented on. User's can view their videos and their favorite videos.

  • Video Ratings
  • Video Time Formatting
  • Video Favorites
  • Video Flagging
  • Video Commenting
  • Video Comment Rating
Also what few people will appreciate is that the rating for a video are not discrete/quantum values but rather can be a floating point anywhere between 0.0 and 5.0, and the stars will display correctly to represent that rather than Youtube's 1.0, 1.5, 2.0, 2.5 etc