Skip to content

Commit

Permalink
- Add api integration for offline LMS app and canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
frankyrumple committed Jul 5, 2017
1 parent 95dd238 commit a0203ea
Show file tree
Hide file tree
Showing 18 changed files with 513 additions and 163 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ cache/
errors/
private/
static/media/
/.idea
10 changes: 10 additions & 0 deletions .pythonhistory
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
db = current.db
db
key = "canvas_secret"
val = "123"
items = {key: val}
items
db(db.my_app_settings).select()
db(db.my_app_settings).update(**items)
db.commit()
quit()
86 changes: 63 additions & 23 deletions controllers/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,22 +179,48 @@ def config_student_settings():
response.flash = "Error! " + str(form.errors)
return dict(form=form)


@auth.requires_membership("Administrators")
def config_canvas_settings():
ensure_settings()

msg = ""

auto_config_form = SQLFORM.factory(submit_button="OPE Auto Configure", _name="auto_config").process(formname="auto_config")

if (auto_config_form.accepted):
# Try to auto config
access_token, msg1 = Canvas.EnsureAdminAccessToken()
if access_token != "":
# Pull the canvas URL from the environment
canvas_url = ""
try:
canvas_url = str(os.environ["CANVAS_SERVER"]).strip() + ""
except KeyError as ex:
# not set!!
canvas_url = ""
if canvas_url != "":
AppSettings.SetValue('canvas_server_url', canvas_url)
# Make sure to turn enable the canvas integration if it worked
AppSettings.SetValue("canvas_import_enabled", True)
response.flash = "Canvas integration auto configured and enabled"
else:
resposne.flash = "Unable to auto configure canvas integration!"

rows = db().select(db.my_app_settings.ALL)
form = SQLFORM(db.my_app_settings, rows[0], showid=False,
fields=["canvas_import_enabled", "canvas_dev_key", "canvas_server_url", "canvas_student_quota", "canvas_faculty_quota", "canvas_auto_create_courses" ]).process()

form = SQLFORM(db.my_app_settings, rows[0], showid=False, _name="canvas_config",
fields=["canvas_import_enabled", "canvas_access_token", "canvas_secret", "canvas_server_url",
"canvas_student_quota", "canvas_faculty_quota",
"canvas_auto_create_courses" ]).process(formname="canvas_config")

if (form.accepted):
# Saved
response.flash = "Settings Saved!"
Canvas.Close()
pass
elif (form.errors):
response.flash = "Error! " + str(form.errors)
return dict(form=form)
return dict(form=form, auto_config_form=auto_config_form, msg=msg)


@auth.requires_membership("Administrators")
def switchmode():
Expand Down Expand Up @@ -255,7 +281,7 @@ def verify_settings():
ret += "<h4>Active Directory Settings</h4><div style='font-size: 10px;'>"
r = AD.VerifyADSettings(auto_create)
ret += AD.GetErrorString()
if (r != True):
if r is not True:
ret += "<div style='font-weight: bold; color: red;'>Active Directory Error</div>"
else:
ret += "<div style='font-weight: bold; color: green;'>Tests Passed</div>"
Expand All @@ -264,22 +290,25 @@ def verify_settings():
ret += "<h4>Canvas Settings</h4><div style='font-size: 10px;'>"
r = Canvas.VerifyCanvasSettings()
ret += Canvas.GetErrorString()
if (r != True):
if r is not True:
ret += "<div style='font-weight: bold; color: red;'>Canvas Error</div>"
else:
ret += "<div style='font-weight: bold; color: green;'>Tests Passed</div>"
ret += "</div><hr />"

return ret


@auth.requires_membership("Administrators")
def config_verify():
return dict()


@auth.requires_membership("Administrators")
def config_verify_auto():
return dict()


@auth.requires_membership("Administrators")
def changepassword():
form = SQLFORM.factory(
Expand All @@ -290,51 +319,60 @@ def changepassword():
error_message="Password fields don't match")),
submit_button="Change Password").process()

if (form.accepted):
if form.accepted:
confirm_pw = request.vars.get('confirm_new_password')
pw = request.vars.get('new_password', '')
#admin_user = db(db.auth_user.username=='admin').select().first()
if (pw != "" and confirm_pw == pw):
rows = db(db.auth_user.username=='admin').select()
# admin_user = db(db.auth_user.username=='admin').select().first()
if pw != "" and confirm_pw == pw:
rows = db(db.auth_user.username == 'admin').select()
for row in rows:
id = row['id']
# Set Web2py password
db(db.auth_user.id==id).update( password=db.auth_user.password.validate(pw)[0] )
response.flash = "Password Changed."
else:
response.flash = "Password not changed."
elif (form.errors):
elif form.errors:
response.flash = "Unable to set new password"
return dict(form=form)


@auth.requires_membership("Administrators")
def reset_smc():
reset_smc_form = SQLFORM.factory(submit_button="Reset SMC App", _name="reset_smc_form").process(formname="reset_smc_form")

kill_scheduler = SQLFORM.factory(submit_button="Kill Scheduler Process", _name="kill_scheduler").process(formname="kill_scheduler")

ffmpeg_running=isFFMPEGRunning()
if (ffmpeg_running == "is NOT"):
if ffmpeg_running == "is NOT":
ffmpeg_running = SPAN("is NOT", _style="color: red; font-weight: bold;")
else:
ffmpeg_running = SPAN("IS", _style="color: green; font-weight: bold;")

if (reset_smc_form.accepted):

