Skip to content

Commit d55af8d

Browse files
committed
Add an is_link flag to the list of things returned in the context data for each instance. Removed the is_direct flag, as there were no tests for it and I have no idea what it was for.
1 parent f625c15 commit d55af8d

File tree

6 files changed

+79
-7
lines changed

6 files changed

+79
-7
lines changed

example_project/example_project.db

0 Bytes
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 field 'Pepulator.address'
12+
db.add_column('pepulator_factory_pepulator', 'address', self.gf('django.db.models.fields.URLField')(max_length=200, null=True), keep_default=False)
13+
14+
15+
def backwards(self, orm):
16+
17+
# Deleting field 'Pepulator.address'
18+
db.delete_column('pepulator_factory_pepulator', 'address')
19+
20+
21+
models = {
22+
'pepulator_factory.distributor': {
23+
'Meta': {'object_name': 'Distributor'},
24+
'capacity': ('django.db.models.fields.IntegerField', [], {}),
25+
'name': ('django.db.models.fields.CharField', [], {'max_length': '256', 'primary_key': 'True'})
26+
},
27+
'pepulator_factory.jamb': {
28+
'Meta': {'object_name': 'Jamb'},
29+
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
30+
'pepulator': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'jambs'", 'to': "orm['pepulator_factory.Pepulator']"}),
31+
'power': ('django.db.models.fields.FloatField', [], {})
32+
},
33+
'pepulator_factory.knuckle': {
34+
'Meta': {'object_name': 'Knuckle'},
35+
'hardness': ('django.db.models.fields.FloatField', [], {}),
36+
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
37+
'img_url': ('django.db.models.fields.URLField', [], {'max_length': '200'}),
38+
'pepulator': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'knuckles'", 'to': "orm['pepulator_factory.Pepulator']"})
39+
},
40+
'pepulator_factory.pepulator': {
41+
'Meta': {'object_name': 'Pepulator'},
42+
'address': ('django.db.models.fields.URLField', [], {'max_length': '200', 'null': 'True'}),
43+
'color': ('django.db.models.fields.CharField', [], {'max_length': '32'}),
44+
'distributed_by': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'stock'", 'null': 'True', 'to': "orm['pepulator_factory.Distributor']"}),
45+
'height': ('django.db.models.fields.IntegerField', [], {}),
46+
'manufacture_date': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
47+
'serial_number': ('django.db.models.fields.IntegerField', [], {'primary_key': 'True'}),
48+
'width': ('django.db.models.fields.IntegerField', [], {})
49+
}
50+
}
51+
52+
complete_apps = ['pepulator_factory']

example_project/pepulator_factory/models.py

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class Pepulator (models.Model):
66
width = models.IntegerField()
77
manufacture_date = models.DateTimeField(auto_now=True)
88
color = models.CharField(max_length=32)
9+
address = models.URLField(null=True)
910

