1 | # This is a shell archive. Save it in a file, remove anything before
|
---|
2 | # this line, and then unpack it by entering "sh file". Note, it may
|
---|
3 | # create directories; files and directories will be owned by you and
|
---|
4 | # have default permissions.
|
---|
5 | #
|
---|
6 | # This archive contains:
|
---|
7 | #
|
---|
8 | # devices
|
---|
9 | # devices/__init__.py
|
---|
10 | # devices/admin.py
|
---|
11 | # devices/models.py
|
---|
12 | # devices/tests.py
|
---|
13 | # devices/fixtures
|
---|
14 | # devices/fixtures/msk.json
|
---|
15 | #
|
---|
16 | echo c - devices
|
---|
17 | mkdir -p devices > /dev/null 2>&1
|
---|
18 | echo x - devices/__init__.py
|
---|
19 | sed 's/^X//' >devices/__init__.py << 'cb149f71f097b0bc51fcd4315d659ec2'
|
---|
20 | cb149f71f097b0bc51fcd4315d659ec2
|
---|
21 | echo x - devices/admin.py
|
---|
22 | sed 's/^X//' >devices/admin.py << '5441acfd00fe2f753bc5553ac68101d7'
|
---|
23 | Xfrom devices import models
|
---|
24 | Xfrom django.contrib import admin
|
---|
25 | X
|
---|
26 | Xadmin.site.register(models.DevicePciId)
|
---|
27 | 5441acfd00fe2f753bc5553ac68101d7
|
---|
28 | echo x - devices/models.py
|
---|
29 | sed 's/^X//' >devices/models.py << '93737618aac33064567404e01de70784'
|
---|
30 | X# vim: ts=4 sw=4 fdm=marker tw=158
|
---|
31 | Xfrom django.db import models
|
---|
32 | Xfrom django import forms
|
---|
33 | Xfrom django.utils.translation import ugettext_lazy as _
|
---|
34 | X
|
---|
35 | Xclass HexField(models.Field):
|
---|
36 | X description = 'Integers provided and presented as hexadecimal strings'
|
---|
37 | X
|
---|
38 | X __metaclass__ = models.SubfieldBase
|
---|
39 | X def __init__(self, *args, **kwargs):
|
---|
40 | X if( kwargs['max_length'] % 2 != 0 ) :
|
---|
41 | X kwargs['max_length'] += 1
|
---|
42 | X if( kwargs['max_length'] > 16 ) :
|
---|
43 | X kwargs['max_length'] = 16
|
---|
44 | X kwargs['max_length'] /= 2
|
---|
45 | X super(HexField, self).__init__(*args, **kwargs)
|
---|
46 | X
|
---|
47 | X def db_type(self, connection) :
|
---|
48 | X if self.max_length > 3 :
|
---|
49 | X return 'BIGINT'
|
---|
50 | X elif self.max_length > 1 :
|
---|
51 | X return 'INTEGER'
|
---|
52 | X else :
|
---|
53 | X return 'SMALLINT'
|
---|
54 | X
|
---|
55 | X def to_python(self, value) :
|
---|
56 | X if isinstance(value, int) :
|
---|
57 | X if value < 0 :
|
---|
58 | X raise ValidationError(
|
---|
59 | X "Negative values are not supported ({})".format(value))
|
---|
60 | X
|
---|
61 | X if self.max_length > 4 :
|
---|
62 | X fmt = '0x{:016x}'
|
---|
63 | X elif self.max_length > 2 :
|
---|
64 | X fmt = '0x{:08x}'
|
---|
65 | X else :
|
---|
66 | X fmt = '0x{:04x}'
|
---|
67 | X valid = fmt.format(value)
|
---|
68 | X
|
---|
69 | X elif isinstance(value, (unicode, str)) :
|
---|
70 | X if value == '' :
|
---|
71 | X valid = '0x00'
|
---|
72 | X else :
|
---|
73 | X try :
|
---|
74 | X check = int(value, 16)
|
---|
75 | X valid = value
|
---|
76 | X except ValueError:
|
---|
77 | X raise ValidationError(
|
---|
78 | X "Unable to convert >{}< to hexadecimal".format(value))
|
---|
79 | X elif value is None :
|
---|
80 | X if self.max_length > 4 :
|
---|
81 | X valid = '0x0000000000000000'
|
---|
82 | X elif self.max_length > 2 :
|
---|
83 | X valid = '0x00000000'
|
---|
84 | X else :
|
---|
85 | X valid = '0x0000'
|
---|
86 | X else :
|
---|
87 | X raise ValidationError(
|
---|
88 | X "Expected string or int and got {}".format(type(value)))
|
---|
89 | X
|
---|
90 | X return valid
|
---|
91 | X
|
---|
92 | X def get_prep_value(self, value) :
|
---|
93 | X if isinstance(value, unicode) or isinstance(value, str) :
|
---|
94 | X val = int(value, 16)
|
---|
95 | X elif isinstance(value, int) :
|
---|
96 | X raise ValidationError(
|
---|
97 | X 'get_prep_value called with already prepped value'
|
---|
98 | X )
|
---|
99 | X else :
|
---|
100 | X raise ValidationError(
|
---|
101 | X "Value {} of type {} not expected".format(
|
---|
102 | X value, type(value)
|
---|
103 | X )
|
---|
104 | X )
|
---|
105 | X
|
---|
106 | X return val
|
---|
107 | X
|
---|
108 | X def get_prep_lookup(self, lookup_type, value) :
|
---|
109 | X if lookup_type == 'exact' :
|
---|
110 | X return self.get_prep_value(value)
|
---|
111 | X elif lookup_type == 'in' :
|
---|
112 | X return [self.get_prep_value(v) for v in value]
|
---|
113 | X else :
|
---|
114 | X raise TypeError('Unsupported lookup type')
|
---|
115 | X
|
---|
116 | X def formfield(self, **kwargs) :
|
---|
117 | X """OVerride this later with hex-aware widget"""
|
---|
118 | X defaults = { 'form_class': forms.CharField }
|
---|
119 | X defaults.update(kwargs)
|
---|
120 | X
|
---|
121 | X return super(HexField, self).formfield(**defaults)
|
---|
122 | X
|
---|
123 | Xclass VendorHardwareIdentifier(models.Model) :
|
---|
124 | X identifier = HexField(
|
---|
125 | X max_length=4,
|
---|
126 | X primary_key=True,
|
---|
127 | X verbose_name=_('identifier')
|
---|
128 | X )
|
---|
129 | X name = models.CharField(
|
---|
130 | X max_length=96,
|
---|
131 | X default='Unknown ID',
|
---|
132 | X db_index=True,
|
---|
133 | X verbose_name=_('name')
|
---|
134 | X )
|
---|
135 | X
|
---|
136 | X def __unicode__(self) :
|
---|
137 | X return self.name
|
---|
138 | X
|
---|
139 | X class Meta:
|
---|
140 | X abstract = True
|
---|
141 | X
|
---|
142 | Xclass HardwareDeviceIdentifier(models.Model) :
|
---|
143 | X identifier = HexField(
|
---|
144 | X max_length=4,
|
---|
145 | X verbose_name=_('identifier')
|
---|
146 | X )
|
---|
147 | X name = models.CharField(
|
---|
148 | X max_length=192,
|
---|
149 | X verbose_name=_('device name')
|
---|
150 | X )
|
---|
151 | X
|
---|
152 | X def __unicode__(self) :
|
---|
153 | X return self.name
|
---|
154 | X
|
---|
155 | X class Meta :
|
---|
156 | X abstract = True
|
---|
157 | X
|
---|
158 | Xclass VendorPciId(VendorHardwareIdentifier) :
|
---|
159 | X added = models.DateField(auto_now_add=True)
|
---|
160 | X
|
---|
161 | Xclass DevicePciId(HardwareDeviceIdentifier) :
|
---|
162 | X vendor = models.ForeignKey(VendorPciId,
|
---|
163 | X verbose_name=_('vendor'),
|
---|
164 | X )
|
---|
165 | X added = models.DateField(auto_now_add=True)
|
---|
166 | X
|
---|
167 | X class Meta :
|
---|
168 | X verbose_name = _('PCI device identifier')
|
---|
169 | X verbose_name_plural = _('PCI device identifiers')
|
---|
170 | X unique_together = (('vendor', 'identifier'))
|
---|
171 | X ordering = [ 'name' ]
|
---|
172 | 93737618aac33064567404e01de70784
|
---|
173 | echo x - devices/tests.py
|
---|
174 | sed 's/^X//' >devices/tests.py << '5c6e7aac64209866496e51a471254275'
|
---|
175 | Xfrom __future__ import absolute_import
|
---|
176 | X# vim: ts=4 sw=4 fdm=marker et tw=78 ai
|
---|
177 | Xfrom django.test import TestCase
|
---|
178 | Xfrom django.forms.models import modelform_factory
|
---|
179 | Xfrom .models import DevicePciId, VendorPciId
|
---|
180 | Xfrom datetime import datetime, tzinfo, timedelta
|
---|
181 | X
|
---|
182 | XPK_OUTPUT="""<select name="vendor" id="id_vendor">
|
---|
183 | X<option value="">---------</option>
|
---|
184 | X<option value="0x11ab" selected="selected">Marvell Technology Group
|
---|
185 | XLtd.</option>
|
---|
186 | X<option value="0xfeda">Broadcom Inc</option>
|
---|
187 | X</select>"""
|
---|
188 | X
|
---|
189 | Xclass UTC(tzinfo) :
|
---|
190 | X def utcoffset(self, dt):
|
---|
191 | X return timedelta(0)
|
---|
192 | X def tzname(self, dt) :
|
---|
193 | X return "UTC"
|
---|
194 | X def dst(self, dt) :
|
---|
195 | X return timedelta(0)
|
---|
196 | X
|
---|
197 | Xutc = UTC()
|
---|
198 | X
|
---|
199 | Xclass SimpleTest(TestCase):
|
---|
200 | X def test_custom_field_as_pk(self) :
|
---|
201 | X now = datetime.utcnow().replace(tzinfo=utc)
|
---|
202 | X bc = VendorPciId.objects.create(identifier='0xfeda',
|
---|
203 | X name='Broadcom Inc', added=now)
|
---|
204 | X mtg = VendorPciId.objects.create(identifier='0x11ab',
|
---|
205 | X name='Marvell Technology Group Ltd.', added=now)
|
---|
206 | X msk = DevicePciId.objects.create(identifier='0x4363', vendor=mtg,
|
---|
207 | X name='88E8055 PCI-E Gigabit Ethernet Controller', added=now)
|
---|
208 | X form_class = modelform_factory(DevicePciId)
|
---|
209 | X form = form_class(instance=msk)
|
---|
210 | X output = unicode(form['vendor'])
|
---|
211 | X #print output
|
---|
212 | X self.assertHTMLEqual(output, PK_OUTPUT)
|
---|
213 | 5c6e7aac64209866496e51a471254275
|
---|
214 | echo c - devices/fixtures
|
---|
215 | mkdir -p devices/fixtures > /dev/null 2>&1
|
---|
216 | echo x - devices/fixtures/msk.json
|
---|
217 | sed 's/^X//' >devices/fixtures/msk.json << '399e6afc29059243ac06a621d3380c06'
|
---|
218 | X[
|
---|
219 | X{
|
---|
220 | X "pk": "0x11ab",
|
---|
221 | X "model": "devices.vendorpciid",
|
---|
222 | X "fields": {
|
---|
223 | X "added": "2012-07-17",
|
---|
224 | X "name": "Marvell Technology Group Ltd."
|
---|
225 | X }
|
---|
226 | X},
|
---|
227 | X{
|
---|
228 | X "pk": "0xfeda",
|
---|
229 | X "model": "devices.vendorpciid",
|
---|
230 | X "fields": {
|
---|
231 | X "added": "2012-07-17",
|
---|
232 | X "name": "Broadcom Inc"
|
---|
233 | X }
|
---|
234 | X},
|
---|
235 | X{
|
---|
236 | X "pk": 1,
|
---|
237 | X "model": "devices.devicepciid",
|
---|
238 | X "fields": {
|
---|
239 | X "identifier": "0x4363",
|
---|
240 | X "vendor": 4523,
|
---|
241 | X "name": "88E8055 PCI-E Gigabit Ethernet Controller",
|
---|
242 | X "added": "2012-07-17"
|
---|
243 | X }
|
---|
244 | X}
|
---|
245 | X]
|
---|
246 | 399e6afc29059243ac06a621d3380c06
|
---|
247 | exit
|
---|
248 |
|
---|