Skip to content

Commit 5212286

Browse files
S0S-90tomasbedrich
authored andcommitted
Get log date for my_logs() (#120)
1 parent 8891327 commit 5212286

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

pycaching/cache.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from pycaching.log import Log, Type as LogType
1212
from pycaching.util import parse_date, rot13, lazy_loaded
1313

14-
# prefix _type() function to avoid colisions with cache type
14+
# prefix _type() function to avoid collisions with cache type
1515
_type = type
1616

1717

@@ -167,7 +167,7 @@ def __init__(self, geocaching, wp, **kwargs):
167167
known_kwargs = {"name", "type", "location", "original_location", "state", "found", "size",
168168
"difficulty", "terrain", "author", "hidden", "attributes", "summary",
169169
"description", "hint", "favorites", "pm_only", "url", "waypoints", "_logbook_token",
170-
"_trackable_page_url", "guid"}
170+
"_trackable_page_url", "guid", "visited"}
171171

172172
for name in known_kwargs:
173173
if name in kwargs:
@@ -463,6 +463,25 @@ def hidden(self, hidden):
463463
"Passed object is not datetime.date instance nor string containing a date.")
464464
self._hidden = hidden
465465

466+
@property
467+
def visited(self):
468+
"""The cache log date (filled by function geocaching.my_logs() if cache is created there)
469+
470+
:setter: Set a cache log date. If :class:`str` is passed, then :meth:`.util.parse_date`
471+
is used and its return value is stored as a date.
472+
:type: :class:`datetime.date`
473+
"""
474+
return self._visited
475+
476+
@visited.setter
477+
def visited(self, visited):
478+
if isinstance(visited, str):
479+
visited = parse_date(visited)
480+
elif not isinstance(visited, datetime.date):
481+
raise errors.ValueError(
482+
"Passed object is not datetime.date instance nor string containing a date.")
483+
self._visited = visited
484+
466485
@property
467486
@lazy_loaded
468487
def attributes(self):

pycaching/geocaching.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -408,19 +408,22 @@ def my_logs(self, log_type=None, limit=float('inf')):
408408
log_type = log_type.value
409409
url += '?lt={lt}'.format(lt=log_type)
410410
cache_table = self._request(url).find(class_='Table')
411-
if cache_table is None: # no finds on the account
411+
if cache_table is None: # no logs on the account
412412
return
413413
cache_table = cache_table.tbody
414414

415415
yielded = 0
416416
for row in cache_table.find_all('tr'):
417-
link = row.find(class_='ImageLink')['href']
418-
guid = parse_qs(urlparse(link).query)['guid'][0]
419-
420417
if yielded >= limit:
421418
break
422419

423-
yield self._try_getting_cache_from_guid(guid)
420+
link = row.find(class_='ImageLink')['href']
421+
guid = parse_qs(urlparse(link).query)['guid'][0]
422+
current_cache = self._try_getting_cache_from_guid(guid)
423+
date = row.find_all('td')[2].text.strip()
424+
current_cache.visited = date
425+
426+
yield current_cache
424427
yielded += 1
425428

426429
def my_finds(self, limit=float('inf')):

test/test_cache.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,16 @@ def test_hidden(self):
137137
with self.assertRaises(PycachingValueError):
138138
self.c.hidden = None
139139

140+
def test_visited(self):
141+
142+
with self.subTest("automatic str conversion"):
143+
self.c.visited = "1/30/2000"
144+
self.assertEqual(self.c.visited, date(2000, 1, 30))
145+
146+
with self.subTest("give date object"):
147+
self.c.visited = date(2000, 1, 30)
148+
self.assertEqual(self.c.visited, date(2000, 1, 30))
149+
140150
def test_attributes(self):
141151
self.assertEqual(self.c.attributes, {"onehour": True, "kids": False, "available": True})
142152

0 commit comments

Comments
 (0)