def queryMoodle(self):
     username = self.vc("formData").get("username")
     # We are going to bypass the authentication Jython
     #   object to directly access the moodle plugin.
     self.roleManager.setActivePlugin("moodle")
     roles = self.roleManager.getRoles(username)
     if len(roles) == 0:
         self.throw_error("The given user is invalid or has no courses.")
     else:
         roleIds = []
         roleList = []
         # For each role (courseId)
         for role in roles:
             thisRole = JsonSimple()
             # Query the database for the course's name
             name = self.__dbGet(role)
             if name is not None:
                 roleIds.append("\"" + role + "\"")
                 thisRole.getJsonObject().put("name", name)
                 psData = self.__dbGetPS(role)
                 if psData is not None:
                     thisRole.getJsonObject().put("psData", psData)
                 roleList.append("\"" + role + "\" : " + thisRole.toString())
         keys = "\"keys\" : [" + ",".join(roleIds) + "]"
         data = "\"data\" : {" + ",".join(roleList) + "}"
         response = "{" + keys + "," + data + "}"
         self.writer.println(response)
         self.writer.close()
Пример #2
0
 def __messages(self):
     if self.message_list is not None and len(self.message_list) > 0:
         msg = JsonSimple()
         msg.getJsonObject().put("oid", self.oid)
         message = msg.toString()
         for target in self.message_list:
             self.utils.sendMessage(target, message)
Пример #3
0
    def process_tags(self, result):
        tags = []
        tagsDict = {}
        # Build a dictionary of the tags
        for doc in result:
            # Get Anotar data from Solr data
            doc = JsonSimple(doc.get("jsonString"))
            # Get actual tag text
            tag = doc.getString(None, ["content", "literal"])
            # Find out if they have locators
            locs = doc.getJsonSimpleList(["annotates", "locators"]).size()
            if locs == 0:
                # Basic tags, just aggregate counts
                if tag in tagsDict:
                    # We've seen it before, just increment the counter
                    existing = tagsDict[tag]
                    count = existing.getInteger(0, ["tagCount"])
                    existing.getJsonObject().put("tagCount", str(count + 1))
                else:
                    # First time, store this object
                    doc.getJsonObject().put("tagCount", str(1))
                    tagsDict[tag] = doc
            else:
                # Tags with a locator, special case for images etc.
                tags.append(doc.toString())

        # Push all the 'basic' counts into the list to return
        for tag in tagsDict:
            tags.append(tagsDict[tag].toString())
        return "[" + ",".join(tags) + "]"
Пример #4
0
 def get_image(self):
     self.type = "http://www.purl.org/anotar/ns/type/0.1#Tag"
     mediaFragType = "http://www.w3.org/TR/2009/WD-media-frags-20091217"
     result = '{"result":' + self.search_solr() + "}"
     if result:
         imageTagList = []
         imageTags = JsonSimple(result).getJsonSimpleList(["result"])
         for imageTag in imageTags:
             imageAno = JsonSimple()
             # We only want tags with locators, not basic tags
             locators = imageTag.getJsonSimpleList(["annotates", "locators"])
             if locators and not locators.isEmpty():
                 locatorValue = locators.get(0).getString(None, ["value"])
                 locatorType = locators.get(0).get(None, ["type"])
                 if locatorValue and locatorValue.find("#xywh=") > -1 and locatorType == mediaFragType:
                     _, locatorValue = locatorValue.split("#xywh=")
                     left, top, width, height = locatorValue.split(",")
                     object = imageAno.getJsonObject()
                     object.put("top", top)
                     object.put("left", left)
                     object.put("width", width)
                     object.put("height", height)
                     object.put("creator", imageTag.getString(None, ["creator", "literal"]))
                     object.put("creatorUri", imageTag.getString(None, ["creator", "uri"]))
                     object.put("id", imageTag.getString(None, ["id"]))
                     # tagCount = imageTag.getString(None, ["tagCount"])
                     object.put("text", imageTag.getString(None, ["content", "literal"]))
                     object.put("editable", "true")
                     imageTagList.append(imageAno.toString())
         result = "[" + ",".join(imageTagList) + "]"
     return result
    def queryMoodle(self, usernames):
        
        self.roleManager.setActivePlugin("moodle")
        
        rolesMap = {}
        
        courseMap = {}
