Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix date #597

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 27 additions & 25 deletions src/hamster/edit_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class CustomFactController(Controller):
def __init__(self, action, fact_id=None):
Controller.__init__(self)

self._date = None # for the date property
self._day = None # for the day property

self._gui = load_ui_file("edit_activity.ui")
self.window = self.get_widget('custom_fact_window')
Expand Down Expand Up @@ -100,8 +100,7 @@ def __init__(self, action, fact_id=None):
self.fact = Fact(start_time=dt.datetime.now())

original_fact = self.fact
# TODO: should use hday, not date.
self.date = self.fact.date
self.day = self.fact.date

self.update_fields()
self.update_cmdline(select=True)
Expand Down Expand Up @@ -129,14 +128,14 @@ def __init__(self, action, fact_id=None):
self.window.show_all()

@property
def date(self):
def day(self):
"""Default hamster day."""
return self._date
return self._day

@date.setter
def date(self, value):
delta = value - self._date if self._date else None
self._date = value
@day.setter
def day(self, value):
delta = value - self._day if self._day else None
self._day = value
self.cmdline.default_day = value
if self.fact and delta:
if self.fact.start_time:
Expand All @@ -146,22 +145,22 @@ def date(self, value):
# self.update_fields() here would enter an infinite loop

def on_prev_day_clicked(self, button):
self.increment_date(-1)
self.increment_day(-1)

def on_next_day_clicked(self, button):
self.increment_date(+1)
self.increment_day(+1)

def draw_preview(self, start_time, end_time=None):
day_facts = runtime.storage.get_facts(self.date)
self.dayline.plot(self.date, day_facts, start_time, end_time)
day_facts = runtime.storage.get_facts(self.day)
self.dayline.plot(self.day, day_facts, start_time, end_time)

def get_widget(self, name):
""" skip one variable (huh) """
return self._gui.get_object(name)

def increment_date(self, days):
def increment_day(self, days):
delta = dt.timedelta(days=days)
self.date += delta
self.day += delta
self.update_fields()

def show(self):
Expand All @@ -186,7 +185,7 @@ def on_category_changed(self, widget):

def on_cmdline_changed(self, widget):
if self.master_is_cmdline:
fact = Fact.parse(self.cmdline.get_text(), default_day=self.date)
fact = Fact.parse(self.cmdline.get_text(), default_day=self.day)
previous_cmdline_fact = self.cmdline_fact
# copy the entered fact before any modification
self.cmdline_fact = fact.copy()
Expand Down Expand Up @@ -249,7 +248,9 @@ def on_start_date_changed(self, widget):
# preserve fact duration
self.fact.end_time += delta
self.end_date.date = self.fact.end_time
self.date = self.fact.date or dt.hday.today()
if self.fact.date:
# change default day only if date is set
self.day = self.fact.date
self.validate_fields()
self.update_cmdline()

Expand All @@ -269,7 +270,7 @@ def on_start_time_changed(self, widget):
new_time)
else:
# date not specified; result must fall in current hamster_day
new_start_time = dt.datetime.from_day_time(dt.hday.today(), new_time)
new_start_time = dt.datetime.from_day_time(self.day, new_time)
else:
new_start_time = None
self.fact.start_time = new_start_time
Expand All @@ -289,7 +290,7 @@ def present(self):
def update_cmdline(self, select=None):
"""Update the cmdline entry content."""
self.cmdline_fact = self.fact.copy(description=None)
label = self.cmdline_fact.serialized(default_day=self.date)
label = self.cmdline_fact.serialized(default_day=self.day)
with self.cmdline.handler_block(self.cmdline.checker):
self.cmdline.set_text(label)
if select:
Expand Down Expand Up @@ -329,30 +330,31 @@ def validate_fields(self):
"""Check fields information.

Update gui status about entry and description validity.
Try to merge date, activity and description informations.
Try to merge day, activity and description informations.

Return the consolidated fact if successful, or None.
"""
fact = self.fact

now = dt.datetime.now()
self.get_widget("button-next-day").set_sensitive(self.date < now.date())
today = now.hday()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> datetime.now
<built-in method now of type object at 0xa346e0>
>>> datetime.now()
datetime.datetime(2020, 4, 27, 13, 16, 56, 954984)
>>> datetime.now().hday()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'datetime.datetime' object has no attribute 'hday'

Copy link
Collaborator Author

@ederag ederag Apr 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this theoretical, or based on an actual test ?
Please look at the imports: from hamster.lib import datetime as dt

~/share/prog/python/hamster/src>GSETTINGS_SCHEMA_DIR=../build/data   python3 
Python 3.6.10 (default, Jan 16 2020, 09:12:04) [GCC] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from hamster.lib import datetime as dt
>>> now = dt.datetime.now()
>>> now.hday()
hday(2020, 4, 27)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah okay, that's where the object came from! I didn't recognize hday, was just trying to be helpful, thinking this was part of the untested code :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also stepped in this at some point in the past, confusing python's datetime with hamster's datetime. Maybe some renaming could help here at some point.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe some renaming could help here at some point.

And forget about compatibility with existing external code ?

self.get_widget("button-next-day").set_sensitive(self.day < today)

if self.date == now.date():
if self.day == today:
default_dt = now
else:
default_dt = dt.datetime.combine(self.date, now.time())
default_dt = dt.datetime.from_day_time(self.day, now.time())

self.draw_preview(fact.start_time or default_dt,
fact.end_time or default_dt)

try:
runtime.storage.check_fact(fact, default_day=self.date)
runtime.storage.check_fact(fact, default_day=self.day)
except FactError as error:
self.update_status(status="wrong", markup=str(error))
return None

roundtrip_fact = Fact.parse(fact.serialized(), default_day=self.date)
roundtrip_fact = Fact.parse(fact.serialized(), default_day=self.day)
if roundtrip_fact != fact:
self.update_status(status="wrong", markup="Fact could not be parsed back")
return None
Expand Down
1 change: 1 addition & 0 deletions src/hamster/lib/fact.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def copy(self, **kwds):
fact._set(**kwds)
return fact

# TODO: rename "date" to "day" or "hday"
@property
def date(self):
"""hamster day, determined from start_time.
Expand Down