if reset_smc_form.accepted:
# Kill in VM
cmd = "/usr/bin/nohup /bin/sleep 1; /usr/bin/killall -15 index.fcgi > /dev/null 2>&1 &"
p = subprocess.Popen(cmd, shell=True, close_fds=True)
ret = ""
#p = subprocess.Popen("/bin/sleep 1 &; /usr/bin/killall -15 index.fcgi;", shell=True)
#ret = p.wait()
# p = subprocess.Popen("/bin/sleep 1 &; /usr/bin/killall -15 index.fcgi;", shell=True)
# ret = p.wait()

# Kill in docker (touch the wsgi file)
# Find the web2py folder - Starts in the Controllers folder
f = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
wsgi_file = os.path.join(f, "wsgihandler.py")
os.system("touch " + wsgi_file)

response.flash = "SMC App Reset" + str(ret)

if (kill_scheduler.accepted):
if kill_scheduler.accepted:
#kill_process_videos_schedule_process()
kill_all_workers()
response.flash = "Scheduler process killed" # + str(ret)

return dict(reset_smc_form=reset_smc_form, ffmpeg_running=ffmpeg_running, kill_scheduler=kill_scheduler)


def kill_all_workers():
# ps ax |grep "web2py.py -K smc" | grep -v "grep" | awk '{print $1}' | xargs kill -9 $1
# Kill the worker process
Expand All @@ -343,25 +381,27 @@ def kill_all_workers():
p = subprocess.Popen(cmd, shell=True, close_fds=True)
ret = ""
ret = p.wait()
#ret = p.communicate()[0]
# ret = p.communicate()[0]
return ret



def kill_process_videos_schedule_process():
# Kill the worker process
app_name = request.application
cmd = "pkill -9 -f 'web2py.py -K " + app_name + ":process_videos'"
p = subprocess.Popen(cmd, shell=True, close_fds=True)
ret = ""
ret = p.wait()
#ret = p.communicate()[0]
# ret = p.communicate()[0]
return ret


def isFFMPEGRunning():
ret = "is NOT"
cmd1 = ["/bin/ps ax | grep 'ffmpeg'"]
p1 = subprocess.Popen(cmd1, stdout=subprocess.PIPE, shell=True)
out = p1.communicate()[0]

if ('ffmpeg -y' in out):
if 'ffmpeg -y' in out:
ret = "IS"
return ret
45 changes: 45 additions & 0 deletions controllers/lms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import sys
import os
import subprocess

from gluon import current

import paramiko

from ednet.ad import AD
from ednet.canvas import Canvas
from ednet.appsettings import AppSettings


# Needed for remote connection?
auth.settings.allow_basic_login = True
#auth.settings.actions_disabled.append('login')
#auth.settings.allow_basic_login_only = True
#auth.settings.actions.login_url=URL('your_own_error_page')

@auth.requires_membership("Administrators")
def credential_student():
response.view = 'generic.json'
db = current.db

key = ""
msg = ""
hash = ""
user_name = ""
full_name = ""
# Get the user in question
if len(request.args) > 0:
user_name = request.args[0]
if user_name is not None:
# First - does the user exist?
user_exists = False
rows = db(db.auth_user.username == user_name).select(db.auth_user.id)
for row in rows:
user_exists = True
if user_exists is True:
key, msg, hash, full_name = Canvas.EnsureStudentAccessToken(user_name)

else:
# User doesn't exit!
msg = "Invalid User!"
return dict(key=key, msg=msg, hash=hash, full_name=full_name)
6 changes: 3 additions & 3 deletions controllers/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,9 @@ def process_queue():
query = (db.media_file_import_queue)
fields = [db.media_file_import_queue.title, db.media_file_import_queue.status, db.media_file_import_queue.modified_on, db.media_file_import_queue.id, db.media_file_import_queue.media_guid]
links = [
(dict( header=T('Title'), body=lambda row: A(row.title, _href=(URL('media', 'player', extension=False) + "/" + row.media_guid) , _target='blank' ) ) ),
(dict(header=T('Title'), body=lambda row: A(row.title, _href=(URL('media', 'player', extension=False) + "/" + row.media_guid) , _target='blank' ) ) ),
(dict(header=T('Status'),body=lambda row: DIV( getTaskStatus(row.id), BR(), A('Re-Queue', _href=URL('media', 'reset_queued_item', args=[row.id], user_signature=True)) ) ) ),
(dict( header=T('Queued On'), body=lambda row: row.modified_on ) ),
(dict(header=T('Queued On'), body=lambda row: row.modified_on ) ),
(dict(header=T('Progress'), body=lambda row: getTaskProgress(row.id) ) ),
]

Expand Down Expand Up @@ -430,7 +430,7 @@ def find_wamap_videos():
# See if this item is in the database
p = os.path.join(wamap_folder, link)
try:
json_data=open(p).read()
json_data = open(p).read()
dat = loads(json_data)
v = db(db.wamap_videos.source_url==dat["yt_url"]).select().first()
if (v == None):
Expand Down
17 changes: 15 additions & 2 deletions controllers/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,29 @@
from ednet.ad import AD
from ednet.faculty import Faculty
from ednet.util import Util

from ednet.canvas import Canvas
from pytube import YouTube
import os

import ldap

import sys


def test():
a = os.environ["IT_PW"]

#db_canvas = current.db_canvas

#sql = "select * from users"

#rows = db_canvas.executesql(sql)

#test = Canvas.EnsureAdminAccessToken()

#student_test = Canvas.EnsureStudentAccessToken("s777777")

initial_run = cache.ram("startup", lambda:True, time_expire=3600)
cache_time = cache.ram("tmptime", lambda:time.ctime(), time_expire=30)
return locals()

def index():
Expand Down
Loading

0 comments on commit a0203ea

Please sign in to comment.