Skip to content
This repository

solr currencyfield #56

Closed
wants to merge 2 commits into from

5 participants

Simon de Vlieger Bengt Lüers André Cruz Daan Wynen Toby White
Simon de Vlieger

I've left the currency field as a string. I can't decide on what would be the most used and comfortable way to represent monetary values in Python in a localised and internationalised way. I've fiddled with Decimal, there are other obvious ideas but I've decided to let it stay at the notation exactly how currency fields are stored in solr.

This relates to issue #48.

Bengt Lüers
Bengt commented

Thanks, this solves #48 for me. Is there any reason why is this not in tow/master, yet?

André Cruz

Yes, I would like to know this as well. Is there any problem with this patch?

Daan Wynen

shouldnt this be fixed with the patch for issue #66?

Bengt Lüers
Bengt commented

Yes, looking at the attached commits, I guess this pull request might be superseded by #66, but I am not versed well enough with this project to be sure.

Toby White
Owner

#66 did indeed fix this, in that it provides exactly the same behaviour as this patch, plus more.

Toby White tow closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.

Showing 2 changed files with 46 additions and 2 deletions. Show diff stats Hide diff stats

  1. +31 2 sunburnt/schema.py
  2. +15 0 sunburnt/test_schema.py
33 sunburnt/schema.py
@@ -5,6 +5,7 @@
5 5 import operator
6 6 import uuid
7 7 import warnings
  8 +import decimal
8 9
9 10 from lxml.builder import E
10 11 import lxml.etree
@@ -119,6 +120,21 @@ def __unicode__(self):
119 120
120 121 return solr_point
121 122
  123 +class solr_currency(object):
  124 + def __init__(self, value, currency=None):
  125 + if not currency:
  126 + # XXX get from schema
  127 + self.currency = "USD"
  128 + else:
  129 + self.currency = currency
  130 +
  131 + self.value = value
  132 +
  133 + def __repr__(self):
  134 + return "solr_currency(%s)" % unicode(self)
  135 +
  136 + def __unicode__(self):
  137 + return u"%s,%s" % (self.value, self.currency)
122 138
123 139 class SolrField(object):
124 140 def __init__(self, name, indexed=None, stored=None, required=False, multiValued=False, dynamic=False, **kwargs):
@@ -195,8 +211,8 @@ def normalize(self, value):
195 211 elif value.lower() == "false":
196 212 return False
197 213 else:
198   - raise ValueError("sorry, I only understand simple boolean strings (field %s)" %
199   - self.name)
  214 + raise ValueError("sorry, I only understand simple boolean strings (field %s, value %s)" %
  215 + self.name, value)
200 216 return bool(value)
201 217
202 218
@@ -227,6 +243,18 @@ def normalize(self, value):
227 243 (value, self.__class__, self.name))
228 244 return v
229 245
  246 +class SolrCurrencyField(SolrField):
  247 + def normalize(self, value):
  248 + return solr_currency(v)
  249 +
  250 + def from_user_data(self, value):
  251 + return solr_currency(*value.split(","))
  252 +
  253 + def to_solr(self, value):
  254 + return unicode(value)
  255 +
  256 + def from_solr(self, value):
  257 + return solr_currency(*value.split(","))
230 258
231 259 class SolrShortField(SolrNumericalField):
232 260 base_type = int
@@ -391,6 +419,7 @@ class SolrSchema(object):
391 419 'solr.PointType':SolrPointField,
392 420 'solr.LatLonType':SolrPoint2Field,
393 421 'solr.GeoHashField':SolrPoint2Field,
  422 + 'solr.CurrencyField':SolrCurrencyField
394 423 }
395 424 def __init__(self, f):
396 425 """initialize a schema object from a
15 sunburnt/test_schema.py
@@ -383,6 +383,7 @@ def test_delete_queries():
383 383 <!-- And just to check it works: -->
384 384 <fieldType name="point3" class="solr.PointType" dimension="3" subFieldSuffix="_d"/>
385 385 <fieldType name="uuid" class="solr.UUIDField" indexed="true" />
  386 + <fieldType name="currency" class="solr.CurrencyField" precisionStep="8" currencyConfig="currency.xml" defaultCurrency="USD" />
386 387 </types>
387 388 <fields>
388 389 <field name="binary_field" required="false" type="binary"/>
@@ -391,6 +392,7 @@ def test_delete_queries():
391 392 <field name="geohash_field" required="false" type="geohash"/>
392 393 <field name="point3_field" required="false" type="point3"/>
393 394 <field name="id" type="uuid" indexed="true" stored="true" default="NEW"/>
  395 + <field name="currency" type="currency" indexed="true" stored="true" />
394 396 </fields>
395 397 </schema>
396 398 """
@@ -445,3 +447,16 @@ def test_uuid_data_understood_ok():
445 447 solr_data = "12980286-591b-40c6-aa08-b4393a6d13b3"
446 448 uuid_field = s.match_field("id")
447 449 assert uuid_field.from_solr(solr_data) == uuid.UUID("12980286-591b-40c6-aa08-b4393a6d13b3")
  450 +
  451 +def test_currency_data_understood_ok():
  452 + s = SolrSchema(StringIO.StringIO(new_field_types_schema))
  453 +
  454 + # Test with a currency added (different from the default one, we'll use Norwegian
  455 + # Kroner. I like Norwegians!)
  456 + user_data = "1.00,NOK"
  457 + field_inst = s.field_from_user_data("currency", user_data)
  458 + assert unicode(field_inst.value) == u"1.00,NOK"
  459 +
  460 + user_data = "1.00"
  461 + field_inst = s.field_from_user_data("currency", user_data)
  462 + assert unicode(field_inst.value) == u"1.00,USD"

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.