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(403) 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 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) response.headers['Access-Control-Allow-Origin'] = '*' return render('idea/idea-edit.html')
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 _to_python(self, value, state): user = h.get_user(value) if not user: raise formencode.Invalid( 'No user is registered under the username or email %s.'%value, value, state) return value
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 _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 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 _delete_evaluation(self, evaltype, id, id2, uid=None, username=None): id2 = request.params.get('id2', id2) uid = request.params.get('uid', uid) username = request.params.get('username', username) # look for a specific user's feedback evaluation = self._get_evaluation(evaltype, id, id2, uid, username, autoCreate=False) # if that feedback does not exist, unleash the nuclear option and delete # ALL evaluation facts for this relation, wiping it from the database. if h.auth.is_admin() and not evaluation: eval_q = Session.query(evaltype) eval_q = eval_q.filter_by(ante_id=id, cons_id=id2) evals = eval_q.all() # wipe them out. all of them. for evaluation in evals: h.delete_obj(evaluation) # return ok, with how many were deleted response.status_int = 200 return "OK %d" % len(evals) elif not evaluation: abort(404) # simply return an error (not evaluated), if not admin current_uid = h.get_user(request.environ['REMOTE_USER']).ID if evaluation.uid != current_uid and not h.auth.is_admin(): abort(401) h.delete_obj(evaluation) response.status_int = 200 return "OK"
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 edit(self): '''Renders the registration form.''' if not h.auth.is_logged_in(): abort(401) c.user = h.get_user(request.environ['REMOTE_USER']) return render('account/edit.html')
def review(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.evaluations = ieq.filter(and_(IdeaEvaluation.uid==c.user.ID, or_(IdeaEvaluation.generality>-1, IdeaEvaluation.relatedness>-1))).all() return render('account/review.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 evaluation(self, id, id2): c.entity = h.fetch_obj(Idea, id) c.entity2 = h.fetch_obj(Entity, id2) if isinstance(c.entity2, Node): c.entity2 = c.entity2.idea id2 = c.entity2.ID if not isinstance(c.entity2, Idea): # no evaluation implemented response.status_int = 501 return '' c.edit = True c.alert = request.params.get('alert', True) # retrieve evaluation for pair c.generality = int(request.params.get('generality', -1)) c.relatedness = int(request.params.get('relatedness', -1)) # retrieve user information identity = request.environ.get('repoze.who.identity') c.uid = None if not identity else identity['user'].ID #TODO: Place cookie auth here try: cookie = request.params.get('cookieAuth', 'null') username = h.auth.get_username_from_cookie(cookie) or '' user = h.get_user(username) if user is not None: c.uid = user.ID except ValueError: # invalid IP, abort abort(403) # use the user's evaluation if present, otherwise a null eval if c.uid and (c.generality == -1 or c.relatedness == -1): eval_q = Session.query(IdeaEvaluation.generality, IdeaEvaluation.relatedness) eval_q = eval_q.filter_by(uid=c.uid, ante_id=id, cons_id=id2) c.generality, c.relatedness = eval_q.first() or\ (int(request.params.get('generality', -1)), int(request.params.get('relatedness', -1))) if c.relatedness != -1: c.edit = request.params.get('edit', False) return render('idea/eval.html')
def review(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.evaluations = ieq.filter( and_( IdeaEvaluation.uid == c.user.ID, or_(IdeaEvaluation.generality > -1, IdeaEvaluation.relatedness > -1))).all() return render('account/review.html')
def _delete_evaluation(self, evaltype, id, id2, uid=None, username=None): 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, autoCreate=False) if not evaluation: abort(404) current_uid = h.get_user(request.environ['REMOTE_USER']).ID if evaluation.uid != current_uid and not h.auth.is_admin(): abort(401) h.delete_obj(evaluation) response.status_int = 200 return "OK"
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')