Skip to content

Commit

Permalink
A first version to support currency fields. The string input will be …
Browse files Browse the repository at this point in the history
…replaced by a tuple.
  • Loading branch information
ikanobori committed Jun 8, 2012
1 parent 0faa3ed commit 7901b6a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
29 changes: 29 additions & 0 deletions sunburnt/schema.py
Expand Up @@ -5,6 +5,7 @@
import operator
import uuid
import warnings
import decimal

from lxml.builder import E
import lxml.etree
Expand Down Expand Up @@ -119,6 +120,21 @@ def __unicode__(self):

return solr_point

class solr_currency(object):
def __init__(self, value, currency=None):
if not currency:
# XXX get from schema
self.currency = "USD"
else:
self.currency = currency

self.value = value

def __repr__(self):
return "solr_currency(%s)" % unicode(self)

def __unicode__(self):
return u"%s,%s" % (self.value, self.currency)

class SolrField(object):
def __init__(self, name, indexed=None, stored=None, required=False, multiValued=False, dynamic=False, **kwargs):
Expand Down Expand Up @@ -227,6 +243,18 @@ def normalize(self, value):
(value, self.__class__, self.name))
return v

class SolrCurrencyField(SolrField):
def normalize(self, value):
return solr_currency(v)

def from_user_data(self, value):
return solr_currency(*value.split(","))

def to_solr(self, value):
return unicode(value)

def from_solr(self, value):
return solr_currency(*value.split(","))

class SolrShortField(SolrNumericalField):
base_type = int
Expand Down Expand Up @@ -391,6 +419,7 @@ class SolrSchema(object):
'solr.PointType':SolrPointField,
'solr.LatLonType':SolrPoint2Field,
'solr.GeoHashField':SolrPoint2Field,
'solr.CurrencyField':SolrCurrencyField
}
def __init__(self, f):
"""initialize a schema object from a
Expand Down
15 changes: 15 additions & 0 deletions sunburnt/test_schema.py
Expand Up @@ -383,6 +383,7 @@ def test_delete_queries():
<!-- And just to check it works: -->
<fieldType name="point3" class="solr.PointType" dimension="3" subFieldSuffix="_d"/>
<fieldType name="uuid" class="solr.UUIDField" indexed="true" />
<fieldType name="currency" class="solr.CurrencyField" precisionStep="8" currencyConfig="currency.xml" defaultCurrency="USD" />
</types>
<fields>
<field name="binary_field" required="false" type="binary"/>
Expand All @@ -391,6 +392,7 @@ def test_delete_queries():
<field name="geohash_field" required="false" type="geohash"/>
<field name="point3_field" required="false" type="point3"/>
<field name="id" type="uuid" indexed="true" stored="true" default="NEW"/>
<field name="currency" type="currency" indexed="true" stored="true" />
</fields>
</schema>
"""
Expand Down Expand Up @@ -445,3 +447,16 @@ def test_uuid_data_understood_ok():
solr_data = "12980286-591b-40c6-aa08-b4393a6d13b3"
uuid_field = s.match_field("id")
assert uuid_field.from_solr(solr_data) == uuid.UUID("12980286-591b-40c6-aa08-b4393a6d13b3")

def test_currency_data_understood_ok():
s = SolrSchema(StringIO.StringIO(new_field_types_schema))

# Test with a currency added (different from the default one, we'll use Norwegian
# Kroner. I like Norwegians!)
user_data = "1.00,NOK"
field_inst = s.field_from_user_data("currency", user_data)
assert unicode(field_inst.value) == u"1.00,NOK"

user_data = "1.00"
field_inst = s.field_from_user_data("currency", user_data)
assert unicode(field_inst.value) == u"1.00,USD"

0 comments on commit 7901b6a

Please sign in to comment.