Django

Code

Ticket #913: lunchbox.py

File lunchbox.py, 3.1 kB (added by eugene@lazutkin.com, 3 years ago)
Line 
1 from django.core import meta
2
3 class Food(meta.Model):
4     parent = meta.ForeignKey('self', blank=True, null=True, related_name='child')
5     name   = meta.CharField(maxlength=50)
6
7     class META:
8         ordering = ('name',)
9         admin = meta.Admin(
10             fields = (
11                 (None, {'fields': ('name', 'parent',)}),
12             ),
13             list_display  = ('name',),
14             search_fields = ['name',],
15         )
16    
17     def __repr__(self):
18         return self.name
19        
20     def get_all_children(self):
21         output = []
22         children = self.get_child_list()
23         for c in children:
24             output.append(c)
25             output = output + c.get_all_children()
26         return output
27        
28     def get_all_parents(self):
29         id_list = []
30         object = self
31         while True:
32             id_list.append(object)
33             if not object.parent:
34                 break
35             object = object.get_parent()
36         id_list.reverse()
37         return id_list
38    
39 class Restaurant(meta.Model):
40     foods  = meta.ManyToManyField(Food, filter_interface=meta.HORIZONTAL)
41     last_modified = meta.DateTimeField(auto_now=True )
42     name   = meta.CharField(maxlength=50)
43     lat    = meta.FloatField(max_digits=10, decimal_places=6)
44     lng    = meta.FloatField(max_digits=10, decimal_places=6)
45     street = meta.CharField(maxlength=50)
46     apt    = meta.CharField( maxlength=10, blank=True)
47     city   = meta.CharField(maxlength=32)
48     state  = meta.USStateField(default='TX')
49     zip    = meta.PositiveIntegerField()
50     phone  = meta.PhoneNumberField(blank=True)
51     image  = meta.ImageField(upload_to='lunchbox/restaurants/images', blank=True, null=True)
52     text   = meta.TextField(blank=True, null=True)
53     notes  = meta.TextField(blank=True, null=True)
54
55     class META:
56         admin = meta.Admin(
57             fields = (
58                 (None,        {'fields': ('name', 'foods', 'phone', 'image',)}),
59                 ('Geocoding', {'fields': ('lat', 'lng',)}),
60                 ('Address',   {'fields': ('street', 'apt', 'city', 'state', 'zip',)}),
61                 (None,        {'fields': ('text', 'notes',)}),
62             ),
63             list_display  = ('name', 'street', 'city', 'state', 'zip', 'has_image',),
64             list_filter   = ('city', 'state', 'zip',),
65             ordering      = ('name',),
66             save_on_top   = True,
67             search_fields = ['name', 'street', 'city',],
68             js = (
69                 'http://localhost/tiny_mce/tiny_mce.js',
70                 'http://localhost/appmedia/admin/js/textareas.js',
71             ),
72         )
73         get_latest_by = 'last_modified'
74    
75     def __repr__(self):
76         return self.name
77
78     def has_image(self):
79         if self.get_image_url():
80             return True
81         return False
82     has_image.short_description = 'image?'
83
84 class Link(meta.Model):
85     restaurant = meta.ForeignKey(Restaurant, edit_inline=meta.TABULAR, num_in_admin=2)
86     url  = meta.URLField (verify_exists=False, core=True)
87     name = meta.CharField(maxlength=10, blank=True)