def post(self, id): idea = Idea.get_by_id(int(id)) if(idea): author_key = Idea.author.get_value_for_datastore(idea) if(author_key == self.current_user.key() or self.admin): validated, title, answers = Idea.validate(self.request, idea) if not validated: self.redirect("/idea/"+str(idea.key().id())+"/edit") else: idea.title = title idea.answers = answers idea.put() values = { "response": "Idea updated", "next": { "content": "Back", "url": "/idea/"+str(idea.key().id()), } } path = "feedback.html" self.render(path, values) else: raise GetpitchdError("Not authorized to edit idea") else: raise GetpitchdError("Idea does not exist")
def get(self, selector): values = {} offset = self.request.get("offset") new_offset = None if selector == "all": ideas, new_offset = Idea.get_last_ideas(offset=offset) else: country_name = User.get_country_name(selector) if country_name: country = selector values["country_name"] = country_name.lower().capitalize() values["country_code"] = country.lower() ideas, new_offset = Idea.get_last_ideas(country=country,offset=offset) else: raise GetpitchdError("Country does not exist") author_keys = [] for idea in ideas: author_keys.append(Idea.author.get_value_for_datastore(idea)) authors = db.get(author_keys) values["feed"] = zip(ideas, authors) if new_offset: values["more_ideas_url"] = self.request.path + "?offset="+str(new_offset) path = "ideas.html" self.render(path, values)
def create_card(self, message): message_out = self.generate_message('createCard', message['data']) self.broadcast_to_room(self, message_out) room_id = self.rooms.get_room_id(self) self.cards.add(room_id, message['data']) # -- idea = Idea(card_id=message['data']['id']) idea.save()
def post(self): data = request.get_json() user_id = get_jwt_identity() if not are_idea_params_valid(**data): return { "error": "Input parameters not valid. Scores should be between 1 and 10" }, 400 idea = Idea(user_id=user_id, **data) db.session.add(idea) db.session.commit() return idea.get_idea_json(), 201
def post(self, id): idea = Idea.get_by_id(int(id)) if(idea): author_key = Idea.author.get_value_for_datastore(idea) if(author_key == self.current_user.key() or self.admin): idea.delete() user = self.current_user user.ideas -= 1 user.put() values = { "response": "Your idea has been deleted.", "next": { "content": "Back to homepage", "url":"/", } } path = "feedback.html" self.render(path, values) else: raise GetpitchdError("Not authorized to delete idea") else: raise GetpitchdError("Idea does not exist")
def test_idea_delete_should_check_delete_permission(self): with self.assertRaises(ForbiddenActionException) as context: idea = Idea(discussion_id='54354654', creator_id='78987987', title='Test idea', description='Test description').delete() self.assertIsInstance(context.exception, ForbiddenActionException)
def put(self, id): idea = Idea.find_by_id(id) data = request.get_json() for key in data: setattr(idea, key, data[key]) db.session.commit() return idea.json()
def get(self): data = request.args page = 1 if data and "page" in data: page = data["page"] user_id = get_jwt_identity() return Idea.get_idea_page(user_id=user_id, page=int(page)), 200
def test_idea_persist_should_pass(self): try: Idea(discussion_id='54354654', creator_id='78987987', title='Test idea', description='Test description').delete() except ForbiddenActionException: self.fail("delete() raised ForbiddenActionException unexpectedly")
def delete(self, idea_id): user_id = get_jwt_identity() idea = Idea.get_idea(idea_id) if idea.user_id == user_id: db.session.delete(idea) db.session.commit() return None, 204 return {"error": "User not authorized to delete the idea"}, 400
def get(self): values = { "status": "new", "url": "/idea/pitch", "steps": Idea.get_current_steps(), } path = "idea/pitch.html" self.render(path, values)
def post(self): validated, title, answers = Idea.validate(self.request) if not validated: self.redirect("/idea/pitch") else: # Record the idea and redirect to homepage idea = Idea( title = title, author = self.current_user.key(), answers = answers, version = Idea.get_current_version(), country = self.current_user.country, ) idea.put() user = self.current_user user.ideas += 1 user.put() text = "I just pitched a new startup idea on @gopitchme: " + idea.title url = config.SITE_URL + "/idea/" + str(idea.key().id()) tweet = generate_tweet(text, url) response = "Idea pitched" call_to_action = "Now, tweet your friends about it!" values = { "response": response, "call_to_action": call_to_action, "tweet": tweet, "skip_url": "/idea/"+str(idea.key().id()), } path = "idea/tweet.html" self.render(path, values)
def create_card(self, message): user_id = self.get_current_user_id() message['data']['user_id'] = user_id message_out = self.generate_message('createCard', message['data']) self.broadcast(message_out) room_id = self.rooms.get_room_id(self) self.cards.add(room_id, message['data']) idea = Idea(card_id=message['data']['id']) idea.save() if message['data']['ai_switch']: sentence_generator = SentenceGenerator() res = yield from sentence_generator.generate_sentence(message['data']['text']) for sent in res: message_out = self.generate_message('advice', {'sent': sent}) self.send_message(message_out) yield from asyncio.sleep(2.5)
def init_ideas(discussion_id, user_bob_id, user_john_id, user_anna_id): return [ Idea(discussion_id=discussion_id, title="The parent idea about saving the environment", description="One idea to rule them all", creator_id=user_john_id).persist(), Idea(discussion_id=discussion_id, title='Cars', description='We should avoid using cars', creator_id=user_bob_id).persist(), Idea(discussion_id=discussion_id, title='Plane flights should be replace with boat trips', description='We should avoid using planes', creator_id=user_anna_id).persist(), Idea(discussion_id=discussion_id, title="We should avoid helicopter flights", description="How about not using jet pack either", creator_id=user_anna_id).persist() ]
def create_card(self, message): user_id = self.get_current_user_id() message['data']['user_id'] = user_id message_out = self.generate_message('createCard', message['data']) self.broadcast(message_out) room_id = self.rooms.get_room_id(self) self.cards.add(room_id, message['data']) idea = Idea(card_id=message['data']['id']) idea.save() if message['data']['ai_switch']: sentence_generator = SentenceGenerator() res = yield from sentence_generator.generate_sentence( message['data']['text']) for sent in res: message_out = self.generate_message('advice', {'sent': sent}) self.send_message(message_out) yield from asyncio.sleep(2.5)
def test_idea_init_should_set_attributes(self): idea = Idea(discussion_id='54354654', creator_id='78987987', title='Test idea', description='Test description') self.assertEqual(idea.discussion_id, '54354654') self.assertEqual(idea.creator_id, '78987987') self.assertEqual(idea.title, 'Test idea') self.assertEqual(idea.description, 'Test description') self.assertTrue(isinstance(idea.creation_date, datetime)) self.assertEqual(idea.ideas, [])
def vote_up(self, message): message_id = message['data']['id'] thumb_up_count = message['data']['thumb-up-count'] user = User.get(BaseHandler.get_current_user_id(self)) idea = Idea.get_from_cardID(card_id=message_id) if idea not in user.ideas: user.ideas.append(idea) user.save() message_out = self.generate_message('voteUp', {'id': message_id, 'thumb-up-count': thumb_up_count + 1}) self.broadcast(message_out) room_id = self.rooms.get_room_id(self) self.cards.update_vote_count(room_id, card_id=message_id)
def put(self, idea_id): data = request.get_json() if not are_idea_params_valid(**data): return { "error": "Input parameters not valid. Scores should be between 1 and 10" }, 400 user_id = get_jwt_identity() idea = Idea.get_idea(idea_id) if user_id != idea.user_id: return {"error": "User not authorized to update the idea"}, 400 idea.update_idea(**data) return idea.get_idea_json(), 200
def get(self, id): idea = Idea.get_by_id(int(id)) if(idea): author_key = Idea.author.get_value_for_datastore(idea) if(author_key == self.current_user.key() or self.admin): extended_idea = Idea.get_extended_idea(idea) values = { "status": "edit", "title": extended_idea["title"], "id": extended_idea["id"], "url": "/idea/" + str(extended_idea["id"]) + "/edit", "steps": extended_idea["extended_steps"], } path = "idea/pitch.html" self.render(path, values) else: raise GetpitchdError("Not authorized to edit idea") else: raise GetpitchdError("Idea does not exist")
def vote_up(self, message): message_id = message['data']['id'] user = User.get(BaseHandler.get_current_user_id(self)) idea = Idea.get_from_cardID(card_id=message_id) if idea not in user.ideas: user.ideas.append(idea) user.save() message_out = self.generate_message('voteUp', {'id': message_id}) self.broadcast_to_room(self, message_out) room_id = self.rooms.get_room_id(self) self.cards.update_vote_count(room_id, card_id=message_id) else: message_out = self.generate_message('voteDown', {'id': message_id}) self.send_message(message_out)
def vote_up(self, message): message_id = message['data']['id'] thumb_up_count = message['data']['thumb-up-count'] user = User.get(BaseHandler.get_current_user_id(self)) idea = Idea.get_from_cardID(card_id=message_id) if idea not in user.ideas: user.ideas.append(idea) user.save() message_out = self.generate_message( 'voteUp', { 'id': message_id, 'thumb-up-count': thumb_up_count + 1 }) self.broadcast(message_out) room_id = self.rooms.get_room_id(self) self.cards.update_vote_count(room_id, card_id=message_id)
def get(self, username): user = User.all().filter("username ="******"offset")) authors = [] for idea in ideas: authors.append(user) values = { "feed": zip(ideas, authors), "user": user, } if offset: values["more_ideas_url"] = "/"+user.username+"?offset="+str(offset) path = "user.html" self.render(path, values) else: raise GetPitchdError("User does not exist")
def post(self, id): comment = Comment.get_by_id(int(id)) if(comment): text = self.request.get("text") if(text is not ""): idea_key = Comment.idea.get_value_for_datastore(comment) idea = Idea.get(idea_key) reply = Comment( idea = idea_key, author = self.current_user.key(), reply_to = comment.key(), text = text, ) reply.put() idea.comments += 1 idea.put() event = CommentReplyEvent(self.current_user, reply, comment) values = { "response" : "Reply sent", "next":{ "content": "Back", "url": "/idea/"+str(idea.key().id()) } } path = "feedback.html" self.render(path, values) else: raise GetpitchdError("Comment text is empty") else: raise GetpitchdError("Comment does not exist.")
def post(self, id): idea = Idea.get_by_id(int(id)) if(idea): content = self.request.get("feedback") if(content=="positive" or content=="negative"): key_name = str(self.current_user.key().id()) + "_" + str(idea.key().id()) existing_feedback = Feedback.get_by_key_name(key_name) if not existing_feedback: feedback = Feedback( key_name = key_name, author = self.current_user.key(), idea = idea.key(), content = content, ) feedback.put() if(content == "positive"): idea.positive += 1 else: idea.negative += 1 text = self.request.get("text") if text: comment = Comment( idea = idea.key(), author = self.current_user.key(), text = text, ) comment.put() idea.comments += 1 idea.put() if text: event = IdeaFeedbackEvent(self.current_user, feedback, idea, text) else: event = IdeaFeedbackEvent(self.current_user, feedback, idea) if(content == "positive"): author_key = Idea.author.get_value_for_datastore(idea) author = User.get(author_key) text = "I'm enthusiastic about a new startup idea pitched by @" + author.username + " on @gopitchme: " + idea.title url = config.SITE_URL + "/idea/" + str(idea.key().id()) tweet = generate_tweet(text, url) response = "Feedback sent" call_to_action = "Why not tweeting this idea to your friends?" values = { "response": response, "call_to_action": call_to_action, "tweet": tweet, "skip_url": "/idea/" + str(idea.key().id()), } path = "idea/tweet.html" else: values = { "response": "Thanks for your feedback", "next": { "content": "Back", "url": "/idea/"+str(idea.key().id()), } } path = "feedback.html" self.render(path, values) else: raise GetpitchdError("Feedback already sent") else: raise GetpitchdError("Forbidden feedback") else: raise GetpitchdError("Idea does not exist")
mock_user_forbidden.set_global_permissions(['WRONG_PERMISSION']) mock_user_allowed_for_persist = User(username='******', email='*****@*****.**', password='******') mock_user_allowed_for_persist.set_global_permissions([PermissionType.ADD_IDEA]) mock_user_allowed_for_delete = User(username='******', email='*****@*****.**', password='******') mock_user_allowed_for_delete.set_global_permissions( [PermissionType.REMOVE_IDEA]) idea1 = Idea(discussion_id='1234', title='Test1', description='Test1', creator_id='1234', parent_idea_id=None) idea1.id = '1' idea2 = Idea(discussion_id='1234', title='Test2', description='Test2', creator_id='1234', parent_idea_id='1') idea2.id = '2' idea3 = Idea(discussion_id='1234', title='Test3', description='Test3', creator_id='1234',
def get(self, id): idea = Idea.find_by_id(id) return idea.json()
def get(self, id): idea = Idea.get_by_id(int(id)) if(idea): extended_idea = Idea.get_extended_idea(idea) author = User.get(Idea.author.get_value_for_datastore(idea)) feedback = None if(self.current_user): feedback = Feedback.all().filter("author =", self.current_user.key()).filter("idea =", idea.key()).get() # Fetch comments comments = Comment.all().filter('idea = ', idea.key()).fetch(1000) # Filter comments which are not replies to and those from the others threaded, children, depths = [], [], [] for comment in comments: if Comment.reply_to.get_value_for_datastore(comment) is None: threaded.append(comment) depths.append(0) else: children.append(comment) # Filter comments which are not replies to by date (desc) tosort = [(c.created, c) for c in threaded] tosort.sort() tosort.reverse() threaded = [c for _, c in tosort] # Filter children by date (asc) tosort = [(c.created, c) for c in children] tosort.sort() children = [c for _, c in tosort] # Insert children comments at the right place for comment in children: i = 0 parents = list(threaded) for parent in parents: if parent.key() == Comment.reply_to.get_value_for_datastore(comment): threaded.insert(1 + i, comment) depths.insert(1 + i, depths[i] + 1) i = i + 2 else: i = i + 1 # Get authors authors_keys = [Comment.author.get_value_for_datastore(c) for c in threaded] authors = User.get(authors_keys) values = { "extended_idea": extended_idea, "author": author, "feedback": feedback, "comments": zip(authors, threaded, depths), } path = "idea/idea.html" self.render(path, values) else: raise GetpitchdError("Idea does not exist")
def post(self): data = request.get_json() idea = Idea(**data) idea.create() return idea.json(), 201
def delete(self, id): idea = Idea.find_by_id(id) db.session.delete(idea) db.session.commit() return {"msg": 'Idea deleted', 'payload': idea.id}
def get(self): data = Idea.find_all() results = [u.json() for u in data] return results
parent_post_id='1' ) post4.id = '4' post5 = Post( discussion_id='1234', text='Test5', creator_id='1234', parent_post_id='1' ) post5.id = '4' idea1 = Idea( discussion_id='1234', title='Test1', description='Test1', creator_id='1234', parent_idea_id=None ) idea1.id = '1' mock_posts = [post1, post2, post3, post4, post5] mock_ideas = [idea1] class TestPost(unittest.TestCase): def setUp(self): print('In method ' + self._testMethodName) @mock.patch("security.auth.UserSession.current_user", mock_user_forbidden)