ISBN model field: displays 10- and 13-digit variants and external links

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
###################################### modelfields.py ######################################

class FSISBNField(models.CharField):
	def __init__(self, *args, **kwargs):
		kwargs['max_length'] = 13
		super(FSISBNField, self).__init__(*args, **kwargs)
	def formfield(self, **kwargs):
		kwargs['widget'] = FSAltISBNWidget()
		return super(FSISBNField, self).formfield(**kwargs)

###################################### widgets.py #########################################

class AltISBNWidget(forms.TextInput):
	def __init__(self, language=None, attrs={}):
		super(AltISBNWidget, self).__init__(attrs=attrs)
	def render(self, name, value, attrs={}):
		if (not value) or len(str(value)) < 2:
			return super(FSAltISBNWidget, self).render(name, value, attrs)
		return super(FSAltISBNWidget, self).render(name, value, attrs) + mark_safe("""
			<br />
			<div class="alt">%s-digit: <span class="altisbn">%s <span class="doodad">
				<a href="http://www.librarything.com/isbn/%s">more &#9901;</a>
			</span></span></div>
		""" % (
			(len(str(value)) == 10) and "13" or "10",
			pyisbn.convert(value),
			value
		))

More like this

  1. Amazon product-data interface class for Django-friendly PyAWS queries by fish2000 3 years, 3 months ago
  2. Percent Field by fylb 3 years, 3 months ago
  3. Username form field by sma 4 years, 6 months ago
  4. models ColorField with clean minimal widget by andybak 2 years ago
  5. decorators for creating paramaterized decorators and easy monkeypatching by fish2000 3 years, 3 months ago

Comments

miernik (on January 22, 2011):

Why is it called FSISBNField() but the instruction on the right is to write isbn = ISBNField()?

What is this "FS" about?

I think all FS should be removed from the source.

#

(Forgotten your password?)