Task Validation
- Implemented more relaxed validation rules. - Added quiet corrections for missing data.
This commit is contained in:
90
src/Task.cpp
90
src/Task.cpp
@@ -980,21 +980,74 @@ void Task::substitute (
|
|||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
void Task::validate () const
|
// The purpose of Task::validate is three-fold:
|
||||||
|
// 1) To provide missing attributes where possible
|
||||||
|
// 2) To provide suitable warnings about odd states
|
||||||
|
// 3) To generate errors when the inconsistencies are not fixable
|
||||||
|
//
|
||||||
|
void Task::validate ()
|
||||||
{
|
{
|
||||||
// Every task needs an ID, entry and description attribute.
|
// 1) Provide missing attributes where possible
|
||||||
if (!has ("uuid"))
|
// Provide a UUID if necessary.
|
||||||
throw std::string (STRING_TASK_VALID_UUID);
|
if (! has ("uuid"))
|
||||||
|
set ("uuid", uuid ());
|
||||||
|
|
||||||
|
// Recurring tasks get a special status.
|
||||||
|
if (has ("due") &&
|
||||||
|
has ("recur"))
|
||||||
|
{
|
||||||
|
setStatus (Task::recurring);
|
||||||
|
set ("mask", "");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tasks with a wait: date get a special status.
|
||||||
|
else if (has ("wait"))
|
||||||
|
setStatus (Task::waiting);
|
||||||
|
|
||||||
|
// By default, tasks are pending.
|
||||||
|
else if (! has ("status"))
|
||||||
|
setStatus (Task::pending);
|
||||||
|
|
||||||
|
// Provide an entry date unless user already specified one.
|
||||||
if (!has ("entry"))
|
if (!has ("entry"))
|
||||||
throw std::string (STRING_TASK_VALID_ENTRY);
|
setEntry ();
|
||||||
|
|
||||||
if (!has ("description"))
|
// Completed tasks need an end date, so inherit the entry date.
|
||||||
throw std::string (STRING_TASK_VALID_DESC);
|
if (! has ("end") &&
|
||||||
|
(getStatus () == Task::completed ||
|
||||||
|
getStatus () == Task::deleted))
|
||||||
|
set ("end", get ("entry"));
|
||||||
|
|
||||||
if (get ("description") == "")
|
// Override with default.project, if not specified.
|
||||||
throw std::string (STRING_TASK_VALID_BLANK);
|
if (! has ("project"))
|
||||||
|
{
|
||||||
|
std::string defaultProject = context.config.get ("default.project");
|
||||||
|
if (defaultProject != "" &&
|
||||||
|
context.columns["project"]->validate (defaultProject))
|
||||||
|
set ("project", defaultProject);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Override with default.priority, if not specified.
|
||||||
|
if (get ("priority") == "")
|
||||||
|
{
|
||||||
|
std::string defaultPriority = context.config.get ("default.priority");
|
||||||
|
if (defaultPriority != "" &&
|
||||||
|
context.columns["priority"]->validate (defaultPriority))
|
||||||
|
set ("priority", defaultPriority);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Override with default.due, if not specified.
|
||||||
|
if (get ("due") == "")
|
||||||
|
{
|
||||||
|
std::string defaultDue = context.config.get ("default.due");
|
||||||
|
if (defaultDue != "" &&
|
||||||
|
context.columns["due"]->validate (defaultDue))
|
||||||
|
set ("due", Date (defaultDue).toEpoch ());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2) To provide suitable warnings about odd states
|
||||||
|
|
||||||
|
// When a task has a due date, other dates should conform.
|
||||||
if (has ("due"))
|
if (has ("due"))
|
||||||
{
|
{
|
||||||
Date due (get_date ("due"));
|
Date due (get_date ("due"));
|
||||||
@@ -1023,12 +1076,20 @@ void Task::validate () const
|
|||||||
context.footnote (STRING_TASK_VALID_END);
|
context.footnote (STRING_TASK_VALID_END);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
if (has ("recur"))
|
|
||||||
throw std::string (STRING_TASK_VALID_REC_DUE);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// 3) To generate errors when the inconsistencies are not fixable
|
||||||
|
|
||||||
|
// There is no fixing a missing description.
|
||||||
|
if (!has ("description"))
|
||||||
|
throw std::string (STRING_TASK_VALID_DESC);
|
||||||
|
else if (get ("description") == "")
|
||||||
|
throw std::string (STRING_TASK_VALID_BLANK);
|
||||||
|
|
||||||
|
// Cannot have a recur frequency with no due date - when would it recur?
|
||||||
|
if (! has ("due") && has ("recur"))
|
||||||
|
throw std::string (STRING_TASK_VALID_REC_DUE);
|
||||||
|
|
||||||
|
// Cannot have an until date no recurrence frequency.
|
||||||
if (has ("until") && !has ("recur"))
|
if (has ("until") && !has ("recur"))
|
||||||
throw std::string (STRING_TASK_VALID_UNTIL);
|
throw std::string (STRING_TASK_VALID_UNTIL);
|
||||||
|
|
||||||
@@ -1045,6 +1106,7 @@ void Task::validate () const
|
|||||||
getStatus () == Task::recurring)
|
getStatus () == Task::recurring)
|
||||||
throw std::string (STRING_TASK_VALID_WAIT_RECUR);
|
throw std::string (STRING_TASK_VALID_WAIT_RECUR);
|
||||||
|
|
||||||
|
// Priorities must be valid.
|
||||||
if (has ("priority"))
|
if (has ("priority"))
|
||||||
{
|
{
|
||||||
std::string priority = get ("priority");
|
std::string priority = get ("priority");
|
||||||
|
|||||||
@@ -97,7 +97,7 @@ public:
|
|||||||
|
|
||||||
void substitute (const std::string&, const std::string&, bool);
|
void substitute (const std::string&, const std::string&, bool);
|
||||||
|
|
||||||
void validate () const;
|
void validate ();
|
||||||
|
|
||||||
float urgency_c () const;
|
float urgency_c () const;
|
||||||
float urgency ();
|
float urgency ();
|
||||||
|
|||||||
Reference in New Issue
Block a user