def assertionLookup(self, obj_url): """Method called to look up an assertion by its id number. Accessed by going to URL /api/assertion/{id}. Returns a serialized version of the assertion.""" try: return Assertion.get(obj_url.replace('/assertion/', '')).serialize() except DoesNotExist: return rc.NOT_FOUND except ValidationError: #raised if input is not a valid id return rc.NOT_FOUND
def assertionLookup(self, obj_url): """Method called to look up an assertion by its id number. Accessed by going to URL /api/assertion/{id}. Returns a serialized version of the assertion.""" try: return Assertion.get(obj_url.replace('/assertion/', '')).serialize() except DoesNotExist: return rc.NOT_FOUND except ValidationError: #raised if input is not a valid id return rc.NOT_FOUND
def assertionExpressionLookup(self, request, obj_url): assertionID = request.GET['id'] start = int(request.GET.get('start', '0')) limit = int(request.GET.get('limit', '10')) #NOTE: should return ranked by confidence score. For now assume that they do. try: assertion = Assertion.get(assertionID) except DoesNotExist: return rc.NOT_FOUND cursor = Expression.objects(assertion = assertion)[start:start + limit] expressions = [] while (True): try: expressions.append(str(cursor.next().id)) except StopIteration: break #no more assertions within the skip/limit boundaries if len(expressions) == 0: #no assertions were found for the concept return rc.NOT_FOUND return "{expressions: " + str(expressions) + "}"
def assertionExpressionLookup(self, request, obj_url): assertionID = request.GET['id'] start = int(request.GET.get('start', '0')) limit = int(request.GET.get('limit', '10')) #NOTE: should return ranked by confidence score. For now assume that they do. try: assertion = Assertion.get(assertionID) except DoesNotExist: return rc.NOT_FOUND cursor = Expression.objects(assertion=assertion)[start:start + limit] expressions = [] while (True): try: expressions.append(str(cursor.next().id)) except StopIteration: break #no more assertions within the skip/limit boundaries if len(expressions) == 0: #no assertions were found for the concept return rc.NOT_FOUND return "{expressions: " + str(expressions) + "}"
def assertionVote(self, request, obj_url): """Assertion vote is called whenever someone is voting on an assertion. It can be accessed in one of 2 ways: voting on an assertion identified directly by its id or voting on an assertion identified by its unique attributes. To add a positive vote, the user should make vote=1. A negative vote is vote=-1. Any other values will result in no action being taken. Can be accessed through either of the following URLS: /api/assertionvote?dataset={dataset}&rel={relation}&concept={concept1,concept2,etc} &polarity={polarity}&context={context}&vote={vote}&user={username}&password={password} polarity and context are optional values, defaulting to polarity = 1 and context = None /api/assertionidvote?id={id}&vote={vote} """ user = request.POST['user'] password = request.POST['password'] if obj_url.startswith('/assertionvote'): dataset = request.POST['dataset'] relation = request.POST['rel'] argstr = request.POST['concepts'] polarity = int(request.POST.get('polarity','1')) context = request.POST.get('context','None') if context == "None": context = None try: assertion = Assertion.objects.get( dataset = dataset, relation = relation, argstr = argstr, polarity = polarity, context = context) except DoesNotExist: return rc.NOT_FOUND else: id = request.POST['id'] try: assertion = Assertion.get(id) dataset = assertion.dataset except DoesNotExist: return rc.NOT_FOUND if User.objects.get(username=user).check_password(password): #the user's password is correct. Get their reason and add try: ReasonConjunction.objects.get(target = dataset + '/contributor/' + user) except DoesNotExist: return rc.FORBIDDEN else: #incorrect password return rc.FORBIDDEN vote = request.POST['vote'] if vote == "1": #vote in favor assertion.add_support([dataset + '/contributor/' + user]) elif vote == "-1": #vote against assertion.add_oppose([dataset + '/contributor/' + user]) else: #invalid vote return rc.BAD_REQUEST return assertion.serialize()
def assertionVote(self, request, obj_url): """Assertion vote is called whenever someone is voting on an assertion. It can be accessed in one of 2 ways: voting on an assertion identified directly by its id or voting on an assertion identified by its unique attributes. To add a positive vote, the user should make vote=1. A negative vote is vote=-1. Any other values will result in no action being taken. Can be accessed through either of the following URLS: /api/assertionvote?dataset={dataset}&rel={relation}&concept={concept1,concept2,etc} &polarity={polarity}&context={context}&vote={vote}&user={username}&password={password} polarity and context are optional values, defaulting to polarity = 1 and context = None /api/assertionidvote?id={id}&vote={vote} """ user = request.POST['user'] password = request.POST['password'] if obj_url.startswith('/assertionvote'): dataset = request.POST['dataset'] relation = request.POST['rel'] argstr = request.POST['concepts'] polarity = int(request.POST.get('polarity', '1')) context = request.POST.get('context', 'None') if context == "None": context = None try: assertion = Assertion.objects.get(dataset=dataset, relation=relation, argstr=argstr, polarity=polarity, context=context) except DoesNotExist: return rc.NOT_FOUND else: id = request.POST['id'] try: assertion = Assertion.get(id) dataset = assertion.dataset except DoesNotExist: return rc.NOT_FOUND if User.objects.get(username=user).check_password(password): #the user's password is correct. Get their reason and add try: ReasonConjunction.objects.get(target=dataset + '/contributor/' + user) except DoesNotExist: return rc.FORBIDDEN else: #incorrect password return rc.FORBIDDEN vote = request.POST['vote'] if vote == "1": #vote in favor assertion.add_support([dataset + '/contributor/' + user]) elif vote == "-1": #vote against assertion.add_oppose([dataset + '/contributor/' + user]) else: #invalid vote return rc.BAD_REQUEST return assertion.serialize()