#        codeList = []
#        yearList = []
#        semesterList = []
#        otherList = []
                
        for username in usernames:
            roles = self.roleManager.getRoles(username)
            for role in roles:
                thisRole = JsonSimple()
                name = self.__dbGet(role)
                if name is not None:
                    thisRole.getJsonObject().put("name", name)
                    
                    psData = self.__dbGetPS(role)
                    if psData is not None:
                        thisRole.getJsonObject().put("psData", psData)
                        code, year, semester = psData.split("_")
                        if code not in self.codeList:
                            self.codeList.append(code)
                        if year not in self.yearList:
                            self.yearList.append(year)
                        if semester not in self.semesterList:
                            self.semesterList.append(semester)
                            
                        courseMap["%s %s %s" % (code, year, semester)] = {"id": role, "name": name, "psData": psData}
                    else:
                        courseName = "%s_%s" % (name, role)
                        if courseName not in self.otherList:
                            self.otherList.append(courseName)
                        courseMap[courseName] = {"id": role, "name": name, "psData": None}
                        #otherList.append({"id": role, "name": name, "psData": None})
                    rolesMap[role] = thisRole
        
        self.codeList.sort()
        self.yearList.sort()
        self.yearList.reverse()
        self.semesterList.sort()
        self.otherList.sort()
        
        return courseMap
Пример #6
0
 def __activate__(self, context):
     response = context["response"]
     writer = response.getPrintWriter("text/plain; charset=UTF-8")
     auth = context["page"].authentication
     result = JsonSimple()
     obj = result.getJsonObject()
     obj.put("status", "error")
     obj.put("message", "An unknown error has occurred")
     if auth.is_logged_in():
         services = context["Services"]
         formData = context["formData"]
         sessionState = context["sessionState"]
         urlBase = context["urlBase"]
         if urlBase.endswith("/"):
             urlBase = urlBase[:-1]
         func = formData.get("func")
         portalManager = services.portalManager
         if func == "create-view":
             try:
                 fq = [q for q in sessionState.get("fq") if q != 'item_type:"object"']
                 id = formData.get("id")
                 description = formData.get("description")
                 print "Creating view '%s': '%s'" % (id, description)
                 portal = Portal(id)
                 portal.setDescription(formData.get("description"))
                 portal.setQuery(" OR ".join(fq))
                 portal.setSearchQuery(sessionState.get("searchQuery"))
                 portal.setFacetFields(portalManager.default.facetFields)
                 portalManager.add(portal)
                 portalManager.save(portal)
                 obj.put("status", "ok")
                 obj.put("message", "View '%s' successfully created" % id)
                 obj.put("url", "%s/%s/home" % (urlBase, id))
             except Exception, e:
                 response.setStatus(500)
                 obj.put("message", str(e))
         elif func == "delete-view":
             defaultPortal = context["defaultPortal"]
             portalId = formData.get("view")
             if auth.is_admin():
                 if not portalId:
                     response.setStatus(500)
                     obj.put("message", "No view specified to be deleted")
                 elif portalId != defaultPortal:
                     # sanity check: don't delete default portal
                     print "Deleting view '%s'" % portalId
                     try:
                         portalManager.remove(portalId)
                         obj.put("status", "ok")
                         obj.put("message", "View '%s' successfully removed" % portalId)
                         obj.put("url", "%s/%s/home" % (urlBase, defaultPortal))
                     except Exception, e:
                         obj.put("message", str(e))
                 else:
                     response.setStatus(500)
                     obj.put("message", "The default view cannot be deleted")
             else:
                 response.setStatus(403)
                 obj.put("message", "Only administrative users can access this API")
    def __messages(self):
        # Generic workflow messages
        if self.message_list is not None and len(self.message_list) > 0:
            msg = JsonSimple()
            msg.getJsonObject().put("oid", self.oid)
            message = msg.toString()
            for target in self.message_list:
                self.utils.sendMessage(target, message)

        # Camtasia integration
        emailStep = self.params.getProperty("emailStep")
        # First pass though... harvest queue
        if emailStep is None:
            emailStep = "emailOne"
            # The next message
            self.params.setProperty("emailStep", "emailTwo")
        else:
            # Second step... render queue
            if emailStep == "emailTwo":
                self.params.setProperty("emailStep", "completed")
        if emailStep is not None and emailStep != "completed":
            email = self.params.getProperty("relayEmail")
            body = self.__getEmailBody(emailStep)
            if email is not None and body is not None:
                msg = JsonSimple()
                msg.getJsonObject().put("to", email)
                msg.getJsonObject().put("body", body)
                message = msg.toString()
                self.utils.sendMessage("emailnotification", message)
    def queryMoodleOld(self, username):
        # We are going to bypass the authentication Jython
        #   object to directly access the moodle plugin.
        
        self.roleManager.setActivePlugin("moodle")
        roles = self.roleManager.getRoles(username)

        rolesMap = {}
        # For each role (courseId)
        for role in roles:
            thisRole = JsonSimple()
            # Query the database for the course's name
            name = self.__dbGet(role)
            if name is not None:
                thisRole.getJsonObject().put("name", name)

                # Query the database for the course's Peoplesoft data
                psData = self.__dbGetPS(role)
                if psData is not None:
                    thisRole.getJsonObject().put("psData", psData)
                rolesMap[role] = thisRole
        return rolesMap
