def testBasicPublish(self): json = shiftJson() json["createdBy"] = self.fakemary.id newShift = Shift.create(json) newShift.publish({"private": False}) # should exist in user/public db theShift = Shift.load(core.connect(SSUser.publicDb(self.fakemary.id)), newShift.id) self.assertEqual(theShift.summary, newShift.summary) self.assertTrue(not theShift.publishData.draft) self.assertTrue(not theShift.publishData.private) # should exist in shiftspace/public db theShift = Shift.load(core.connect("shiftspace/public"), newShift.id) self.assertEqual(theShift.summary, newShift.summary) self.assertTrue(not theShift.publishData.draft) self.assertTrue(not theShift.publishData.private) # should exist in shiftspace/shared db theShift = Shift.load(core.connect("shiftspace/shared"), newShift.id) self.assertEqual(theShift.summary, newShift.summary) self.assertTrue(not theShift.publishData.draft) self.assertTrue(not theShift.publishData.private) # should _not_ exist in user/private db theShift = Shift.load(core.connect(SSUser.privateDb(self.fakemary.id)), newShift.id) self.assertEqual(theShift, None)
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 shifts(self, start=None, end=None, limit=25, filter=False, query=None): from server.models.shift import Shift db = core.connect("shiftspace/shared") if not filter: if not start: start = [self.id] if not end: end = [self.id, {}] results = Shift.by_user_and_created(db, limit=limit) return Shift.joinData(core.objects(results[start:end])) else: lucene = core.lucene() queryString = "createdBy:%s" % self.id theFilter = core.dictToQuery(query) if theFilter: queryString = queryString + " AND " + theFilter try: print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" print queryString rows = lucene.search(db, "shifts", q=queryString, include_docs=True, sort="\modified") except Exception, err: print err return [] shifts = [row["doc"] for row in rows] return Shift.joinData(shifts, self.id)
def testDelete(self): json = shiftJson() json["createdBy"] = self.fakemary.id newShift = Shift.create(json) self.assertNotEqual(newShift, None) newShift.delete() theShift = Shift.read(newShift.id, self.fakemary.id) self.assertEqual(theShift, None)
def testUpdate(self): json = shiftJson() json["createdBy"] = self.fakemary.id newShift = Shift.create(json) newShift.update({"summary":"changed!"}) theShift = Shift.read(newShift.id, self.fakemary.id) self.assertEqual(theShift.summary, "changed!") db = core.connect(SSUser.privateDb(self.fakemary.id)) del db[theShift.id]
def testRead(self): json = shiftJson() json["createdBy"] = self.fakemary.id newShift = Shift.create(json) theShift = Shift.read(newShift.id, self.fakemary.id) self.assertEqual(theShift.source.server, newShift.source.server) self.assertEqual(theShift.source.database, newShift.source.database) self.assertEqual(theShift.createdBy, self.fakemary.id) db = core.connect(SSUser.privateDb(self.fakemary.id)) del db[theShift.id]
def testPublishToUser(self): json = shiftJson() json["createdBy"] = self.fakemary.id newShift = Shift.create(json) publishData = { "dbs": [SSUser.db(self.fakejohn.id)] } newShift.publish(publishData) # should exist in user feed # TODO: in inbox if peer - David 11/18/09 theShift = Shift.load(core.connect("shiftspace/shared"), newShift.id) self.assertEqual(theShift.summary, newShift.summary)
def testPagingFeatures(self): json = shiftJson() json["createdBy"] = self.fakemary.id newShift1 = Shift.create(json) newShift2 = Shift.create(json) newShift3 = Shift.create(json) fav1 = Favorite.create(self.fakejohn.id, newShift1.id) fav2 = Favorite.create(self.fakejohn.id, newShift2.id) fav3 = Favorite.create(self.fakejohn.id, newShift3.id) favorites = self.fakejohn.favorites(limit=2) self.assertEqual(len(favorites), 2) fav1.delete() fav2.delete() fav3.delete()
def testPublishToFollowers(self): json = shiftJson() json["createdBy"] = self.fakemary.id newShift = Shift.create(json) self.fakejohn.follow(self.fakemary) fakejohn = SSUser.read(self.fakejohn.id) # should be in the list of people fakejohn is following self.assertTrue(self.fakemary.id in fakejohn.following()) # should be in the list of fakemary's followers followers = self.fakemary.followers() self.assertTrue(self.fakejohn.id in followers) newShift.publish({"private":False}) # should exist in shiftspace/shared db theShift = Shift.load(core.connect("shiftspace/shared"), newShift.id) self.assertEqual(theShift.summary, newShift.summary)
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 testCanModify(self): json = shiftJson() json["createdBy"] = self.fakemary.id newShift = Shift.create(json) self.assertTrue(self.fakemary.canModify(newShift)) self.assertTrue(not self.fakejohn.canModify(newShift)) self.assertTrue(self.root.canModify(newShift))
def publish(self, id): # NOTE: should maybe take publishData url parameter - David 9/5/2009 loggedInUser = helper.getLoggedInUser() theShift = Shift.read(id, loggedInUser) if not theShift: return error("Resource does not exist.", ResourceDoesNotExistError) if theShift.type != "shift": return error("Resource is not of type shift", ResourceTypeError) publishData = json.loads(helper.getRequestBody()) # convert targets to actual database references if publishData.get("targets"): from server.models.group import Group from server.models.ssuser import SSUser theUser = SSUser.read(loggedInUser) targets = publishData["targets"] # convert short names to group ids shortNames = [target[1:] for target in targets if target[0] == "&"] groupIds = Group.shortNamesToIds(shortNames) # convert user name to user ids userNames = [target[1:] for target in targets if target[0] == "@"] userIds = SSUser.namesToIds(userNames) # create list of dbs being published to dbs = [Group.db(groupId) for groupId in groupIds] # validate groups writeable = theUser.writeable() if not set(dbs).issubset(set(writeable)): return error( "Operation not permitted. You don't have permission to publish to some of these groups", PermissionError) # TODO: validate against blocked users - David 2/15/10 dbs.extend([SSUser.db(userId) for userId in userIds]) publishData["dbs"] = dbs return data(theShift.publish(publishData))
def shifts(self, byHref=None, byDomain=None, byFollowing=False, byGroups=False, bySpace=None, start=0, limit=25, count=False, filter=False, query=None, all=False): from server.models.ssuser import SSUser loggedInUser = helper.getLoggedInUser() if loggedInUser: theUser = SSUser.read(loggedInUser) else: theUser = None if query != None: query = json.loads(query) allShifts = Shift.shifts(user=theUser, byHref=byHref, byDomain=byDomain, byFollowing=byFollowing, byGroups=byGroups, bySpace=bySpace, start=start, limit=limit, filter=filter, query=query, all=False) if count: return data(len(allShifts)) else: return data(allShifts)
def publish(self, id): # NOTE: should maybe take publishData url parameter - David 9/5/2009 loggedInUser = helper.getLoggedInUser() theShift = Shift.read(id, loggedInUser) if not theShift: return error("Resource does not exist.", ResourceDoesNotExistError) if theShift.type != "shift": return error("Resource is not of type shift", ResourceTypeError) publishData = json.loads(helper.getRequestBody()) # convert targets to actual database references if publishData.get("targets"): from server.models.group import Group from server.models.ssuser import SSUser theUser = SSUser.read(loggedInUser) targets = publishData["targets"] # convert short names to group ids shortNames = [target[1:] for target in targets if target[0] == "&"] groupIds = Group.shortNamesToIds(shortNames) # convert user name to user ids userNames = [target[1:] for target in targets if target[0] == "@"] userIds = SSUser.namesToIds(userNames) # create list of dbs being published to dbs = [Group.db(groupId) for groupId in groupIds] dbs.extend([SSUser.db(userId) for userId in userIds]) # validate writeable = theUser.writeable() if set(writeable) != set(dbs): return error("Operation not permitted. You don't have permission to publish to some of these gruops", PermissionError) publishData["dbs"] = dbs return data(theShift.publish(publishData))
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 testBasicPublish(self): json = shiftJson() json["createdBy"] = self.fakemary.id newShift = Shift.create(json) newShift.publish({"private":False}) # should exist in user/public db theShift = Shift.load(core.connect(SSUser.publicDb(self.fakemary.id)), newShift.id) self.assertEqual(theShift.summary, newShift.summary) # should exist in master/public db theShift = Shift.load(core.connect("shiftspace/public"), newShift.id) self.assertEqual(theShift.summary, newShift.summary) # should exist in shiftspace/shared db theShift = Shift.load(core.connect("shiftspace/shared"), newShift.id) self.assertEqual(theShift.summary, newShift.summary) # should _not_ exist in user/private db theShift = Shift.load(core.connect(SSUser.privateDb(self.fakemary.id)), newShift.id) self.assertEqual(theShift, None)
def unpublish(self, id): loggedInUser = helper.getLoggedInUser() theShift = Shift.read(id, loggedInUser) if not theShift: return error("Resource does not exist.", ResourceDoesNotExistError) if theShift.type != "shift": return error("Resource is not of type shift", ResourceTypeError) return data(theShift.unpublish())
def create(self): loggedInUser = helper.getLoggedInUser() jsonData = helper.getRequestBody() if jsonData != "": theData = json.loads(jsonData) id = loggedInUser theData['createdBy'] = id return data(Shift.create(theData)) else: return error("No data for shift.", NoDataError)
def create(cls, userId, shiftId, text, subscribe=False): from server.models.ssuser import SSUser from server.models.shift import Shift from server.models.message import Message # first try the public feed theShift = Shift.load(core.connect("shiftspace/shared"), shiftId) shiftAuthor = SSUser.load(core.connect(), theShift.createdBy) theUser = SSUser.load(core.connect(), userId) server = core.server() # create the comment db if necessary dbexists = True if not theShift.hasThread(): server.create(Comment.db(shiftId)) dbexists = False # get the db db = core.connect(Comment.db(shiftId)) # if db just created, sync the views and subscribe shift author if not dbexists: Comment.by_created.sync(db) Comment.all_subscribed.sync(db) shiftAuthor.subscribe(theShift) # subscribe the user making the comment if not theUser.isSubscribed(theShift) and subscribe: theUser.subscribe(theShift) # create comment and comment stub json = { "createdBy": userId, "shiftId": shiftId, "shiftAuthor": theShift.createdBy, "text": text, } newComment = Comment(**utils.clean(json)) newComment.store(db) subscribers = theShift.subscribers() # make a private copy # TODO: need to think about the implications of a private copy here - David newComment.copyTo(SSUser.privateDb(theUser.id)) # send each subscriber a message if len(subscribers) > 0: # TODO: needs to be optimized with a fast join - David for subscriber in subscribers: if subscriber != userId: astr = ((subscriber == theShift.createdBy) and "your") or ("%s's" % shiftAuthor.userName) json = { "fromId": userId, "toId": subscriber, "title": "%s just commented on %s shift!" % (theUser.userName, astr), "text": "%s just commented on %s shift!" % (theUser.userName, astr), "meta": "comment" } Message.create(**utils.clean(json)) # TODO: don't replicate if peer - David 11/21/09 core.replicate(Comment.db(shiftId), "shiftspace/shared") return newComment
def create(cls, userId, shiftId, text, subscribe=False): from server.models.ssuser import SSUser from server.models.shift import Shift from server.models.message import Message # first try the public feed theShift = Shift.load(core.connect("shiftspace/shared"), shiftId) shiftAuthor = SSUser.load(core.connect(), theShift.createdBy) theUser = SSUser.load(core.connect(), userId) server = core.sharedServer() # create the comment db if necessary dbexists = True if not theShift.hasThread(): server.create(Comment.db(shiftId)) dbexists = False # get the db db = core.connect(Comment.db(shiftId)) # if db just created, sync the views and subscribe shift author if not dbexists: Comment.by_created.sync(db) Comment.all_subscribed.sync(db) shiftAuthor.subscribe(theShift) # subscribe the user making the comment if not theUser.isSubscribed(theShift) and subscribe: theUser.subscribe(theShift) # create comment and comment stub json = { "createdBy": userId, "shiftId": shiftId, "shiftAuthor": theShift.createdBy, "text": text, } newComment = Comment(**utils.clean(json)) newComment.store(db) subscribers = theShift.subscribers() # make a private copy # TODO: need to think about the implications of a private copy here - David newComment.copyTo(SSUser.privateDb(theUser.id)) # send each subscriber a message if len(subscribers) > 0: # TODO: needs to be optimized with a fast join - David for subscriber in subscribers: if subscriber != userId: astr = ((subscriber == theShift.createdBy) and "your") or ("%s's" % shiftAuthor.userName) json = { "fromId": userId, "toId": subscriber, "title": "%s just commented on %s shift!" % (theUser.userName, astr), "text": "%s just commented on %s shift!" % (theUser.userName, astr), "meta": "comment" } Message.create(**utils.clean(json)) # TODO: don't replicate if peer - David 11/21/09 core.replicate(Comment.db(shiftId), "shiftspace/shared") return newComment
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 share(self, id, users): from server.models.ssuser import SSUser loggedInUser = helper.getLoggedInUser() theShift = Shift.read(id) if not theShift or theShift.publishData.private: return error("You don't have permission to view this shift.", PermissionError) targets = users.split(" ") userNames = [target[1:] for target in targets if target[0] == "@"] userIds = SSUser.namesToIds(userNames) theShift.shareWith(userIds, fromUser=SSUser.read(loggedInUser)) return ack
def info(self): from server.models.follow import Follow from server.models.shift import Shift # TODO: use the bulk API - David 12/10/09 result = {} db = core.connect() shared = core.connect("shiftspace/shared") result["followerCount"] = core.value(Follow.followers_count(db, key=self.id)) or 0 result["followingCount"] = core.value(Follow.following_count(db, key=self.id)) or 0 result["publishedShiftCount"] = core.value(Shift.count_by_user_and_published(shared, key=self.id)) or 0 return result
def testPublishToGroupAndUser(self): json = shiftJson() json["createdBy"] = self.fakemary.id newShift = Shift.create(json) json = groupJson() json["createdBy"] = self.fakemary.id newGroup = Group.create(json) newPerm = Permission.create("shiftspace", newGroup.id, self.fakejohn.id, level=1) publishData = { "dbs": [Group.db(newGroup.id), SSUser.db(self.fakebob.id)] } newShift.publish(publishData) # should exist in subscriber's feed db = core.connect("shiftspace/shared") theShift = Shift.load(db, newShift.id) self.assertEqual(theShift.summary, newShift.summary) newGroup.delete() # should exist in shiftspace/shared # TODO: inbox if user is peer - David 11/18/09 theShift = Shift.load(core.connect("shiftspace/shared"), newShift.id) self.assertEqual(theShift.summary, newShift.summary)
def feed(self, start=None, end=None, limit=25): # NOT IMPLEMENTED: will have to wait until we get to the point where # we're writing p2p code - David from server.models.shift import Shift results = Shift.by_created(core.connect(SSUser.feedDb(self.id)), limit=limit) if start and not end: return core.objects(results[start:]) if not start and end: return core.objects(results[:end]) if start and end: return core.objects(results[start:end]) return core.objects(results[start:end])
def read(self, id): from server.models.ssuser import SSUser loggedInUser = helper.getLoggedInUser() theUser = SSUser.read(loggedInUser) theShift = Shift.read(id, loggedInUser) if theShift and theUser.canRead(theShift): return data(theShift) else: if not theShift: return error("Resource does not exist.", ResourceDoesNotExistError) else: return error("Operation not permitted. You don't have permission to view this shift. %s" % theShift, PermissionError)
def comments(self, id, start=None, end=None, limit=25): loggedInUser = helper.getLoggedInUser() 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) if theShift.isPublic() or theUser.canRead(theShift): return data(theShift.comments(start=start, end=end, limit=limit)) else: return error("Operation not permitted. You don't have permission to view comments on this shift.", PermissionError)
def testCreate(self): json = shiftJson() json["createdBy"] = self.fakemary.id theShift = Shift.create(json) self.assertEqual(theShift.type, "shift") self.assertEqual(theShift.createdBy, self.fakemary.id) self.assertNotEqual(theShift.created, None) self.assertEqual(type(theShift.created), datetime.datetime) self.assertNotEqual(theShift.modified, None) self.assertEqual(type(theShift.modified), datetime.datetime) self.assertEqual(theShift.domain, "http://google.com") db = core.connect(SSUser.privateDb(self.fakemary.id)) del db[theShift.id]
def unfavorite(self, id): loggedInUser = helper.getLoggedInUser() 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) if theUser.canRead(theShift): return data(theUser.unfavorite(theShift)) else: return error("Operation not permitted. You don't have permission to unfavorite this shift.", PermissionError)
def info(self): from server.models.follow import Follow from server.models.shift import Shift # TODO: use the bulk API - David 12/10/09 result = {} db = core.connect() shared = core.connect("shiftspace/shared") result["followerCount"] = core.value( Follow.followers_count(db, key=self.id)) or 0 result["followingCount"] = core.value( Follow.following_count(db, key=self.id)) or 0 result["publishedShiftCount"] = core.value( Shift.count_by_user_and_published(shared, key=self.id)) or 0 return result
def testPublishToGroup(self): json = shiftJson() json["createdBy"] = self.fakemary.id newShift = Shift.create(json) json = groupJson() json["createdBy"] = self.fakemary.id newGroup = Group.create(json) # make sure fakemary owns the group newPerm = Permission.readByUserAndGroup(self.fakemary.id, newGroup.id) self.assertTrue(newPerm.level == 4) # create read permission for fakejohn newPerm = Permission.create("shiftspace", newGroup.id, self.fakejohn.id, level=1) fakejohn = SSUser.read(self.fakejohn.id) self.assertTrue(Group.db(newGroup.id) in fakejohn.readable()) publishData = { "dbs": [Group.db(newGroup.id)] } newShift.publish(publishData) # should exists in shiftspace/shared db = core.connect("shiftspace/shared") theShift = Shift.load(db, newShift.id) self.assertEqual(theShift.summary, newShift.summary) newGroup.delete()
def comments(self, id, start=None, end=None, limit=25): loggedInUser = helper.getLoggedInUser() theShift = Shift.read(id, userId=loggedInUser) 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) if theShift.isPublic() or theUser.canRead(theShift): return data(theShift.comments(start=start, end=end, limit=limit)) else: return error( "Operation not permitted. You don't have permission to view comments on this shift.", PermissionError)
def delete(self, id): from server.models.ssuser import SSUser loggedInUser = helper.getLoggedInUser() theShift = Shift.read(id, loggedInUser) 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) if theUser.canModify(theShift): theShift.delete() return ack else: return error("Operation not permitted. You don't have permission to delete this shift.", PermissionError)
def read(self, id): from server.models.ssuser import SSUser loggedInUser = helper.getLoggedInUser() theUser = SSUser.read(loggedInUser) theShift = Shift.read(id, loggedInUser) if theShift and theUser.canRead(theShift): return data(theShift) else: if not theShift: return error("Resource does not exist.", ResourceDoesNotExistError) else: return error( "Operation not permitted. You don't have permission to view this shift. %s" % theShift, PermissionError)
def unfavorite(self, id): loggedInUser = helper.getLoggedInUser() 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) if theUser.canRead(theShift): return data(theUser.unfavorite(theShift)) else: return error( "Operation not permitted. You don't have permission to unfavorite this shift.", PermissionError)
def unnotify(self, id): loggedInUser = helper.getLoggedInUser() 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) if theUser.canRead(theShift): if theUser.isSubscribed(theShift): theUser.unsubscribe(theShift) return ack else: return error("You are not getting notification from this comment thread.", NotBeingNotifiedError) else: return error("Operation not permitted. You don't have permission to be notified of events on this stream.", PermissionError)
def delete(self, id): from server.models.ssuser import SSUser loggedInUser = helper.getLoggedInUser() theShift = Shift.read(id, loggedInUser) 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) if theUser.canModify(theShift): theShift.delete() return ack else: return error( "Operation not permitted. You don't have permission to delete this shift.", PermissionError)
def testUnfavorite(self): json = shiftJson() json["createdBy"] = self.fakemary.id newShift = Shift.create(json) newFavorite = Favorite.create(self.fakejohn.id, newShift.id) favorites = self.fakejohn.favorites() # user should have 1 favorite self.assertEqual(len(favorites), 1) # favorite count for that shift should be 1 count = newShift.favoriteCount() self.assertEqual(count, 1) newFavorite.delete() # user should have 0 favorites favorites = self.fakejohn.favorites() self.assertEqual(len(favorites), 0) # favorite count for that shift should be 0 count = newShift.favoriteCount() self.assertEqual(count, 0)
def update(self, id): from server.models.ssuser import SSUser loggedInUser = helper.getLoggedInUser() jsonData = helper.getRequestBody() if jsonData != "": theShift = Shift.read(id, loggedInUser) 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 shiftData = json.loads(jsonData) theUser = SSUser.read(loggedInUser) if theUser.canModify(theShift): return data(theShift.update(shiftData)) else: return error("Operation not permitted. You don't have permission to update this shift.", PermissionError) else: return error("No data for shift.", 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 unnotify(self, id): loggedInUser = helper.getLoggedInUser() 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) if theUser.canRead(theShift): if theUser.isSubscribed(theShift): theUser.unsubscribe(theShift) return ack else: return error( "You are not getting notification from this comment thread.", NotBeingNotifiedError) else: return error( "Operation not permitted. You don't have permission to be notified of events on this stream.", PermissionError)
def shifts(self, byHref=None, byDomain=None, byFollowing=False, byGroups=False, start=0, limit=25, count=False, filter=False, query=None): from server.models.ssuser import SSUser loggedInUser = helper.getLoggedInUser() if loggedInUser: theUser = SSUser.read(loggedInUser) else: theUser = None if query != None: query = json.loads(query) allShifts = Shift.shifts(user=theUser, byHref=byHref, byDomain=byDomain, byFollowing=byFollowing, byGroups=byGroups, start=start, limit=limit, filter=filter, query=query) if count: return data(len(allShifts)) else: return data(allShifts)
def update(self, id): from server.models.ssuser import SSUser loggedInUser = helper.getLoggedInUser() jsonData = helper.getRequestBody() if jsonData != "": theShift = Shift.read(id, loggedInUser) 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 shiftData = json.loads(jsonData) theUser = SSUser.read(loggedInUser) if theUser.canModify(theShift): return data(theShift.update(shiftData)) else: return error( "Operation not permitted. You don't have permission to update this shift.", PermissionError) else: return error("No data for shift.", NoDataError)
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 favorite(self, aShift): from server.models.favorite import Favorite from server.models.shift import Shift Favorite.create(self.id, aShift.id) return Shift.joinData(Shift.read(aShift.id), self.id)
def unfavorite(self, aShift): from server.models.favorite import Favorite from server.models.shift import Shift Favorite.readByUserAndShift(self.id, aShift.id).delete() return Shift.joinData(Shift.read(aShift.id), self.id)