Skip to content

Commit

Permalink
Reformat frontend code
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeDerval committed Jun 28, 2015
1 parent 86dd423 commit 5daddbb
Show file tree
Hide file tree
Showing 42 changed files with 121 additions and 110 deletions.
4 changes: 2 additions & 2 deletions frontend/accessible_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@


class AccessibleTime(object):

""" represents the period of time when a course/task is accessible """

def __init__(self, val=None):
Expand Down Expand Up @@ -57,7 +56,8 @@ def _parse_date(cls, date, default):
if date == "":
return default

for format_type in ["%Y-%m-%d %H:%M:%S", "%Y-%m-%d %H:%M", "%Y-%m-%d %H", "%Y-%m-%d", "%d/%m/%Y %H:%M:%S", "%d/%m/%Y %H:%M", "%d/%m/%Y %H", "%d/%m/%Y"]:
for format_type in ["%Y-%m-%d %H:%M:%S", "%Y-%m-%d %H:%M", "%Y-%m-%d %H", "%Y-%m-%d", "%d/%m/%Y %H:%M:%S", "%d/%m/%Y %H:%M", "%d/%m/%Y %H",
"%d/%m/%Y"]:
try:
return datetime.strptime(date, format_type)
except ValueError:
Expand Down
7 changes: 5 additions & 2 deletions frontend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
""" Starts the frontend """

import os.path
import posixpath
import urllib

import web

Expand All @@ -29,8 +31,6 @@
from frontend.plugins.plugin_manager import PluginManager
import frontend.session
from frontend.template_helper import TemplateHelper
import posixpath
import urllib

urls = (
'/', 'frontend.pages.index.IndexPage',
Expand All @@ -55,6 +55,7 @@
'/.*', 'frontend.pages.maintenance.MaintenancePage'
)


def get_app(config_file):
""" Get the application. config_file is the path to the configuration file """
frontend.configuration.INGIniousConfiguration.load(config_file)
Expand All @@ -71,6 +72,7 @@ def get_app(config_file):
def not_found():
""" Display the error 404 page """
return web.notfound(frontend.base.renderer.notfound('Page not found'))

appli.notfound = not_found

plugin_manager = PluginManager(appli, frontend.configuration.INGIniousConfiguration.get("plugins", []))
Expand Down Expand Up @@ -114,6 +116,7 @@ def normpath(self, path):
path2 += "/"
return path2


def start_app(config_file, app=None):
""" Get and start the application. config_file is the path to the configuration file"""
if app is None:
Expand Down
10 changes: 8 additions & 2 deletions frontend/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,20 @@
# You should have received a copy of the GNU Affero General Public
# License along with INGInious. If not, see <http://www.gnu.org/licenses/>.
""" Basic dependencies for the frontend """
import os

from gridfs import GridFS
from pymongo import MongoClient
import web

from frontend.configuration import INGIniousConfiguration
import os


def add_to_template_globals(name, value):
""" Add a variable to will be accessible in the templates """
add_to_template_globals.globals[name] = value


add_to_template_globals.globals = {}


