Skip to content
This repository has been archived by the owner on Oct 4, 2019. It is now read-only.

Commit

Permalink
Merge pull request #8 from DrClockwork/dev
Browse files Browse the repository at this point in the history
Dev to master
  • Loading branch information
DrClockwork committed Jun 13, 2017
2 parents 772ce64 + 6de9754 commit 873a92a
Show file tree
Hide file tree
Showing 33 changed files with 10,634 additions and 544 deletions.
14 changes: 8 additions & 6 deletions h5pp/forms.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from django import forms
from django.conf import settings
from h5pp.models import h5p_libraries
from h5pp.h5p.h5pclasses import H5PDjango
from h5pp.h5p.h5pmodule import h5pInsert, h5pGetContent
from h5pp.h5p.editor.h5peditormodule import createContent
from h5pp.models import h5p_libraries
import json
import os

Expand All @@ -13,14 +13,16 @@


def handleUploadedFile(files, filename):
if not os.path.exists(settings.MEDIA_ROOT + '/h5pp/tmp'):
os.makedirs(settings.MEDIA_ROOT + '/h5pp/tmp')
tmpdir = os.path.join(settings.MEDIA_ROOT, 'h5pp', 'tmp')

if not os.path.exists(tmpdir):
os.makedirs(tmpdir)

with open(settings.MEDIA_ROOT + '/h5pp/tmp/' + filename, 'wb+') as destination:
with open(os.path.join(tmpdir, filename), 'wb+') as destination:
for chunk in files.chunks():
destination.write(chunk)

return {'folderPath': settings.MEDIA_ROOT + '/h5pp/tmp', 'path': settings.MEDIA_ROOT + '/h5pp/tmp/' + filename}
return {'folderPath': tmpdir, 'path': os.path.join(tmpdir, filename)}

##
# Form for upload/update h5p libraries
Expand Down Expand Up @@ -71,7 +73,7 @@ def clean(self):
interface.updateTutorial()
elif unins == False:
raise forms.ValidationError(
'No actions selected.')
'No actions selected.')

return self.cleaned_data

Expand Down
33 changes: 17 additions & 16 deletions h5pp/h5p/editor/h5peditorclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ def __init__(self, h5p, storage, basePath, filesDir, editorFilesDir=None):
self.h5p = h5p
self.storage = storage
self.basePath = basePath
self.contentFilesDir = filesDir + 'content'
self.editorFilesDir = (filesDir if editorFilesDir ==
None else editorFilesDir) + 'editor'
self.contentFilesDir = os.path.join(filesDir, 'content')
self.editorFilesDir = os.path.join(filesDir if editorFilesDir ==
None else editorFilesDir, 'editor')

