def submit(self): ''' This function validates the submitted registration form and creates a new user. Restricted to ``POST`` requests. If successful, redirects to the result action to prevent resubmission. ''' user = User( self.form_result['username'], self.form_result['password'], email=self.form_result['email'], first_area_id=self.form_result['first_area'], first_area_level=self.form_result['first_area_level'], second_area_id=self.form_result['second_area'], second_area_level=self.form_result['second_area_level'] ) Session.add(user) Session.commit() msg = Message("*****@*****.**", self.form_result['email'], "InPhO registration") msg.plain = """%s, thank you for registering with the Indiana Philosophy Ontology Project (InPhO). You can access your """ % self.form_result['username'] msg.send() h.redirect(h.url(controller='account', action='result'))
def submit(self): ''' This function validates the submitted registration form and creates a new user. Restricted to ``POST`` requests. If successful, redirects to the result action to prevent resubmission. ''' user = User(self.form_result['username'], self.form_result['password'], email=self.form_result['email'], first_area_id=self.form_result['first_area'], first_area_level=self.form_result['first_area_level'], second_area_id=self.form_result['second_area'], second_area_level=self.form_result['second_area_level']) Session.add(user) Session.commit() msg = Message("*****@*****.**", self.form_result['email'], "InPhO registration") msg.plain = """%s, thank you for registering with the Indiana Philosophy Ontology Project (InPhO). You can access your """ % self.form_result[ 'username'] msg.send() h.redirect(h.url(controller='account', action='result'))
def _evaluate(self, evaltype, id, id2=None, uid=None, username=None, degree=-1, maxdegree=4): """ Function to submit an evaluation. Takes a POST request containing the consequesnt id and all or none of: generality, relatedness, hyperrank, hyporank. """ if not h.auth.is_logged_in(): abort(401) id2 = request.params.get('id2', id2) uid = request.params.get('uid', uid) username = request.environ.get('REMOTE_USER', username) print "grabbing eval for", username, uid if request.environ.get('REMOTE_USER', False): evaluation = self._get_evaluation(id, id2, None, username) else: evaluation = self._get_anon_evaluation(id, id2, request.environ.get('REMOTE_ADDR', '0.0.0.0')) # Populate proper generality, relatedness, hyperrank and hyporank values # Attempt to convert to integers, if unable, throw HTTP 400 try: setattr(evaluation, evaltype, int(request.params.get('degree', getattr(evaluation, evaltype)))) except TypeError: abort(400) # Create and commit evaluation Session.flush() Session.commit() # Issue an HTTP success response.status_int = 200 return "OK"
def _delete_evaluation(self, evaltype, id, id2, uid=None, username=None): if not h.auth.is_logged_in(): abort(401) id2 = request.params.get('id2', id2) uid = request.params.get('uid', uid) username = request.params.get('username', username) evaluation = self._get_evaluation(id, id2, uid, username, autoCreate=False) if not evaluation: abort(404) current_uid = h.get_user(request.environ['REMOTE_USER']).ID if evaluation.uid != current_uid or not h.auth.is_admin(): abort(401) setattr(evaluation, evaltype, -1) # Delete evaluation if this eliminates both settings, new db schema # will eliminate this need if evaluation.generality == -1 and evaluation.relatedness == -1: h.delete_obj(evaluation) else: # save change in evaluation Session.flush() Session.commit() response.status_int = 200 return "OK"
def create(self): if not h.auth.is_logged_in(): abort(401) if not h.auth.is_admin(): abort(403) valid_params = [ "ISSN", "noesisInclude", "URL", "source", "abbr", "language", "student", "active" ] params = request.params.mixed() if '_method' in params: del params['_method'] if 'name' in params: name = params['name'] del params['name'] else: abort(400) for k in params.keys(): if k not in valid_params: abort(400) journal = Journal(name, **params) Session.add(journal) Session.flush() # Issue an HTTP success response.status_int = 302 response.headers['location'] = h.url(controller='journal', action='view', id=journal.ID) return "Moved temporarily"
def create(self): if not h.auth.is_logged_in(): abort(401) if not h.auth.is_admin(): abort(403) valid_params = ["sep_dir", "wiki"] params = request.params.mixed() if '_method' in params: del params['_method'] if 'name' in params: name = params['name'] del params['name'] else: abort(400) for k in params.keys(): if k not in valid_params: abort(400) thinker = Thinker(name, **params) Session.add(thinker) Session.flush() # Issue an HTTP success response.status_int = 302 response.headers['location'] = h.url(controller='thinker', action='view', id=thinker.ID) return "Moved temporarily"
def create(self): if not h.auth.is_logged_in(): abort(401) if not h.auth.is_admin(): abort(403) valid_params = ["sep_dir", "searchstring", "searchpattern"] params = request.params.mixed() if '_method' in params: del params['_method'] if 'label' in params: label = params['label'] del params['label'] else: abort(400) for k in params.keys(): if k not in valid_params: abort(400) idea = Idea(label) Session.add(idea) Session.flush() # Issue an HTTP success response.status_int = 302 response.headers['location'] = h.url(controller='idea', action='view', id=idea.ID) return "Moved temporarily"
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 _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 _reset(self, username=None): username = username or request.environ.get('REMOTE_USER', False) if not username: abort(401) try: user = h.get_user(username) except: abort(400) new_password = user.reset_password() msg = Message("*****@*****.**", user.email, "InPhO password reset") msg.plain = """ %(name)s, your password at the Indiana Philosophy Ontology (InPhO) has been changed to: Username: %(uname)s Password: %(passwd)s The Indiana Philosophy Ontology (InPhO) Team [email protected] """ % {'passwd' : new_password, 'uname' : user.username, 'name' : user.fullname or user.username or ''} msg.send() Session.commit() h.redirect(h.url(controller='account', action='reset_result'))
def submit_changes(self): ''' This function validates the submitted profile edit form and commits the changes. Restricted to ``POST`` requests. If successful, redirects to the result action to prevent resubmission. ''' if not h.auth.is_logged_in(): abort(401) c.user = h.get_user(request.environ['REMOTE_USER']) if self.form_result['password'] != '': c.user.set_password(self.form_result['password']) # TODO: Enable area editing #c.user.first_area_id=self.form_result['first_area'], #user.first_area_level=self.form_result['first_area_level'], #if self.form_result['second_area']: # c.user.second_area_id=self.form_result['second_area'], # c.user.second_area_level=self.form_result['second_area_level'] c.user.fullname = self.form_result['fullname'] Session.flush() Session.commit() h.redirect(h.url(controller='account', action='profile', message='edited'))
def _thinker_evaluate(self, evaltype=None, id=None, id2=None, uid=None, username=None, degree=1, maxdegree=1): """ Private method to handle generic evaluations. See ``teacher_of`` and ``has_influenced`` for use. """ id2 = request.params.get('id2', id2) uid = request.params.get('uid', uid) username = request.params.get('username', username) evaluation = self._get_evaluation(evaltype, id, id2, uid, username) try: evaluation.degree = int(request.params.get('degree', degree)) except TypeError: abort(400) # Create and commit evaluation Session.flush() # Issue an HTTP success response.status_int = 200 return "OK"
def _delete_evaluation(self, evaltype, id, id2, uid=None, username=None): if not h.auth.is_logged_in(): abort(401) id2 = request.params.get('id2', id2) uid = request.params.get('uid', uid) username = request.params.get('username', username) evaluation = self._get_evaluation(id, id2, uid, username, autoCreate=False) if not evaluation: abort(404) current_uid = h.get_user(request.environ['REMOTE_USER']).ID if evaluation.uid != current_uid or not h.auth.is_admin(): abort(401) setattr(evaluation, evaltype, -1) # Delete evaluation if this eliminates both settings, new db schema # will eliminate this need #if evaluation.generality == -1 and evaluation.relatedness == -1: # h.delete_obj(evaluation) Session.flush() Session.commit() response.status_int = 200 return "OK"
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 create(self): if not h.auth.is_logged_in(): abort(401) if not h.auth.is_admin(): abort(403) valid_params = ["ISSN", "noesisInclude", "URL", "source", "abbr", "language", "student", "active"] params = request.params.mixed() if '_method' in params: del params['_method'] if 'name' in params: name = params['name'] del params['name'] else: abort(400) for k in params.keys(): if k not in valid_params: abort(400) journal = Journal(name, **params) Session.add(journal) Session.flush() # Issue an HTTP success response.status_int = 302 response.headers['location'] = h.url(controller='journal', action='view', id=journal.ID) return "Moved temporarily"
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 delete_obj(obj): """ Deletes any arbitrary object from the SQLAlchemy Session and cascades deletes to evaluations. :param obj: object to delete """ Session.delete(obj) Session.flush()
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 _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 _delete_unary(self, type, id, id2=None): thinker = h.fetch_obj(Thinker, id) id2 = request.params.get('id2', id2) obj = h.fetch_obj(unary_vars[type]['object'], id2) if obj in getattr(thinker, unary_vars[type]['property']): getattr(thinker, unary_vars[type]['property']).remove(obj) Session.commit() response.status_int = 200 return "OK"
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 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 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 != '') 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 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 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 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 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 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 update_obj(obj, attributes, params): """ Updates any arbitrary object. Takes a list of attributes and a dictionary of update parameters. Checks if each key is in the list of approved attributes and then attempts to set it. If the object does not have that key, throw an HTTP 400 Bad Request :param obj: object to update :param attributes: list of approved attributes :param params: dictionary of update parameters """ for key in params.keys(): if key in attributes: try: set_attribute(obj, key, params[key]) except: abort(400) Session.flush()
def submit(self): ''' This function validates the submitted registration form and creates a new user. Restricted to ``POST`` requests. If successful, redirects to the result action to prevent resubmission. ''' user = User( self.form_result['username'], fullname=self.form_result['fullname'], email=self.form_result['email'], first_area_id=self.form_result['first_area'], first_area_level=self.form_result['first_area_level'], second_area_id=self.form_result['second_area'], second_area_level=self.form_result['second_area_level'] ) Session.add(user) password = user.reset_password() Session.commit() msg = Message("*****@*****.**", self.form_result['email'], "InPhO registration") msg.plain = """Dear %(name)s, Thank you for registering with the Indiana Philosophy Ontology Project (InPhO). You can sign in at https://inpho.cogs.indiana.edu/signin with the following information: Username: %(uname)s Password: %(passwd)s You may change your password at https://inpho.cogs.indiana.edu/account/edit . The Indiana Philosophy Ontology Project (InPhO) Team [email protected] """ % {'passwd' : password, 'uname' : user.username, 'name' : user.fullname or user.username or ''} msg.send() h.redirect(h.url(controller='account', action='result'))
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 _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 _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_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 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 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'): 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 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 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')