reset auto increment counter in postgres Jun
11
1
3

postgres uses something called a sequence to keep track of the auto increment counts. To list the sequences us:

\ds

The sequence name is in the format of ${table}_{variable}_seq

You can reset the counter using the function

setval('product_id_seq', 1453);

or

select setval('product_id_seq', (SELECT MAX(id) FROM product));

If you do not know the name you can use the pg_get_serial_sequence function on the table 

select pg_get_serial_sequence('product', 'id');

You can also manually update the sequence with the command:

ALTER SEQUENCE product_id_seq RESTART WITH 1453;

comments

remove domain from ssh known_hosts file Jun
11
1
3

ssh-keygen -R <domain>
ssh keygen -R example.com
ssh-keygen -R [localhost]:22

comments

recursively chmod only files or directories Mar
06
1
3

recursively chmod directories only

find . -type d -exec chmod 755 {} \;

recursively chmod files only

find . -type f -exec chmod 644 {} \;

comments

download website with wget Jan
18
1
3

wget --mirror -p --convert-links -P /path/to/download/directory example.com

comments

pretty print json in python Jan
15
1
3

import json
mydata = json.loads(output)
print json.dumps(mydata, indent=4)

comments

ordinal number in python Jan
15
1
3

## {{{ http://code.activestate.com/recipes/576888/ (r10)
#!/usr/bin/env python

def ordinal(value):
    """
    Converts zero or a *postive* integer (or their string 
    representations) to an ordinal value.

    >>> for i in range(1,13):
    ...     ordinal(i)
    ...     
    u'1st'
    u'2nd'
    u'3rd'
    u'4th'
    u'5th'
    u'6th'
    u'7th'
    u'8th'
    u'9th'
    u'10th'
    u'11th'
    u'12th'

    >>> for i in (100, '111', '112',1011):
    ...     ordinal(i)
    ...     
    u'100th'
    u'111th'
    u'112th'
    u'1011th'

    """
    try:
        value = int(value)
    except ValueError:
        return value

    if value % 100//10 != 1:
        if value % 10 == 1:
            ordval = u"%d%s" % (value, "st")
        elif value % 10 == 2:
            ordval = u"%d%s" % (value, "nd")
        elif value % 10 == 3:
            ordval = u"%d%s" % (value, "rd")
        else:
            ordval = u"%d%s" % (value, "th")
    else:
        ordval = u"%d%s" % (value, "th")

    return ordval

if __name__ == '__main__':
    import doctest
    doctest.testmod()
## end of http://code.activestate.com/recipes/576888/ }}}

comments

ordinal number in javascript Jan
15
1
3

Here's a slick way to create ordinal numbers in javascript that I found.

function getGetOrdinal(n) {
   var s=["th","st","nd","rd"],
       v=n%100;
   return n+(s[(v-20)%10]||s[v]||s[0]);
}

comments

mount virtualbox shared folder in ubuntu Jan
15
1
3

on virtualbox settings share a folder and don't auto mount

inside vm check uid/gid

id

create empty dir

mkdir share_folder

edit file

/etc/fstab

enter line

share_folder /path/to/mount vboxsf uid=1000,gid=1000 0 0

comments

change an existing model, auth_user.username, max_length in django Jul
06
1
2

So lets say I had a model an an app that I didn't create that I wanted to change. Well, I would like to avoid going to that app and adjusting the code on the fly. Instead I would rather make the change in my custom application in order to keep the integrity of the code strong.

In my personal case I typically need to increase the max_length of the username in the django.contrib.auth package. You can perform the change by using the class_prepared signal:

# models.py
from django.db.models.signals import class_prepared

def longer_username(sender, *args, **kwargs):
    # you can't just do `if sender == django.contrib.auth.models.User`
    # because you have to import the model
    if sender.__name__ == 'User' and sender.__model__ == 'django.contrib.auth.models.User':
        sender._meta.get_field('username').max_length = 100

class_prepared.connect(longer_username)

You might need to change any help_text and forms.

This code will not update the underlying database. In order to make the update in the database I add a South schema migration.

import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models


class Migration(SchemaMigration):
    def fowards(self, orm):
        db.alter_column('auth_user', 'username', models.CharField(max_length=100))

    def backwards(self, orm):
        db.alter_column('auth_user', 'username', models.CharField(max_length=35))

# copy the rest of the file from the previous migration
# update the value for auth.user / username / maxlength

I don't think I need to say this, but this is hackish and should be used with great caution.

comments

html5 django form fields Jul
06
1
2

If you're ever developing for mobile it's really handy to use the HTML5 form inputs for things like email and url to bring up the contextual keyboard layouts.

An easy way to create the HTML5 input elements is to extend the Input widget input_type.

from django import forms
from django.widgets import Input


class Html5EmailInput(Input):
    input_type = 'email'


class Html5URLInput(Input):
    input_type = 'url'


class CustomForm(forms.Form):
    email = forms.CharField(widget=Html5EmailInput())
    url = forms.CharField(widget=Html5URLInput())

comments