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 joinData(cls, shifts, userId=None): from server.models.favorite import Favorite from server.models.comment import Comment if core._local: db = core.conect("user/%s" % core._local_id) else: db = core.connect("shiftspace/shared") single = False if type(shifts) != list: single = True shifts = [shifts] ids = [shift['_id'] for shift in shifts] favIds = [Favorite.makeId(userId, shiftId) for shiftId in ids] isFavorited = core.fetch(db, keys=favIds) favCounts = core.fetch(db, view=Favorite.count_by_shift, keys=ids) commentCounts = core.fetch(db, view=Comment.count_by_shift, keys=ids) userIds = [shift["createdBy"] for shift in shifts] users = core.fetch(keys=userIds) for i in range(len(shifts)): shifts[i]["favorite"] = (isFavorited[i] != None) shifts[i]["favoriteCount"] = favCounts[i] shifts[i]["commentCount"] = commentCounts[i] shifts[i]["gravatar"] = (users[i]["gravatar"] or "images/default.png") shifts[i]["userName"] = users[i]["userName"] shifts[i]["createdStr"] = utils.pretty_date(utils.futcstr(shifts[i]["created"])) shifts[i]["modifiedStr"] = utils.pretty_date(utils.futcstr(shifts[i]["modified"])) if single: return shifts[0] else: return shifts
def favorites(self, start=None, end=None, limit=25): from server.models.favorite import Favorite db = core.connect("shiftspace/shared") if not start: start = [self.id] if not end: end = [self.id, {}] results = Favorite.by_user_and_created(db, limit=limit) favs = core.objects(results[start:end]) return core.fetch(db, keys=[fav.shiftId for fav in favs])
def testFavorite(self): json = shiftJson() json["createdBy"] = self.fakemary.id newShift = Shift.create(json) 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 = 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()
def joinData(cls, shifts, userId=None): from server.models.favorite import Favorite from server.models.comment import Comment if core._local: db = core.conect("user/%s" % core._local_id) else: db = core.connect("shiftspace/shared") single = False if type(shifts) != list: single = True shifts = [shifts] ids = [shift['_id'] for shift in shifts] favIds = [Favorite.makeId(userId, shiftId) for shiftId in ids] isFavorited = core.fetch(db, keys=favIds) favCounts = core.fetch(db, view=Favorite.count_by_shift, keys=ids) commentCounts = core.fetch(db, view=Comment.count_by_shift, keys=ids) userIds = [shift["createdBy"] for shift in shifts] users = core.fetch(keys=userIds) for i in range(len(shifts)): shifts[i]["favorite"] = (isFavorited[i] != None) shifts[i]["favoriteCount"] = favCounts[i] shifts[i]["commentCount"] = commentCounts[i] shifts[i]["gravatar"] = (users[i]["gravatar"] or "images/default.png") shifts[i]["userName"] = users[i]["userName"] shifts[i]["createdStr"] = utils.pretty_date( utils.futcstr(shifts[i]["created"])) shifts[i]["modifiedStr"] = utils.pretty_date( utils.futcstr(shifts[i]["modified"])) if single: return shifts[0] else: return shifts
def favoriteCount(self): from server.models.favorite import Favorite db = core.connect("shiftspace/shared") return core.value(Favorite.count_by_shift(db, key=self.id)) or 0
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)
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 isFavorite(self, aShift): from server.models.favorite import Favorite db = core.connect("shiftspace/shared") return Favorite.isFavorite(self.id, aShift.id)