def comment(self, id): loggedInUser = helper.getLoggedInUser() jsonData = helper.getRequestBody() if jsonData != "": theShift = Shift.read(id, userId=loggedInUser) if not theShift: return error("Shift does not exist.", ResourceDoesNotExistError) if theShift.type != "shift": return error("Resource is not of type shift", ResourceTypeError) from server.models.ssuser import SSUser theUser = SSUser.read(loggedInUser) theData = json.loads(jsonData) if theUser.canRead(theShift): from server.models.comment import Comment Comment.create(theUser.id, theShift.id, theData["text"], theData.get("subscribe") or False) return data(Shift.read(theShift.id, theUser.id)) else: return error( "Operation not permitted. You don't have permission to comment on this shift.", PermissionError) else: return error("No data for comment.", NoDataError)
def testUnsubscribe(self): json = shiftJson() json["createdBy"] = self.fakemary.id newShift = Shift.create(json) newShift.publish({"private": False}) # another user makes a comment newComment = Comment.create(self.fakebob.id, newShift.id, "1st comment!", subscribe=True) # subscribe fakejohn self.fakejohn.subscribe(newShift) # another user makes another comment newComment = Comment.create(self.fakebob.id, newShift.id, "2nd comment!") # check that fakejohn has one message messages = self.fakejohn.messages() self.assertEqual(len(messages), 1) self.assertEqual(messages[0].text, "fakebob just commented on fakemary's shift!") # unsubscribe fakejohn self.fakejohn.unsubscribe(newShift) newComment = Comment.create(self.fakebob.id, newShift.id, "3rd comment!") # check that fakejohn still only has one message messages = self.fakejohn.messages() self.assertEqual(len(messages), 1) # check that fakemary has two messages messages = self.fakemary.messages() self.assertEqual(len(messages), 3) # check that fakebob has no messages messages = self.fakebob.messages() self.assertEqual(len(messages), 0) newShift.deleteThread()
def comments(self, start=None, end=None, limit=25): from server.models.comment import Comment if not self.hasThread(): return [] db = core.connect(Comment.db(self.id)) return Comment.joinData( core.objects(Comment.by_created(db, limit=limit)))
def testSubscribe(self): json = shiftJson() json["createdBy"] = self.fakemary.id newShift = Shift.create(json) newShift.publish({"private":False}) # make a comment newComment = Comment.create( self.fakejohn.id, newShift.id, "1st comment!", subscribe=True ) # check that shift author is subscribed subscribers = newShift.subscribers() self.assertTrue(self.fakemary.id in subscribers) # check that commenter is subscribed self.assertTrue(self.fakejohn.id in subscribers) # check that there is a message in shift author message db messages = self.fakemary.messages() self.assertEqual(len(messages), 1) self.assertEqual(messages[0].text, "fakejohn just commented on your shift!") # check that there is _not_ a message in commenters message db messages = self.fakejohn.messages() self.assertEqual(len(messages), 0) newShift.deleteThread()
def hasThread(self): from server.models.comment import Comment try: server = core.sharedServer() thread = server[Comment.db(self.id)] return thread != None except Exception: return False
def comments(self, start=None, end=None, limit=25): from server.models.comment import Comment db = core.connect("shiftspace/shared") if not start: start = [self.id] if not end: end = [self.id, {}] results = Comment.by_user_and_created(db, limit=limit) return core.objects(results[start:end])
def subscribe(self, aShift): from server.models.comment import Comment db = core.connect(Comment.db(aShift.id)) if not self.isSubscribed(aShift): db.create({ "_id": "user:%s" % self.id, "shiftId": aShift.id, "type": "subscription", "userId": self.id, })
def comment(self, id): loggedInUser = helper.getLoggedInUser() jsonData = helper.getRequestBody() if jsonData != "": theShift = Shift.read(id) if not theShift: return error("Resource does not exist.", ResourceDoesNotExistError) if theShift.type != "shift": return error("Resource is not of type shift", ResourceTypeError) from server.models.ssuser import SSUser theUser = SSUser.read(loggedInUser) theData = json.loads(jsonData) if theUser.canRead(theShift): from server.models.comment import Comment Comment.create(theUser.id, theShift.id, theData["text"], theData.get("subscribe") or False) return data(Shift.read(theShift.id, theUser.id)) else: return error("Operation not permitted. You don't have permission to comment on this shift.", PermissionError) else: return error("No data for comment.", NoDataError)
def testUnsubscribe(self): json = shiftJson() json["createdBy"] = self.fakemary.id newShift = Shift.create(json) newShift.publish({"private":False}) # another user makes a comment newComment = Comment.create( self.fakebob.id, newShift.id, "1st comment!", subscribe=True ) # subscribe fakejohn self.fakejohn.subscribe(newShift) # another user makes another comment newComment = Comment.create( self.fakebob.id, newShift.id, "2nd comment!" ) # check that fakejohn has one message messages = self.fakejohn.messages() self.assertEqual(len(messages), 1) self.assertEqual(messages[0].text, "fakebob just commented on fakemary's shift!") # unsubscribe fakejohn self.fakejohn.unsubscribe(newShift) newComment = Comment.create( self.fakebob.id, newShift.id, "3rd comment!" ) # check that fakejohn still only has one message messages = self.fakejohn.messages() self.assertEqual(len(messages), 1) # check that fakemary has two messages messages = self.fakemary.messages() self.assertEqual(len(messages), 3) # check that fakebob has no messages messages = self.fakebob.messages() self.assertEqual(len(messages), 0) newShift.deleteThread()
def testCreate(self): json = shiftJson() json["createdBy"] = self.fakemary.id newShift = Shift.create(json) newShift.publish({"private":False}) # create new comment newComment = Comment.create(self.fakejohn.id, newShift.id, "1st comment!") # shift comment db should now exist self.assertNotEqual(core.connect(Comment.db(newShift.id)), None) # shift should have thread self.assertTrue(newShift.hasThread()) # should be a comment count of 1 for shift count = newShift.commentCount() self.assertEqual(count, 1) # should be one message in fakemary's inbox from fakejohn messages = self.fakemary.messages() self.assertEqual(len(messages), 1) # delete the comment # TODO: separate fixture - David newComment.delete() # delete the thread newShift.deleteThread()
def testCreate(self): json = shiftJson() json["createdBy"] = self.fakemary.id newShift = Shift.create(json) newShift.publish({"private": False}) # create new comment newComment = Comment.create(self.fakejohn.id, newShift.id, "1st comment!") # shift comment db should now exist self.assertNotEqual(core.connect(Comment.db(newShift.id)), None) # shift should have thread self.assertTrue(newShift.hasThread()) # should be a comment count of 1 for shift count = newShift.commentCount() self.assertEqual(count, 1) # should be one message in fakemary's inbox from fakejohn messages = self.fakemary.messages() self.assertEqual(len(messages), 1) # delete the comment # TODO: separate fixture - David newComment.delete() # delete the thread newShift.deleteThread()
def testSubscribe(self): json = shiftJson() json["createdBy"] = self.fakemary.id newShift = Shift.create(json) newShift.publish({"private": False}) # make a comment newComment = Comment.create(self.fakejohn.id, newShift.id, "1st comment!", subscribe=True) # check that shift author is subscribed subscribers = newShift.subscribers() self.assertTrue(self.fakemary.id in subscribers) # check that commenter is subscribed self.assertTrue(self.fakejohn.id in subscribers) # check that there is a message in shift author message db messages = self.fakemary.messages() self.assertEqual(len(messages), 1) self.assertEqual(messages[0].text, "fakejohn just commented on your shift!") # check that there is _not_ a message in commenters message db messages = self.fakejohn.messages() self.assertEqual(len(messages), 0) newShift.deleteThread()
def commentCount(self): from server.models.comment import Comment db = core.connect("shiftspace/shared") return core.value(Comment.count_by_shift(db, key=self.id))
def subscribers(self): from server.models.comment import Comment db = core.connect(Comment.db(self.id)) return core.values(Comment.all_subscribed(db))
def deleteThread(self): from server.models.comment import Comment server = core.sharedServer() # TODO - use bulk API to delete all comment stubs - David del server[Comment.db(self.id)]
def comments(self, start=None, end=None, limit=25): from server.models.comment import Comment if not self.hasThread(): return [] db = core.connect(Comment.db(self.id)) return Comment.joinData(core.objects(Comment.by_created(db, limit=limit)))
def unsubscribe(self, aShift): from server.models.comment import Comment db = core.connect(Comment.db(aShift.id)) if self.isSubscribed(aShift): del db["user:%s" % self.id]
def isSubscribed(self, aShift): from server.models.comment import Comment db = core.connect(Comment.db(aShift.id)) return db.get("user:%s" % self.id) != None