PJBlog : Pierre-Jean, WinLibre et Cie...

Pierre-Jean Coudert - Logiciels Libres - Développement - etc...

Aller au contenu | Aller au menu | Aller à la recherche

samedi, janvier 29 2011

How to use South to migrate models in a multiple databases Django project

Django 1.2 brings multiple databases handling through the use of database routers. The actual version of South (0.7.3) is not able to handle correctly multiple databases projects out of the box.

Here is a workaround: the idea is to create a new fake django project. And then to use South migration within this project.

 cd head_directory
 django-admin.py startproject my_south_db_project
 cd my_south_db_project

Next,edit my_south_db_project/settings.py

  • we have to set the path correctly to south, to your app and to the corresponding database
  • The only installed apps should be South and your app
 
 import sys
 import os.path
 ...  
 current_path = os.path.abspath(os.path.dirname(_ _file_ _))
 head, tail = os.path.split(current_path)
 sys.path.append(os.path.join(head, "my_db_app_dir"))
 sys.path.append(os.path.join(head, "south_sdir"))
 dbpath = os.path.join(head, "my_initial_project_dir")
 ...
 DATABASES = {
   'default': {
       'ENGINE': 'django.db.backends.sqlite3',
       'NAME' : os.path.join(dbpath,'my_db_app.sqlite')
       'USER': '',
       'PASSWORD': '',
       'HOST': '',
       'PORT': '',
   },
 }
 ...  
 INSTALLED_APPS = (
   'south',
   'my_db_app',)
 ...

Create south table in the new db

 ./manage.py syncdb

Now you can work in this project to manage your apps related to this new db

 manage.py convert_to_south my_db_app

on other computers

 manage.py migrate my_db_app 0001 --fake

modify models.py

 manage.py schemamigration my_db_app --auto
 manage.py migrate my_db_app

As you can see, this way you keep the standard South workflow !

Edit: I use this line in the main project's settings.py in order to be able to launch tests:

 SOUTH_TESTS_MIGRATE = False

dimanche, septembre 27 2009

Django code snippets for Notepad++

Here is the content for my Quicktext.ini. This adds Django shortcuts to Notepad++ with the QuickText Plugin.

ie: type model + <TAB> to insert the following model's skeleton:


class ModelName(models.Model):
    """Model docstring"""
    slug = models.SlugField(max_length=200)
    name = models.CharField(max_length=100, default='name')

    updated = models.DateTimeField('update date', auto_now=True )
    created = models.DateTimeField('creation date', auto_now_add=True )

    class Meta:
        pass

    def __unicode__(self):
        return name

    def save(self, force_insert=False, force_update=False):
        pass

    @models.permalink
    def get_absolute_url(self):
       return ('view_or_url_name')


class ModelNameAdmin(admin.ModelAdmin):
    list_display = ('name',)
    search_fields = ['name',]

admin.site.register(ModelName, ModelNameAdmin)

Lire la suite...

dimanche, novembre 16 2008

Python Sidebar update

The original Python Sidebar is actually unusable due to the python 2.6 documentation refactoring...

But thanks to Bruno there is now an updated Python 2.6 Sidebar for Firefox.

Click here to bookmark the Python 2.6 Sidebar for Firefox.

lundi, juin 25 2007

Pwytter has now its own domain

Pwytter, the free, multi-platform graphical twitter client has now its own domain:

Calvin Spealman, one of the first tester said: I like it. Such a slick looking Tk app is surprising.

dimanche, juin 24 2007

Pwytter 0.3 Windows executable

Pwytter is a graphical client for Twitter written in Python / tkInter.

If you want to try this 0.3 release, download the following file, decompress it and double click on pwytter.exe

The source distribution should work on Mac OSX and Linux too.

More info on Pwytter 0.3...

vendredi, juin 22 2007

Pwytter 0.3: soon to be usable

Pwytter is a twitter client written in Python.

pwytter, a twitter client written in Python / tkInter