1011
distributed_by = models.ForeignKey('Distributor', null=True,
1112
related_name='stock')
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[{"pk": 1234, "model": "pepulator_factory.pepulator", "fields": {"color": "red", "width": 15, "manufacture_date": "2011-06-10 11:12:21", "distributed_by": "Walmart", "height": 12}}, {"pk": 1235, "model": "pepulator_factory.pepulator", "fields": {"color": "red", "width": 15, "manufacture_date": "2011-06-10 11:12:33", "distributed_by": "Walmart", "height": 12}}, {"pk": 1238, "model": "pepulator_factory.pepulator", "fields": {"color": "chartreuse", "width": 17, "manufacture_date": "2011-06-10 11:13:30", "distributed_by": "Mom & Pop", "height": 12}}, {"pk": 2345, "model": "pepulator_factory.pepulator", "fields": {"color": "blue", "width": 15, "manufacture_date": "2011-06-10 11:12:49", "distributed_by": "Walmart", "height": 12}}, {"pk": 2346, "model": "pepulator_factory.pepulator", "fields": {"color": "blue", "width": 15, "manufacture_date": "2011-06-10 11:13:02", "distributed_by": "Walmart", "height": 12}}, {"pk": "Walmart", "model": "pepulator_factory.distributor", "fields": {"capacity": 1000000}}, {"pk": "Home Depot", "model": "pepulator_factory.distributor", "fields": {"capacity": 700000}}, {"pk": "Mom & Pop", "model": "pepulator_factory.distributor", "fields": {"capacity": 175}}, {"pk": 1, "model": "pepulator_factory.knuckle", "fields": {"pepulator": 1235, "hardness": 2.3450000000000002}}, {"pk": 2, "model": "pepulator_factory.knuckle", "fields": {"pepulator": 1235, "hardness": 1.1000000000000001}}]
1+
[{"pk": 1234, "model": "pepulator_factory.pepulator", "fields": {"color": "red", "width": 15, "manufacture_date": "2011-06-10 11:12:21", "distributed_by": "Walmart", "height": 12}}, {"pk": 1235, "model": "pepulator_factory.pepulator", "fields": {"address": "ppr://1235/", "color": "red", "width": 15, "manufacture_date": "2011-06-10 11:12:33", "distributed_by": "Walmart", "height": 12}}, {"pk": 1238, "model": "pepulator_factory.pepulator", "fields": {"color": "chartreuse", "width": 17, "manufacture_date": "2011-06-10 11:13:30", "distributed_by": "Mom & Pop", "height": 12}}, {"pk": 2345, "model": "pepulator_factory.pepulator", "fields": {"color": "blue", "width": 15, "manufacture_date": "2011-06-10 11:12:49", "distributed_by": "Walmart", "height": 12}}, {"pk": 2346, "model": "pepulator_factory.pepulator", "fields": {"color": "blue", "width": 15, "manufacture_date": "2011-06-10 11:13:02", "distributed_by": "Walmart", "height": 12}}, {"pk": "Walmart", "model": "pepulator_factory.distributor", "fields": {"capacity": 1000000}}, {"pk": "Home Depot", "model": "pepulator_factory.distributor", "fields": {"capacity": 700000}}, {"pk": "Mom & Pop", "model": "pepulator_factory.distributor", "fields": {"capacity": 175}}, {"pk": 1, "model": "pepulator_factory.knuckle", "fields": {"pepulator": 1235, "hardness": 2.3450000000000002}}, {"pk": 2, "model": "pepulator_factory.knuckle", "fields": {"pepulator": 1235, "hardness": 1.1000000000000001}}]

model_blocks/templatetags/model_nodes.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,12 @@ def get_context_data(self, instance, context):
136136
label = field.verbose_name
137137
value = getattr(instance, field.name)
138138
is_list = False
139-
is_direct = True
139+
is_link = (type(field).__name__ in ('URLField',))
140140
model = instance._meta.module_name
141141

142142
if value is not None:
143143
fields.append((
144-
name, label, value, is_list, is_direct, model,
144+
name, label, value, is_list, is_link, model,
145145
))
146146

147147
for rel_obj, model in instance._meta.get_all_related_objects_with_model():
@@ -156,11 +156,11 @@ def get_context_data(self, instance, context):
156156
label = name
157157
value = getattr(instance, name)
158158
is_list = isinstance(value, (list, tuple, Manager))
159-
is_direct = False
159+
is_link = False
160160

161161
if value is not None:
162162
fields.append((
163-
name, label, value, is_list, is_direct, model,
163+
name, label, value, is_list, is_link, model,
164164
))
165165

166166
# If include_fields was defined, then sort by the order.

model_blocks/tests.py

