ui.cli: Repeat the basic help when the user calls `help`
[quizzer.git] / quizzer / answerdb.py
index 869641ec29a13221c715027efa73af13739da514..c8c802f9bc3606d012823cd3a7a8d53e469a7f7e 100644 (file)
@@ -1,4 +1,21 @@
+# Copyright (C) 2013 W. Trevor King <wking@tremily.us>
+#
+# This file is part of quizzer.
+#
+# quizzer is free software: you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation, either version 3 of the License, or (at your option) any later
+# version.
+#
+# quizzer is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along with
+# quizzer.  If not, see <http://www.gnu.org/licenses/>.
+
 import codecs as _codecs
+import datetime as _datetime
 import json as _json
 
 from . import __version__
@@ -34,25 +51,29 @@ class AnswerDatabase (dict):
         with self._open(mode='w', **kwargs) as f:
             _json.dump(
                 data, f, indent=2, separators=(',', ': '), sort_keys=True)
+            f.write('\n')
 
     def add(self, question, answer, correct):
-        if question.prompt not in self:
-            self[question.prompt] = []
-        self[question.prompt].append({
+        if question.id not in self:
+            self[question.id] = []
+        timezone = _datetime.timezone.utc
+        timestamp = _datetime.datetime.now(tz=timezone).isoformat()
+        self[question.id].append({
                 'answer': answer,
                 'correct': correct,
+                'timestamp': timestamp,
                 })
 
     def get_answered(self, questions):
-        return [q for q in questions if q.prompt in self]
+        return [q for q in questions if q.id in self]
 
     def get_unanswered(self, questions):
-        return [q for q in questions if q.prompt not in self]
+        return [q for q in questions if q.id not in self]
 
     def get_correctly_answered(self, questions):
         return [q for q in questions
-                if True in [a['correct'] for a in self.get(q.prompt, [])]]
+                if True in [a['correct'] for a in self.get(q.id, [])]]
 
     def get_never_correctly_answered(self, questions):
         return [q for q in questions
-                if True not in [a['correct'] for a in self.get(q.prompt, [])]]
+                if True not in [a['correct'] for a in self.get(q.id, [])]]