Пример #9
0
    def modify_json(self):
        # print "**** anotar.py : add_json() : adding json : " + json
        jsonSimple = JsonSimple(self.json)
        jsonObj = jsonSimple.getJsonObject()
        jsonObj.put("id", self.pid)
        rootUri = jsonSimple.getString(None, ["annotates", "rootUri"])
        if rootUri is not None:
            baseUrl = "http://%s:%s/" % (self.vc("request").serverName, self.vc("serverPort"))
            myUri = baseUrl + rootUri + "#" + self.pid
            jsonObj.put("uri", myUri)

        jsonObj.put("schemaVersionUri", "http://www.purl.org/anotar/schema/0.1")
        self.json = jsonSimple.toString()
Пример #10
0
    def __activate__(self, context):
        request = context["request"]
        response = context["response"]
        writer = response.getPrintWriter("text/javascript; charset=UTF-8")
        result = JsonSimple()

        ## Look for the JSONP callback to use
        jsonpCallback = request.getParameter("callback")
        if jsonpCallback is None:
            jsonpCallback = request.getParameter("jsonp_callback")
            if jsonpCallback is None:
                response.setStatus(403)
                writer.println("Error: This interface only responds to JSONP")
                writer.close()
                return

        if context["page"].authentication.is_logged_in():
            result.getJsonObject().put("isAuthenticated", "true")
        else:
            result.getJsonObject().put("isAuthenticated", "false")

        writer.println(jsonpCallback + "(" + result.toString() + ")")
        writer.close()
 def __mergeData(self):
     
     requiredMetadata = ["format", "width", "height", "timeSpent", "size"]
     
     try:
         # ffmpeg.info data:
         ffmpegPayload = self.__object.getPayload("ffmpeg.info")
         ffmpegInfo = JsonSimple(ffmpegPayload.open())
         
         ffmpegOutput = ffmpegInfo.getJsonSimpleMap(["outputs"])
         
         map = JsonSimple()
         for key in ffmpegOutput.keySet():
             data = ffmpegOutput.get(key)
             detailMap = JsonSimple()
             for field in data.getJsonObject().keySet():
                 if field in requiredMetadata:
                     detailMap.getJsonObject().put(field, data.getString("", [field]))
             map.getJsonObject().put(key, detailMap)
         
         self.__metadata.getJsonObject().put("outputs", map)
         ffmpegPayload.close()
     except Exception, e:
         print str(e)
Пример #12
0
 def getJson(self, state = "open"):
     title = "%s (%s)" % (self.getName(), self.getCount())
     json = JsonSimple()
     jsonObj = json.getJsonObject()
     attributes = JsonObject()
     attributes.put("id", self.getId())
     attributes.put("fq", self.getFacetQuery())
     attributes.put("title", title)
     jsonObj.put("data", title)
     jsonObj.put("attributes", attributes)
     hasSubFacets = not self.getSubFacets().isEmpty()
     if hasSubFacets:
         jsonObj.put("state", state)
         subFacetList = ArrayList()
         for subFacet in self.getSubFacets():
             subFacetList.add(subFacet.getJson("closed"))
         children = JSONArray()
         children.addAll(subFacetList)
         jsonObj.put("children", children)
     return json