+21-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,20 @@ def setUp(self):
2121
# Mock Django's get_template so that it doesn't load a real file;
2222
# instead just return a template that allows us to verify the context
2323
model_nodes.get_template = Mock(
24-
return_value=Template('{{ title|default_if_none:instance|safe }}:{{ model|safe }},{% for name, label, value, is_list in fields %}{{ name|safe }},{{ label|safe }},{% if not is_list %}{{ value|safe }}{% else %}[{% for item in value.all %}{{ item|safe }},{% endfor %}]{% endif %},{% endfor %}'))
24+
return_value=Template(('{{ title|default_if_none:instance|safe }}:{{ model|safe }},'
25+
'{% for name, label, value, is_list, is_link in fields %}'
26+
'{{ name|safe }},'
27+
'{{ label|safe }},'
28+
'{% if not is_list %}'
29+
'{% if is_link %}'
30+
'@{{ value }}'
31+
'{% else %}'
32+
'{{ value|safe }}'
33+
'{% endif %}'
34+
'{% else %}'
35+
'[{% for item in value.all %}{{ item|safe }},{% endfor %}]'
36+
'{% endif %},'
37+
'{% endfor %}')))
2538

2639

2740
def test_model_format(self):
@@ -34,6 +47,7 @@ def test_model_format(self):
3447
"width,width,15,"
3548
"manufacture_date,manufacture date,2011-06-10 11:12:33,"
3649
"color,color,red,"
50+
"address,address,@ppr://1235/,"
3751
"distributed_by,distributed by,Walmart,"
3852
"knuckles,knuckles,[Knuckle of hardness 2.35,Knuckle of hardness 1.10,],"
3953
"jambs,jambs,[],"
@@ -59,6 +73,7 @@ def test_filter_is_registered(self):
5973
"width,width,15,"
6074
"manufacture_date,manufacture date,2011-06-10 11:12:33,"
6175
"color,color,red,"
76+
"address,address,@ppr://1235/,"
6277
"distributed_by,distributed by,Walmart,"
6378
"knuckles,knuckles,[Knuckle of hardness 2.35,Knuckle of hardness 1.10,],"
6479
"jambs,jambs,[],"
@@ -84,6 +99,7 @@ def test_title_is_used(self):
8499
"width,width,15,"
85100
"manufacture_date,manufacture date,2011-06-10 11:12:33,"
86101
"color,color,red,"
102+
"address,address,@ppr://1235/,"
87103
"distributed_by,distributed by,Walmart,"
88104
"knuckles,knuckles,[Knuckle of hardness 2.35,Knuckle of hardness 1.10,],"
89105
"jambs,jambs,[],"
@@ -129,6 +145,7 @@ def test_model_format(self):
129145
"width,width,15,"
130146
"manufacture_date,manufacture date,2011-06-10 11:12:33,"
131147
"color,color,red,"
148+
"address,address,ppr://1235/,"
132149
"distributed_by,distributed by,Walmart,"
133150
"knuckles,knuckles,[Knuckle of hardness 2.35,Knuckle of hardness 1.10,],"
134151
"jambs,jambs,[],"
@@ -267,6 +284,7 @@ def test_tag_is_registered(self):
267284
"width,width,15,"
268285
"manufacture_date,manufacture date,2011-06-10 11:12:33,"
269286
"color,color,red,"
287+
"address,address,ppr://1235/,"
270288
"distributed_by,distributed by,Walmart,"
271289
"knuckles,knuckles,[Knuckle of hardness 2.35,Knuckle of hardness 1.10,],"
272290
"jambs,jambs,[],"
@@ -306,7 +324,7 @@ def test_with_excluded_fields(self):
306324

307325
template = Template(('{% load model_tags %}'
308326
'{% with pepulator_factory_pepulator_detail_template="pepulator_factory/pepulator_detail.html" %}'
309-
'{% with pepulator_factory_pepulator_exclude="knuckles, jambs, color" %}'
327+
'{% with pepulator_factory_pepulator_exclude="knuckles, jambs, color, address" %}'
310328
'{% detail_block pepulator %}'
311329
'{% endwith %}'
312330
'{% endwith %}'))
@@ -362,6 +380,7 @@ def test_tag_is_registered(self):
362380
"width,width,15,"
363381
"manufacture_date,manufacture date,2011-06-10 11:12:33,"
364382
"color,color,red,"
383+
"address,address,ppr://1235/,"
365384
"distributed_by,distributed by,Walmart,"
366385
"knuckles,knuckles,[Knuckle of hardness 2.35,Knuckle of hardness 1.10,],"
367386
"jambs,jambs,[],"

0 commit comments

Comments
 (0)