예제 #1
0
    def generate(self, prefix):
        came_from = "/publish"
        identity = request.environ.get("repoze.who.identity")
        if not identity:
            session['login_flash'] = "Please login to convert vocabularies published and managed by you"
            session.save()
            destination = "/login?came_from=%s"%came_from
            return redirect(destination, code=303)
            
        #Check if userid has permissions for this vocab
        userid = identity['repoze.who.userid']
        vocablist = get_mediator_vocabs(userid)
        if vocablist and not prefix in vocablist.keys():
            session['welcome_flash'] = "You are not authorized to make any changes to %s"%prefix
            session.save()
            return redirect(url(controller='vocabs', action='publish'), code=303)

        params = request.params
        if not 'file' in params or not params['file']:
            session['welcome_flash'] = """No file was selected to modify"""
            session.save()
            return redirect(url(controller='vocabs', action='publish'), code=303)

        c.vocabprefix = prefix
        c.filename = params.get('file')

        #Get vocab properties
        vocab_properties = get_vocab_properties(c.vocabprefix)

        #Get vocab rdf file properties
        files = get_vocab_files(c.vocabprefix)
        rdf_vocab_properties = None
        for f, val in files.iteritems():
            if val['name'] == c.filename and val['format'] == 'application/rdf+xml':
                rdf_vocab_properties = val
                rdf_vocab_properties['uri'] = f    
                break
        #Check file exists
        if not rdf_vocab_properties or not os.path.isfile(rdf_vocab_properties['path']):
            session['welcome_flash'] = """Could not locate the file %s for %s"""%(c.filename, c.vocabprefix)
            session.save()
            return redirect(url(controller='vocabs', action='publish'), code=303)

        #Get editorial notes
        notes = get_vocab_editorial_note(c.vocabprefix)
        vocab_msgs = []
        for (msg, recepient) in notes:
            if (not recepient) or recepient == c.filename:
                vocab_msgs.append(msg)
                #vocab_file_msgs.append(msg)

        if vocab_editorial_descriptions[5] in vocab_msgs:
            #TODO: Get the URI for the html file
            session['welcome_flash'] = "HTML file exists for %s"%c.vocabprefix
            session.save()
            return redirect(url(controller='vocabs', action='publish'), code=303)

        if not vocab_editorial_descriptions[3] in vocab_msgs:
            if vocab_editorial_descriptions[1] in vocab_msgs:
                session['welcome_flash'] = "%s in %s"%(vocab_editorial_descriptions[1], c.vocabprefix)
                session.save()
                return redirect(url(controller='vocabs', action='publish'), code=303)
            if vocab_editorial_descriptions[2] in vocab_msgs:
                session['welcome_flash'] = "%s in %s"%(vocab_editorial_descriptions[2], c.filename)
                session.save()
                return redirect(url(controller='vocabs', action='publish'), code=303)
            if vocab_editorial_descriptions[4] in vocab_msgs:
                session['welcome_flash'] = "%s in %s"%(vocab_editorial_descriptions[4], c.filename)
                session.save()
                return redirect(url(controller='vocabs', action='publish'), code=303)

        del_status(c.vocabprefix, rdf_vocab_properties['uri'], 'skos:editorialNote', vocab_editorial_descriptions[3])

        #Check if file is rdf
        if not check_rdf(rdf_vocab_properties['path']):
            add_status(c.vocabprefix, rdf_vocab_properties['uri'], 'skos:editorialNote', vocab_editorial_descriptions[2])
            session['welcome_flash'] = "Could not parse %s as an rdf file"%c.filename
            session.save()
            return redirect(url(controller='vocabs', action='publish'), code=303)

        html_vocab_properties = {}
        html_vocab_properties['format'] = 'text/html'
        html_vocab_properties['name'] = "%s.html"%os.path.splitext(rdf_vocab_properties['name'])[0]
        (head, tail) = os.path.split(rdf_vocab_properties['path'])
        html_vocab_properties['path'] = os.path.join(head, html_vocab_properties['name'])
        uri_parts = rdf_vocab_properties['uri'].split('/')
        for i in range(len(uri_parts)-1, -1, -1):
            if uri_parts[i] == rdf_vocab_properties['name']:
                uri_parts[i] = html_vocab_properties['name']
                break
        html_vocab_properties['uri'] = '/'.join(uri_parts)

        loc = rdf_vocab_properties['path']
        dest = html_vocab_properties['path']
        #try:
        #    s = SpecGen(loc, c.vocabprefix, vocab_properties['preferredNamespaceUri'], ag.conversion_template, dest)
        #    s.create_file()
        #except Exception, e:
        #    add_status(c.vocabprefix, rdf_vocab_properties['uri'], 'skos:editorialNote', vocab_editorial_descriptions[4])
        #    add_status(c.vocabprefix, rdf_vocab_properties['uri'], 'skos:note', str(e))
        #    session['welcome_flash'] = """%s <br>%s"""%(vocab_editorial_descriptions[4], str(e))
        #    session.save()
        #    return redirect(url(controller='vocabs', action='publish'), code=303)        
        s = SpecGen(loc, c.vocabprefix, vocab_properties['preferredNamespaceUri'], ag.conversion_template, dest)
        s.create_file()

        add_file_to_vocab_status(c.vocabprefix, html_vocab_properties)
        add_status(c.vocabprefix, rdf_vocab_properties['uri'], 'skos:editorialNote', vocab_editorial_descriptions[5])
        add_status(c.vocabprefix, html_vocab_properties['uri'], 'dcterms:isFormatOf', rdf_vocab_properties['uri'])

        message = ["""Generated the html file %s"""%html_vocab_properties['uri']]   
        #Commit svn changes
        (status1, msg1) = svn_add(dest, "Created HTML file")
        if not status1:
            message.append("""Error committing to SVN: %s"""%msg1)
       
        (status2, msg2) = svn_commit([dest], "Generated HTML file. Committing changes")
        if not status2:
            message.append("""Error committing to SVN: %s"""%msg2)
        
        session['welcome_flash'] = '<br>'.join(message)
        session.save()
        return redirect(url(controller='vocabs', action='publish'), code=303)
