def evaluate(self, id=None): if not h.auth.is_logged_in(): abort(401) c.idea = h.fetch_obj(Idea, id, new_id=True) node_q = Session.query(Node).filter_by(concept_id=id) c.node = node_q.first() if request.environ.get('REMOTE_USER', False): user = h.get_user(request.environ['REMOTE_USER']) sq = Session.query(IdeaEvaluation.cons_id) sq = sq.filter(IdeaEvaluation.ante==c.idea) sq = sq.filter(IdeaEvaluation.uid==user.ID) sq = sq.subquery() to_evaluate = c.idea.related.outerjoin((sq, Idea.ID==sq.c.cons_id)) to_evaluate = to_evaluate.filter(sq.c.cons_id==None) else: to_evaluate = c.idea.related c.paginator = paginate.Page( to_evaluate, page=int(request.params.get('page', 1)), items_per_page=10, controller='idea', action='edit', id=id ) return render('idea/idea-edit.html')
def graph_all(self, filetype='html', limit=False): sep_filter = request.params.get('sep_filter', False) c.sep_filter = sep_filter idea_q = Session.query(Idea) c.ideas = idea_q.all() edge_q =\ Session.query(IdeaGraphEdge).order_by(IdeaGraphEdge.jweight.desc()).limit(3*len(c.ideas)) c.edges = edge_q.all() return render('idea/graph_all.' + filetype)
def addlist(): #simply returns the list of published or about to be published sepentries that do not yet have sep_dir fields in the entity table entities_q = Session.query(Entity) entities_q = entities_q.filter(Entity.sep_dir != None) entities_q = entities_q.subquery() missing = Session.query(SEPEntry) missing = missing.outerjoin((entities_q, SEPEntry.sep_dir == entities_q.c.sep_dir)) missing = missing.filter(entities_q.c.sep_dir == None) #suppress snark, sample missing = missing.filter(SEPEntry.title != "Snark") missing = missing.filter(SEPEntry.title != "Sample") missing = missing.filter(or_(SEPEntry.published == 1, SEPEntry.status == 'au_submit_proofread')) return missing.all()
def addlist(): #simply returns the list of published or about to be published sepentries that do not yet have sep_dir fields in the entity table entities_q = Session.query(Entity) entities_q = entities_q.filter(Entity.sep_dir != None) entities_q = entities_q.subquery() missing = Session.query(SEPEntry) missing = missing.outerjoin( (entities_q, SEPEntry.sep_dir == entities_q.c.sep_dir)) missing = missing.filter(entities_q.c.sep_dir == None) #suppress snark, sample missing = missing.filter(SEPEntry.title != "Snark") missing = missing.filter(SEPEntry.title != "Sample") missing = missing.filter( or_(SEPEntry.published == 1, SEPEntry.status == 'au_submit_proofread')) return missing.all()
def list(self, filetype="html", redirect=False): node_q = Session.query(Node) # check for query if request.params.get("q"): node_q = node_q.filter(Node.name.like(u"%" + request.params["q"] + "%")) # if only 1 result, go ahead and view that node if redirect and node_q.count() == 1: h.redirect(h.url(controller="taxonomy", action="view", id=node_q.first().ID, filetype=filetype)) if filetype == "html": c.nodes = Session.query(Node).filter(Node.parent_id == None).order_by("name").all() return render("taxonomy/node-list.html") else: c.nodes = node_q.all() return render("taxonomy/node-list.%s" % filetype)
def complete_mining(entity_type=Idea, filename='graph.txt', root='./'): occur_filename = root + "graph-" + filename edge_filename = root + "edge-" + filename sql_filename = root + "sql-" + filename print "processing articles..." process_articles(entity_type, occur_filename) print "running apriori miner..." dm.apriori(occur_filename, edge_filename) print "processing edges..." edges = dm.process_edges(occur_filename, edge_filename) ents = dm.calculate_node_entropy(edges) edges = dm.calculate_edge_weight(edges, ents) print "creating sql files..." with open(sql_filename, 'w') as f: for edge, props in edges.iteritems(): ante, cons = edge row = "%s::%s" % edge row += "::%(confidence)s::%(jweight)s::%(weight)s\n" % props f.write(row) print "updating term entropy..." for term_id, entropy in ents.iteritems(): term = Session.query(Idea).get(term_id) if term: term.entropy = entropy Session.flush() Session.commit()
def list(self, filetype='html', redirect=False): thinker_q = Session.query(Thinker) c.query = '' c.sep = '' if request.params.get('sep_filter'): idea_q = idea_q.filter(Idea.sep_dir != '') if filetype=='json': response.content_type = 'application/json' # check for query if request.params.get('q'): c.query = request.params['q'] thinker_q = thinker_q.filter(Thinker.name.like(u'%'+request.params['q']+'%')) # if only 1 result, go ahead and view that thinker if redirect and thinker_q.count() == 1: return self.view(thinker_q.first().ID, filetype) if request.params.get('sep'): thinker_q = thinker_q.filter(Thinker.sep_dir == request.params['sep']) c.sep = request.params['sep'] # if only 1 result, go ahead and view that thinker if redirect and thinker_q.count() == 1: return self.view(thinker_q.first().ID, filetype) c.thinkers = thinker_q.all() return render('thinker/thinker-list.' + filetype)
def _get_evaluation(self, evaltype, id, id2, uid=None, username=None, autoCreate=True): thinker1 = h.fetch_obj(Thinker, id) thinker2 = h.fetch_obj(Thinker, id2) # Get user information if uid: uid = h.fetch_obj(User, uid).ID elif username: user = h.get_user(username) uid = user.ID if user else abort(404) else: uid = h.get_user(request.environ['REMOTE_USER']).ID evaluation_q = Session.query(evaltype) evaluation = evaluation_q.filter_by(ante_id=id, cons_id=id2, uid=uid).first() # if an evaluation does not yet exist, create one if autoCreate and not evaluation: evaluation = evaltype(id, id2, uid) Session.add(evaluation) return evaluation
def fuzzymatch(string1): #note: fuzzymatch.php must be in php path, e.g. /usr/lib/php/!!! #put in a cron job that runs every half hour for new entries? entities = Session.query(Entity) matches = [] ##string1 = string1.decode('utf8') for entity in entities: php = PHP("require 'fuzzymatch.php';") #php = PHP() #print "testing " + entity.label.encode('utf8') + " against " + string1.encode('utf8') + "\n" code = '$string1 = utf8_decode("' + string1.encode('utf8') + '");' #code = code + "$string2 = '" + entity.label.encode('latin-1', 'replace') + "';" #code = code + "print $string1; print $string2;" #print code + '$string2 = utf8_decode("' + entity.label.encode('utf8') + '");' code = code + '$string2 = utf8_decode("' + entity.label.encode( 'utf8') + '");' code = code + """print fuzzy_match($string1, $string2, 2);""" verdict = php.get_raw(code) #print "verdict is " + verdict + "\n" if float(verdict) >= .5: #print entity.label + " is a match!\n" entity.matchvalue = verdict matches.append(entity) return matches
def list(self, filetype='html', redirect=False): thinker_q = Session.query(Thinker) c.query = '' c.sep = '' if request.params.get('sep_filter'): idea_q = idea_q.filter(Idea.sep_dir != '') # check for query if request.params.get('q'): c.query = request.params['q'] thinker_q = thinker_q.filter( Thinker.name.like(u'%' + request.params['q'] + '%')) # if only 1 result, go ahead and view that thinker if redirect and thinker_q.count() == 1: return self.view(thinker_q.first().ID, filetype) if request.params.get('sep'): thinker_q = thinker_q.filter( Thinker.sep_dir == request.params['sep']) c.sep = request.params['sep'] # if only 1 result, go ahead and view that thinker if redirect and thinker_q.count() == 1: return self.view(thinker_q.first().ID, filetype) c.thinkers = thinker_q.all() return render('thinker/thinker-list.' + filetype)
def complete_mining(entity_type=Idea, filename='graph.txt', root='./'): occur_filename = root + "graph-" + filename edge_filename = root + "edge-" + filename sql_filename = root + "sql-" + filename print "processing articles..." process_articles(entity_type, occur_filename) print "running apriori miner..." dm.apriori(occur_filename, edge_filename) print "processing edges..." edges = dm.process_edges(occur_filename, edge_filename) ents = dm.calculate_node_entropy(edges) edges = dm.calculate_edge_weight(edges, ents) print "creating sql files..." with open(sql_filename, 'w') as f: for edge, props in edges.iteritems(): ante,cons = edge row = "%s::%s" % edge row += "::%(confidence)s::%(jweight)s::%(weight)s\n" % props f.write(row) print "updating term entropy..." for term_id, entropy in ents.iteritems(): term = Session.query(Idea).get(term_id) if term: term.entropy = entropy Session.flush() Session.commit()
def fuzzymatch(string1): #note: fuzzymatch.php must be in php path, e.g. /usr/lib/php/!!! #put in a cron job that runs every half hour for new entries? entities = Session.query(Entity) matches = [] ##string1 = string1.decode('utf8') for entity in entities: php = PHP("require 'fuzzymatch.php';") #php = PHP() #print "testing " + entity.label.encode('utf8') + " against " + string1.encode('utf8') + "\n" code = '$string1 = utf8_decode("' + string1.encode('utf8') + '");' #code = code + "$string2 = '" + entity.label.encode('latin-1', 'replace') + "';" #code = code + "print $string1; print $string2;" #print code + '$string2 = utf8_decode("' + entity.label.encode('utf8') + '");' code = code + '$string2 = utf8_decode("' + entity.label.encode('utf8') + '");' code = code + """print fuzzy_match($string1, $string2, 2);""" verdict = php.get_raw(code) #print "verdict is " + verdict + "\n" if float(verdict)>=.5: #print entity.label + " is a match!\n" entity.matchvalue = verdict matches.append(entity) return matches
def admin(self, id=None): if not h.auth.is_logged_in(): abort(401) if not h.auth.is_admin(): abort(403) c.sepdirnew = False c.alreadysepdir = False redirect = request.params.get('redirect', False) add = request.params.get('add', False) limit = request.params.get('limit', None) entity_q = Session.query(Entity) c.found = False c.custom = False c.new = False if request.params.get('q'): q = request.params['q'] o = Entity.label.like(q) entity_q = entity_q.filter(o).order_by(func.length(Entity.label)) # if only 1 result, go ahead and view that idea if redirect and entity_q.count() == 1: print "have a q, entityq count = 1" c.journal = h.fetch_obj(Journal, entity_q.first().ID) c.found = True id = c.journal.ID c.message = 'Entity edit page for journal ' + c.journal.name if request.params.get('entry_sep_dir'): entry_sep_dir = request.params['entry_sep_dir'] if not (c.journal.sep_dir): c.journal.sep_dir = request.params['entry_sep_dir'] c.sepdirnew = True else: c.alreadysepdir = True c.entry_sep_dir = request.params['entry_sep_dir'] return render('admin/journal-edit.html') else: print "That didn't journal." if id is None: print "I am here" c.message = "Please input an entity label using the search bar to the left." return render('admin/idea-edit.html') else: c.journal = h.fetch_obj(Journal, id) c.found = True c.message = 'Entity edit page for journal ' + c.journal.name if request.params.get('entry_sep_dir'): entry_sep_dir = request.params['entry_sep_dir'] if not (c.journal.sep_dir): c.journal.sep_dir = request.params['entry_sep_dir'] c.sepdirnew = True else: c.alreadysepdir = True c.entry_sep_dir = request.params['entry_sep_dir'] return render('admin/journal-edit.html')
def admin(self, id=None): if not h.auth.is_logged_in(): abort(401) if not h.auth.is_admin(): abort(403) c.sepdirnew = False c.alreadysepdir = False redirect = request.params.get('redirect', False) add = request.params.get('add', False) limit = request.params.get('limit', None) entity_q = Session.query(Entity) c.found = False c.custom = False c.new = False if request.params.get('q'): q = request.params['q'] o = Entity.label.like(q) entity_q = entity_q.filter(o).order_by(func.length(Entity.label)) # if only 1 result, go ahead and view that idea if redirect and entity_q.count() == 1: print "have a q, entityq count = 1" c.journal = h.fetch_obj(Journal, entity_q.first().ID) c.found = True id = c.journal.ID c.message = 'Entity edit page for journal ' + c.journal.name if request.params.get('entry_sep_dir'): entry_sep_dir = request.params['entry_sep_dir'] if not (c.journal.sep_dir): c.journal.sep_dir = request.params['entry_sep_dir'] c.sepdirnew = True else: c.alreadysepdir = True c.entry_sep_dir = request.params['entry_sep_dir'] return render('admin/journal-edit.html') else: print "That didn't journal." if id is None: print "I am here" c.message = "Please input an entity label using the search bar to the left." return render ('admin/idea-edit.html') else: c.journal = h.fetch_obj(Journal, id) c.found = True c.message = 'Entity edit page for journal ' + c.journal.name if request.params.get('entry_sep_dir'): entry_sep_dir = request.params['entry_sep_dir'] if not (c.journal.sep_dir): c.journal.sep_dir = request.params['entry_sep_dir'] c.sepdirnew = True else: c.alreadysepdir = True c.entry_sep_dir = request.params['entry_sep_dir'] return render ('admin/journal-edit.html')
def list(self, filetype='html'): redirect = request.params.get('redirect', False) limit = request.params.get('limit', None) idea_q = Session.query(Idea) c.query = '' c.sep = '' #c.nodes = Session.query(Node).filter(Node.parent_id == None).order_by("name").all() if request.params.get('sep_filter'): idea_q = idea_q.filter(Idea.sep_dir != '') # Check for query if request.params.get('q'): q = request.params['q'] c.query = q o = or_(Idea.label.like(q+'%'), Idea.label.like('% '+q+'%')) idea_q = idea_q.filter(o).order_by(Idea.entropy.desc()) # if only 1 result, go ahead and view that idea if redirect and idea_q.count() == 1: h.redirect(h.url(controller='idea', action='view', id=idea_q.first().ID,filetype=filetype)) else: c.ideas = idea_q.limit(limit) return render('idea/idea-list.' + filetype) #TODO: Error handling - we shouldn't have multiple results if request.params.get('sep'): idea_q = idea_q.filter(Idea.sep_dir == request.params['sep']) c.sep = request.params['sep'] # if only 1 result, go ahead and view that idea if redirect and idea_q.count() == 1: h.redirect(h.url(controller='idea', action='view', id=idea_q.first().ID,filetype=filetype)) elif idea_q.count() == 0: h.redirect(h.url(controller='entity', action='list', filetype=filetype, sep=request.params['sep'], redirect=redirect)) else: c.ideas = idea_q.limit(limit) return render('idea/idea-list.' + filetype) all_param = request.params.get('all', False) node_param = request.params.get('nodes', True) instance_param = request.params.get('instances', True) node_q = idea_q.join((Node,Node.concept_id==Idea.ID)) instance_q = idea_q.join(Instance.idea) if all_param: idea_q = idea_q if not node_param: idea_q = idea_q.except_(node_q) if not instance_param: idea_q = idea_q.except_(instance_q) elif node_param: idea_q = node_q if instance_param: idea_q = idea_q.union(instance_q) elif instance_param: idea_q = instance_q c.ideas = idea_q.limit(limit) return render('idea/idea-list.' + filetype)
def get_subgraph(ids, thresh=None): edge_q = Session.query(IdeaGraphEdge) edge_q = edge_q.order_by(IdeaGraphEdge.jweight.desc()) edge_q = edge_q.filter(IdeaGraphEdge.cons_id.in_(ids)) edge_q = edge_q.filter(IdeaGraphEdge.ante_id.in_(ids)) if thresh: edge_q = edge_q.filter(IdeaGraphEdge.jweight > thresh) return edge_q.all()
def get_user(login): """ Returns the User object from the model. :rtype: :class:`inphosite.model.User` """ user = Session.query(User).filter( or_(User.email == login, User.username == login.lower())).first() return user
def get_user(login): """ Returns the User object from the model. :rtype: :class:`inphosite.model.User` """ user = Session.query(User).filter(or_(User.email==login, User.username==login.lower())).first() return user
def list(self, filetype='html', redirect=False): journal_q = Session.query(Journal) # check for query if request.params.get('q'): journal_q = journal_q.filter(Journal.name.like(u'%'+request.params['q']+'%')) # if only 1 result, go ahead and view that journal if redirect and journal_q.count() == 1: return self.view(journal_q.first().id, filetype) c.journals = list(journal_q) return render('journal/journal-list.' + filetype)
def list(self, filetype='html', redirect=False): journal_q = Session.query(Journal) # check for query if request.params.get('q'): journal_q = journal_q.filter( Journal.name.like(u'%' + request.params['q'] + '%')) # if only 1 result, go ahead and view that journal if redirect and journal_q.count() == 1: return self.view(journal_q.first().id, filetype) c.journals = list(journal_q) return render('journal/journal-list.' + filetype)
def list(self, filetype='html', redirect=False): node_q = Session.query(Node) # check for query if request.params.get('q'): node_q = node_q.filter( Node.name.like(u'%' + request.params['q'] + '%')) # if only 1 result, go ahead and view that node if redirect and node_q.count() == 1: h.redirect( h.url(controller='taxonomy', action='view', id=node_q.first().ID, filetype=filetype)) if filetype == 'html': c.nodes = Session.query(Node).filter( Node.parent_id == None).order_by("name").all() return render('taxonomy/node-list.html') else: c.nodes = node_q.all() return render('taxonomy/node-list.%s' % filetype)
def _get_anon_evaluation(self, id, id2, ip, autoCreate=True): idea1 = h.fetch_obj(Idea, id, new_id=True) idea2 = h.fetch_obj(Idea, id2, new_id=True) evaluation_q = Session.query(AnonIdeaEvaluation) evaluation = evaluation_q.filter_by(ante_id=id, cons_id=id2, ip=ip).first() # if an evaluation does not yet exist, create one if autoCreate and not evaluation: evaluation = AnonIdeaEvaluation(id, id2,ip) Session.add(evaluation) return evaluation
def process_articles(entity_type=Idea, filename='output.txt'): # process entities ideas = Session.query(entity_type) # do not process Nodes or Journals ideas = ideas.filter(and_(Entity.typeID!=2, Entity.typeID!=4)) ideas = ideas.all() articles = Session.query(entity_type).filter(entity_type.sep_dir!='').all() corpus_root = config['app_conf']['corpus'] with open(filename, 'w') as f: for article in articles: filename = article.get_filename(corpus_root) if filename and os.path.isfile(filename): print "processing:", article.sep_dir try: doc = extract_article_body(filename) lines = dm.prepare_apriori_input(doc, ideas, article) f.writelines(lines) except: print "ERROR PROCESSING:", article.sep_dir else: print "BAD SEP_DIR:", article.sep_dir
def _list_property(self, property, id, filetype='html', limit=False, sep_filter=False, type='idea'): c.idea = h.fetch_obj(Idea, id) limit = request.params.get('limit', limit) sep_filter = request.params.get('sep_filter', sep_filter) property = getattr(c.idea, property) if sep_filter: property = property.filter(Entity.sep_dir != '') if limit: property = property[0:limit-1] c.ideas = property c.nodes = Session.query(Node).filter(Node.parent_id == None).order_by("name").all() return render('%s/%s-list.%s' %(type, type, filetype))
def list_stale_url(self, filetype='html', redirect=False): if not h.auth.is_logged_in(): abort(401) if not h.auth.is_admin(): abort(403) journal_q = Session.query(Journal) # check for query if request.params.get('q'): journal_q = journal_q.filter(Journal.name.like(u'%'+request.params['q']+'%')) journal_q = journal_q.filter(Journal.last_accessed < (time.time() - 2419200)) c.journals = list(journal_q) return render('journal/stale-url-list.' + filetype)
def process_articles(entity_type=Idea, filename='output.txt'): # process entities ideas = Session.query(entity_type) # do not process Nodes or Journals ideas = ideas.filter(and_(Entity.typeID != 2, Entity.typeID != 4)) ideas = ideas.all() articles = Session.query(entity_type).filter( entity_type.sep_dir != '').all() corpus_root = config['app_conf']['corpus'] with open(filename, 'w') as f: for article in articles: filename = article.get_filename(corpus_root) if filename and os.path.isfile(filename): print "processing:", article.sep_dir try: doc = extract_article_body(filename) lines = dm.prepare_apriori_input(doc, ideas, article) f.writelines(lines) except: print "ERROR PROCESSING:", article.sep_dir else: print "BAD SEP_DIR:", article.sep_dir
def list_stale_url(self, filetype='html', redirect=False): if not h.auth.is_logged_in(): abort(401) if not h.auth.is_admin(): abort(403) journal_q = Session.query(Journal) # check for query if request.params.get('q'): journal_q = journal_q.filter( Journal.name.like(u'%' + request.params['q'] + '%')) journal_q = journal_q.filter( Journal.last_accessed < (time.time() - 2419200)) c.journals = list(journal_q) return render('journal/stale-url-list.' + filetype)
def view(self, id=None, filetype='html'): if filetype == 'html': redirect = request.params.get('redirect', True) else: redirect = request.params.get('redirect', False) sep_filter = request.params.get('sep_filter', False) c.sep_filter = sep_filter c.idea = h.fetch_obj(Idea, id, new_id=True) c.count = len(c.idea.nodes) + len(c.idea.instance_of) + len(c.idea.links_to) if len(c.idea.nodes) > 0: c.node = c.idea.nodes[0] elif len(c.idea.instance_of) > 0: c.node = c.idea.instance_of[0] else: c.node = None c.evaluations = defaultdict(lambda: (-1, -1)) identity = request.environ.get('repoze.who.identity') if identity: c.uid = identity['user'].ID #c.evaluations = Session.query(IdeaEvaluation).filter_by(ante_id=c.idea.ID, uid=uid).all() eval_q = Session.query(IdeaEvaluation.cons_id, IdeaEvaluation.generality, IdeaEvaluation.relatedness) eval_q = eval_q.filter_by(uid=c.uid, ante_id=c.idea.ID) evals = eval_q.all() evals = map(lambda x: (x[0], (x[1], x[2])), evals) c.evaluations.update(dict(evals)) else: c.uid = None if redirect and len(c.idea.nodes) == 1: h.redirect(h.url(controller='taxonomy', action='view', id=c.idea.nodes[0].ID,filetype=filetype), code=303) if filetype=='json': response.content_type = 'application/json' return render('idea/idea.' + filetype)
def addentry(self, title=None, sep_dir=None): #action to serve individual page addentry.html to rectify each entry without a sep_dir in the entity table #passed ID, title, sep_dir from previous page #displays a list of urls for edit pages for everything which is a fuzzymatch to the target title #also gives a default option to add a new entry, taking the user to the appropriate entity_add page title = request.params.get('title', None) sep_dir = request.params.get('sep_dir', "") if not sep_dir: raise Exception( 'I don\'t know how you got here, but you shouldn\'t be here without a "title"...please start over at the main page.' ) c.title = title c.sep_dir = sep_dir #c.linklist will contain the list of links to be displayed c.linklist = [] entry = Session.query(SEPEntry).get(sep_dir) #get fuzzymatch list to article title #matchlist will now contain a set of entities which have a fuzzymatch to the title of the article #entity.matchvalue will contain the proportion of matched words to total--e.g. 2/3 words matched = .66 c.message = "" matchlist = [] for match in entry.fmatches: #for fuzzymatch in matchlist: try: entity = h.fetch_obj(Entity, match.entityID) except: raise Exception( 'No entity corresponds to your ID. Invalid ID:' + match.entityID + ". Perhaps the fuzzymatches are not done populating.") #entity.link = h.url(controller=entity, action='admin', redirect=True, q=entity.label) entity.link = entity.url() + "/admin?entry_sep_dir=" + sep_dir entity.strength = match.strength entity.edits = match.edits c.linklist.append(entity) c.message = c.message + "To add the sep_dir to one of the existing entities below, click on the appropriate button in the first list. (Note that your changes will not be committed until you click 'modify' on the next page.) \n\nAlternatively if none of the existing entity entries correspond to the entry, you may add a new entity by clicking on the final button below." return render('admin/addentry.html')
def view(self, id=None, filetype="html"): c.node = h.fetch_obj(Node, id, new_id=True) c.idea = c.node.idea c.evaluations = defaultdict(lambda: (-1, -1)) identity = request.environ.get("repoze.who.identity") if identity: c.uid = identity["user"].ID # c.evaluations = Session.query(IdeaEvaluation).filter_by(ante_id=c.idea.ID, uid=uid).all() eval_q = Session.query(IdeaEvaluation.cons_id, IdeaEvaluation.generality, IdeaEvaluation.relatedness) eval_q = eval_q.filter_by(uid=c.uid, ante_id=c.idea.ID) evals = eval_q.all() evals = map(lambda x: (x[0], (x[1], x[2])), evals) c.evaluations.update(dict(evals)) else: c.uid = None return render("taxonomy/node.%s" % filetype)
def view(self, id=None, filetype='html'): c.node = h.fetch_obj(Node, id, new_id=True) c.idea = c.node.idea c.evaluations = defaultdict(lambda: (-1, -1)) identity = request.environ.get('repoze.who.identity') if identity: c.uid = identity['user'].ID #c.evaluations = Session.query(IdeaEvaluation).filter_by(ante_id=c.idea.ID, uid=uid).all() eval_q = Session.query(IdeaEvaluation.cons_id, IdeaEvaluation.generality, IdeaEvaluation.relatedness) eval_q = eval_q.filter_by(uid=c.uid, ante_id=c.idea.ID) evals = eval_q.all() evals = map(lambda x: (x[0], (x[1], x[2])), evals) c.evaluations.update(dict(evals)) else: c.uid = None return render('taxonomy/node.%s' % filetype)
def fetch_obj(type, id, error=404, new_id=False): """ Fetches the object with the given id from the collection of type type. If the object does not exist, throw an HTTP error (default: 404 Not Found). :param type: object type :type type: class in :mod:`inphosite.model` :param id: object id :type id: integer or None :param error: HTTP error code. :rtype: *type* """ if id is None: abort(error) obj_q = Session.query(type) obj = obj_q.get(int(id)) #else: # obj = obj_q.filter(type.ID==int(id)).first() if obj is None: abort(error) return obj
def addentry(self, title=None, sep_dir=None): #action to serve individual page addentry.html to rectify each entry without a sep_dir in the entity table #passed ID, title, sep_dir from previous page #displays a list of urls for edit pages for everything which is a fuzzymatch to the target title #also gives a default option to add a new entry, taking the user to the appropriate entity_add page title = request.params.get('title', None) sep_dir = request.params.get('sep_dir', "") if not sep_dir: raise Exception('I don\'t know how you got here, but you shouldn\'t be here without a "title"...please start over at the main page.') c.title = title c.sep_dir = sep_dir #c.linklist will contain the list of links to be displayed c.linklist = [] entry = Session.query(SEPEntry).get(sep_dir) #get fuzzymatch list to article title #matchlist will now contain a set of entities which have a fuzzymatch to the title of the article #entity.matchvalue will contain the proportion of matched words to total--e.g. 2/3 words matched = .66 c.message = "" matchlist = [] for match in entry.fmatches: #for fuzzymatch in matchlist: try: entity = h.fetch_obj(Entity, match.entityID) except: raise Exception('No entity corresponds to your ID. Invalid ID:' + match.entityID + ". Perhaps the fuzzymatches are not done populating.") #entity.link = h.url(controller=entity, action='admin', redirect=True, q=entity.label) entity.link = entity.url() + "/admin?entry_sep_dir="+sep_dir entity.strength = match.strength entity.edits = match.edits c.linklist.append(entity) c.message = c.message + "To add the sep_dir to one of the existing entities below, click on the appropriate button in the first list. (Note that your changes will not be committed until you click 'modify' on the next page.) \n\nAlternatively if none of the existing entity entries correspond to the entry, you may add a new entity by clicking on the final button below." return render ('admin/addentry.html')
def _get_evaluation(self, id, id2, uid=None, username=None, autoCreate=True): idea1 = h.fetch_obj(Idea, id, new_id=True) idea2 = h.fetch_obj(Idea, id2, new_id=True) # Get user information if uid: uid = h.fetch_obj(User, uid).ID elif username: user = h.get_user(username) uid = user.ID if user else abort(404) else: uid = h.get_user(request.environ['REMOTE_USER']).ID evaluation_q = Session.query(IdeaEvaluation) evaluation = evaluation_q.filter_by(ante_id=id, cons_id=id2, uid=uid).first() # if an evaluation does not yet exist, create one if autoCreate and not evaluation: evaluation = IdeaEvaluation(id, id2, uid) Session.add(evaluation) return evaluation
def process(self, id=None): if not h.auth.is_logged_in(): abort(401) if not h.auth.is_admin(): abort(403) c.sepdirnew = False c.alreadysepdir = False label = request.params.get('label', None) id = request.params.get('ID', id) sep_dir = request.params.get('sep_dir', None) action = request.params.get('action', None) action2 = request.params.get('action2', None) if action2: action = action2 values = dict(request.params) if action=="Add": thinker_add = Thinker(label) thinker_add.label = label #setup search string and search pattern lastname = thinker_add.label.split(' ').pop() lastname_q = Session.query(Entity) o = Entity.searchstring.like(lastname) lastname_q = lastname_q.filter(o).order_by(func.length(Entity.label)) if lastname_q.count() == 0: #if there's no match currently to last name, can use last name alone as searchpattern/searchstring thinker_add.searchpatterns.append(lastname) thinker_add.searchstring = lastname else: #otherwise, we need to use the whole name for both, and reset the other pattern to full name too thinker_add.searchpatterns.append(label) thinker_add.searchstring = label #reset old thinker pattern to whole name too to avoid conflict oldthinker = h.fetch_obj(Thinker, lastname_q.first().ID) oldthinker.searchpatterns = [oldthinker.label] oldthinker.searchstring = oldthinker.label Session.add(oldthinker) if sep_dir: thinker_add.sep_dir = sep_dir c.thinker = thinker_add Session.add(thinker_add) Session.flush() Session.commit() c.found = True c.message = "Thinker " + c.thinker.label + " added successfully." return render ('admin/thinker-edit.html') elif action=="Modify": c.thinker = h.fetch_obj(Thinker, id) c.found = True changed = False searchpatterns = [] for k, v in values.items(): key = "" if k.startswith('searchpatterns'): varname, num = k.split('.') key = 'delsearchpattern.%s'%(num) keyval = request.params.get(key, False) if not keyval: searchpatterns.append(v) if values['newsearchpattern']: searchpatterns.append(values['newsearchpattern']) changed = True #do manually edited searchpatterns first, so we don't write over them with the new default ones if the searchstring has been changed if c.thinker.searchpatterns != searchpatterns: c.thinker.searchpatterns = searchpatterns changed = True #set values from form if c.thinker.name != values['name']: c.thinker.name = values['name'] changed = True if c.thinker.label != values['label']: c.thinker.name = values['label'] changed = True if c.thinker.searchstring != values['searchstring']: c.thinker.searchstring = values['searchstring'] changed = True if c.thinker.sep_dir != values['sep_dir']: c.thinker.sep_dir = values['sep_dir'] changed = True if c.thinker.wiki != values['wiki']: c.thinker.wiki = values['wiki'] changed = True if c.thinker.birth_day != values['birth_day']: c.thinker.birth_day = values['birth_day'] changed = True c.thinker.birth_day = values['birth_day'] if c.thinker.death_day != values['death_day']: c.thinker.death_day = values['death_day'] changed = True if c.thinker.birth_month != values['birth_month']: c.thinker.birth_month = values['birth_month'] changed = True if c.thinker.death_month != values['death_month']: c.thinker.death_month = values['death_month'] changed = True if not (c.thinker.birth_year == values['birth_year'] + " " + values['bornbc']): if c.thinker.birth_year != values['birth_year']: c.thinker.birth_year = values['birth_year'] changed = True if c.thinker.birth_year and values['bornbc'] and not re.search("(BC)|(AD)",c.thinker.birth_year): c.thinker.birth_year = c.thinker.birth_year + " " + values['bornbc'] changed = True if not (c.thinker.death_year == values['death_year'] + " " + values['diedbc']): if c.thinker.death_year != values['death_year']: c.thinker.death_year = values['death_year'] changed = True if c.thinker.death_year and values['diedbc']and not re.search("(BC)|(AD)",c.thinker.death_year): c.thinker.death_year = c.thinker.death_year + " " + values['diedbc'] changed = True #commit changes Session.flush() Session.commit() if changed: c.message = "Thinker " + c.thinker.label + " modified successfully." else: c.message = "No changes detected. Thinker " + c.thinker.label + " not modified." return render ('admin/thinker-edit.html') elif action == "Delete": c.thinker = h.fetch_obj(Thinker, values['ID']) c.message = "Thinker # " + values['ID'] + " ("+ c.thinker.label + ") deleted; please search for a new entity label on the left." h.delete_obj(c.thinker) Session.flush() Session.commit() c.found = False return render('admin/thinker-edit.html')
def process(self, id=None): if not h.auth.is_logged_in(): abort(401) if not h.auth.is_admin(): abort(403) c.sepdirnew = False c.alreadysepdir = False label = request.params.get('label', None) id = request.params.get('ID', id) sep_dir = request.params.get('sep_dir', None) URL = request.params.get('URL', None) language = request.params.get('language', None) queries = [request.params.get('queries', None)] openAccess = request.params.get('openAccess', None) active = request.params.get('active', None) student = request.params.get('student', None) ISSN = request.params.get('ISSN', None) action = request.params.get('action', None) action2 = request.params.get('action2', None) if action2: action = action2 values = dict(request.params) #abbrs = [request.params.get('abbrs', None)] abbrs = [] queries = [] for k, v in values.items(): key = "" if k.startswith('abbrs'): varname, num = k.split('.') key = 'delabbr.%s' % (num) keyval = request.params.get(key, False) if not keyval: abbrs.append(v) elif k.startswith('queries'): varname, num = k.split('.') key = 'delquer.%s' % (num) keyval = request.params.get(key, False) if not keyval: queries.append(v) if action == "Add": journal_add = Journal() journal_add.label = label #setup search string and search pattern journalname = journal_add.label journalname_q = Session.query(Entity) o = Entity.searchpattern.like('( ' + journalname + ' )') journalname_q = journalname_q.filter(o).order_by( func.length(Entity.label)) if journalname_q.count() == 0: journal_add.searchpattern = "( " + journalname + " )" journal_add.searchstring = journalname else: journal_add.searchpattern = "( " + label + " )" journal_add.searchcstring = label #reset old journal pattern to whole name too to avoid conflict oldjournal = h.fetch_obj(Journal, journalname_q.first().ID) oldjournal.searchpattern = "( " + oldjournal.label + " )" oldjournal.searchstring = oldjournal.label Session.add(oldjournal) if sep_dir: journal_add.sep_dir = sep_dir c.journal = journal_add Session.add(journal_add) Session.flush() Session.commit() c.found = True c.message = "Journal " + c.journal.label + " added successfully." return render('admin/journal-edit.html') elif action == "Modify": c.journal = h.fetch_obj(Journal, id) c.found = True changed = False #set values from form if c.journal.label != label: c.journal.label = label changed = True if c.journal.sep_dir != sep_dir: c.journal.sep_dir = sep_dir changed = True if c.journal.URL != URL: c.journal.URL = URL changed = True if c.journal.language != language: c.journal.language = language changed = True if c.journal.abbrs != abbrs: c.journal.abbrs = abbrs changed = True if c.journal.queries != queries: c.journal.queries = queries changed = True if c.journal.openAccess != openAccess: c.journal.openAccess = openAccess changed = True if c.journal.active != active: c.journal.active = active changed = True if c.journal.student != student: c.journal.student = student changed = True if c.journal.ISSN != ISSN: c.journal.ISSN = ISSN changed = True if values['newabbr']: c.journal.abbrs.append(values['newabbr']) changed = True if values['newquery']: c.journal.queries.append(values['newquery']) changed = True #commit changes Session.flush() Session.commit() if changed: c.message = "Journal " + c.journal.label + " modified successfully." else: c.message = "No change required; Journal " + c.journal.label + " not modified." return render('admin/journal-edit.html') elif action == "Delete": c.journal = h.fetch_obj(Journal, values['ID']) c.message = "Journal # " + values[ 'ID'] + " (" + c.journal.label + ") deleted; please search for a new entity label on the left." h.delete_obj(c.journal) Session.flush() Session.commit() c.found = False return render('admin/journal-edit.html')
def profile(self): if not request.environ.get('REMOTE_USER', False): abort(401) c.user = h.get_user(request.environ['REMOTE_USER']) ieq = Session.query(IdeaEvaluation).order_by(IdeaEvaluation.time.desc()) c.recent = ieq.filter(and_(IdeaEvaluation.uid==c.user.ID, or_(IdeaEvaluation.generality>-1, IdeaEvaluation.relatedness>-1))) c.recent = c.recent.limit(5) # GENERALITY gen_u = ieq.filter(and_(IdeaEvaluation.uid==c.user.ID, IdeaEvaluation.generality>-1)) gen_nu = ieq.filter(and_(IdeaEvaluation.uid!=c.user.ID, IdeaEvaluation.generality>-1)) gen_nu = gen_nu.subquery() c.gen_agree = gen_u.join((gen_nu,and_(IdeaEvaluation.ante_id==gen_nu.c.ante_id, IdeaEvaluation.cons_id==gen_nu.c.cons_id, IdeaEvaluation.generality==gen_nu.c.generality))).count() #for std dev calcs ie1 = aliased(IdeaEvaluation) ie2 = aliased(IdeaEvaluation) gen_overlaps = Session.query(ie1.generality - ie2.generality) c.gen_overlaps = gen_overlaps.filter(and_(ie1.ante_id==ie2.ante_id, ie1.cons_id==ie2.cons_id, ie1.uid==c.user.ID, ie1.uid!=ie2.uid, ie1.generality>-1, ie2.generality>-1)) gen_overlaps = c.gen_overlaps[:] c.gen_overlap = float(len(gen_overlaps[:])) if c.gen_overlap == 0: c.gen_agree_str = 'No evaluations' c.gen_avg = 0 c.gen_stddev = 0 else: c.gen_agree_str = '%.1f%%' % ((c.gen_agree / c.gen_overlap) * 100) c.gen_avg = sum([abs(x[0]) for x in gen_overlaps]) / c.gen_overlap c.gen_stddev = sqrt(sum(map(lambda x: (abs(x[0]) - c.gen_avg)\ * (abs(x[0]) - c.gen_avg), gen_overlaps))\ / c.gen_overlap) # RELATEDNESS rel_u = ieq.filter(and_(IdeaEvaluation.uid==c.user.ID, IdeaEvaluation.relatedness>-1)) rel_nu = ieq.filter(and_(IdeaEvaluation.uid!=c.user.ID, IdeaEvaluation.relatedness>-1)).subquery() c.rel_agree = rel_u.join((rel_nu,and_(IdeaEvaluation.ante_id==rel_nu.c.ante_id, IdeaEvaluation.cons_id==rel_nu.c.cons_id, IdeaEvaluation.relatedness==rel_nu.c.relatedness))) c.rel_agree = c.rel_agree.count() #for std dev calcs ie1 = aliased(IdeaEvaluation) ie2 = aliased(IdeaEvaluation) rel_overlaps = Session.query(ie1.relatedness - ie2.relatedness) c.rel_overlaps = rel_overlaps.filter(and_(ie1.ante_id==ie2.ante_id, ie1.cons_id==ie2.cons_id, ie1.uid==c.user.ID, ie1.uid!=ie2.uid, ie1.relatedness>-1, ie2.relatedness>-1)) rel_overlaps = c.rel_overlaps[:] c.rel_overlap = float(len(rel_overlaps[:])) if c.rel_overlap == 0: c.rel_agree_str = 'No evaluations' c.rel_avg = 0 c.rel_stddev = 0 else: c.rel_agree_str = '%.1f%%' % ((c.rel_agree / c.rel_overlap) * 100) c.rel_avg = sum(map(lambda x: abs(x[0]), rel_overlaps)) / c.rel_overlap c.rel_stddev = sqrt(sum(map(lambda x: (abs(x[0]) - c.rel_avg)\ * (abs(x[0]) - c.rel_avg), rel_overlaps))\ / c.rel_overlap) return render('account/profile.html')
def profile(self): if not request.environ.get('REMOTE_USER', False): abort(401) c.user = h.get_user(request.environ['REMOTE_USER']) ieq = Session.query(IdeaEvaluation).order_by(IdeaEvaluation.time.desc()) c.recent = ieq.filter(and_(IdeaEvaluation.uid==c.user.ID, or_(IdeaEvaluation.generality>-1, IdeaEvaluation.relatedness>-1))) c.recent = c.recent.limit(5) c.message = request.params.get('message', None) # GENERALITY gen_u = ieq.filter(and_(IdeaEvaluation.uid==c.user.ID, IdeaEvaluation.generality>-1)) gen_nu = ieq.filter(and_(IdeaEvaluation.uid!=c.user.ID, IdeaEvaluation.generality>-1)) gen_nu = gen_nu.subquery() c.gen_agree = gen_u.join((gen_nu,and_(IdeaEvaluation.ante_id==gen_nu.c.ante_id, IdeaEvaluation.cons_id==gen_nu.c.cons_id, IdeaEvaluation.generality==gen_nu.c.generality))).count() #for std dev calcs ie1 = aliased(IdeaEvaluation) ie2 = aliased(IdeaEvaluation) gen_overlaps = Session.query(ie1.generality - ie2.generality) c.gen_overlaps = gen_overlaps.filter(and_(ie1.ante_id==ie2.ante_id, ie1.cons_id==ie2.cons_id, ie1.uid==c.user.ID, ie1.uid!=ie2.uid, ie1.generality>-1, ie2.generality>-1)) gen_overlaps = c.gen_overlaps[:] c.gen_overlap = float(len(gen_overlaps[:])) if c.gen_overlap == 0: c.gen_agree_str = 'No evaluations' c.gen_avg = 0 c.gen_stddev = 0 else: c.gen_agree_str = '%.1f%%' % ((c.gen_agree / c.gen_overlap) * 100) c.gen_avg = sum([abs(x[0]) for x in gen_overlaps]) / c.gen_overlap c.gen_stddev = sqrt(sum(map(lambda x: (abs(x[0]) - c.gen_avg)\ * (abs(x[0]) - c.gen_avg), gen_overlaps))\ / c.gen_overlap) # RELATEDNESS rel_u = ieq.filter(and_(IdeaEvaluation.uid==c.user.ID, IdeaEvaluation.relatedness>-1)) rel_nu = ieq.filter(and_(IdeaEvaluation.uid!=c.user.ID, IdeaEvaluation.relatedness>-1)).subquery() c.rel_agree = rel_u.join((rel_nu,and_(IdeaEvaluation.ante_id==rel_nu.c.ante_id, IdeaEvaluation.cons_id==rel_nu.c.cons_id, IdeaEvaluation.relatedness==rel_nu.c.relatedness))) c.rel_agree = c.rel_agree.count() #for std dev calcs ie1 = aliased(IdeaEvaluation) ie2 = aliased(IdeaEvaluation) rel_overlaps = Session.query(ie1.relatedness - ie2.relatedness) c.rel_overlaps = rel_overlaps.filter(and_(ie1.ante_id==ie2.ante_id, ie1.cons_id==ie2.cons_id, ie1.uid==c.user.ID, ie1.uid!=ie2.uid, ie1.relatedness>-1, ie2.relatedness>-1)) rel_overlaps = c.rel_overlaps[:] c.rel_overlap = float(len(rel_overlaps[:])) if c.rel_overlap == 0: c.rel_agree_str = 'No evaluations' c.rel_avg = 0 c.rel_stddev = 0 else: c.rel_agree_str = '%.1f%%' % ((c.rel_agree / c.rel_overlap) * 100) c.rel_avg = sum(map(lambda x: abs(x[0]), rel_overlaps)) / c.rel_overlap c.rel_stddev = sqrt(sum(map(lambda x: (abs(x[0]) - c.rel_avg)\ * (abs(x[0]) - c.rel_avg), rel_overlaps))\ / c.rel_overlap) return render('account/profile.html')
def process(self, id=None): if not h.auth.is_logged_in(): abort(401) if not h.auth.is_admin(): abort(403) c.sepdirnew = False c.alreadysepdir = False label = request.params.get('label', None) id = request.params.get('ID', id) sep_dir = request.params.get('sep_dir', None) action = request.params.get('action', None) action2 = request.params.get('action2', None) if action2: action = action2 values = dict(request.params) if action == "Add": thinker_add = Thinker(label) thinker_add.label = label #setup search string and search pattern lastname = thinker_add.label.split(' ').pop() lastname_q = Session.query(Entity) o = Entity.searchstring.like(lastname) lastname_q = lastname_q.filter(o).order_by( func.length(Entity.label)) if lastname_q.count() == 0: #if there's no match currently to last name, can use last name alone as searchpattern/searchstring thinker_add.searchpatterns.append(lastname) thinker_add.searchstring = lastname else: #otherwise, we need to use the whole name for both, and reset the other pattern to full name too thinker_add.searchpatterns.append(label) thinker_add.searchstring = label #reset old thinker pattern to whole name too to avoid conflict oldthinker = h.fetch_obj(Thinker, lastname_q.first().ID) oldthinker.searchpatterns = [oldthinker.label] oldthinker.searchstring = oldthinker.label Session.add(oldthinker) if sep_dir: thinker_add.sep_dir = sep_dir c.thinker = thinker_add Session.add(thinker_add) Session.flush() Session.commit() c.found = True c.message = "Thinker " + c.thinker.label + " added successfully." return render('admin/thinker-edit.html') elif action == "Modify": c.thinker = h.fetch_obj(Thinker, id) c.found = True changed = False searchpatterns = [] for k, v in values.items(): key = "" if k.startswith('searchpatterns'): varname, num = k.split('.') key = 'delsearchpattern.%s' % (num) keyval = request.params.get(key, False) if not keyval: searchpatterns.append(v) if values['newsearchpattern']: searchpatterns.append(values['newsearchpattern']) changed = True #do manually edited searchpatterns first, so we don't write over them with the new default ones if the searchstring has been changed if c.thinker.searchpatterns != searchpatterns: c.thinker.searchpatterns = searchpatterns changed = True #set values from form if c.thinker.name != values['name']: c.thinker.name = values['name'] changed = True if c.thinker.label != values['label']: c.thinker.name = values['label'] changed = True if c.thinker.searchstring != values['searchstring']: c.thinker.searchstring = values['searchstring'] changed = True if c.thinker.sep_dir != values['sep_dir']: c.thinker.sep_dir = values['sep_dir'] changed = True if c.thinker.wiki != values['wiki']: c.thinker.wiki = values['wiki'] changed = True if c.thinker.birth_day != values['birth_day']: c.thinker.birth_day = values['birth_day'] changed = True c.thinker.birth_day = values['birth_day'] if c.thinker.death_day != values['death_day']: c.thinker.death_day = values['death_day'] changed = True if c.thinker.birth_month != values['birth_month']: c.thinker.birth_month = values['birth_month'] changed = True if c.thinker.death_month != values['death_month']: c.thinker.death_month = values['death_month'] changed = True if not (c.thinker.birth_year == values['birth_year'] + " " + values['bornbc']): if c.thinker.birth_year != values['birth_year']: c.thinker.birth_year = values['birth_year'] changed = True if c.thinker.birth_year and values['bornbc'] and not re.search( "(BC)|(AD)", c.thinker.birth_year): c.thinker.birth_year = c.thinker.birth_year + " " + values[ 'bornbc'] changed = True if not (c.thinker.death_year == values['death_year'] + " " + values['diedbc']): if c.thinker.death_year != values['death_year']: c.thinker.death_year = values['death_year'] changed = True if c.thinker.death_year and values['diedbc'] and not re.search( "(BC)|(AD)", c.thinker.death_year): c.thinker.death_year = c.thinker.death_year + " " + values[ 'diedbc'] changed = True #commit changes Session.flush() Session.commit() if changed: c.message = "Thinker " + c.thinker.label + " modified successfully." else: c.message = "No changes detected. Thinker " + c.thinker.label + " not modified." return render('admin/thinker-edit.html') elif action == "Delete": c.thinker = h.fetch_obj(Thinker, values['ID']) c.message = "Thinker # " + values[ 'ID'] + " (" + c.thinker.label + ") deleted; please search for a new entity label on the left." h.delete_obj(c.thinker) Session.flush() Session.commit() c.found = False return render('admin/thinker-edit.html')
def admin(self, id=None): if not h.auth.is_logged_in(): abort(401) if not h.auth.is_admin(): abort(403) redirect = request.params.get('redirect', False) add = request.params.get('add', False) limit = request.params.get('limit', None) sep_dir = request.params.get('sep_dir', "") entity_q = Session.query(model.Entity) c.found = False c.custom = False c.new = False c.sep_dir = sep_dir c.sepdirnew = False c.alreadysepdir = False if request.params.get('q'): q = request.params['q'] o = model.Entity.label.like(q) entity_q = entity_q.filter(o).order_by(func.length(model.Entity.label)) # if only 1 result, go ahead and view that entity if redirect and entity_q.count() == 1: c.entity = h.fetch_obj(model.Entity, entity_q.first().ID) #now get type and route to correct edit page #first, if it is an idea, typeID = 1 if c.entity.typeID == 1: print "have an idea, q, entityq count = 1" c.idea = h.fetch_obj(model.Idea, entity_q.first().ID) c.found = True id = c.idea.ID c.message = 'Entity edit page for idea ' + c.idea.label #set up c.search_string_list which will be used to structure intersection/union search pattern option if request.params.get('sep_dir'): sep_dir = request.params['sep_dir'] if not (c.idea.sep_dir): c.idea.sep_dir = request.params['sep_dir'] c.sepdirnew = True else: c.alreadysepdir = True c.entry_sep_dir = request.params['sep_dir'] c.search_string_list = c.idea.setup_SSL() if re.search(' and ', c.idea.label): c.search_pattern_list = ['union', 'intersection'] return render ('admin/idea-edit.html') #thinkers elif c.entity.typeID == 3: print "have a thinker, q, entityq count = 1" c.thinker = h.fetch_obj(model.Thinker, entity_q.first().ID) c.found = True id = c.thinker.ID c.message = 'Entity edit page for thinker ' + c.thinker.label if request.params.get('sep_dir'): sep_dir = request.params['sep_dir'] if not (c.thinker.sep_dir): c.thinker.sep_dir = request.params['sep_dir'] c.sepdirnew = True else: c.alreadysepdir = True c.entry_sep_dir = request.params['sep_dir'] return render ('admin/thinker-edit.html') elif c.entity.typeID == 4: print "have a journal, q, entityq count = 1" c.journal = h.fetch_obj(model.Journal, entity_q.first().ID) c.found = True id = c.journal.ID c.message = 'Entity edit page for journal ' + c.journal.label if request.params.get('sep_dir'): sep_dir = request.params['sep_dir'] if not (c.journal.sep_dir): c.journal.sep_dir = request.params['sep_dir'] c.sepdirnew = True else: c.alreadysepdir = True c.entry_sep_dir = request.params['sep_dir'] return render ('admin/journal-edit.html') elif c.entity.typeID == 5: print "have a work, q, entityq count = 1" c.work = h.fetch_obj(model.Work, entity_q.first().ID) c.found = True id = c.work.ID c.message = 'Entity edit page for work ' + c.work.label if request.params.get('sep_dir'): sep_dir = request.params['sep_dir'] if not (c.work.sep_dir): c.work.sep_dir = request.params['sep_dir'] c.sepdirnew = True else: c.alreadysepdir = True c.entry_sep_dir = request.params['sep_dir'] return render ('admin/work-edit.html') elif c.entity.typeID == 6: print "have a school_of_thought, q, entityq count = 1" c.school_of_thought = h.fetch_obj(model.SchoolOfThought, entity_q.first().ID) c.found = True id = c.school_of_thought.ID c.message = 'Entity edit page for school_of_thought ' + c.school_of_thought.label if request.params.get('sep_dir'): sep_dir = request.params['sep_dir'] if not (c.school_of_thought.sep_dir): c.school_of_thought.sep_dir = request.params['sep_dir'] c.sepdirnew = True else: c.alreadysepdir = True c.entry_sep_dir = request.params['sep_dir'] return render ('admin/school_of_thought-edit.html') elif redirect and entity_q.count() == 0: c.message = "No match found for your search; if you would like to add your idea, please enter its label and sepdir into the field below." c.new = True c.prevvalue = q return render ('admin/entity-add.html') else: return ('No exact match for your search; please click "back" and try again.') if id is None: print "I am here" c.message = "Please input an idea using the search bar to the left." return render ('admin/idea-edit.html') else: c.entity = h.fetch_obj(model.Entity, id) c.found = True c.message = 'Entity edit page for entity ' + c.entity.label #get sep_dir if present--from admin.py action addentry if request.params.get('sep_dir'): sep_dir = request.params['sep_dir'] if not (c.entity.sep_dir): c.entity.sep_dir = sep_dir else: c.message = c.message + "WARNING: entity already has a sep_dir [" + c.entity.sep_dir + "]. Not replacing with [" + sep_dir + "]. If you would like to do so, please do so manually in the form below." if request.params.get('entry_sep_dir'): entry_sep_dir = request.params['entry_sep_dir'] if not (c.entity.sep_dir): c.entity.sep_dir = entry_sep_dir else: c.message = c.message + "WARNING: entity already has a sep_dir [" + c.entity.sep_dir + "]. Not replacing with [" + sep_dir + "]. If you would like to do so, please do so manually in the form below." #set up c.search_string_list which will be used to structure intersection/union search pattern option if c.entity.typeID == 1: c.idea = h.fetch_obj(model.Idea, c.entity.ID) c.search_string_list = c.idea.setup_SSL() return render ('admin/idea-edit.html') elif c.entity.typeID == 3: c.thinker = h.fetch_obj(model.Thinker, c.entity.ID) return render('admin/thinker-edit.html') elif c.entity.typeID == 4: c.journal = h.fetch_obj(model.Journal, c.entity.ID) return render('admin/journal-edit.html') elif c.entity.typeID == 5: c.work = h.fetch_obj(model.Work, c.entity.ID) return render('admin/work-edit.html') elif c.entity.typeID == 6: c.school_of_thought = h.fetch_obj(model.SchoolOfThought, c.entity.ID) return render('admin/school_of_thought-edit.html') c.entity = h.fetch_obj(model.Entity, id, new_id=True) redirect(c.entity.url(action='admin'), code=303)
def fuzzymatchall(SEPEntrieslist): #takes outputs from addlist() and saves all fuzzy match IDs to SEPEntry.fuzzymatch with verdicts (percent of words matched) #now change so that it only updates ones that don't currently have a fuzzymatchlist #clear out fuzzymatch table--otherwise old fuzzies will accumulate, and nobody wants that delquery = Session.query(Fuzzymatch) delquery.delete() Session.flush() Session.commit() for SEPEntry in SEPEntrieslist: print "working on " + SEPEntry.title.encode('utf-8') + "\n" entities = Session.query(Entity) #exclude journals and nodes from fuzzy matching entities = entities.filter(Entity.typeID != 2) entities = entities.filter(Entity.typeID != 4) #reset fuzzymatches for that entry #SEPEntry.fuzzymatches = "" ##string1 = string1.decode('utf8') for entity in entities: php = PHP("set_include_path('/usr/lib/php/');") php = PHP("require 'fuzzymatch.php';") #php = PHP() #print "testing " + entity.label.encode('utf8') + " against " + string1.encode('utf8') + "\n" code = '$string1 = utf8_decode("' + SEPEntry.title.encode( 'utf8') + '");' #code = code + "$string2 = '" + entity.label.encode('latin-1', 'replace') + "';" #code = code + "print $string1; print $string2;" #print code + '$string2 = utf8_decode("' + entity.label.encode('utf8') + '");' code = code + '$string2 = utf8_decode("' + entity.label.encode( 'utf8') + '");' code = code + """print fuzzy_match($string1, $string2, 2);""" verdict = php.get_raw(code) #print "verdict is " + verdict + "\n" verdict = verdict.split(',') if float(verdict[0]) >= .20: #print entity.label + " is a match!\n" #entity.matchvalue = verdict #string = SEPEntry.fuzzymatches + "|" + str(entity.ID) + "," + verdict #if len(string) < 400: # SEPEntry.fuzzymatches = SEPEntry.fuzzymatches + "|" + str(entity.ID) + "," + verdict #else: # print "sorry, too many matches! Can't add " + str(entity.ID) + " to fuzzy matches; over 400 chars." fmatch = Fuzzymatch(entity.ID) fmatch.sep_dir = SEPEntry.sep_dir fmatch.strength = verdict[0] fmatch.edits = verdict[1] SEPEntry.fmatches.append(fmatch) Session.flush() Session.commit()
def getentries(db_dir): #to do feb 10: do something more elegant than just delete existing and rewrite...perhaps only write new? #first, clear out the current table #Session.query(SEPEntry).delete() #Session.flush() #Session.commit() entries = defaultdict(lambda: {'title' : '', 'published' : False, 'status' : ''}) pars = HTMLParser.HTMLParser() #get published entries published = open('/Users/inpho/SitesOld/dev/entries-published.txt') status = open(os.path.join(db_dir , 'entrystatus.txt')) entrylist = open(os.path.join(db_dir, 'entries.txt')) #set up entries dict for line in entrylist: line = line.split('::') sep_dir = pars.unescape(re.sub('<(/)?[a-zA-Z]*>', '', line[0])) title = re.sub("\\\\\'", "'", pars.unescape(re.sub('<(/)?[a-zA-Z]*>', '', line[1]))) entries[sep_dir]['title'] = title for line in published: line = re.sub('\\n', '', line) if entries[line]['title']: entries[line]['published'] = True else: print "uh-oh, " + line + "doesn't appear to be in dict object" for line in status: line = line.split('::') if entries[line[0]]['title']: entries[line[0]]['status'] = line[1] #print "status = " + line[1] + ' for ' + line[0] else: print "uh-oh, " + line[0] + "doesn't appear to be in dict object" for key in entries.keys(): #so, what I should really do here is figure out whether the entry already has a place in the table; if so, update its existing stats; if not, #insert a new one; #also need to check if old entries in sepentries table are no longer in file... entry_q = Session.query(SEPEntry) o = SEPEntry.title.like(entries[key]['title']) entry_q = entry_q.filter(o) # if only 1 result, go ahead and view that idea if entry_q.count() == 0: entry_add = SEPEntry(entries[key]['title'], key, entries[key]['published'], entries[key]['status']) Session.add(entry_add) elif entry_q.count() == 1: #replace data from most recent from SEPMirror entry = entry_q.first() entry.title = entries[key]['title'] entry.published = entries[key]['published'] entry.status = entries[key]['status'] #need to really add something here to delete entries no longer in the DB... entry_q2 = Session.query(SEPEntry) for entry in entry_q2: if not entries[entry.sep_dir]['title']: print str(entry.title) + " appears to have been deleted from SEPMirror; deleting from InPhO database." Session.delete(entry) Session.flush() Session.commit()
def process(self, id=None): if not h.auth.is_logged_in(): abort(401) if not h.auth.is_admin(): abort(403) c.sepdirnew = False c.alreadysepdir = False label = request.params.get('label', None) id = request.params.get('ID', id) sep_dir = request.params.get('sep_dir', None) URL = request.params.get('URL', None) language = request.params.get('language', None) queries = [request.params.get('queries', None)] openAccess = request.params.get('openAccess', None) active = request.params.get('active', None) student = request.params.get('student', None) ISSN = request.params.get('ISSN', None) action = request.params.get('action', None) action2 = request.params.get('action2', None) if action2: action = action2 values = dict(request.params) #abbrs = [request.params.get('abbrs', None)] abbrs = [] queries = [] for k, v in values.items(): key = "" if k.startswith('abbrs'): varname, num = k.split('.') key = 'delabbr.%s'%(num) keyval = request.params.get(key, False) if not keyval: abbrs.append(v) elif k.startswith('queries'): varname, num = k.split('.') key = 'delquer.%s'%(num) keyval = request.params.get(key, False) if not keyval: queries.append(v) if action=="Add": journal_add = Journal() journal_add.label = label #setup search string and search pattern journalname = journal_add.label journalname_q = Session.query(Entity) o = Entity.searchpattern.like('( '+ journalname + ' )') journalname_q = journalname_q.filter(o).order_by(func.length(Entity.label)) if journalname_q.count() == 0: journal_add.searchpattern = "( " + journalname + " )" journal_add.searchstring = journalname else: journal_add.searchpattern = "( " + label + " )" journal_add.searchcstring = label #reset old journal pattern to whole name too to avoid conflict oldjournal = h.fetch_obj(Journal, journalname_q.first().ID) oldjournal.searchpattern = "( " + oldjournal.label + " )" oldjournal.searchstring = oldjournal.label Session.add(oldjournal) if sep_dir: journal_add.sep_dir = sep_dir c.journal = journal_add Session.add(journal_add) Session.flush() Session.commit() c.found = True c.message = "Journal " + c.journal.label + " added successfully." return render ('admin/journal-edit.html') elif action=="Modify": c.journal = h.fetch_obj(Journal, id) c.found = True changed = False #set values from form if c.journal.label != label: c.journal.label = label changed = True if c.journal.sep_dir != sep_dir: c.journal.sep_dir = sep_dir changed = True if c.journal.URL != URL: c.journal.URL = URL changed = True if c.journal.language != language: c.journal.language = language changed = True if c.journal.abbrs != abbrs: c.journal.abbrs = abbrs changed = True if c.journal.queries != queries: c.journal.queries = queries changed = True if c.journal.openAccess != openAccess: c.journal.openAccess = openAccess changed = True if c.journal.active != active: c.journal.active = active changed = True if c.journal.student != student: c.journal.student = student changed = True if c.journal.ISSN != ISSN: c.journal.ISSN = ISSN changed = True if values['newabbr']: c.journal.abbrs.append(values['newabbr']) changed = True if values['newquery']: c.journal.queries.append(values['newquery']) changed = True #commit changes Session.flush() Session.commit() if changed: c.message = "Journal " + c.journal.label + " modified successfully." else: c.message = "No change required; Journal " + c.journal.label + " not modified." return render ('admin/journal-edit.html') elif action == "Delete": c.journal = h.fetch_obj(Journal, values['ID']) c.message = "Journal # " + values['ID'] + " ("+ c.journal.label + ") deleted; please search for a new entity label on the left." h.delete_obj(c.journal) Session.flush() Session.commit() c.found = False return render('admin/journal-edit.html')
def getentries(db_dir): #to do feb 10: do something more elegant than just delete existing and rewrite...perhaps only write new? #first, clear out the current table #Session.query(SEPEntry).delete() #Session.flush() #Session.commit() entries = defaultdict(lambda: { 'title': '', 'published': False, 'status': '' }) pars = HTMLParser.HTMLParser() #get published entries published = open('/Users/inpho/SitesOld/dev/entries-published.txt') status = open(os.path.join(db_dir, 'entrystatus.txt')) entrylist = open(os.path.join(db_dir, 'entries.txt')) #set up entries dict for line in entrylist: line = line.split('::') sep_dir = pars.unescape(re.sub('<(/)?[a-zA-Z]*>', '', line[0])) title = re.sub("\\\\\'", "'", pars.unescape(re.sub('<(/)?[a-zA-Z]*>', '', line[1]))) entries[sep_dir]['title'] = title for line in published: line = re.sub('\\n', '', line) if entries[line]['title']: entries[line]['published'] = True else: print "uh-oh, " + line + "doesn't appear to be in dict object" for line in status: line = line.split('::') if entries[line[0]]['title']: entries[line[0]]['status'] = line[1] #print "status = " + line[1] + ' for ' + line[0] else: print "uh-oh, " + line[0] + "doesn't appear to be in dict object" for key in entries.keys(): #so, what I should really do here is figure out whether the entry already has a place in the table; if so, update its existing stats; if not, #insert a new one; #also need to check if old entries in sepentries table are no longer in file... entry_q = Session.query(SEPEntry) o = SEPEntry.title.like(entries[key]['title']) entry_q = entry_q.filter(o) # if only 1 result, go ahead and view that idea if entry_q.count() == 0: entry_add = SEPEntry(entries[key]['title'], key, entries[key]['published'], entries[key]['status']) Session.add(entry_add) elif entry_q.count() == 1: #replace data from most recent from SEPMirror entry = entry_q.first() entry.title = entries[key]['title'] entry.published = entries[key]['published'] entry.status = entries[key]['status'] #need to really add something here to delete entries no longer in the DB... entry_q2 = Session.query(SEPEntry) for entry in entry_q2: if not entries[entry.sep_dir]['title']: print str( entry.title ) + " appears to have been deleted from SEPMirror; deleting from InPhO database." Session.delete(entry) Session.flush() Session.commit()
def admin(self, id=None): if not h.auth.is_logged_in(): abort(401) if not h.auth.is_admin(): abort(403) redirect = request.params.get('redirect', False) add = request.params.get('add', False) limit = request.params.get('limit', None) sep_dir = request.params.get('sep_dir', "") entity_q = Session.query(model.Entity) c.found = False c.custom = False c.new = False c.sep_dir = sep_dir c.sepdirnew = False c.alreadysepdir = False if request.params.get('q'): q = request.params['q'] o = model.Entity.label.like(q) entity_q = entity_q.filter(o).order_by( func.length(model.Entity.label)) # if only 1 result, go ahead and view that entity if redirect and entity_q.count() == 1: c.entity = h.fetch_obj(model.Entity, entity_q.first().ID) #now get type and route to correct edit page #first, if it is an idea, typeID = 1 if c.entity.typeID == 1: print "have an idea, q, entityq count = 1" c.idea = h.fetch_obj(model.Idea, entity_q.first().ID) c.found = True id = c.idea.ID c.message = 'Entity edit page for idea ' + c.idea.label #set up c.search_string_list which will be used to structure intersection/union search pattern option if request.params.get('sep_dir'): sep_dir = request.params['sep_dir'] if not (c.idea.sep_dir): c.idea.sep_dir = request.params['sep_dir'] c.sepdirnew = True else: c.alreadysepdir = True c.entry_sep_dir = request.params['sep_dir'] c.search_string_list = c.idea.setup_SSL() if re.search(' and ', c.idea.label): c.search_pattern_list = ['union', 'intersection'] return render('admin/idea-edit.html') #thinkers elif c.entity.typeID == 3: print "have a thinker, q, entityq count = 1" c.thinker = h.fetch_obj(model.Thinker, entity_q.first().ID) c.found = True id = c.thinker.ID c.message = 'Entity edit page for thinker ' + c.thinker.label if request.params.get('sep_dir'): sep_dir = request.params['sep_dir'] if not (c.thinker.sep_dir): c.thinker.sep_dir = request.params['sep_dir'] c.sepdirnew = True else: c.alreadysepdir = True c.entry_sep_dir = request.params['sep_dir'] return render('admin/thinker-edit.html') elif c.entity.typeID == 4: print "have a journal, q, entityq count = 1" c.journal = h.fetch_obj(model.Journal, entity_q.first().ID) c.found = True id = c.journal.ID c.message = 'Entity edit page for journal ' + c.journal.label if request.params.get('sep_dir'): sep_dir = request.params['sep_dir'] if not (c.journal.sep_dir): c.journal.sep_dir = request.params['sep_dir'] c.sepdirnew = True else: c.alreadysepdir = True c.entry_sep_dir = request.params['sep_dir'] return render('admin/journal-edit.html') elif c.entity.typeID == 5: print "have a work, q, entityq count = 1" c.work = h.fetch_obj(model.Work, entity_q.first().ID) c.found = True id = c.work.ID c.message = 'Entity edit page for work ' + c.work.label if request.params.get('sep_dir'): sep_dir = request.params['sep_dir'] if not (c.work.sep_dir): c.work.sep_dir = request.params['sep_dir'] c.sepdirnew = True else: c.alreadysepdir = True c.entry_sep_dir = request.params['sep_dir'] return render('admin/work-edit.html') elif c.entity.typeID == 6: print "have a school_of_thought, q, entityq count = 1" c.school_of_thought = h.fetch_obj(model.SchoolOfThought, entity_q.first().ID) c.found = True id = c.school_of_thought.ID c.message = 'Entity edit page for school_of_thought ' + c.school_of_thought.label if request.params.get('sep_dir'): sep_dir = request.params['sep_dir'] if not (c.school_of_thought.sep_dir): c.school_of_thought.sep_dir = request.params[ 'sep_dir'] c.sepdirnew = True else: c.alreadysepdir = True c.entry_sep_dir = request.params['sep_dir'] return render('admin/school_of_thought-edit.html') elif redirect and entity_q.count() == 0: c.message = "No match found for your search; if you would like to add your idea, please enter its label and sepdir into the field below." c.new = True c.prevvalue = q return render('admin/entity-add.html') else: return ( 'No exact match for your search; please click "back" and try again.' ) if id is None: print "I am here" c.message = "Please input an idea using the search bar to the left." return render('admin/idea-edit.html') else: c.entity = h.fetch_obj(model.Entity, id) c.found = True c.message = 'Entity edit page for entity ' + c.entity.label #get sep_dir if present--from admin.py action addentry if request.params.get('sep_dir'): sep_dir = request.params['sep_dir'] if not (c.entity.sep_dir): c.entity.sep_dir = sep_dir else: c.message = c.message + "WARNING: entity already has a sep_dir [" + c.entity.sep_dir + "]. Not replacing with [" + sep_dir + "]. If you would like to do so, please do so manually in the form below." if request.params.get('entry_sep_dir'): entry_sep_dir = request.params['entry_sep_dir'] if not (c.entity.sep_dir): c.entity.sep_dir = entry_sep_dir else: c.message = c.message + "WARNING: entity already has a sep_dir [" + c.entity.sep_dir + "]. Not replacing with [" + sep_dir + "]. If you would like to do so, please do so manually in the form below." #set up c.search_string_list which will be used to structure intersection/union search pattern option if c.entity.typeID == 1: c.idea = h.fetch_obj(model.Idea, c.entity.ID) c.search_string_list = c.idea.setup_SSL() return render('admin/idea-edit.html') elif c.entity.typeID == 3: c.thinker = h.fetch_obj(model.Thinker, c.entity.ID) return render('admin/thinker-edit.html') elif c.entity.typeID == 4: c.journal = h.fetch_obj(model.Journal, c.entity.ID) return render('admin/journal-edit.html') elif c.entity.typeID == 5: c.work = h.fetch_obj(model.Work, c.entity.ID) return render('admin/work-edit.html') elif c.entity.typeID == 6: c.school_of_thought = h.fetch_obj(model.SchoolOfThought, c.entity.ID) return render('admin/school_of_thought-edit.html') c.entity = h.fetch_obj(model.Entity, id, new_id=True) redirect(c.entity.url(action='admin'), code=303)
def admin(self, id=None): if not h.auth.is_logged_in(): abort(401) if not h.auth.is_admin(): abort(403) c.sepdirnew = False c.alreadysepdir = False redirect = request.params.get('redirect', False) add = request.params.get('add', False) limit = request.params.get('limit', None) entity_q = Session.query(Entity) c.found = False c.custom = False c.new = False if request.params.get('q'): q = request.params['q'] o = Entity.label.like(q) entity_q = entity_q.filter(o).order_by(func.length(Entity.label)) # if only 1 result, go ahead and view that idea if redirect and entity_q.count() == 1: print "have a q, entityq count = 1" c.idea = h.fetch_obj(Idea, entity_q.first().ID) c.found = True id = c.idea.ID c.message = 'Entity edit page for idea ' + c.idea.label if request.params.get('entry_sep_dir'): entry_sep_dir = request.params['entry_sep_dir'] if not (c.idea.sep_dir): c.idea.sep_dir = request.params['entry_sep_dir'] c.sepdirnew = True else: c.alreadysepdir = True c.entry_sep_dir = request.params['entry_sep_dir'] #set up c.search_string_list which will be used to structure intersection/union search pattern option c.search_string_list = c.idea.setup_SSL() if re.search(' and ', c.idea.label): c.search_pattern_list = ['union', 'intersection'] return render ('admin/idea-edit.html') elif redirect and entity_q.count() == 0: c.message = "No match found for your search; if you would like to add your idea, please enter its label and sepdir into the field below." c.new = True c.prevvalue = q return render ('admin/idea-edit.html') else: return ('No exact match for your search; please click "back" and try again.') if id is None: print "I am here" c.message = "Please input an idea using the search bar to the left." return render ('admin/idea-edit.html') else: c.idea = h.fetch_obj(Idea, id) c.found = True c.message = 'Entity edit page for idea ' + c.idea.label if request.params.get('entry_sep_dir'): entry_sep_dir = request.params['entry_sep_dir'] if not (c.idea.sep_dir): c.idea.sep_dir = request.params['entry_sep_dir'] c.sepdirnew = True else: c.alreadysepdir = True c.entry_sep_dir = request.params['entry_sep_dir'] #set up c.search_string_list which will be used to structure intersection/union search pattern option c.search_string_list = c.idea.setup_SSL() print "c.idea.searchpattern is " + c.idea.searchpattern return render ('admin/idea-edit.html')
def process(self): if not h.auth.is_logged_in(): abort(401) if not h.auth.is_admin(): abort(403) c.sepdirnew = False c.alreadysepdir = False action = request.params.get('action', None) action2 = request.params.get('action2', None) if action2: action = action2 q = request.params.get('label', None) label = q searchpattern = request.params.get('searchpattern', None) searchstring = request.params.get('searchstring', None) sep_dir = request.params.get('sep_dir', None) ioru = request.params.get('ioru', 'u') print "***" print "sep_dir is " print sep_dir print "and label is " print q print "***" values = dict(request.params) c.found = values.get('found', False) c.custom = values.get('custom', False) c.new = values.get('new', False) #ADD ACTION -- add a new entity (id/label cannot already exist) if action=='Add': #lowercase label and check to see whether it is valid #if so commit idea with standard pluralizations as searchpattern and give user a message that add was successful q = q.lower() #check to see whether label already exists idea_q = Session.query(Idea) # Check for query o = Idea.label.like(q) idea_q = idea_q.filter(o) # if 0 results, proceed to add idea if idea_q.count() == 0: print "***action add, idea q count == 0" #if no exact match for label, create new object with that label to add idea_add = Idea(q) #add searchstring = label idea_add.searchstring = q #add pluralizations to existing searchpatterns idea_add.searchpatterns = idea_add.pluralize() #setup the search string list c.idea = idea_add c.search_string_list = c.idea.setup_SSL() #setup sep_dir if present if sep_dir: c.idea.sep_dir = sep_dir #commit #take user to disambiguation page if the label contains an 'and' #otherwise, commit idea Session.add(idea_add) Session.flush() Session.commit() c.found = True something = 1 else: #already a match; give user error message that they should edit the preexisting page, and send them back to the submit idea #for edit page print "****action add, idea_q.count() not equal 0" c.message = "Idea with that name already exists in database; please edit the existing entry (returned below) or try a new label." c.idea = idea_q.first() c.search_string_list = c.idea.setup_SSL() return render('admin/idea-edit.html') c.message = "Idea added successfully." return render('admin/idea-edit.html') #Modify action -- edit an existing entry, id must exist elif action == 'Modify': #retrieve entity corresponding to id c.idea = h.fetch_obj(Idea, values['ID']) changed = False #generate searchpattern from searchstring if not already provided; current value stored in searchpattern, generate from searchstring if null #update parameters with form values if c.idea.sep_dir != values['sep_dir']: c.idea.sep_dir = values['sep_dir'] changed = True if c.idea.label != label: c.idea.label = label changed = True if not c.custom: if c.idea.searchstring != s.convertSS(searchstring, ioru)[0]: c.idea.searchstring = s.convertSS(searchstring, ioru)[0] #add default pluralizations to searchpatterns for label in c.idea.pluralize(): if label not in c.idea.searchpatterns: c.idea.searchpatterns.append(label) changed = True else: searchpatterns = [] for k, v in values.items(): key = "" if k.startswith('searchpatterns'): varname, num = k.split('.') key = 'delsearchpattern.%s'%(num) keyval = request.params.get(key, False) if not keyval: searchpatterns.append(v) if values['newsearchpattern']: searchpatterns.append(values['newsearchpattern']) changed = True #do manually edited searchpatterns first, so we don't write over them with the new default ones if the searchstring has been changed if c.idea.searchpatterns != searchpatterns: c.idea.searchpatterns = searchpatterns changed = True if c.idea.searchstring != searchstring: c.idea.searchstring = searchstring #add default pluralizations for new searchstring as well for label in c.idea.pluralize(): if label not in c.idea.searchpatterns: c.idea.searchpatterns.append(label) changed = True #make sure that searchpattern and searchstring are not shared by another entity #print "searchpatterns is " #print searchpatterns #print "c.idea.searchpatterns is" #print c.idea.searchpatterns #commit changes Session.flush() Session.commit() #return success message if changed == True: c.message = "Idea modified successfully." else: c.message = "No change required; idea not modified." c.found = True c.search_string_list = c.idea.setup_SSL() #prepare form return render('admin/idea-edit.html') #Delete action -- Delete an existing entity (id must exist) elif action=='Delete': c.idea = h.fetch_obj(Idea, values['ID']) c.message = "Idea # " + values['ID'] + " ("+ c.idea.label + ") deleted; please search for a new entity label on the left." h.delete_obj(c.idea) Session.flush() Session.commit() return render('admin/idea-edit.html') #use custom searchstring action elif action=='Use Custom Searchstring/Searchpattern': #toggle the c.custom to True and re-render form with htmlfill c.custom = True c.idea = h.fetch_obj(Idea, values['ID']) c.found = True c.message = 'Enter custom searchstring for ' + c.idea.label + "." return render('admin/idea-edit.html') #use standard searchstring action elif action=='Use Standard Searchstring/Searchpattern': #toggle the c.custom to False and re-render form with htmlfill c.custom = False c.idea = h.fetch_obj(Idea, values['ID']) c.found = True c.search_string_list = c.idea.setup_SSL() c.message = 'Now displaying standard searchstring options for ' + c.idea.label + "." return render('admin/idea-edit.html') else: raise Exception('I don\'t know how you got here, but you shouldn\'t be here...please start at the main page. Invalid action %s'%action)
def list(self, filetype='html'): redirect = request.params.get('redirect', False) limit = request.params.get('limit', None) idea_q = Session.query(Idea) c.query = '' c.sep = '' #c.nodes = Session.query(Node).filter(Node.parent_id == None).order_by("name").all() if request.params.get('sep_filter'): idea_q = idea_q.filter(Idea.sep_dir != '') if filetype=='json': response.content_type = 'application/json' # Check for query if request.params.get('q'): q = request.params['q'] c.query = q o = or_(Idea.label.like(q+'%'), Idea.label.like('% '+q+'%')) idea_q = idea_q.filter(o).order_by(Idea.entropy.desc()) # if only 1 result, go ahead and view that idea if redirect and idea_q.count() == 1: h.redirect(h.url(controller='idea', action='view', id=idea_q.first().ID,filetype=filetype)) else: c.ideas = idea_q.limit(limit) return render('idea/idea-list.' + filetype) #TODO: Error handling - we shouldn't have multiple results if request.params.get('sep'): idea_q = idea_q.filter(Idea.sep_dir == request.params['sep']) c.sep = request.params['sep'] # if only 1 result, go ahead and view that idea if redirect and idea_q.count() == 1: h.redirect(h.url(controller='idea', action='view', id=idea_q.first().ID,filetype=filetype)) elif idea_q.count() == 0: h.redirect(h.url(controller='entity', action='list', filetype=filetype, sep=request.params['sep'], redirect=redirect)) else: c.ideas = idea_q.limit(limit) return render('idea/idea-list.' + filetype) all_param = request.params.get('all', False) node_param = request.params.get('nodes', True) instance_param = request.params.get('instances', True) node_q = idea_q.join((Node,Node.concept_id==Idea.ID)) instance_q = idea_q.join(Instance.idea) if all_param: idea_q = idea_q if not node_param: idea_q = idea_q.except_(node_q) if not instance_param: idea_q = idea_q.except_(instance_q) elif node_param: idea_q = node_q if instance_param: idea_q = idea_q.union(instance_q) elif instance_param: idea_q = instance_q c.ideas = idea_q.limit(limit) return render('idea/idea-list.' + filetype)
def fuzzymatchall(SEPEntrieslist): #takes outputs from addlist() and saves all fuzzy match IDs to SEPEntry.fuzzymatch with verdicts (percent of words matched) #now change so that it only updates ones that don't currently have a fuzzymatchlist #clear out fuzzymatch table--otherwise old fuzzies will accumulate, and nobody wants that delquery = Session.query(Fuzzymatch) delquery.delete() Session.flush() Session.commit() for SEPEntry in SEPEntrieslist: print "working on " + SEPEntry.title.encode('utf-8') + "\n" entities = Session.query(Entity) #exclude journals and nodes from fuzzy matching entities = entities.filter(Entity.typeID != 2) entities = entities.filter(Entity.typeID != 4) #reset fuzzymatches for that entry #SEPEntry.fuzzymatches = "" ##string1 = string1.decode('utf8') for entity in entities: php = PHP("set_include_path('/usr/lib/php/');") php = PHP("require 'fuzzymatch.php';") #php = PHP() #print "testing " + entity.label.encode('utf8') + " against " + string1.encode('utf8') + "\n" code = '$string1 = utf8_decode("' + SEPEntry.title.encode('utf8') + '");' #code = code + "$string2 = '" + entity.label.encode('latin-1', 'replace') + "';" #code = code + "print $string1; print $string2;" #print code + '$string2 = utf8_decode("' + entity.label.encode('utf8') + '");' code = code + '$string2 = utf8_decode("' + entity.label.encode('utf8') + '");' code = code + """print fuzzy_match($string1, $string2, 2);""" verdict = php.get_raw(code) #print "verdict is " + verdict + "\n" verdict = verdict.split(',') if float(verdict[0])>=.20: #print entity.label + " is a match!\n" #entity.matchvalue = verdict #string = SEPEntry.fuzzymatches + "|" + str(entity.ID) + "," + verdict #if len(string) < 400: # SEPEntry.fuzzymatches = SEPEntry.fuzzymatches + "|" + str(entity.ID) + "," + verdict #else: # print "sorry, too many matches! Can't add " + str(entity.ID) + " to fuzzy matches; over 400 chars." fmatch = Fuzzymatch(entity.ID) fmatch.sep_dir = SEPEntry.sep_dir fmatch.strength = verdict[0] fmatch.edits = verdict[1] SEPEntry.fmatches.append(fmatch) Session.flush() Session.commit()