forms.py :  » Ajax » pyjamas » src » examples » djangowanted » wanted » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Ajax » pyjamas 
pyjamas » src » examples » djangowanted » wanted » forms.py
from django.forms import ModelForm
from django import forms
from wanted.models import *

class FlagForm(ModelForm):
    item = forms.ModelChoiceField(queryset=Item.objects.all())
    type = forms.ModelChoiceField(queryset=FlagType.objects.all())
    value = forms.CharField(max_length=255)

class ItemForm(ModelForm):
    class Meta:
        model = Item
    id = forms.IntegerField(required=False)
    name = forms.CharField(max_length=50, label="Name")
    short_description = forms.CharField(max_length=100, label="Description")
    description = forms.CharField(max_length=1000, label="Details")
    price = forms.FloatField(label="Price")

    def _is_valid(self):
        res = ModelForm.is_valid(self)
        if self.instance.id is None:
            return res
        for f in FlagType.objects.all():
            fv = self.data[f.name]
            d = {'item': self.instance.id, 'type': f.id, 'value': fv}
            try:
                fg = Flag.objects.get(item=self.instance.id, type=f.id)
            except Flag.DoesNotExist:
                continue
            ff = FlagForm(d, instance=fg)
            if not ff.is_valid():
                res = False
                self.errors[f.name] = ff.errors
        return res

    def get(self, id):
        res = {}
        instance = Item.objects.get(id=id)
        for k in instance._meta.fields:
            res[k.name] = getattr(instance, k.name)
        for f in FlagType.objects.all():
            try:
                fg = Flag.objects.get(item=instance.id, type=f.id)
                res[f.name] = fg.value
            except Flag.DoesNotExist:
                continue
        return res

    def delete(self, id=None):
        if id is None:
            instance = self.instance
        else:
            instance = Item.objects.get(id=id)
        for f in FlagType.objects.all():
            try:
                fg = Flag.objects.get(item=instance.id, type=f.id)
            except Flag.DoesNotExist:
                continue
            fg.delete()
        instance.delete()

    def save(self, **args):
        res = ModelForm.save(self, **args)
        for f in FlagType.objects.all():
            fv = self.data[f.name]
            d = {'item': self.instance.id, 'type': f.id, 'value': fv}
            try:
                fg = Flag.objects.get(item=self.instance.id, type=f.id)
            except Flag.DoesNotExist:
                fg = Flag()
            ff = FlagForm(d, instance=fg)
            fv = ff.save()
            setattr(res, f.name, fv)
        return res

    for f in FlagType.objects.all():
        locals()[f.name] = forms.CharField(max_length=100, label=f.description)
    #ItemForm.Meta.fields.append(str(f.name))

def test_item_form():
    for idx in range(1,314):
        try:
            i = Item.objects.get(id=idx)
        except Item.DoesNotExist:
            i = Item()
            i.id = idx
        d = {'id': idx, 'name': 'fred %d' % (idx % 10), 'short_description': 'joe', 'description': 'longer', 'price': 20, 'vehicletype': 'a car', 'numdoors': 5}
        f = ItemForm(d, instance=i)
        if not f.is_valid():
            for (e, k) in f.errors.items():
                print e, k
        it = f.save()

        print it, it.id, it.price, it.vehicletype.id, it.vehicletype.value, it.numdoors.value

        it.price = 25
        if not f.is_valid():
            for (e, k) in f.errors.items():
                print e, k
        it = f.save()

        print it, it.id, it.price, it.vehicletype.id, it.vehicletype.value, it.numdoors.value

def test_add_flagtypes():
    f = FlagType(name="num_doors", description="Number of Doors")
    f.save()
    f = FlagType(name="max_climb_rate", description="Maximum Flying Climb Rate")
    f.save()
    f = FlagType(name="num_ballerinas", description="Maximum Ballerina Storage Capacity")
    f.save()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.