예제 #2
0
    def generate(self, prefix):
        came_from = "/publish"
        identity = request.environ.get("repoze.who.identity")
        if not identity:
            session[
                'login_flash'] = "Please login to convert vocabularies published and managed by you"
            session.save()
            destination = "/login?came_from=%s" % came_from
            return redirect(destination, code=303)

        #Check if userid has permissions for this vocab
        userid = identity['repoze.who.userid']
        vocablist = get_mediator_vocabs(userid)
        if vocablist and not prefix in vocablist.keys():
            session[
                'welcome_flash'] = "You are not authorized to make any changes to %s" % prefix
            session.save()
            return redirect(url(controller='vocabs', action='publish'),
                            code=303)

        params = request.params
        if not 'file' in params or not params['file']:
            session['welcome_flash'] = """No file was selected to modify"""
            session.save()
            return redirect(url(controller='vocabs', action='publish'),
                            code=303)

        c.vocabprefix = prefix
        c.filename = params.get('file')

        #Get vocab properties
        vocab_properties = get_vocab_properties(c.vocabprefix)

        #Get vocab rdf file properties
        files = get_vocab_files(c.vocabprefix)
        rdf_vocab_properties = None
        for f, val in files.iteritems():
            if val['name'] == c.filename and val[
                    'format'] == 'application/rdf+xml':
                rdf_vocab_properties = val
                rdf_vocab_properties['uri'] = f
                break
        #Check file exists
        if not rdf_vocab_properties or not os.path.isfile(
                rdf_vocab_properties['path']):
            session[
                'welcome_flash'] = """Could not locate the file %s for %s""" % (
                    c.filename, c.vocabprefix)
            session.save()
            return redirect(url(controller='vocabs', action='publish'),
                            code=303)

        #Get editorial notes
        notes = get_vocab_editorial_note(c.vocabprefix)
        vocab_msgs = []
        for (msg, recepient) in notes:
            if (not recepient) or recepient == c.filename:
                vocab_msgs.append(msg)
                #vocab_file_msgs.append(msg)

        if vocab_editorial_descriptions[5] in vocab_msgs:
            #TODO: Get the URI for the html file
            session[
                'welcome_flash'] = "HTML file exists for %s" % c.vocabprefix
            session.save()
            return redirect(url(controller='vocabs', action='publish'),
                            code=303)

        if not vocab_editorial_descriptions[3] in vocab_msgs:
            if vocab_editorial_descriptions[1] in vocab_msgs:
                session['welcome_flash'] = "%s in %s" % (
                    vocab_editorial_descriptions[1], c.vocabprefix)
                session.save()
                return redirect(url(controller='vocabs', action='publish'),
                                code=303)
            if vocab_editorial_descriptions[2] in vocab_msgs:
                session['welcome_flash'] = "%s in %s" % (
                    vocab_editorial_descriptions[2], c.filename)
                session.save()
                return redirect(url(controller='vocabs', action='publish'),
                                code=303)
            if vocab_editorial_descriptions[4] in vocab_msgs:
                session['welcome_flash'] = "%s in %s" % (
                    vocab_editorial_descriptions[4], c.filename)
                session.save()
                return redirect(url(controller='vocabs', action='publish'),
                                code=303)

        del_status(c.vocabprefix, rdf_vocab_properties['uri'],
                   'skos:editorialNote', vocab_editorial_descriptions[3])

        #Check if file is rdf
        if not check_rdf(rdf_vocab_properties['path']):
            add_status(c.vocabprefix, rdf_vocab_properties['uri'],
                       'skos:editorialNote', vocab_editorial_descriptions[2])
            session[
                'welcome_flash'] = "Could not parse %s as an rdf file" % c.filename
            session.save()
            return redirect(url(controller='vocabs', action='publish'),
                            code=303)

        html_vocab_properties = {}
        html_vocab_properties['format'] = 'text/html'
        html_vocab_properties['name'] = "%s.html" % os.path.splitext(
            rdf_vocab_properties['name'])[0]
        (head, tail) = os.path.split(rdf_vocab_properties['path'])
        html_vocab_properties['path'] = os.path.join(
            head, html_vocab_properties['name'])
        uri_parts = rdf_vocab_properties['uri'].split('/')
        for i in range(len(uri_parts) - 1, -1, -1):
            if uri_parts[i] == rdf_vocab_properties['name']:
                uri_parts[i] = html_vocab_properties['name']
                break
        html_vocab_properties['uri'] = '/'.join(uri_parts)

        loc = rdf_vocab_properties['path']
        dest = html_vocab_properties['path']
        #try:
        #    s = SpecGen(loc, c.vocabprefix, vocab_properties['preferredNamespaceUri'], ag.conversion_template, dest)
        #    s.create_file()
        #except Exception, e:
        #    add_status(c.vocabprefix, rdf_vocab_properties['uri'], 'skos:editorialNote', vocab_editorial_descriptions[4])
        #    add_status(c.vocabprefix, rdf_vocab_properties['uri'], 'skos:note', str(e))
        #    session['welcome_flash'] = """%s <br>%s"""%(vocab_editorial_descriptions[4], str(e))
        #    session.save()
        #    return redirect(url(controller='vocabs', action='publish'), code=303)
        s = SpecGen(loc, c.vocabprefix,
                    vocab_properties['preferredNamespaceUri'],
                    ag.conversion_template, dest)
        s.create_file()

        add_file_to_vocab_status(c.vocabprefix, html_vocab_properties)
        add_status(c.vocabprefix, rdf_vocab_properties['uri'],
                   'skos:editorialNote', vocab_editorial_descriptions[5])
        add_status(c.vocabprefix, html_vocab_properties['uri'],
                   'dcterms:isFormatOf', rdf_vocab_properties['uri'])

        message = [
            """Generated the html file %s""" % html_vocab_properties['uri']
        ]
        #Commit svn changes
        (status1, msg1) = svn_add(dest, "Created HTML file")
        if not status1:
            message.append("""Error committing to SVN: %s""" % msg1)

        (status2, msg2) = svn_commit([dest],
                                     "Generated HTML file. Committing changes")
        if not status2:
            message.append("""Error committing to SVN: %s""" % msg2)

        session['welcome_flash'] = '<br>'.join(message)
        session.save()
        return redirect(url(controller='vocabs', action='publish'), code=303)
