#!/usr/bin/python
import sys, os
import datetime
from django.core.management import setup_environ
import settings 
project_directory = setup_environ(settings)
project_name = os.path.basename(project_directory)
os.environ['DJANGO_SETTINGS_MODULE'] = "%s.settings" % project_name
from django.db import models

def textform_for_model(model_name):
    app,mdl = model_name.split('.')
    model = models.get_model(app,mdl)
    opts = model._meta
    field_list = []
    for f in opts.fields + opts.many_to_many:
        if not f.editable:
            continue
        formfield = f.formfield()
        if formfield:
            kw=[]
            for a in  ['queryset','maxlength','label','initial','help_text','required']:
                if hasattr(formfield,a):
                    attr = getattr(formfield,a)
                    if attr in [True,False,None]:
                        kw.append("%s=%s" % (a,attr))
                    elif a == 'queryset':
                        kw.append("%s=%s" %(a,"%s.objects.all()" % attr.model.__name__))
                    elif attr:
                        kw.append("%s='%s'" % (a,attr))
            f_text = "    %s = forms.%s(%s)" % (f.name,formfield.__class__.__name__ ,','.join(kw))
            field_list.append(f_text)
    return "class %sForm(forms.Form):\n" % model.__name__ + '\n'.join(field_list)

if __name__ == "__main__":
    cmdl = sys.argv
    for n in cmdl[1:]:
           print textform_for_model(n)
           print "\n"