'Creates new revision number based on previous one.
Increments it + random bytes. On overflow starts from 0 again.'

def create_new_rev(self, old_rev=None):

    if old_rev:
        try:
            rev_num = int(old_rev[:4], 16)
        except:
            raise RevConflict()
        rev_num += 1
        if (rev_num > 65025):
            rev_num = 0
        rnd = randrange(65536)
        return ('%04x%04x' % (rev_num, rnd))
    else:
        rnd = randrange((256 ** 2))
        return ('0001%04x' % rnd)