예제 #3
0
    def rename(self, prefix):
        identity = request.environ.get("repoze.who.identity")
        if not identity:
            session['login_flash'] = "Please login to modify vocabularies published and managed by you"
            session.save()
            destination = "/login?came_from=rename/%s"%prefix
            return redirect(destination, code=303)
        
        #Check if userid has permissions for this vocab
        userid = identity['repoze.who.userid']
        mediatorfile = os.path.join(ag.mediatorsdir, '%s.rdf'%userid)
        vocablist = get_mediator_vocabs(userid)
        if vocablist and not prefix in vocablist.keys():
            session['welcome_flash'] = "You are not authorized to make any changes to %s"%prefix
            session.save()
            return redirect(url(controller='vocabs', action='publish'), code=303)

        vocabs =  os.listdir(ag.vocabulariesdir)
        mediators_dir_name = ag.mediatorsdir.strip('/').split('/')[-1]
        if not prefix in vocabs:
            session['welcome_flash'] = "Vocab %s does not exist"%prefix
            session.save()
            return redirect(url(controller='vocabs', action='publish'), code=303)
        
        params = request.params
        c.vocabprefix = None
        c.base_URI = None
        if params.has_key('ns_prefix') and params['ns_prefix']:
            c.vocabprefix = params['ns_prefix']
        if params.has_key('base_URI') and params['base_URI']:
            c.base_URI = params['base_URI']
            if c.base_URI:
                c.base_URI = c.base_URI.strip()
                if (c.base_URI[-1]!="/" and c.base_URI[-1]!="#"):
                    c.base_URI += "#"
        if not c.vocabprefix or not c.base_URI:    
            c.message = 'Enter the preferred namespace prefix and namespace URI for the vocab'
            properties = get_vocab_properties(prefix)
            c.base_URI = properties['preferredNamespaceUri']
            c.vocabprefix = prefix
            return render('rename.html')
        vocabdir = "%s/%s"%(ag.vocabulariesdir, prefix)
        newvocabdir = "%s/%s"%(ag.vocabulariesdir, c.vocabprefix)
        if c.vocabprefix != prefix:
            if os.path.isdir(newvocabdir):
                message = []
                message.append("""A vocabulary already exists with the namespace prefix <a href="http://vocab.ox.ac.uk/%s">%s</a>"""%(c.vocabprefix, c.vocabprefix))
                message.append("Please select another preferred namespace prefix")
                c.message = "<br />".join(message)
                properties = get_vocab_properties(prefix)
                c.base_URI = properties['preferredNamespaceUri']
                c.vocabprefix = prefix
                return render('rename.html')
        
            os.rename(vocabdir, newvocabdir)

            #Change the rdf status file for the vocab and the mediator file
            update_vocab_uri_in_statusfile(userid, prefix, c.vocabprefix, vocabdir, newvocabdir)
        vocab_uri = "http://vocab.ox.ac.uk/%s"%c.vocabprefix
        del_status(c.vocabprefix, vocab_uri, 'skos:editorialNote', vocab_editorial_descriptions[0])
        del_status(c.vocabprefix, vocab_uri, 'vann:preferredNamespaceUri', None)
        del_status(c.vocabprefix, vocab_uri, 'vann:preferredNamespacePrefix', None)
        add_status(c.vocabprefix, vocab_uri, 'vann:preferredNamespaceUri', c.base_URI)
        add_status(c.vocabprefix, vocab_uri, 'vann:preferredNamespacePrefix', c.vocabprefix)
 
        message = ["Successfully added the properties and changed the preferred namespace prefix from %s to %s"%(prefix, c.vocabprefix)] 

        #Commit svn changes
        (status1, msg1) = svn_add(newvocabdir, "Renamed vocabulary. Added %s"%c.vocabprefix)
        if not status1:
            message.append("""Error committing to SVN: %s"""%msg1)

        #(status2, msg2) = svn_remove(vocabdir, "Removed vocabulary. removed %s"%prefix)
        #if not status2:
        #    message.append("""Error committing to SVN: %s"""%msg2)
       
        (status2, msg2) = svn_commit([newvocabdir, mediatorfile], "New vocabulary %s added by user %s"%(c.vocabprefix, userid))
        if not status2:
            message.append("""Error committing to SVN: %s"""%msg2)

        session['welcome_flash'] = "<br />".join(message)
        session.save()
        return redirect(url(controller='vocabs', action='publish'), code=303)
