def setUp(self): self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() ndb.get_context().set_cache_policy(False) self.story_uid = 'STORY' self.clue_uid = Clue.build_uid(self.story_uid, 'START') self.answer_uid = Answer.build_uid(self.story_uid, 'START', 'TRANSITION') Story.from_uid(self.story_uid, default_hint='default hint').put() Clue.from_uid(self.clue_uid, text='Start the story', hint='clue hint').put() Answer.from_uid( self.answer_uid, pattern=r'my answer is (?P<user_answer>\w+)', next_clue=self.clue_uid, ).put()
def _pre_delete_hook(cls, key): clue = cls.get_by_id(key.id()) ndb.delete_multi([ndb.Key('Answer', uid) for uid in clue.answer_uids]) story = Story.get_by_id(clue.story_uid) if story is None: return story.remove_clue(key.id()) story.put()
def get(self): stories = [s.to_dict() for s in Story.query().fetch()] clues = [c.to_dict() for c in Clue.query().fetch()] answers = [a.to_dict() for a in Answer.query().fetch()] self.request.headers['Content-Type'] = 'application/json' self.response.body = json.dumps({ 'stories': stories, 'clues': clues, 'answers': answers, }, indent=2)
def get_stories(): print("REQUEST ARGS") print(request.args) if request.method == "POST": req_data = request.get_json() summary = req_data["summary"] description = req_data["description"] story_type = req_data["story_type"] complexity = req_data["complexity"] estimated_time = req_data["estimated_time"] cost = float(req_data["cost"]) # created_at = Column(TIMESTAMP) created_by = request.args['user'] status = "DRAFT" # last_updated_at = Column(TIMESTAMP) # last_updated_by = Column(String) story = Story(summary=summary, description=description, story_type=story_type, complexity=complexity, estimated_time=estimated_time, cost=cost, created_by=created_by,status=status) session.add(story) session.commit() story_id = story.id return jsonify("story with id %s created."% (story_id)) elif request.method == "GET": #TODO handle scenario where username is not present in users table user = User.query.filter(User.username == request.args['user']).first() if user.user_type == 'admin': stories = Story.query.all() else: stories = Story.query.filter(Story.created_by == request.args['user']) res = [] for story in stories: res.append(story.to_dict()) return jsonify(res)
def setUp(self): super(TestScavenger, self).setUp() self.story = Story.from_uid('STORY', default_hint='default hint') self.story.put() self.story_code = StoryCode.from_words('salsa tacos', story_uid=self.story.uid) self.story_code.put() self.start_clue = Clue.from_uid(Clue.build_uid(self.story.uid, 'START'), text='Start the story', hint='clue hint') self.start_clue.put() self.next_clue = Clue.from_uid(Clue.build_uid(self.story.uid, 'NEXT'), text='You made it!', sender="+555") self.next_clue.put() self.answer = Answer.from_uid( Answer.build_uid('STORY', 'START', 'TRANSITION'), pattern=r'my answer is (?P<user_answer>\w+)', next_clue=self.next_clue.uid, ) self.answer.put()
def _pre_put_hook(self): story = Story.get_by_id(self.story_uid) if story is None: raise ValueError("A story doesn't exist for this clue") story.add_clue(self.uid) story.put()
def story(self): return Story.get_by_id(self.story_uid)
def test_deleting_story_deletes_answer(self): Story.build_key(self.story_uid).delete() self.assertIsNone(Answer.get_by_id(self.answer_uid))
def story(self): if not self.story_uid: return None return Story.get_by_id(self.story_uid)
def test_deleting_story_deletes_clue(self): Story.build_key(self.story_uid).delete() self.assertIsNone(Clue.get_by_id(self.clue_uid))