##
# This does alot of the same as getLibraries in library/h5pclasses.py. Use that instead ?
Expand Down Expand Up @@ -169,16 +169,16 @@ def getLibraryLanguage(self, machineName, majorVersion, minorVersion, langageCod
# Create directories for uploaded content
##
def createDirectories(self, contentId):
self.contentDirectory = self.contentFilesDir + \
'/' + str(contentId) + '/'
if not os.path.isdir(self.basePath + self.contentFilesDir):
os.mkdir(self.basePath + self.contentFilesDir, 0777)
self.contentDirectory = os.path.join(
self.contentFilesDir, str(contentId))
if not os.path.isdir(self.contentFilesDir):
os.mkdir(os.path.join(self.basePath, self.contentFilesDir), 0777)

subDirectories = ['', 'files', 'images', 'videos', 'audios']
for subDirectory in subDirectories:
subDirectory = self.contentDirectory + subDirectory
if not os.path.isdir(self.basePath + subDirectory):
os.mkdir(self.basePath + subDirectory)
subDirectory = os.path.join(self.contentDirectory, subDirectory)
if not os.path.isdir(subDirectory):
os.mkdir(subDirectory)

return True

Expand Down Expand Up @@ -255,20 +255,21 @@ def processField(self, field, params, files):
return

def processFile(self, params, files):
editorPath = self.editorFilesDir + '/'
editorPath = self.editorFilesDir

matches = re.search(self.h5p.relativePathRegExp, params['path'])
if matches:
source = self.contentDirectory + \
matches.group(1) + matches.group(4) + '/' + matches.group(5)
dest = self.contentDirectory + matches.group(5)
source = os.path.join(self.contentDirectory, matches.group(
1), matches.group(4), matches.group(5))
dest = os.path.join(self.contentDirectory, matches.group(5))
if os.path.exists(source) and not os.path.exists(dest):
shutil.copy(source, dest)

params['path'] = matches.group(5)
else:
oldPath = self.basePath + editorPath + params['path']
newPath = self.basePath + self.contentDirectory + params['path']
oldPath = os.path.join(self.basePath, editorPath, params['path'])
newPath = os.path.join(
self.basePath, self.contentDirectory, params['path'])
if not os.path.exists(newPath) and os.path.exists(oldPath):
shutil.copy(oldPath, newPath)

Expand Down
42 changes: 25 additions & 17 deletions h5pp/h5p/editor/h5peditormodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
STYLES = ["libs/darkroom.css",
"styles/css/application.css"]

OVERRIDE_STYLES = '/static/h5p/styles/h5pp.css'

SCRIPTS = [
"scripts/h5peditor.js",
"scripts/h5peditor-semantic-structure.js",
Expand Down Expand Up @@ -42,7 +44,7 @@
"ckeditor/ckeditor.js"]


def h5peditorContent(request):
def h5peditorContent(request, contentId=None):
assets = h5pAddCoreAssets()
coreAssets = h5pAddCoreAssets()
editor = h5pAddFilesAndSettings(request, True)
Expand All @@ -53,6 +55,9 @@ def h5peditorContent(request):
css = settings.STATIC_URL + 'h5p/h5peditor/' + style
assets['css'].append(css)

#Override Css
assets['css'].append(OVERRIDE_STYLES)

for script in SCRIPTS:
if script != 'scripts/h5peditor-editor.js':
js = settings.STATIC_URL + 'h5p/h5peditor/' + script
Expand All @@ -71,7 +76,7 @@ def h5peditorContent(request):

contentValidator = framework.h5pGetInstance('contentvalidator')
editor['editor'] = {
'filesPath': settings.MEDIA_URL + 'h5pp/editor',
'filesPath': os.path.join(settings.MEDIA_URL, 'h5pp', 'editor'),
'fileIcon': {
'path': settings.BASE_URL + settings.STATIC_URL + 'h5p/h5peditor/images/binary-file.png',
'width': 50,
Expand Down Expand Up @@ -122,12 +127,12 @@ def handleContentUserData(request):
# Fetch user data
userData = getUserData(contentId, subContentId,
dataId, request.user.id)
if userData == False:
if not userData:
# Did not find data, return nothing
return ajaxSuccess()
else:
# Found data, return encoded data
return ajaxSuccess(userData['data'])
return ajaxSuccess(userData.data)

return

Expand All @@ -137,9 +142,13 @@ def handleContentUserData(request):


def getUserData(contentId, subContentId, dataId, userId):
result = h5p_content_user_data.objects.filter(
user_id=userId, content_main_id=contentId, sub_content_id=subContentId, data_id=dataId).values()
return result[0] if len(result) > 0 else False
try:
result = h5p_content_user_data.objects.get(
user_id=userId, content_main_id=contentId, sub_content_id=subContentId, data_id=dataId)
except:
result = False

return result

##
# Save user data for specific content in database
Expand All @@ -164,16 +173,14 @@ def saveUserData(contentId, subContentId, dataId, preload, invalidate, data, use
delete_on_content_change=invalidate
)
else:
newDataId = update['id']
userData = h5p_content_user_data.objects.get(id=update['id'])
userData.user_id = userId
userData.content_main_id = contentId
userData.sub_content_id = subContentId
userData.data_id = dataId
userData.data = data
userData.preloaded = preload
userData.delete_on_content_change = invalidate
userData.save()
update.user_id = userId
update.content_main_id = contentId
update.sub_content_id = subContentId
update.data_id = dataId
update.data = data
update.preloaded = preload
update.delete_on_content_change = invalidate
update.save()

##
# Delete user data with specific content from database
Expand Down Expand Up @@ -222,6 +229,7 @@ def getLibraryProperty(library, prop='all'):
else:
return False


def ajaxSuccess(data=None):
response = {
'success': True
Expand Down
2 changes: 1 addition & 1 deletion h5pp/h5p/editor/library/h5peditorfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __init__(self, request, files, framework):
self.result = dict()
self.field = json.loads(field)
self.files = files['file']
self.path = settings.MEDIA_ROOT + '/h5pp/tmp/' + self.files.name
self.path = os.path.join(settings.MEDIA_ROOT, 'h5pp', 'tmp', self.files.name)

# Check if uploaded base64 encoded file
if 'dataURI' in request.POST and request.POST['dataURI'] != '':
Expand Down
25 changes: 15 additions & 10 deletions h5pp/h5p/h5pclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def h5pGetInstance(self, typ, h5pdir=None, h5p=None):
self.interface.getUploadedH5pPath(h5p)

if not hasattr(self, 'core'):
self.core = H5PCore(self.interface, settings.MEDIA_ROOT + '/h5pp', settings.BASE_DIR,
self.core = H5PCore(self.interface, os.path.join(settings.MEDIA_ROOT, 'h5pp'), settings.BASE_DIR,
'en', True if getattr(settings, 'H5P_EXPORT') else False, False)

if typ == 'validator':
Expand All @@ -57,7 +57,7 @@ def h5pGetInstance(self, typ, h5pdir=None, h5p=None):
return self.core
elif typ == 'editor':
storage = H5PEditorStorage()
return H5PDjangoEditor(self.core, storage, settings.BASE_DIR, settings.MEDIA_URL + 'h5pp/')
return H5PDjangoEditor(self.core, storage, settings.BASE_DIR, os.path.join(settings.MEDIA_ROOT, 'h5pp'))

##
# Returns info for the current platform
Expand Down Expand Up @@ -279,7 +279,8 @@ def saveLibraryData(self, libraryData, new=True):
semantics=libraryData['semantics'])
libraryData['libraryId'] = libraryId.library_id
else:
library = h5p_libraries.objects.get(library_id=libraryData['libraryId'])
library = h5p_libraries.objects.get(
library_id=libraryData['libraryId'])
library.title = libraryData['title']
library.patch_version = libraryData['patchVersion']
library.runnable = libraryData['runnable']
Expand Down Expand Up @@ -329,7 +330,8 @@ def deleteLibrary(self, libraryId):
library = h5p_libraries.objects.get(library_id=libraryId)

# Delete files
self.deleteFileTree(settings.MEDIA_ROOT + '/h5pp/libraries/' + library.machine_name + '-' + library.major_version + '.' + library.minor_version)
self.deleteFileTree(os.path.join(settings.MEDIA_ROOT, 'h5pp', 'libraries',
library.machine_name + '-' + library.major_version + '.' + library.minor_version))

# Delete data in database (won't delete content)
h5p_libraries_libraries.objects.get(library_id=libraryId).delete()
Expand Down Expand Up @@ -404,14 +406,17 @@ def insertContent(self, content, contentMainId=None):
def resetContentUserData(self, contentId):
if h5p_content_user_data.objects.filter(content_main_id=contentId, delete_on_content_change=1).count() > 0:
# Reset user datas for this content
userData = h5p_content_user_data.objects.get(content_main_id=contentId, delete_on_content_change=1)
userData.timestamp = int(time.time())
userData.data = 'RESET'
userData.save()
userData = h5p_content_user_data.objects.filter(
content_main_id=contentId, delete_on_content_change=1)
for user in userData:
user.timestamp = int(time.time())
user.data = 'RESET'
user.save()
##
# Get file extension whitelist
# The default extension list is part of h5p, but admins should be allowed to modify it
##

def getWhitelist(self, isLibrary, defaultContentWhitelist, defaultLibraryWhitelist):
global h5pWhitelist, h5pWhitelistExtras
whitelist = h5pWhitelist
Expand Down Expand Up @@ -502,8 +507,8 @@ def loadLibrary(self, machineName, majorVersion, minorVersion):
return library

def getSemanticsFromFile(self, machineName, majorVersion, minorVersion):
semanticsPath = settings.H5P_PATH + '/libraries/' + machineName + '-' + \
str(majorVersion) + '.' + str(minorVersion) + '/semantics.json'
semanticsPath = os.path.join(settings.H5P_PATH, 'libraries', machineName + '-' +
str(majorVersion) + '.' + str(minorVersion), 'semantics.json')
if os.path.exists(semanticsPath):
semantics = semanticsPath.read()
if not json.loads(semantics):
Expand Down
3 changes: 2 additions & 1 deletion h5pp/h5p/h5pevent.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def saveStats(self):
type=atype, library_name=self.library_name, library_version=self.library_version, num=1)
else:
# Update counter with num+1
counter = h5p_counters.objects.get(type=atype, library_name=self.library_name, library_version=self.library_version)
counter = h5p_counters.objects.get(
type=atype, library_name=self.library_name, library_version=self.library_version)
counter.num = F('num') + 1
counter.save()
Loading

0 comments on commit 873a92a

Please sign in to comment.