From 65a5ad4aceead32255bb3bbd04a3f0d715c63519 Mon Sep 17 00:00:00 2001 From: Miguel Barão Date: Wed, 3 Aug 2022 22:08:06 +0100 Subject: [PATCH] Small fixes --- aprendizations/learnapp.py | 64 +++++++++++++++++++--------------------------------------------- aprendizations/templates/maintopics-table.html | 38 ++++++++++++-------------------------- 2 files changed, 31 insertions(+), 71 deletions(-) diff --git a/aprendizations/learnapp.py b/aprendizations/learnapp.py index bb565c7..5c3e410 100644 --- a/aprendizations/learnapp.py +++ b/aprendizations/learnapp.py @@ -6,7 +6,6 @@ This is the main controller of the application. # python standard library import asyncio from collections import defaultdict -# from contextlib import contextmanager # `with` statement in db sessions from datetime import datetime import logging from random import random @@ -45,18 +44,25 @@ class LearnApp(): ''' LearnApp - application logic - self.deps - networkx topic dependencies - self.courses - dict {course_id: {'title': ..., - 'description': ..., - 'goals': ...,}, ...} - self.factory = dict {qref: QFactory()} - self.online - dict {student_id: {'number': ..., - 'name': ..., - 'state': StudentState(), - 'counter': ...}, ...} + self.deps = networkx topic dependencies + self.courses = { + course_id: { + 'title': ..., + 'description': ..., + 'goals': ..., + }, ... + } + self.factory = { qref: QFactory() } + self.online = { + student_id: { + 'number': ..., + 'name': ..., + 'state': StudentState(), + 'counter': ... + }, ... + } ''' - # ------------------------------------------------------------------------ def __init__(self, courses: str, # filename with course configurations prefix: str, # path to topics @@ -114,7 +120,6 @@ class LearnApp(): if check: self._sanity_check_questions() - # ------------------------------------------------------------------------ def _sanity_check_questions(self) -> None: ''' Unit tests for all questions @@ -159,7 +164,6 @@ class LearnApp(): raise LearnException('Sanity checks') logger.info(' 0 errors found.') - # ------------------------------------------------------------------------ async def login(self, uid: str, password: str) -> bool: '''user login''' @@ -212,13 +216,11 @@ class LearnApp(): return pw_ok - # ------------------------------------------------------------------------ def logout(self, uid: str) -> None: '''User logout''' del self.online[uid] logger.info('User "%s" logged out', uid) - # ------------------------------------------------------------------------ async def change_password(self, uid: str, password: str) -> bool: ''' Change user Password. @@ -242,7 +244,6 @@ class LearnApp(): logger.info('User "%s" changed password', uid) return True - # ------------------------------------------------------------------------ async def check_answer(self, uid: str, answer) -> Question: ''' Checks answer and update database. @@ -271,7 +272,6 @@ class LearnApp(): return question - # ------------------------------------------------------------------------ async def get_question(self, uid: str) -> Optional[Question]: ''' Get the question to show (current or new one) @@ -318,7 +318,6 @@ class LearnApp(): return question - # ------------------------------------------------------------------------ def start_course(self, uid: str, course_id: str) -> None: '''Start course''' @@ -331,9 +330,6 @@ class LearnApp(): else: logger.info('User "%s" course "%s"', uid, course_id) - # ------------------------------------------------------------------------ - # - # ------------------------------------------------------------------------ async def start_topic(self, uid: str, topic: str) -> None: '''Start new topic''' @@ -350,9 +346,6 @@ class LearnApp(): else: logger.info('User "%s" started topic "%s"', uid, topic) - # ------------------------------------------------------------------------ - # - # ------------------------------------------------------------------------ def _add_missing_topics(self, topics: Iterable[str]) -> None: ''' Fill db table 'Topic' with topics from the graph, if new @@ -365,7 +358,6 @@ class LearnApp(): session.commit() logger.info('Added %d new topic(s) to the database', len(new)) - # ------------------------------------------------------------------------ def _db_setup(self, database: str) -> None: ''' Setup and check database contents @@ -395,7 +387,6 @@ class LearnApp(): logger.info('%6d topics', count_topics) logger.info('%6d answers', count_answers) - # ------------------------------------------------------------------------ def _populate_graph(self, config: Dict[str, Any]) -> None: ''' Populates a digraph. @@ -437,12 +428,10 @@ class LearnApp(): # prefix/topic topic['path'] = join(self.deps.graph['prefix'], tref) - - # ======================================================================== + # ------------------------------------------------------------------------ # methods that do not change state (pure functions) - # ======================================================================== - # ------------------------------------------------------------------------ + def _make_factory(self) -> Dict[str, QFactory]: ''' Buils dictionary of question factories @@ -519,38 +508,31 @@ class LearnApp(): return factory - # ------------------------------------------------------------------------ def get_login_counter(self, uid: str) -> int: '''login counter''' # FIXME return int(self.online[uid]['counter']) - # ------------------------------------------------------------------------ def get_student_name(self, uid: str) -> str: '''Get the username''' return self.online[uid].get('name', '') - # ------------------------------------------------------------------------ def get_student_state(self, uid: str) -> List[Dict[str, Any]]: '''Get the knowledge state of a given user''' return self.online[uid]['state'].get_knowledge_state() - # ------------------------------------------------------------------------ def get_student_progress(self, uid: str) -> float: '''Get the current topic progress of a given user''' return float(self.online[uid]['state'].get_topic_progress()) - # ------------------------------------------------------------------------ def get_current_question(self, uid: str) -> Optional[Question]: '''Get the current question of a given user''' question: Optional[Question] = self.online[uid]['state'].get_current_question() return question - # ------------------------------------------------------------------------ def get_current_question_id(self, uid: str) -> str: '''Get id of the current question for a given user''' return str(self.online[uid]['state'].get_current_question()['qid']) - # ------------------------------------------------------------------------ def get_student_question_type(self, uid: str) -> str: '''Get type of the current question for a given user''' return str(self.online[uid]['state'].get_current_question()['type']) @@ -559,12 +541,10 @@ class LearnApp(): # def get_student_topic(self, uid: str) -> str: # return str(self.online[uid]['state'].get_current_topic()) - # ------------------------------------------------------------------------ def get_student_course_title(self, uid: str) -> str: '''get the title of the current course for a given user''' return str(self.online[uid]['state'].get_current_course_title()) - # ------------------------------------------------------------------------ def get_current_course_id(self, uid: str) -> Optional[str]: '''get the current course (id) of a given user''' cid: Optional[str] = self.online[uid]['state'].get_current_course_id() @@ -574,7 +554,6 @@ class LearnApp(): # def get_topic_name(self, ref: str) -> str: # return str(self.deps.nodes[ref]['name']) - # ------------------------------------------------------------------------ def get_current_public_dir(self, uid: str) -> str: ''' Get the path for the 'public' directory of the current topic of the @@ -586,21 +565,18 @@ class LearnApp(): prefix: str = self.deps.graph['prefix'] return join(prefix, topic, 'public') - # ------------------------------------------------------------------------ def get_courses(self) -> Dict[str, Dict[str, Any]]: ''' Get dictionary with all courses {'course1': {...}, 'course2': {...}} ''' return self.courses - # ------------------------------------------------------------------------ def get_course(self, course_id: str) -> Dict[str, Any]: ''' Get dictionary {'title': ..., 'description':..., 'goals':...} ''' return self.courses[course_id] - # ------------------------------------------------------------------------ def get_rankings(self, uid: str, cid: str) -> List[Tuple[str, str, float]]: ''' Returns rankings for a certain cid (course_id). @@ -639,5 +615,3 @@ class LearnApp(): for u, name in students if u in progress and (len(u) > 2 or len(uid) <= 2)), key=lambda x: x[2], reverse=True) - - # ------------------------------------------------------------------------ diff --git a/aprendizations/templates/maintopics-table.html b/aprendizations/templates/maintopics-table.html index 70062b7..71c49b0 100644 --- a/aprendizations/templates/maintopics-table.html +++ b/aprendizations/templates/maintopics-table.html @@ -105,23 +105,23 @@ {% if t['type']=='chapter' %} -   +
{% elif t['type']=='learn' %} -   +
{% else %} -   +
{% end %}
{% if t['ref'] not in course['goals'] %} -   +
{% end %} {{ t['name'] }}
- +
@@ -131,30 +131,22 @@ {% if t['type']=='chapter' %} -
-   -
+
{% elif t['type']=='learn' %} -
-   -
+
{% else %} -
-   -
+
{% end %} {% if t['ref'] not in course['goals'] %} -
- -
+
{% end %} {{ t['name'] }} @@ -162,18 +154,12 @@ {% if t['level'] < 0.01 %} -
- -
+
{% elif t['type']=='chapter' %} -
- -
+
{% else %} -
- {{ int(t['level']*5+0.25)*'' }}{% if int(t['level']*5+0.25) < 5 %}{{'' if 0.25 <= t['level']*5-int(t['level']*5) < 0.75 else ''}}{% end %}{{ (4-int(t['level']*5+0.25))*'' }} -
+
{{ int(t['level']*5+0.25)*'' }}{% if int(t['level']*5+0.25) < 5 %}{{'' if 0.25 <= t['level']*5-int(t['level']*5) < 0.75 else ''}}{% end %}{{ (4-int(t['level']*5+0.25))*'' }}
{% end %} -- libgit2 0.21.2