ChangeLog, release 0.3

  • Collapsable edit parameters (Saved in XML Files)
  • clickable profile and user URLs, Names and Images
  • Improved UI
  • Multi-lines message display and improved word wrap
  • Better path management
  • icons on button (from famfamfam)
  • balloons suport (Hints)
  • Bug corrected in UserTimeLine Display : error 404

Download from SVN

lundi, juin 18 2007

Pwytter 0.2 Screenshot

My Python Twitter client

The UI is written with Tkinter.

Runs actually on windows, and soon on Linux and Mac OS X.

Features

  • User TimeLine
  • Friends TimeLine
  • Public TimeLine

Source Code

Package used

mercredi, décembre 13 2006

A SimpleXMLRPCServer with allow_reuse_address

XMLRPCServer / Python

Useful trick on Linux when you need to reconnect often to the same socket.

import SocketServer
import SimpleXMLRPCServer        

class MyTCPServer(SocketServer.TCPServer):
    def server_bind(self):
        self.socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
        self.socket.bind(self.server_address)

class MyXMLRPCServer(MyTCPServer, SimpleXMLRPCServer.SimpleXMLRPCDispatcher):
    def __init__(self, addr, requestHandler=SimpleXMLRPCServer.SimpleXMLRPCRequestHandler, logRequests=1):
        self.logRequests = logRequests
        SimpleXMLRPCServer.SimpleXMLRPCDispatcher.__init__(self)
        MyTCPServer.__init__(self, addr, requestHandler)

my_object = MyObject()
server = MyXMLRPCServer(("localhost", 8000),logRequests=False)
server.register_instance(self.my_object)        
while True:
    self.server.handle_request()

mercredi, novembre 29 2006

How to remove SimpleXMLRPCServer Request logs

This is a simple example intended to show how to remove log messages printed on stderr for each request.

 import SimpleXMLRPCServer
 class NoLogHandler(SimpleXMLRPCServer.SimpleXMLRPCRequestHandler):
     def log_request(self, code='-', size='-'):
         pass
 server = SimpleXMLRPCServer.SimpleXMLRPCServer(("localhost", 8888), NoLogHandler)
 object = MyObject()
 server.register_instance(object)        
 server.serve_forever()

vendredi, juin 23 2006

Using pyCairo with pyGame surface

I wanted to use pyCairo with pyGame but can't find any sample on the net.

So, here is a little bit of code to do this without using pyGTK.

#!/usr/bin/env python
import cairo
import pygame
import array
import math
def draw(surface):
    x,y, radius = (250,250, 200)
    ctx = cairo.Context(surface)
    ctx.set_line_width(15)
    ctx.arc(x, y, radius, 0, 2.0 * math.pi)
    ctx.set_source_rgb(0.8, 0.8, 0.8)
    ctx.fill_preserve()
    ctx.set_source_rgb(1, 1, 1)
    ctx.stroke()
    
def input(events): 
   for event in events: 
      if event.type == pygame.QUIT: 
         sys.exit(0) 
      else: 
         print event 

#Create Cairo Surface
Width, Height = 512, 512
data = array.array('c', chr(0) * Width * Height * 4)
stride = Width * 4
surface = cairo.ImageSurface.create_for_data (data, cairo.FORMAT_ARGB32,Width, Height, stride)
#init PyGame
pygame.init()
window = pygame.display.set_mode( (Width,Height) ) 
screen = pygame.display.get_surface()
#Draw with Cairo
draw(surface)
#Create PyGame surface from Cairo Surface
image = pygame.image.frombuffer(data.tostring(),(Width,Height),"ARGB",)
#Tranfer to Screen
screen.blit(image, (0,0)) 
pygame.display.flip() 

while True: 
   input(pygame.event.get())

Links

Notes

  • This is working on Linux and Windows.
  • I'm using pyGame 1.7.1. There seems to be a bug in the surface creation with the "ARGB" byte order...