class MetadataData:
    def __init__(self):
        pass

    def __activate__(self, context):
        self.request = context["request"]
        self.services = context["Services"]
        
        self.__metadata = JsonSimple()
        
        # get the oid
        uri = URLDecoder.decode(self.request.getAttribute("RequestURI"))
        matches = re.match("^(.*?)/(.*?)/(?:(.*?)/)?(.*)$", uri)
        if matches and matches.group(3):
            oid = matches.group(3)
            
        self.__object = self.services.getStorage().getObject(oid)
        
        self.__mergeData()
        
        response = context["response"]
        response.setHeader("Content-Disposition", "attachment; filename=metadata.json")
        writer = response.getPrintWriter("application/json; charset=UTF-8")
        #Content-Disposition
        writer.println(self.__metadata)
        writer.close()
        
    def __mergeData(self):
        
        requiredMetadata = ["format", "width", "height", "timeSpent", "size"]
        
        try:
            # ffmpeg.info data:
            ffmpegPayload = self.__object.getPayload("ffmpeg.info")
            ffmpegInfo = JsonSimple(ffmpegPayload.open())
            
            ffmpegOutput = ffmpegInfo.getJsonSimpleMap(["outputs"])
            
            map = JsonSimple()
            for key in ffmpegOutput.keySet():
                data = ffmpegOutput.get(key)
                detailMap = JsonSimple()
                for field in data.getJsonObject().keySet():
                    if field in requiredMetadata:
                        detailMap.getJsonObject().put(field, data.getString("", [field]))
                map.getJsonObject().put(key, detailMap)
            
            self.__metadata.getJsonObject().put("outputs", map)
            ffmpegPayload.close()
        except Exception, e:
            print str(e)
        
        try:
            # workflow.metadata
            workflowPayload = self.__object.getPayload("workflow.metadata")
            workflowInfo = JsonSimple(workflowPayload.open())
            
            formData = workflowInfo.getObject(["formData"])
            for key in formData.keySet():
                self.__metadata.getJsonObject().put(key, formData.get(key))
            workflowPayload.close()
        except Exception, e:
            print str(e)