예제 #4
0
    def rename(self, prefix):
        identity = request.environ.get("repoze.who.identity")
        if not identity:
            session[
                'login_flash'] = "Please login to modify vocabularies published and managed by you"
            session.save()
            destination = "/login?came_from=rename/%s" % prefix
            return redirect(destination, code=303)

        #Check if userid has permissions for this vocab
        userid = identity['repoze.who.userid']
        mediatorfile = os.path.join(ag.mediatorsdir, '%s.rdf' % userid)
        vocablist = get_mediator_vocabs(userid)
        if vocablist and not prefix in vocablist.keys():
            session[
                'welcome_flash'] = "You are not authorized to make any changes to %s" % prefix
            session.save()
            return redirect(url(controller='vocabs', action='publish'),
                            code=303)

        vocabs = os.listdir(ag.vocabulariesdir)
        mediators_dir_name = ag.mediatorsdir.strip('/').split('/')[-1]
        if not prefix in vocabs:
            session['welcome_flash'] = "Vocab %s does not exist" % prefix
            session.save()
            return redirect(url(controller='vocabs', action='publish'),
                            code=303)

        params = request.params
        c.vocabprefix = None
        c.base_URI = None
        if params.has_key('ns_prefix') and params['ns_prefix']:
            c.vocabprefix = params['ns_prefix']
        if params.has_key('base_URI') and params['base_URI']:
            c.base_URI = params['base_URI']
            if c.base_URI:
                c.base_URI = c.base_URI.strip()
                if (c.base_URI[-1] != "/" and c.base_URI[-1] != "#"):
                    c.base_URI += "#"
        if not c.vocabprefix or not c.base_URI:
            c.message = 'Enter the preferred namespace prefix and namespace URI for the vocab'
            properties = get_vocab_properties(prefix)
            c.base_URI = properties['preferredNamespaceUri']
            c.vocabprefix = prefix
            return render('rename.html')
        vocabdir = "%s/%s" % (ag.vocabulariesdir, prefix)
        newvocabdir = "%s/%s" % (ag.vocabulariesdir, c.vocabprefix)
        if c.vocabprefix != prefix:
            if os.path.isdir(newvocabdir):
                message = []
                message.append(
                    """A vocabulary already exists with the namespace prefix <a href="http://vocab.ox.ac.uk/%s">%s</a>"""
                    % (c.vocabprefix, c.vocabprefix))
                message.append(
                    "Please select another preferred namespace prefix")
                c.message = "<br />".join(message)
                properties = get_vocab_properties(prefix)
                c.base_URI = properties['preferredNamespaceUri']
                c.vocabprefix = prefix
                return render('rename.html')

            os.rename(vocabdir, newvocabdir)

            #Change the rdf status file for the vocab and the mediator file
            update_vocab_uri_in_statusfile(userid, prefix, c.vocabprefix,
                                           vocabdir, newvocabdir)
        vocab_uri = "http://vocab.ox.ac.uk/%s" % c.vocabprefix
        del_status(c.vocabprefix, vocab_uri, 'skos:editorialNote',
                   vocab_editorial_descriptions[0])
        del_status(c.vocabprefix, vocab_uri, 'vann:preferredNamespaceUri',
                   None)
        del_status(c.vocabprefix, vocab_uri, 'vann:preferredNamespacePrefix',
                   None)
        add_status(c.vocabprefix, vocab_uri, 'vann:preferredNamespaceUri',
                   c.base_URI)
        add_status(c.vocabprefix, vocab_uri, 'vann:preferredNamespacePrefix',
                   c.vocabprefix)

        message = [
            "Successfully added the properties and changed the preferred namespace prefix from %s to %s"
            % (prefix, c.vocabprefix)
        ]

        #Commit svn changes
        (status1,
         msg1) = svn_add(newvocabdir,
                         "Renamed vocabulary. Added %s" % c.vocabprefix)
        if not status1:
            message.append("""Error committing to SVN: %s""" % msg1)

        #(status2, msg2) = svn_remove(vocabdir, "Removed vocabulary. removed %s"%prefix)
        #if not status2:
        #    message.append("""Error committing to SVN: %s"""%msg2)

        (status2, msg2) = svn_commit([newvocabdir, mediatorfile],
                                     "New vocabulary %s added by user %s" %
                                     (c.vocabprefix, userid))
        if not status2:
            message.append("""Error committing to SVN: %s""" % msg2)

        session['welcome_flash'] = "<br />".join(message)
        session.save()
        return redirect(url(controller='vocabs', action='publish'), code=303)