예제 #1
0
def comment_line(line, user):
    """ When parse reads a line starting with a #, create a comment line for admin users """
    if not user_is_admin(user):
        return ""

    line = renderMarkdown(line[1:])
    return f"<font color='grey'>{line}</font>"
예제 #2
0
def home():
    """ Display home page """
    return {
        "user": get_user(),
        "base": "Welcome to the home page. It is underconstruction",
        "admin": user_is_admin(get_user())
    }
예제 #3
0
def renderTemplate(name, **kwargs):
   """Render a template being careful to handle errors"""
   try:
      result = template(name, dotenv_values, **kwargs)
      return result
   except:
      user = get_user()
      if user_is_admin(user):
         result = html.escape(traceback.format_exc())
         result = f"\n<pre>{result}</pre>"
         return result
      raise
예제 #4
0
def syllabi(page_name, db):
    """Render a page such as the syllabus, help, etc."""
    user = get_user()
    if user_is_admin(user) and request.query.user:
        original_user = user
        user = request.query.user
    else:
        original_user = ''
    if not user_in_roll(user, db):
        log(f"syllabi: {user} not enrolled in COMP523.")

    path = f"views/COMP523/{page_name}.html"
    if not osp.exists(path):
        not_found(db)

    title = ""
    with open(path, "rt") as fp:
        top = fp.readline()
        if top.startswith("# "):
            title = title + top[2:].strip()

    content = renderTemplate(path, user=user, title=title)
    content = renderMarkdown(content)
    if original_user == '':
        db.execute(
            """
         insert into fetched (time, onyen, key, ip, pages, url) values (%s, %s, %s, %s, %s, %s)""",
            [
                datetime.now(), user, page_name, request.remote_addr, [],
                request.url
            ],
        )
    return {
        "base": content,
        "title": title,
        "user": user,
        "admin": user_is_admin(user),
        "agenda_type": 'student'
    }
예제 #5
0
def get_questions(key, db):  # pylint:  disable=too-many-statements
    """
   Allow rendering a set of questions from a template stored in
   the questions folder.
   """
    user = get_user()
    if 'userid' in request.query:
        raise bottle.HTTPError(401, "Invalid parameter userid")
    if 'onyen' in request.query:
        raise bottle.HTTPError(401, "Invalid parameter onyen")
    if user_is_admin(user) and request.query.user:
        original_user = user
        user = request.query.user
    else:
        original_user = ''
    if not user_in_roll(user, db):
        not_allowed()

    path = f"content/{course_name}/{request.path}.tmd"
    now = datetime.now()
    if not osp.exists(path):
        log(f"mypoll{request.path}: path {path} not found")
        not_found(db)
    ip = request.remote_addr

    inp = inputs(key)

    db.execute(
        """
      select extended, section, rid, zwsp
      from roll 
      where onyen = %s """, [user])
    row = db.fetchone()
    section = row and row.section
    extended = row and row.extended
    zwsp = row and row.zwsp  # pylint: disable=possibly-unused-variable
    rid = row and row.rid

    # get the answers as a json formatted string
    db.execute(
        """
        select info, questions from rubrics
        where key = %s""",
        [key],
    )
    row = db.fetchone()
    if row is None:
        rubrics = {}
        pages = []
        info = {}
        print("no rubrics")
    else:
        rubrics = editRubrics(row.questions)
        info = row.info
        pages = info.get('pages', [])

    pages = get_activePages(db, key, user, section, pages)
    if (('pages' in info) and (pages is not None) and (len(pages) == 0)):
        log(f"mypoll/{request.path}: pages=({pages}) so no page shown")
        not_found(db)
    base = renderTemplate(path,
                          user=user,
                          key=key,
                          pages=pages,
                          rid=rid,
                          section=section,
                          constant_e=math.e,
                          constant_pi=math.pi,
                          log=math.log,
                          **inp.env())
    if base.startswith("\n<pre>"):
        # renderTemplate raised an exception
        return base
    base = renderMarkdown(base)

    # Collect up info variables for psform
    # pylint: disable=possibly-unused-variable
    exam = inp.info.get("exam")
    autosave = ((request.path.find("/exams/") == 0)
                or (request.path.find("/quizzes/") == 0)
                or (request.path.find("/worksheets/") == 0))
    includeTimer = extended and exam
    due = inp.info.get("due")
    result = renderTemplate("psform", **locals())
    # hold the lock for as little time as possible
    if original_user == '':
        db.execute(
            """
         insert into fetched (time, onyen, key, ip, pages, url) values (%s, %s, %s, %s, %s, %s)""",
            [now, user, key, ip, pages, request.url],
        )
    return result