Skip to content

Commit

Permalink
Merge branch 'release/0.5.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
rlskoeser committed May 14, 2020
2 parents 03ce9bb + b343084 commit 811c8f7
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
CHANGELOG
=========

0.5.3
---

* Support default option adding fields to solr schema
* Add utility method to convert Solr timestamp to python datetime

0.5.2
-----

Expand Down
2 changes: 1 addition & 1 deletion parasolr/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

default_app_config = 'parasolr.apps.ParasolConfig'

__version_info__ = (0, 5, 2, None)
__version_info__ = (0, 5, 3, None)

# Dot-connect all but the last. Last is dash-connected if not None.
__version__ = '.'.join([str(i) for i in __version_info__[:-1]])
Expand Down
8 changes: 6 additions & 2 deletions parasolr/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,18 @@ class SolrField:
"""

def __init__(self, fieldtype: str, required: bool=False,
multivalued: bool=False):
multivalued: bool=False, default: str=None):
self.type = fieldtype
self.required = required
self.multivalued = multivalued
self.default = default

def __get__(self, obj, objtype):
return {'type': self.type, 'required': self.required,
opts = {'type': self.type, 'required': self.required,
'multiValued': self.multivalued}
if self.default:
opts['default'] = self.default
return opts

def __set__(self, obj, val):
# enforce read-only descriptor
Expand Down
5 changes: 5 additions & 0 deletions parasolr/tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class TestySolrFields:
required_str = schema.SolrStringField(required=True)
multival_str = schema.SolrStringField(multivalued=True)
custom_field = schema.SolrField('text_en')
last_modified = schema.SolrField('date', default='NOW')

# get on the field returns solr config info
assert TestySolrFields.mystring == \
Expand All @@ -26,6 +27,10 @@ class TestySolrFields:
{'type': 'string', 'required': False, 'multiValued': True}
assert TestySolrFields.custom_field == \
{'type': 'text_en', 'required': False, 'multiValued': False}
# with default specified
assert TestySolrFields.last_modified == \
{'type': 'date', 'required': False, 'multiValued': False,
'default': 'NOW'}

# explicitly read-only
with pytest.raises(AttributeError):
Expand Down
12 changes: 12 additions & 0 deletions parasolr/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from datetime import datetime

from parasolr.utils import solr_timestamp_to_datetime


def test_solr_timestamp_to_datetime():
# with microseconds
solr_dt = solr_timestamp_to_datetime('2018-07-02T21:08:46.428Z')
assert solr_dt == datetime(2018, 7, 2, 21, 8, 46)
# without
solr_dt = solr_timestamp_to_datetime('2018-07-02T21:08:46Z')
assert solr_dt == datetime(2018, 7, 2, 21, 8, 46)
12 changes: 12 additions & 0 deletions parasolr/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from datetime import datetime


def solr_timestamp_to_datetime(solr_time):
"""Convert Solr timestamp (isoformat that may or may not include
microseconds) to :class:`datetime.datetime`"""
# Solr stores date in isoformat; convert to datetime object
# - microseconds only included when second is not exact; strip out if
# they are present
if '.' in solr_time:
solr_time = '%sZ' % solr_time.split('.')[0]
return datetime.strptime(solr_time, '%Y-%m-%dT%H:%M:%SZ')

0 comments on commit 811c8f7

Please sign in to comment.