浏览代码

adding product models and admin

master
Jonathan Moss 15 年前
父节点
当前提交
a16ce65154
共有 4 个文件被更改,包括 190 次插入4 次删除
  1. 18
    0
      product/admin.py
  2. 121
    0
      product/migrations/0001_initial.py
  3. 0
    0
      product/migrations/__init__.py
  4. 51
    4
      product/models.py

+ 18
- 0
product/admin.py 查看文件

1
+from product.models import AttributeType
2
+from product.models import Type
3
+from product.models import Item
4
+from product.models import Attribute
5
+from django.contrib import admin
6
+
7
+class AttributeInline(admin.TabularInline):
8
+    model = Attribute
9
+
10
+class ItemAdmin(admin.ModelAdmin):
11
+    list_display = ('name', 'partner_id', 'date_available', 'date_created', 'is_valid')
12
+    inlines = [AttributeInline]
13
+
14
+
15
+admin.site.register(AttributeType)
16
+admin.site.register(Type)
17
+admin.site.register(Item, ItemAdmin)
18
+admin.site.register(Attribute)

+ 121
- 0
product/migrations/0001_initial.py 查看文件

1
+# encoding: utf-8
2
+import datetime
3
+from south.db import db
4
+from south.v2 import SchemaMigration
5
+from django.db import models
6
+
7
+class Migration(SchemaMigration):
8
+    
9
+    def forwards(self, orm):
10
+        
11
+        # Adding model 'AttributeType'
12
+        db.create_table('product_attributetype', (
13
+            ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
14
+            ('name', self.gf('django.db.models.fields.CharField')(max_length=128)),
15
+        ))
16
+        db.send_create_signal('product', ['AttributeType'])
17
+
18
+        # Adding model 'Type'
19
+        db.create_table('product_type', (
20
+            ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
21
+            ('name', self.gf('django.db.models.fields.CharField')(max_length=128)),
22
+        ))
23
+        db.send_create_signal('product', ['Type'])
24
+
25
+        # Adding M2M table for field attribute_types on 'Type'
26
+        db.create_table('product_type_attribute_types', (
27
+            ('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)),
28
+            ('type', models.ForeignKey(orm['product.type'], null=False)),
29
+            ('attributetype', models.ForeignKey(orm['product.attributetype'], null=False))
30
+        ))
31
+        db.create_unique('product_type_attribute_types', ['type_id', 'attributetype_id'])
32
+
33
+        # Adding model 'Item'
34
+        db.create_table('product_item', (
35
+            ('name', self.gf('django.db.models.fields.CharField')(max_length=128)),
36
+            ('partner_id', self.gf('django.db.models.fields.CharField')(max_length=32)),
37
+            ('date_available', self.gf('django.db.models.fields.DateField')()),
38
+            ('date_created', self.gf('django.db.models.fields.DateTimeField')()),
39
+            ('type', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['product.Type'])),
40
+            ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
41
+        ))
42
+        db.send_create_signal('product', ['Item'])
43
+
44
+        # Adding model 'Attribute'
45
+        db.create_table('product_attribute', (
46
+            ('attribute_type', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['product.AttributeType'])),
47
+            ('product', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['product.Item'])),
48
+            ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
49
+            ('value', self.gf('django.db.models.fields.CharField')(max_length=256)),
50
+        ))
51
+        db.send_create_signal('product', ['Attribute'])
52
+
53
+        # Adding model 'StockRecord'
54
+        db.create_table('product_stockrecord', (
55
+            ('tax', self.gf('django.db.models.fields.FloatField')()),
56
+            ('product', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['product.Item'])),
57
+            ('price_excl_tax', self.gf('django.db.models.fields.FloatField')()),
58
+            ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
59
+        ))
60
+        db.send_create_signal('product', ['StockRecord'])
61
+    
62
+    
63
+    def backwards(self, orm):
64
+        
65
+        # Deleting model 'AttributeType'
66
+        db.delete_table('product_attributetype')
67
+
68
+        # Deleting model 'Type'
69
+        db.delete_table('product_type')
70
+
71
+        # Removing M2M table for field attribute_types on 'Type'
72
+        db.delete_table('product_type_attribute_types')
73
+
74
+        # Deleting model 'Item'
75
+        db.delete_table('product_item')
76
+
77
+        # Deleting model 'Attribute'
78
+        db.delete_table('product_attribute')
79
+
80
+        # Deleting model 'StockRecord'
81
+        db.delete_table('product_stockrecord')
82
+    
83
+    
84
+    models = {
85
+        'product.attribute': {
86
+            'Meta': {'object_name': 'Attribute'},
87
+            'attribute_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['product.AttributeType']"}),
88
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
89
+            'product': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['product.Item']"}),
90
+            'value': ('django.db.models.fields.CharField', [], {'max_length': '256'})
91
+        },
92
+        'product.attributetype': {
93
+            'Meta': {'object_name': 'AttributeType'},
94
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
95
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '128'})
96
+        },
97
+        'product.item': {
98
+            'Meta': {'object_name': 'Item'},
99
+            'date_available': ('django.db.models.fields.DateField', [], {}),
100
+            'date_created': ('django.db.models.fields.DateTimeField', [], {}),
101
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
102
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
103
+            'partner_id': ('django.db.models.fields.CharField', [], {'max_length': '32'}),
104
+            'type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['product.Type']"})
105
+        },
106
+        'product.stockrecord': {
107
+            'Meta': {'object_name': 'StockRecord'},
108
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
109
+            'price_excl_tax': ('django.db.models.fields.FloatField', [], {}),
110
+            'product': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['product.Item']"}),
111
+            'tax': ('django.db.models.fields.FloatField', [], {})
112
+        },
113
+        'product.type': {
114
+            'Meta': {'object_name': 'Type'},
115
+            'attribute_types': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['product.AttributeType']", 'symmetrical': 'False'}),
116
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
117
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '128'})
118
+        }
119
+    }
120
+    
121
+    complete_apps = ['product']

