Rate limiting with django-ratelimit

I was playing around with James Socol’s django-ratelimit tonight. It provides a decorator to rate-limit view functions. Very nice! I wanted to use it for login blocking, and some minor changes helped this out. My forked version of django-ratelimit is a available on GitHub It adds a message when a call is blocked and the ability to clear the count for given user.

Adding it to django-userena sign-in was as simple as:

in userena/views.py:

from ratelimit.decorators import ratelimit, clear
@ratelimit(field='identification',rate="5/m", method="POST", block=True, error_message="You have too many invalid login attempts.  Please try again later")
def signin(request, auth_form=AuthenticationForm,.....
     #after successful login
     clear(request, field='identification') ### clear rate limiting 

Posted in Django, Python | Leave a comment

Custom Tag to Render Avatar in Django Userena

Here is a simple custom tag to render a users avatar (‘mugshot’) when using Django Userena. It optionally takes width and or height in pixels.

Place the following code in /templatetags/my_app_tags.py:


from django import template
register = template.Library()

from django import template
register = template.Library()

def avatar(context, height=None, width=None):
    """ returns html for avatar, optional height/width supplied """
    user = context['user']
    widthAttr = 'width="%s"'%width if width is not None else ""
    heightAttr = 'height="%s"'%height if height is not None else ""
    return """<img %s %s class="avatar" src="%s"" alt="avatar image"  />"""%(heightAttr, widthAttr, user.get_profile().get_mugshot_url())

The following template shows how this could be used

{% load myapp_tags %}

{% avatar %}
{% avatar height=5 %}
{% avatar width=150 %}
{% avatar width=100 height=100 %} 

Posted in Django, Python | Leave a comment

Mothballed Software

We all probably have mothballed software – projects that didn’t go very far. This was one of mine, a rowing game!

Posted in programming, rowing | Leave a comment

Django Image Field Overwrite with ImageKit

I’m using django-imagekit on a new site. I was having a problem that when a user uploaded a new image for the object, the system would not overwrite the existing image file, but instead would create a new file with a sequence number on the end. This was not the behavior I was looking for. Lockjaw’s answer on StackExchange handled the not-overwriting the existing file problem. ImageKit was still caching the previous image for any specfields. The solution is to call ‘.clear()’ after saving the new image to purge the ImageKit cache for that specfield/image.

So, the total solution is:

#Lockjaw's code on StackExchange
class OverwriteStorage(FileSystemStorage):   
    def get_available_name(self, name):
        if self.exists(name):
            os.remove(os.path.join(settings.MEDIA_ROOT, name))
        return name

def issue_plane_image_path(plane, filename):
    "return the canonical file name for an image on this plane instance"
    extension = os.path.splitext(filename)[1]
    return os.path.join('photos/planes', plane.slug+extension) 

class Plane(models.Model):
    image = models.ImageField(upload_to=issue_plane_image_path, storage=OverwriteFileStorage(), null=True, blank=True)
    gallery = ImageSpecField(...)
    thumbnail = ImageSpecField(...) 

    def clearImageCache(self):
       "invalidate all ImageKit spec caches"

and in the edit view:

if form.is_valid():

And now each instance has very few images (usually only one, but if a person uploads different filetypes such as a PNG then a JPG, they will both exist. I am OK with that.)

Posted in Django, Python | Leave a comment

TiddlyWiki with Firefox 15

TiddlyWiki saves (and exports) stopped working with the Firefox upgrade to 15. After trying a few changes to prefs.js, I found the post Will Firefox 16 support TiddlyWiki on the Mozilla site. It had a link to a Firefox extension called Tiddly Wiki 5, which fixed the problems I had with saving and the export plugin.


Posted in Uncategorized | Tagged , | Leave a comment

Custom Sort in Google Apps Spreadsheet

I use a Google Docs spreadsheet to track my personal projects. Only, I’m not aware of a way to save sort options in a Google Docs spreadsheet. The solution is to create a script. The following script will add a menu option that remembers sort parameters and makes use of a header row on the data. Detailed instructions on adding the script are here: http://productforums.google.com/forum/#!topic/docs/Kl9WEj6Kha0 and I just added skipping of the header row.

* sort spreadsheet assuming first row is a header row
function customSort() {
var sheet = SpreadsheetApp.getActiveSheet();
var allData = sheet.getDataRange();
var range = sheet.getRange(2,1, allData.getNumRows()-1, allData.getNumColumns());
range.sort( [1,2,4]); // see https://developers.google.com/apps-script/class_range#sort

* Adds a custom menu to the active spreadsheet, containing a single menu item
* for invoking the readRows() function specified above.
* The onOpen() function, when defined, is automatically invoked whenever the
* spreadsheet is opened.
* For more information on using the Spreadsheet API, see
* https://developers.google.com/apps-script/service_spreadsheet
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : “Custom Sort”,
functionName : “customSort”
sheet.addMenu(“Custom Scripts”, entries);

Posted in Uncategorized | Leave a comment

SimpleJson Error with django under google app engine

Since moving over to 64 bit windows 7, I’ve had this error when starting up:

"... py_zipimport.py:108] zipimporter('C:\\python25\\lib\\site-
packages\\simplejson-2.0.9-py2.5-win.egg', 'simplejson\\') ...
dev_appserver_main.py ... SetGlobals() ...
File "C:\Program 
Files\Google\google_appengine\google\appengine\dist\py_zipimport.py", line 
268, in _getitem__
info = _zipfile_cache[self._archive].getinfo(filename)
File "C:\Python25\lib\zipfile.py", line 462, in getinfo
return self.NameToInfo[name]
KeyError: 'simplejson\\_speedups.pyd'

The solution is from peplafi on the google app engine defect list:

It looks that it gives the error when you have installed simplejson in your python
dist and dev_appserver is mixing its own simplejson (which is located in
django\utils) and the python’s sipmlejson. The solution is to go to

The solution is to go to 

"c:\Program Files\Google\google_appengine\google\appengine\ext\key_range\__init__.py" 
, line 26

and change:

   import simplejson


   from django.utils import simplejson

Posted in Python | Leave a comment

Rynsc on Windows 7 Under Cygwin

I *finally* have rysnc working under Windows 7 without issuing permission-denied messages. Here’s the story:

The goal is to run a bash script that backs up a set of directories to a an external hard-drive. This script will run as me from a bash shell.

The destination hard drive is mounted as E: in this example. The bash script contains a series of rysnc commands similar to:

rsync -rltgovR --delete-after /cygdrive/h/music /cygdrive/e

This will mirror h:\music on E:\h\music with recursion.

The catch was getting permissions correct on the external drive. This took two commands run from a cmd.exe shell (bash shell should work if you escape the parameters correctly, but don’t bother for two commands).

E: is rsync destination for mirrors, ‘Kevin’ is the windows user name the script will be running under.

cd \
takeown /f * /r /d y
icacls * /grant Kevin:(F) /T

This did it for me. Hope this helps.

Some other posts mention adding
none /cygdrive cygdrive binary,posix=0,user,noacl 0 0
to /etc/fstab. I tried this both ways and it did not seem to matter. For now it is commented out.

Posted in programming | Leave a comment

Review of Ergometer Testing for Rowers

This is a review of a new ebook I purchased from Row Perfect called Ergometer Testing for Rowers by Walter Martindale.

The ebook is 10 pages of content plus a font and back sheet.

  1. Introduction
  2. Basic Physiology for Coaches and Athletes
  3. Warming Up
  4. Plan of Attack
  5. Develop a Race Plan
  6. What’s Your Goal
  7. Doing the Test
  8. After the Test
  9. Technique During an Ergometer Test
  10. About the Author
  11. Ergometer Split Chart

Mr. Martindale explains the importance of and reasons for an erg test in the Introduction. The Basic Physiology for Coaches and Athletes section does a nice job of outlining the energy systems in the context of a 2K erg piece. He shines in the Warming Up section by providing the what is probably the best description of what happens when you warm up, and why it is imperative to do so, that I have seen to date. I feel I received my money’s worth for purchase price with this section. The final part of Warming Up outlines a detailed pre-race warm-up plan. The next three sections cover preparing your strategy. They are a bit light. Doing the Test provides coaching on execution of your race plan and practical tips. This contains the most amusing line in the book, regarding people falling to the floor and writhing in pain after a 2K:

    -sure they’re tired and everything hurts, but a lot more people fall of the ergometers than fall out of boats at the end of a really hard 2000m race.

After the Test covers what to do to recover from the piece. Technique During and Ergometer Test talks about people modifying their technique off the water vs. on the water. It also includes a very interesting chart of ergometer test profiles from some univesirty testing in New Zealand. The book then concludes with a short biography of the author and an ergometer split chart. The latter was not as useful, as I prefer the Power Twenty Online Calculator myself(!)

This is a short ebook that shines in its explanation of the physiological aspects of preparing, rowing, and recovering from a 2K ergometer test. I found it a good value for the £5.00 (~$8.00 USD) price and would recommend it for anyone, regardless of experience, that is serious about their body and performance on the 2K.

Posted in indoor-rowing, rowing | 1 Comment

Sequence from every Nth Item of another Sequence

The phrase ‘every Nth item‘ may be off here, but it is close enough what I want to do (reduce a data set for charting purposes).

# data = sequence of data items
# nth = you want every nth element in the array
[data[index] for index in xrange(0, len(data), nth)]

for example:

data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print [data[index] for index in xrange(0, len(data), 2)]   # [1, 3, 5, 7, 9]
print [data[index] for index in xrange(0, len(data), 3)]   # [1, 4, 7, 10]
print [data[index] for index in xrange(0, len(data), 4)]   # [1, 5, 9]
print [data[index] for index in xrange(0, len(data), 5)]   # [1, 6] 

Posted in programming, Python | 12 Comments