From a0a8b360e0926b910f8a4efc412687fc466e46f5 Mon Sep 17 00:00:00 2001 From: Miguel Barão Date: Mon, 31 Oct 2016 10:42:18 +0000 Subject: [PATCH] - addded command line option --allow-all --- README.md | 25 +++++++++---------------- app.py | 8 +++++++- demo/test.yaml | 4 +--- models.py | 38 +++++++++++++++++++++++--------------- serve.py | 5 +++++ 5 files changed, 45 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 9d3efd5..55a1cf5 100644 --- a/README.md +++ b/README.md @@ -23,13 +23,7 @@ These can be installed for a single user (recommended), in a python virtual envi #### Installing packages for a single user (recommended) ```.bash -pip install --user cherrypy -pip install --user mako -pip install --user markdown -pip install --user pyyaml -pip install --user pygments -pip install --user sqlalchemy -pip install --user bcrypt +pip install --user cherrypy mako markdown pyyaml pygments sqlalchemy bcrypt ``` #### Installing packages in a virtual environment @@ -38,13 +32,7 @@ pip install --user bcrypt pyvenv-3.5 venv/perguntations # or other virtualenv directory source venv/perguntations/bin/activate # activate virtualenv -pip install cherrypy -pip install mako -pip install markdown -pip install pyyaml -pip install pygments -pip install sqlalchemy -pip install bcrypt +pip install cherrypy mako markdown pyyaml pygments sqlalchemy bcrypt ``` #### Installing packages system wide @@ -65,15 +53,16 @@ where USERNAME is your account on bitbucket. ## Running a demo -The following commands will show how to run a demonstration test: +The following commands show how to run a demonstration test: ```.bash cd WHERE/YOU/INSTALLED/perguntations -# create and initialize database either from a CSV file, fake students or empty +# create and initialize database using one of the following methods: ./initdb.py students.csv # from CSV file ./initdb.py --demo # initialize with fake students ./initdb.py # empty + mv students.db demo/ # edit test configuration and check if everything looks right @@ -99,6 +88,10 @@ The server can be stoped from the terminal with ^C. ## Troubleshooting +* The server tries to run `python3`. If only a `python3.5` command is available, you need to set the default python using the OS package manager or manually create a symbolic link: + - macOS/macports: sudo port select --set python3 python35 + - FreeBSD 11: cd /usr/local/bin; ln -s python3.5 python3 + * If you are getting any `UnicodeEncodeError` type of errors that's because the terminal is not supporting UTF-8. Try running `locale` on the terminal and see if there is any error messages. Solutions: - debian: fix it with `sudo dpkg-reconfigure locales`, select your UTF-8 locales and try again. diff --git a/app.py b/app.py index 8d65fdc..ede07e8 100644 --- a/app.py +++ b/app.py @@ -46,6 +46,11 @@ class App(object): else: logger.info('Database has {} students registered.'.format(n)) + if conf['allow_all']: + logger.info('Allowing all students') + for student in self.get_all_students(): + self.allow_student(student[0]) + # ----------------------------------------------------------------------- # helper to manage db sessions using the `with` statement, for example @@ -59,7 +64,8 @@ class App(object): # ----------------------------------------------------------------------- def exit(self): - # FIXME what if there are online students? + if len(self.online) > 1: + logger.warning('Students still online: {}'.format(', '.join(self.online))) logger.critical('----------- !!! Server terminated !!! -----------') # ----------------------------------------------------------------------- diff --git a/demo/test.yaml b/demo/test.yaml index 2bdc689..f5097e7 100644 --- a/demo/test.yaml +++ b/demo/test.yaml @@ -11,10 +11,8 @@ title: Exame de Demonstração # The database is an sqlite3 file generate with the script initdb.py database: demo/students.db -# (optional) Generate a file for each test done by a student. +# Generate a file for each test done by a student. # It includes the questions, answers and grades. -# If undefined, then no tests are saved (useful for debug and training). -save_answers: yes answers_dir: demo/ans # (optional, default: False) Show points for each question, scale 0-20. diff --git a/models.py b/models.py index 6449dea..d3f1a9c 100644 --- a/models.py +++ b/models.py @@ -20,8 +20,8 @@ class Student(Base): tests = relationship('Test', back_populates='student') questions = relationship('Question', back_populates='student') - # def __repr__(self): - # return 'Student:\n id: "{0}"\n name: "{1}"\n password: "{2}"'.format(self.id, self.name, self.password) + def __repr__(self): + return 'Student:\n id: "{0}"\n name: "{1}"\n password: "{2}"'.format(self.id, self.name, self.password) # --------------------------------------------------------------------------- @@ -42,8 +42,18 @@ class Test(Base): student = relationship('Student', back_populates='tests') questions = relationship('Question', back_populates='test') - # def __repr__(self): - # return 'Test:\n id: "{0}"\n ref="{1}"\n grade="{2}"\n starttime="{3}"\n finishtime="{4}"\n student_id="{5}"'.format(self.id, self.ref, self.grade, self.starttime, self.finishtime, self.student_id) + def __repr__(self): + return 'Test:\n\ + id: "{}"\n\ + ref="{}"\n\ + title="{}"\n\ + grade="{}"\n\ + state="{}"\n\ + comment="{}"\n\ + starttime="{}"\n\ + finishtime="{}"\n\ + filename="{}"\n\ + student_id="{}"\n'.format(self.id, self.ref, self.title, self.grade, self.state, self.comment, self.starttime, self.finishtime, self.filename, self.student_id) # --------------------------------------------------------------------------- @@ -61,17 +71,15 @@ class Question(Base): student = relationship('Student', back_populates='questions') test = relationship('Test', back_populates='questions') -# def __repr__(self): -# return ''' -# Question: -# id: "{0}" -# ref: "{1}" -# grade: "{2}" -# starttime: "{3}" -# finishtime: "{4}" -# student_id: "{5}" -# test_id: "{6}" -# '''.fotmat(self.id, self.ref, self.grade, self.starttime, self.finishtime, self.student_id, self.test_id) + def __repr__(self): + return 'Question:\n\ + id: "{}"\n\ + ref: "{}"\n\ + grade: "{}"\n\ + starttime: "{}"\n\ + finishtime: "{}"\n\ + student_id: "{}"\n\ + test_id: "{}"\n'.fotmat(self.id, self.ref, self.grade, self.starttime, self.finishtime, self.student_id, self.test_id) # --------------------------------------------------------------------------- diff --git a/serve.py b/serve.py index 71010dc..c72923d 100755 --- a/serve.py +++ b/serve.py @@ -285,6 +285,8 @@ def parse_arguments(): argparser.add_argument('--conf', default=serverconf_file, type=str, help='server configuration file') argparser.add_argument('--debug', action='store_true', help='Show datastructures when rendering questions') + argparser.add_argument('--allow-all', action='store_true', + help='Students are initially allowed to login (can be denied later)') argparser.add_argument('testfile', type=str, nargs='+', help='test/exam in YAML format.') # FIXME only one exam supported at the moment return argparser.parse_args() @@ -368,5 +370,8 @@ if __name__ == '__main__': cherrypy.engine.start() cherrypy.engine.block() + # ...App running... + app.exit() + # --- Server terminated \ No newline at end of file -- libgit2 0.21.2