Пример #14
0
    def __workflow(self):
        # Workflow data
        WORKFLOW_ID = "packaging"
        wfChanged = False
        workflow_security = []
        self.message_list = None
        try:
            wfPayload = self.object.getPayload("workflow.metadata")
            wfMeta = self.utils.getJsonObject(wfPayload.open())
            wfPayload.close()

            # Are we indexing because of a workflow progression?
            targetStep = wfMeta.getString(None, ["targetStep"])
            if targetStep is not None and targetStep != wfMeta.getString(None, ["step"]):
                wfChanged = True
                # Step change
                wfMeta.getJsonObject().put("step", targetStep)
                wfMeta.getJsonObject().remove("targetStep")

            # This must be a re-index then
            else:
                targetStep = wfMeta.getString(None, ["step"])

            # Security change
            stages = self.config.getJsonSimpleList(["stages"])
            for stage in stages:
                if stage.getString(None, ["name"]) == targetStep:
                    wfMeta.getJsonObject().put("label", stage.getString(None, ["label"]))
                    self.item_security = stage.getStringList(["visibility"])
                    workflow_security = stage.getStringList(["security"])
                    if wfChanged == True:
                        self.message_list = stage.getStringList(["message"])

            # Form processing
            formData = wfMeta.getObject(["formData"])
            if formData is not None:
                formData = JsonSimple(formData)
            else:
                formData = None

            coreFields = ["title", "creator", "contributor", "description", "format", "creationDate"]
            if formData is not None:
                # Core fields
                title = formData.getStringList(["title"])
                if title:
                    self.titleList = title
                creator = formData.getStringList(["creator"])
                if creator:
                    self.creatorList = creator
                contributor = formData.getStringList(["contributor"])
                if contributor:
                    self.contributorList = contributor
                description = formData.getStringList(["description"])
                if description:
                    self.descriptionList = description
                format = formData.getStringList(["format"])
                if format:
                    self.formatList = format
                creation = formData.getStringList(["creationDate"])
                if creation:
                    self.creationDate = creation
                # Non-core fields
                data = formData.getJsonObject()
                for field in data.keySet():
                    if field not in coreFields:
                        self.customFields[field] = formData.getStringList([field])

        except StorageException, e:
            # No workflow payload, time to create
            wfChanged = True
            wfMeta = JsonSimple()
            wfMetaObj = wfMeta.getJsonObject()
            wfMetaObj.put("id", WORKFLOW_ID)
            wfMetaObj.put("step", "pending")
            wfMetaObj.put("pageTitle", "Uploaded Files - Management")
            stages = self.config.getJsonSimpleList(["stages"])
            for stage in stages:
                if stage.getString(None, ["name"]) == "pending":
                    wfMetaObj.put("label", stage.getString(None, ["label"]))
                    self.item_security = stage.getStringList(["visibility"])
                    workflow_security = stage.getStringList(["security"])
                    self.message_list = stage.getStringList(["message"])
    def __workflow(self):
        # Workflow data
        WORKFLOW_ID = "usq"
        wfChanged = False
        workflow_security = []
        try:
            wfPayload = self.object.getPayload("workflow.metadata")
            wfMeta = self.utils.getJsonObject(wfPayload.open())
            wfPayload.close()

            # Are we indexing because of a workflow progression?
            targetStep = wfMeta.getString(None, ["targetStep"])
            if targetStep is not None and targetStep != wfMeta.getString(None, ["step"]):
                wfChanged = True
                # Step change
                wfMeta.getJsonObject().put("step", targetStep)
                wfMeta.getJsonObject().remove("targetStep")

            # This must be a re-index then
            else:
                targetStep = wfMeta.getString(None, ["step"])

            # Security change
            stages = self.config.getJsonSimpleList(["stages"])
            for stage in stages:
                if stage.getString(None, ["name"]) == targetStep:
                    wfMeta.getJsonObject().put("label", stage.getString(None, ["label"]))
                    self.item_security = stage.getStringList(["visibility"])
                    workflow_security = stage.getStringList(["security"])
                    if wfChanged == True:
                        self.message_list = stage.getStringList(["message"])

            # Form processing
            formData = wfMeta.getObject(["formData"])
            if formData is not None:
                formData = JsonSimple(formData)
            else:
                formData = None
            coreFields = ["title", "creator", "contributor", "description", "format", "creationDate"]
            if formData is not None:
                # Core fields
                title = formData.getStringList(["title"])
                if title:
                    self.titleList = title
                creator = formData.getStringList(["creator"])
                if creator:
                    self.creatorList = creator
                contributor = formData.getStringList(["contributor"])
                if contributor:
                    self.contributorList = contributor
                description = formData.getStringList(["description"])
                if description:
                    self.descriptionList = description
                format = formData.getStringList(["format"])
                if format:
                    self.formatList = format
                creation = formData.getStringList(["creationDate"])
                if creation:
                    self.creationDate = creation
                # Course security - basic
                course = formData.getString(None, ["course_code"])
                if course:
                    self.item_security.add(course)
                # Course security - moodle
                moodle_courses = formData.getString(None, ["moodleSecurity"])
                if moodle_courses:
                    moodleList = moodle_courses.split(",")
                    for course in moodleList:
                        if course != "":
                            self.item_security.add(course)
                # Course facets - Peoplesoft
                psMoodle_courses = formData.getString(None, ["psMoodle"])
                if psMoodle_courses:
                    psMoodleList = psMoodle_courses.split(",")
                    for course in psMoodleList:
                        if course != "":
                            self.__indexCourse(course)

                # Non-core fields
                data = formData.getJsonObject()
                for field in data.keySet():
                    if field not in coreFields:
                        data = formData.getStringList([field])
                        if field.startswith("dc_subject."):
                            subjectField = "dc_subject"
                            if self.customFields.has_key(subjectField):
                                subjectList = self.customFields[subjectField]
                                if subjectList:
                                    for subject in subjectList:
                                        data.add(subject)
                            field = subjectField
                        self.customFields[field] = data

        except StorageException, e:
            # No workflow payload, time to create
            wfChanged = True
            wfMeta = JsonSimple()
            wfMetaObj = wfMeta.getJsonObject()
            wfMetaObj.put("id", WORKFLOW_ID)
            wfMetaObj.put("step", "pending")
            wfMetaObj.put("pageTitle", "Uploaded Files - Management")
            stages = self.config.getJsonSimpleList(["stages"])
            for stage in stages:
                if stage.getString(None, ["name"]) == "pending":
                    wfMetaObj.put("label", stage.getString(None, ["label"]))
                    self.item_security = stage.getStringList(["visibility"])
                    workflow_security = stage.getStringList(["security"])
                    self.message_list = stage.getStringList(["message"])