+ 0
- 0
product/migrations/__init__.py 查看文件


+ 51
- 4
product/models.py 查看文件

1
 from django.db import models
1
 from django.db import models
2
 
2
 
3
+class AttributeType(models.Model):
4
+    """Defines a product atrribute type"""
5
+    name = models.CharField(max_length = 128)
6
+
7
+    def __unicode__(self):
8
+        return self.name
9
+
10
+
11
+class Type(models.Model):
12
+    """Defines a product type"""
13
+    name = models.CharField(max_length = 128)
14
+    attribute_types = models.ManyToManyField('product.AttributeType')
15
+
16
+    def __unicode__(self):
17
+        return self.name
18
+
3
 class Item(models.Model):
19
 class Item(models.Model):
4
-    title = models.CharField(max_length=255)
5
-    pass
20
+    """The base product object"""
21
+    name = models.CharField(max_length = 128)
22
+    partner_id = models.CharField(max_length = 32)
23
+    type = models.ForeignKey('product.Type')
24
+    date_available = models.DateField()
25
+    date_created = models.DateTimeField()
26
+
27
+    def __unicode__(self):
28
+        return self.name
29
+
30
+    def is_valid(self):
31
+        """
32
+        Returns true if the product is valid.
33
+
34
+        Validity is based on whether the product has all required attribute types
35
+        configured for the product_class it is in.
36
+
37
+        Returns:
38
+            A boolean if the product is valid
39
+        """
40
+        required_attribute_names = []
41
+        for attribute_type in self.type.attribute_types.all():
42
+            required_attribute_names.append(attribute_type.name)
43
+
44
+        for attribute in self.attribute_set.all():
45
+            required_attribute_names.remove(attribute.attribute_type.name)
46
+
47
+        return 0 == len(required_attribute_names)
48
+
49
+class Attribute(models.Model):
50
+    """An individual product attribute"""
51
+    attribute_type = models.ForeignKey('product.AttributeType')
52
+    product = models.ForeignKey('product.Item')
53
+    value = models.CharField(max_length = 256)
6
 
54
 
7
 
55
 
8
 class StockRecord(models.Model):
56
 class StockRecord(models.Model):
57
+    """A stock keeping record"""
9
     product = models.ForeignKey('product.Item')
58
     product = models.ForeignKey('product.Item')
10
     price_excl_tax = models.FloatField()
59
     price_excl_tax = models.FloatField()
11
     tax = models.FloatField()
60
     tax = models.FloatField()
12
-    
13
-    

正在加载...
取消
保存