def save_statements(self, statements): """Save statements to LRS and update their statement id's :param statements: A list of statement objects to be saved :type statements: :class:`StatementList` :return: LRS Response object with the saved list of statements as content :rtype: :class:`tincan.lrs_response.LRSResponse` """ if not isinstance(statements, StatementList): statements = StatementList(statements) request = HTTPRequest(method="POST", resource="statements") request.headers["Content-Type"] = "application/json" request.content = statements.to_json() lrs_response = self._send_request(request) if lrs_response.success: id_list = json.loads(lrs_response.data) for s, statement_id in zip(statements, id_list): s.id = statement_id lrs_response.content = statements return lrs_response
def save_statements(self, statements): """Save statements to LRS and update their statement id's :param statements: A list of statement objects to be saved :type statements: :class:`StatementList` :return: LRS Response object with the saved list of statements as content :rtype: :class:`tincan.lrs_response.LRSResponse` """ if not isinstance(statements, StatementList): statements = StatementList(statements) request = HTTPRequest( method="POST", resource="statements" ) request.headers["Content-Type"] = "application/json" request.content = statements.to_json() lrs_response = self._send_request(request) if lrs_response.success: id_list = json.loads(lrs_response.data) for s, statement_id in zip(statements, id_list): s.id = statement_id lrs_response.content = statements return lrs_response
def statements(self, value): if value is None: self._statements = StatementList() return try: self._statements = StatementList(value) except Exception: raise TypeError(f"Property 'statements' in a 'tincan.{self.__class__.__name__}' object must be set with a " f"list or None." f"\n\n" f"Tried to set it with a '{value.__class__.__name__}' object: {repr(value)}" f"\n\n")
def statements(self, value): if value is None: self._statements = StatementList() return try: self._statements = StatementList(value) except Exception as e: msg = ( "Property 'statements' in a 'tincan.%s' object must be set with a " "list or None." "\n\n" "Tried to set it with a '%s' object: %s" "\n\n" % ( self.__class__.__name__, value.__class__.__name__, repr(value), )) msg += e.message raise TypeError(msg)
def test_save_statements(self): statement1 = Statement(actor=self.agent, verb=self.verb, object=self.activity) statement2 = Statement(actor=self.agent, verb=self.verb, object=self.activity, context=self.context) response = self.lrs.save_statements( StatementList([statement1, statement2])) self.assertIsInstance(response, LRSResponse) self.assertTrue(response.success) self.assertIsNotNone(response.content[0].id) self.assertIsNotNone(response.content[1].id) self._vars_verifier(statement1, response.content[0], ['_authority', '_stored']) self._vars_verifier(statement2, response.content[1], ['_authority', '_stored'])