Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 5 additions & 18 deletions runestone/activecode/activecode.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from .textfield import *
from sqlalchemy import create_engine, Table, MetaData, select, delete
from runestone.server import get_dburl
from runestone.server.componentdb import addQuestionToDB, addHTMLToDB
from runestone.server.componentdb import addQuestionToDB, addHTMLToDB, engine, meta
from runestone.common.runestonedirective import RunestoneDirective

try:
Expand Down Expand Up @@ -122,17 +122,6 @@ def process_activcode_nodes(app, env, docname):
def purge_activecodes(app, env, docname):
pass

database_connection = True
try:
engine = create_engine(get_dburl(locals()))
meta = MetaData()
Source_code = Table('source_code', meta, autoload=True, autoload_with=engine)
Div = Table('div_ids', meta, autoload=True, autoload_with=engine)
except:
print("Cannot connect")
database_connection = False


class ActiveCode(RunestoneDirective):
"""
.. activecode:: uniqueid
Expand Down Expand Up @@ -324,7 +313,8 @@ def run(self):
course_name = env.config.html_context['course_id']
divid = self.options['divid']

try:
if engine:
Source_code = Table('source_code', meta, autoload=True, autoload_with=engine)
engine.execute(Source_code.delete().where(Source_code.c.acid == divid).where(Source_code.c.course_id == course_name))
engine.execute(Source_code.insert().values(
acid = divid,
Expand All @@ -339,6 +329,7 @@ def run(self):
except:
ch, sub_ch = (env.docname, 'null subchapter')

Div = Table('div_ids', meta, autoload=True, autoload_with=engine)
engine.execute(Div.delete()\
.where(Div.c.course_name == course_name)\
.where(Div.c.chapter == ch)\
Expand All @@ -353,11 +344,7 @@ def run(self):
))


except Exception as e:
import traceback
print("The exception is ", e)
traceback.print_exc()
print(env.config.html_context['course_id'])
else:
print("Unable to save to source_code table in activecode.py. Possible problems:")
print(" 1. dburl or course_id are not set in conf.py for your book")
print(" 2. unable to connect to the database using dburl")
Expand Down
2 changes: 1 addition & 1 deletion runestone/activecode/test/pavement.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
)
)

# If DBUSER etc. are in the environment override dburl
# If DBURL is in the environment override dburl
options.build.template_args['dburl'] = get_dburl(outer=locals())

from runestone import build # build is called implicitly by the paver driver.
2 changes: 1 addition & 1 deletion runestone/assess/test/pavement.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
version = pkg_resources.require("runestone")[0].version
options.build.template_args['runestone_version'] = version

# If DBUSER etc. are in the environment override dburl
# If DBURL is in the environment override dburl
options.build.template_args['dburl'] = get_dburl(outer=locals())

from runestone import build # build is called implicitly by the paver driver.
2 changes: 1 addition & 1 deletion runestone/clickableArea/test/pavement.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
version = pkg_resources.require("runestone")[0].version
options.build.template_args['runestone_version'] = version

# If DBUSER etc. are in the environment override dburl
# If DBURL is in the environment override dburl
options.build.template_args['dburl'] = get_dburl(outer=locals())

from runestone import build # build is called implicitly by the paver driver.
2 changes: 1 addition & 1 deletion runestone/common/project_template/pavement.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ options(
version = pkg_resources.require("runestone")[0].version
options.build.template_args['runestone_version'] = version

# If DBUSER etc. are in the environment override dburl
# If DBURL is in the environment override dburl
options.build.template_args['dburl'] = get_dburl(outer=locals())

from runestone import build # build is called implicitly by the paver driver.
9 changes: 3 additions & 6 deletions runestone/datafile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from docutils.parsers.rst import directives
from docutils.parsers.rst import Directive
from sqlalchemy import create_engine, Table, MetaData, select, delete
from runestone.server import get_dburl
from runestone.server.componentdb import engine, meta
from runestone.common.runestonedirective import RunestoneDirective

def setup(app):
Expand Down Expand Up @@ -147,9 +147,7 @@ def run(self):
else:
self.options['edit'] = "false"

try:
engine = create_engine(get_dburl(locals()))
meta = MetaData()
if engine:
Source_code = Table('source_code', meta, autoload=True, autoload_with=engine)
course_name = env.config.html_context['course_id']
divid = self.options['divid']
Expand All @@ -160,8 +158,7 @@ def run(self):
course_id = course_name,
main_code= source,
))
except Exception as e:
print("the error is ", e)
else:
print("Unable to save to source_code table in datafile__init__.py. Possible problems:")
print(" 1. dburl or course_id are not set in conf.py for your book")
print(" 2. unable to connect to the database using dburl")
Expand Down
2 changes: 1 addition & 1 deletion runestone/dragndrop/test/pavement.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
version = pkg_resources.require("runestone")[0].version
options.build.template_args['runestone_version'] = version

# If DBUSER etc. are in the environment override dburl
# If DBURL is in the environment override dburl
options.build.template_args['dburl'] = get_dburl(outer=locals())

from runestone import build # build is called implicitly by the paver driver.
3 changes: 1 addition & 2 deletions runestone/external/external.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from runestone.common.runestonedirective import RunestoneDirective
from docutils.parsers.rst import Directive
from sqlalchemy import create_engine, Table, MetaData, select, delete
from runestone.server import get_dburl
from runestone.server.componentdb import addQuestionToDB, addHTMLToDB
from runestone.common.runestonedirective import RunestoneDirective

Expand All @@ -29,7 +28,7 @@
from cgi import escape # py2

__author__ = 'jczetta'
# Code template is directly from question.py at the moment, which is (c) Bradley N. Miller.
# Code template is directly from question.py at the moment, which is (c) Bradley N. Miller.
#This is intended as the basis for a potential new gradeable directive class, still potential TODO.


Expand Down
2 changes: 1 addition & 1 deletion runestone/fitb/test/pavement.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
version = pkg_resources.require("runestone")[0].version
options.build.template_args['runestone_version'] = version

# If DBUSER etc. are in the environment override dburl
# If DBURL is in the environment override dburl
options.build.template_args['dburl'] = get_dburl(outer=locals())

from runestone import build # build is called implicitly by the paver driver.
2 changes: 1 addition & 1 deletion runestone/poll/test/pavement.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
)
)

# If DBUSER etc. are in the environment override dburl
# If DBURL is in the environment override dburl
options.build.template_args['dburl'] = get_dburl(outer=locals())

from runestone import build # build is called implicitly by the paver driver.
2 changes: 1 addition & 1 deletion runestone/question/test/pavement.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
)
)

# If DBUSER etc. are in the environment override dburl
# If DBURL is in the environment override dburl
options.build.template_args['dburl'] = get_dburl(outer=locals())

from runestone import build # build is called implicitly by the paver driver.
2 changes: 1 addition & 1 deletion runestone/reveal/test/pavement.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
version = pkg_resources.require("runestone")[0].version
options.build.template_args['runestone_version'] = version

# If DBUSER etc. are in the environment override dburl
# If DBURL is in the environment override dburl
options.build.template_args['dburl'] = get_dburl(outer=locals())

from runestone import build # build is called implicitly by the paver driver.
5 changes: 2 additions & 3 deletions runestone/server/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from .chapternames import *
from os import environ
import re

Expand All @@ -14,8 +13,8 @@ def get_dburl(outer={}):
# outer may contain the locals from the calling function
# nonlocal env, settings # Python 3 only

if all([x in environ for x in ['DBUSER', 'DBHOST', 'DBNAME']]):
return 'postgresql://{DBUSER}:{DBPASS}@{DBHOST}/{DBNAME}'.format(**environ)
if 'DBURL' in environ:
return environ['DBURL']

if 'options' in outer:
return outer['options'].build.template_args['dburl']
Expand Down
19 changes: 2 additions & 17 deletions runestone/server/chapternames.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re

from sqlalchemy import create_engine, Table, MetaData, select, delete
from .componentdb import engine, meta
from collections import OrderedDict
import sys
from functools import reduce
Expand Down Expand Up @@ -87,26 +88,10 @@ def getTOCEntries(ftext):


def addChapterInfoToDB(subChapD, chapTitles, course_id):
dbname = 'runestone'
# Provide a default database URI if the ``USER`` environment variables is define (it is on Linux/Mac).
uname = os.environ.get('USER')
if uname == 'bnmnetp':
uname = 'bnmnetp_courselib'
dbname = 'bnmnetp_courselib'

dburl = 'postgresql://{}@localhost/{}'.format(uname,dbname)


if all(name in os.environ for name in ['DBHOST', 'DBPASS', 'DBUSER', 'DBNAME']):
dburl = 'postgresql://{DBUSER}:{DBPASS}@{DBHOST}/{DBNAME}'.format(**os.environ)

try:
engine = create_engine(dburl)
except ImportError as imperr:
if not engine:
print("You need to install a DBAPI module - psycopg2 for Postgres")
return

meta = MetaData()
chapters = Table('chapters', meta, autoload=True, autoload_with=engine)
sub_chapters = Table('sub_chapters', meta, autoload=True, autoload_with=engine)

Expand Down
26 changes: 10 additions & 16 deletions runestone/server/componentdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,25 @@

import os
from sqlalchemy import create_engine, Table, MetaData, select, delete, update, and_
from . import get_dburl

# create a global DB query engine to share for the rest of the file
if all(name in os.environ for name in ['DBHOST', 'DBPASS', 'DBUSER', 'DBNAME']):
dburl = 'postgresql://{DBUSER}:{DBPASS}@{DBHOST}/{DBNAME}'.format(**os.environ)
try:
dburl = get_dburl()
engine = create_engine(dburl)
except RuntimeError as e:
dburl = None
engine = None
meta = None
print("Skipping all DB operations because environment variables not set up")
else:
# If no exceptions are raised, then set up the database.
meta = MetaData()
questions = Table('questions', meta, autoload=True, autoload_with=engine)
assignment_types = Table('assignment_types', meta, autoload=True, autoload_with=engine)
assignment_questions = Table('assignment_questions', meta, autoload=True, autoload_with=engine)
courses = Table('courses', meta, autoload=True, autoload_with=engine)

else:
dburl = None
engine = None
print("Skipping all DB operations because environment variables not set up")

def logSource(self):
sourcelog = self.state.document.settings.env.config.html_context.get('dsource', None)
Expand All @@ -51,11 +55,6 @@ def logSource(self):


def addQuestionToDB(self):
if all(name in os.environ for name in ['DBHOST', 'DBPASS', 'DBUSER', 'DBNAME']):
dburl = 'postgresql://{DBUSER}:{DBPASS}@{DBHOST}/{DBNAME}'.format(**os.environ)
else:
dburl = None

if dburl:
basecourse = self.state.document.settings.env.config.html_context.get('basecourse', "unknown")
if basecourse == "unknown":
Expand Down Expand Up @@ -226,11 +225,6 @@ def addAssignmentToDB(name = None, course_id = None, assignment_type_id = None,
return a_id

def addHTMLToDB(divid, basecourse, htmlsrc):
if all(name in os.environ for name in ['DBHOST', 'DBPASS', 'DBUSER', 'DBNAME']):
dburl = 'postgresql://{DBUSER}:{DBPASS}@{DBHOST}/{DBNAME}'.format(**os.environ)
else:
dburl = None

if dburl:
last_changed = datetime.now()
sel = select([questions]).where(and_(questions.c.name == divid,
Expand Down
2 changes: 1 addition & 1 deletion runestone/shortanswer/test/pavement.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
version = pkg_resources.require("runestone")[0].version
options.build.template_args['runestone_version'] = version

# If DBUSER etc. are in the environment override dburl
# If DBURL is in the environment override dburl
options.build.template_args['dburl'] = get_dburl(outer=locals())

from runestone import build # build is called implicitly by the paver driver.
2 changes: 1 addition & 1 deletion runestone/tabbedStuff/test/pavement.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
version = pkg_resources.require("runestone")[0].version
options.build.template_args['runestone_version'] = version

# If DBUSER etc. are in the environment override dburl
# If DBURL is in the environment override dburl
options.build.template_args['dburl'] = get_dburl(outer=locals())

from runestone import build # build is called implicitly by the paver driver.
13 changes: 4 additions & 9 deletions runestone/usageAssignment/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from sqlalchemy import create_engine, Table, MetaData, select, delete
from sqlalchemy.orm import sessionmaker
from runestone.common.runestonedirective import RunestoneDirective
from runestone.server.componentdb import addAssignmentToDB, getOrCreateAssignmentType, getCourseID, addAssignmentQuestionToDB, getOrInsertQuestionForPage
from runestone.server.componentdb import addAssignmentToDB, getOrCreateAssignmentType, getCourseID, addAssignmentQuestionToDB, getOrInsertQuestionForPage, engine, meta
from datetime import datetime
from collections import OrderedDict
import os
Expand Down Expand Up @@ -65,7 +65,7 @@ def visit_ua_node(self,node):
if d['ch'] not in chapters_and_subchapters:
chapters_and_subchapters[d['ch']] = d['sub_chs']
else:
# The order matters with respect to the list wherein they're added to the dictionary.
# The order matters with respect to the list wherein they're added to the dictionary.
for subch in d['sub_chs']:
chapters_and_subchapters[d['ch']].append(subch)

Expand All @@ -80,7 +80,7 @@ def visit_ua_node(self,node):
s += '</ul>'
s += '</div>'

# is this needed??
# is this needed??
s = s.replace("u'","'") # hack: there must be a better way to include the list and avoid unicode strings

self.body.append(s)
Expand Down Expand Up @@ -141,14 +141,9 @@ def run(self):
:points: <int>
"""

if all(name in os.environ for name in ['DBHOST', 'DBPASS', 'DBUSER', 'DBNAME']):
dburl = 'postgresql://{DBUSER}:{DBPASS}@{DBHOST}/{DBNAME}'.format(**os.environ)
else:
dburl = None
if not engine:
self.state.document.settings.env.warn(self.state.document.settings.env.docname, "Environment variables not set for DB access; can't save usageassignment to DB")
return [usageAssignmentNode(self.options)]
engine = create_engine(dburl)
meta = MetaData()
# create a configured "Session" class
Session = sessionmaker(bind=engine)
session = Session()
Expand Down