Expand All @@ -34,7 +39,8 @@ def get_template_renderer(dir_path, base=None):
*base* is the base layout name.
"""
base_dir_path = os.path.dirname(__file__)
return web.template.render(os.path.join(base_dir_path,dir_path), globals=add_to_template_globals.globals, base=base)
return web.template.render(os.path.join(base_dir_path, dir_path), globals=add_to_template_globals.globals, base=base)


renderer = get_template_renderer('templates/', 'layout')
add_to_template_globals.globals["include"] = get_template_renderer('templates/')
Expand Down
2 changes: 1 addition & 1 deletion frontend/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@


class Configuration(dict):

""" Config class """

def load(self, path):
Expand All @@ -31,4 +30,5 @@ def load(self, path):
self.get('allowed_file_extensions', [".c", ".cpp", ".java", ".oz", ".zip", ".tar.gz", ".tar.bz2", ".txt"]),
self.get('max_file_size', 1024 * 1024))


INGIniousConfiguration = Configuration()
4 changes: 3 additions & 1 deletion frontend/custom/courses.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@


class FrontendCourse(Course):

""" A course with some modification for users """

_task_class = FrontendTask
Expand Down Expand Up @@ -121,6 +120,7 @@ def get_user_completion_percentage(self, username=None):
""" Returns the percentage (integer) of completion of this course by the current user (or username if it is not None)"""
if username is None:
import frontend.user as User

username = User.get_username()
cache = UserData(username).get_course_data(self.get_id())
if cache is None:
Expand All @@ -133,6 +133,7 @@ def get_user_grade(self, username=None):
""" Return the grade (a floating-point number between 0 and 100) of the user (if username is None, it uses the currently logged-in user) """
if username is None:
import frontend.user as User

username = User.get_username()
cache = UserData(username).get_course_data(self.get_id())
if cache is None:
Expand All @@ -153,6 +154,7 @@ def get_user_grade(self, username=None):
def get_user_last_submissions(self, limit=5, one_per_task=False):
""" Returns a given number (default 5) of submissions of task from this course """
from frontend.submission_manager import get_user_last_submissions as extern_get_user_last_submissions

task_ids = []
for task_id in self.get_tasks():
task_ids.append(task_id)
Expand Down
10 changes: 2 additions & 8 deletions frontend/custom/task_problems.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@

from frontend.base import get_template_renderer
from frontend.parsable_text import ParsableText
from common.tasks_problems import BasicProblem, BasicCodeProblem, CodeProblem, CodeSingleLineProblem, MatchProblem, MultipleChoiceProblem, CodeFileProblem
from common.tasks_problems import BasicProblem, BasicCodeProblem, CodeProblem, CodeSingleLineProblem, MatchProblem, MultipleChoiceProblem, \
CodeFileProblem
from frontend.custom.tasks_code_boxes import DisplayableInputBox, DisplayableMultilineBox, DisplayableTextBox, DisplayableFileBox


class DisplayableBasicProblem(BasicProblem):

"""Basic problem """
__metaclass__ = ABCMeta

Expand All @@ -55,7 +55,6 @@ def show_input(self):


class DisplayableBasicCodeProblem(BasicCodeProblem, DisplayableBasicProblem):

""" A basic class to display all BasicCodeProblem derivatives """

@abstractmethod
Expand Down Expand Up @@ -84,25 +83,21 @@ def show_input(self):


class DisplayableCodeSingleLineProblem(CodeSingleLineProblem, DisplayableBasicCodeProblem):

""" A displayable single code line problem """
pass


class DisplayableCodeProblem(CodeProblem, DisplayableBasicCodeProblem):

""" A displayable code problem """
pass


class DisplayableCodeFileProblem(CodeFileProblem, DisplayableBasicCodeProblem):

""" A displayable code problem """
pass


class DisplayableMultipleChoiceProblem(MultipleChoiceProblem, DisplayableBasicProblem):

""" A displayable multiple choice problem """

def __init__(self, task, problemid, content):
Expand Down Expand Up @@ -149,7 +144,6 @@ def show_input(self):


class DisplayableMatchProblem(MatchProblem, DisplayableBasicProblem):

""" A displayable match problem """

def show_input(self):
Expand Down
8 changes: 6 additions & 2 deletions frontend/custom/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
from common.task_file_managers.manage import get_task_file_manager
import common.tasks
from frontend.accessible_time import AccessibleTime
from frontend.custom.task_problems import DisplayableCodeProblem, DisplayableCodeSingleLineProblem, DisplayableMatchProblem, DisplayableMultipleChoiceProblem, DisplayableCodeFileProblem
from frontend.custom.task_problems import DisplayableCodeProblem, DisplayableCodeSingleLineProblem, DisplayableMatchProblem, \
DisplayableMultipleChoiceProblem, DisplayableCodeFileProblem
from frontend.parsable_text import ParsableText
from frontend.plugins.plugin_manager import PluginManager


class FrontendTask(common.tasks.Task):

""" A task that stores additionnal context informations """

# Redefine _problem_types with displayable ones
Expand Down Expand Up @@ -104,13 +104,15 @@ def is_visible_by_user(self, username=None):
""" Returns true if the task is visible by the user """
if username is None:
import frontend.user as User

username = User.get_username()
return (self.get_course().is_open_to_user(username) and self._accessible.after_start()) or username in self.get_course().get_admins()

def can_user_submit(self, username=None):
""" returns true if the user can submit his work for this task """
if username is None:
import frontend.user as User

username = User.get_username()
return (self.get_course().is_open_to_user(username) and self._accessible.is_open()) or username in self.get_course().get_admins()

Expand All @@ -126,6 +128,7 @@ def get_deadline(self):
def get_user_status(self):
""" Returns "succeeded" if the current user solved this task, "failed" if he failed, and "notattempted" if he did not try it yet """
import frontend.user as User # insert here to avoid initialisation of session

task_cache = User.get_data().get_task_data(self.get_course_id(), self.get_id())
if task_cache is None:
return "notviewed"
Expand All @@ -136,6 +139,7 @@ def get_user_status(self):
def get_user_grade(self):
""" Returns the grade (a floating-point number between 0 and 100) of the student """
import frontend.user as User # insert here to avoid initialisation of session

task_cache = User.get_data().get_task_data(self.get_course_id(), self.get_id())
if task_cache is None:
return 0.0
Expand Down
9 changes: 2 additions & 7 deletions frontend/custom/tasks_code_boxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@
import base64
import json


from common.tasks_code_boxes import TextBox, InputBox, MultilineBox, FileBox
from frontend.base import get_template_renderer
from frontend.parsable_text import ParsableText


class DisplayableBox(object):

""" A basic interface for displayable boxes """
__metaclass__ = ABCMeta

Expand All @@ -51,7 +49,6 @@ def show(self):


class DisplayableTextBox(TextBox, DisplayableBox):

""" A displayable text box """

def __init__(self, problem, boxid, boxData):
Expand All @@ -66,12 +63,12 @@ def show(self):


class DisplayableFileBox(FileBox, DisplayableBox):

""" A displayable file box """

def adapt_input_for_backend(self, input_data):
try:
input_data[self.get_complete_id()] = {"filename": input_data[self.get_complete_id()].filename, "value": base64.b64encode(input_data[self.get_complete_id()].value)}
input_data[self.get_complete_id()] = {"filename": input_data[self.get_complete_id()].filename,
"value": base64.b64encode(input_data[self.get_complete_id()].value)}
except:
input_data[self.get_complete_id()] = {}
return input_data
Expand All @@ -82,7 +79,6 @@ def show(self):


class DisplayableInputBox(InputBox, DisplayableBox):

""" A displayable input box """

def show(self):
Expand All @@ -91,7 +87,6 @@ def show(self):


class DisplayableMultilineBox(MultilineBox, DisplayableBox):

""" A displayable multiline box """

def show(self):
Expand Down
4 changes: 3 additions & 1 deletion frontend/database_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# License along with INGInious. If not, see <http://www.gnu.org/licenses/>.
""" Updates the database """
import pymongo

from frontend.base import get_database
from frontend.custom.courses import FrontendCourse

Expand All @@ -37,7 +38,8 @@ def update_database():
get_database().submissions.ensure_index([("courseid", pymongo.ASCENDING), ("taskid", pymongo.ASCENDING)])
get_database().submissions.ensure_index([("submitted_on", pymongo.DESCENDING)]) # sort speed

get_database().user_tasks.ensure_index([("username", pymongo.ASCENDING), ("courseid", pymongo.ASCENDING), ("taskid", pymongo.ASCENDING)], unique=True)
get_database().user_tasks.ensure_index([("username", pymongo.ASCENDING), ("courseid", pymongo.ASCENDING), ("taskid", pymongo.ASCENDING)],
unique=True)
get_database().user_tasks.ensure_index([("username", pymongo.ASCENDING), ("courseid", pymongo.ASCENDING)])
get_database().user_tasks.ensure_index([("courseid", pymongo.ASCENDING), ("taskid", pymongo.ASCENDING)])
get_database().user_tasks.ensure_index([("courseid", pymongo.ASCENDING)])
Expand Down
1 change: 0 additions & 1 deletion frontend/pages/course.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@


class CoursePage(object):

""" Course page """

def GET(self, courseid):
Expand Down
2 changes: 1 addition & 1 deletion frontend/pages/course_admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
#
# You should have received a copy of the GNU Affero General Public
# License along with INGInious. If not, see <http://www.gnu.org/licenses/>.
""" Course administration """
""" Course administration """
1 change: 0 additions & 1 deletion frontend/pages/course_admin/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@


class CourseSettings(object):

""" Couse settings """

def GET(self, courseid):
Expand Down
4 changes: 2 additions & 2 deletions frontend/pages/course_admin/student_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@


class CourseStudentInfoPage(object):

""" List information about a student """

def GET(self, courseid, username):
Expand All @@ -44,7 +43,8 @@ def page(self, course, username):
tasks = course.get_tasks()
result = OrderedDict()
for taskid in tasks:
result[taskid] = {"name": tasks[taskid].get_name(), "submissions": 0, "status": "notviewed", "url": self.submission_url_generator(course, username, taskid)}
result[taskid] = {"name": tasks[taskid].get_name(), "submissions": 0, "status": "notviewed",
"url": self.submission_url_generator(course, username, taskid)}
for taskdata in data:
if taskdata["taskid"] in result:
result[taskdata["taskid"]]["submissions"] = taskdata["tried"]
Expand Down
4 changes: 2 additions & 2 deletions frontend/pages/course_admin/student_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@


class CourseStudentListPage(object):

""" Course administration page: list of registered students """

def GET(self, courseid):
Expand All @@ -39,7 +38,8 @@ def submission_url_generator(self, course, username):
def page(self, course):
""" Get all data and display the page """
data = UserData.get_course_data_for_users(course.get_id(), course.get_registered_users())
data = [dict(f.items() + [("url", self.submission_url_generator(course, username)), ("username", username)]) for username, f in data.iteritems()]
data = [dict(f.items() + [("url", self.submission_url_generator(course, username)), ("username", username)]) for username, f in
data.iteritems()]
if "csv" in web.input():
return make_csv(data)
return renderer.course_admin.student_list(course, data)
5 changes: 2 additions & 3 deletions frontend/pages/course_admin/student_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@


class CourseStudentTaskPage(object):

""" List information about a task done by a student """

def GET(self, courseid, username, taskid):
Expand All @@ -42,15 +41,15 @@ def submission_url_generator(self, course, submissionid):

def page(self, course, username, task):
""" Get all data and display the page """
data = list(get_database().submissions.find({"username": username, "courseid": course.get_id(), "taskid": task.get_id()}).sort([("submitted_on", pymongo.DESCENDING)]))
data = list(get_database().submissions.find({"username": username, "courseid": course.get_id(), "taskid": task.get_id()}).sort(
[("submitted_on", pymongo.DESCENDING)]))
data = [dict(f.items() + [("url", self.submission_url_generator(course, str(f["_id"])))]) for f in data]
if "csv" in web.input():
return make_csv(data)
return renderer.course_admin.student_task(course, username, task, data)


class SubmissionDownloadFeedback(object):

def GET(self, courseid, username, taskid, submissionid):
""" GET request """
course, task = get_course_and_check_rights(courseid, taskid)
Expand Down

0 comments on commit 5daddbb

Please sign in to comment.