Commit 695976f8 authored by Lowik BOURGEOISAT's avatar Lowik BOURGEOISAT Committed by ntome

filters: Add dates to FormValue

parent fb3ebfbb
Pipeline #2631 failed with stages
in 1 minute and 46 seconds
......@@ -16,7 +16,7 @@
#
# You should have received a copy of the GNU Lesser General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
import datetime
from decimal import Decimal
import lxml.html as html
......@@ -196,6 +196,16 @@ class FormValue(Filter):
return int(el.attrib['value'])
except KeyError:
return self.default_or_raise(AttributeNotFound('Element %s does not have attribute value' % el))
# datetime input
try:
if el.attrib.get('type', '') == 'date':
return datetime.datetime.strptime(el.attrib['value'], '%Y-%m-%d').date()
elif el.attrib.get('type', '') == 'time':
return datetime.datetime.strptime(el.attrib['value'], '%H:%M').time()
elif el.attrib.get('type', '') == 'datetime-local':
return datetime.datetime.strptime(el.attrib['value'], '%Y-%m-%dT%H:%M')
except KeyError:
return self.default_or_raise(AttributeNotFound('Element %s does not have attribute value' % el))
else:
raise UnrecognizedElement('Element %s is not recognized' % el)
elif el.tag == 'textarea':
......
......@@ -15,7 +15,7 @@
#
# You should have received a copy of the GNU Lesser General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
import datetime
from decimal import Decimal
from unittest import TestCase
from lxml.html import fromstring
......@@ -62,9 +62,11 @@ class FormValueTest(TestCase):
<input type="number" step="0.01" value="0.05" name="test_number2">
<input type="checkbox" checked="on" name="test_checkbox1">
<input type="checkbox" name="test_checkbox2">
<input type="date" value="2012-12-12" name="test_date">
<input type="range" value="20" name="test_range">
<input type="color" value="#fff666" name="test_color">
<input type="date" value="2010-11-12" name="test_date">
<input type="time" value="12:13" name="test_time">
<input type="datetime-local" value="2010-11-12T13:14" name="test_datetime_local">
</form>
''')
......@@ -76,6 +78,9 @@ class FormValueTest(TestCase):
self.assertEqual(False, FormValue('//form//input[@name="test_checkbox2"]')(self.e))
self.assertEqual(20, FormValue('//form//input[@name="test_range"]')(self.e))
self.assertEqual('#fff666', FormValue('//form//input[@name="test_color"]')(self.e))
self.assertEqual(datetime.date(2010, 11, 12), FormValue('//form//input[@name="test_date"]')(self.e))
self.assertEqual(datetime.time(12, 13), FormValue('//form//input[@name="test_time"]')(self.e))
self.assertEqual(datetime.datetime(2010, 11, 12, 13, 14), FormValue('//form//input[@name="test_datetime_local"]')(self.e))
class LinkTest(TestCase):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment