Esempio n. 1
0
    def _validate_date(self, value, reason, widget, validation):
        """
        Validate that the date string can be parsed.

        Will clear the field if focus is lost and the date cannot be parsed,
        and enable the associated 'Update' button if it can.
        """
        if isinstance(widget, basestring):
            widget = self.nametowidget(widget)
        if reason in ['focusin', 'key']:
            return True
        elif reason == 'focusout':
            # Return True if the date parses
            try:
                new_dt = None
                if value:
                    new_dt = utils.parse_datetime(value)
                    widget.delete(0, 1000)
                    widget.insert(0, utils.format_date(new_dt))
                if new_dt != widget.original:
                    widget['background'] = 'green'
                elif new_dt and utils.is_past(new_dt):
                    widget['background'] = 'red'
                else:
                    widget['background'] = 'systemWindowBody'
                widget.after_idle(widget.config, {'validate': validation})
                return True
            except habitcli.utils.DateParseException:
                print "Invalid date entry,", value, ", deleting..."
                widget.delete(0, 1000)
                widget.after_idle(widget.config, {'validate': validation})
                return False
        # Return True for all other reasons
        else:
            return True
Esempio n. 2
0
 def update_todo_plan_date(self, todo, planned_date):
     """Set the planning date for a task, selected by natural language."""
     selected_todo = self.match_todo_by_string(todo)['todo']
     parsed_date = parse_datetime(planned_date)
     print "Change do-date of '%s' to %s?" % (selected_todo['text'],
                                              pretty.date(parsed_date))
     if confirm(resp=True):
         selected_todo.set_planning_date(parsed_date, update=True)
Esempio n. 3
0
    def add_todo(self, todo, due_date="", plan_date="", *tags):
        """Add a todo with optional tags and due date in natural language."""

        new_todo = Todo(text=todo, hcli=self)

        for tag in tags:
            if tag.replace("+", "") in self.user['reverse_tag_dict'].keys():
                tag_id = self.user['reverse_tag_dict'][tag.replace("+", "")]
                new_todo['tags'][tag_id] = True
            else:
                valid_tags = self.user['reverse_tag_dict'].keys()
                raise NoSuchTagException(tag, valid_tags)

        if due_date:
            new_todo.set_due_date(parse_datetime(due_date))

        if plan_date:
            new_todo.set_planning_date(parse_datetime(plan_date))

        new_todo.create()
Esempio n. 4
0
        def btn_callback():
            """Update the associated todo with the changed fields."""
            fragments = []
            updates = {}

            date_fmt_str = "%s:\n\tFrom: %s\n\tTo:     %s"

            # Changes in planning date
            if plan.get():
                old_plan = datum.get_planning_date()
                new_plan = utils.parse_datetime(plan.get())
                if new_plan != old_plan:
                    fragments.append(date_fmt_str %
                                     ('Plan Date',
                                      utils.format_date(old_plan),
                                      utils.format_date(new_plan)))
                    updates['plan'] = new_plan

            # Changes in due date
            if due.get():
                old_due = datum.get_due_date()
                new_due = utils.parse_datetime(due.get())
                if new_due != old_due:
                    fragments.append(date_fmt_str %
                                     ('Due Date',
                                      utils.format_date(old_due),
                                      utils.format_date(new_due)))
                    updates['due'] = new_due

            # Changes in tag
            old_tag = datum.get_primary_tag()
            new_tag = tag.get()
            if new_tag != old_tag:
                fragments.append("Tag:\n\tFrom: %s\n\tTo: %s" %
                                 (old_tag, new_tag))
                updates['tag'] = new_tag

            if fragments:
                message = "\n".join(fragments)
                if tkMessageBox.askyesno("Update %s?" %
                                         datum['text'], message):

                    if 'tag' in updates:
                        datum.set_primary_tag(updates['tag'])
                    if 'plan' in updates:
                        datum.set_planning_date(updates['plan'])
                    if 'due' in updates:
                        datum.set_due_date(updates['due'])

                    datum.update_db()

                    tag.original = datum.get_primary_tag()
                    tag.original = tag.original if tag.original else ""
                    tag.set(tag.original)
                    self._val_tag(tag.get(), tag)

                    plan.delete(0, 1000)
                    plan.original = datum.get_planning_date()
                    plan.insert(0, utils.format_date(plan.original))
                    self._validate_date(plan.get(), 'focusout', plan, 'all')

                    due.delete(0, 1000)
                    due.original = datum.get_due_date()
                    due.insert(0, utils.format_date(due.original))
                    self._validate_date(due.get(), 'focusout', due, 'all')

                    print datum['text'], "updated!"