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 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 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 shifts(cls, user, byHref=None, byDomain=None, byFollowing=False, byGroups=False, bySpace=None, start=0, limit=25, filter=False, query=None, all=False): from server.models.ssuser import SSUser db = core.connect("shiftspace/shared") lucene = core.lucene() queryString = "" # TODO: validate all fields - David if byHref or byDomain: if byHref: queryString = "hrefExact:\"%s_HREF_EXACT\"" % byHref elif byDomain: queryString = "domain:\"%s\""% byDomain if bySpace: queryString = queryString + " spaceName:" + bySpace queryString = queryString + " AND ((draft:false AND private:false)" if user: queryString = queryString + " OR createdBy:%s" % user.id dbs = user.readable() dbs.append(SSUser.db(user.id)) dbsStr = " ".join(dbs) queryString = queryString + ((" OR (draft:false%s)" % ((len(dbs) > 0 and (" AND dbs:(%s)" % dbsStr)) or ""))) queryString = queryString + ")" elif byFollowing: from server.models.follow import Follow # FIXME: impossible to make this scale in a simple way for many followers w/o p2p - David 12/2/09 # when p2p we can tag the shift as a follow shift when we get it results = Follow.following_by_created(core.connect()) following = " ".join(core.values(results[[user.id]:[user.id, {}]])) queryString = "(draft:false AND private:false AND createdBy:(%s)) OR dbs:(%s)" % (following, SSUser.db(user.id)) elif byGroups: from server.models.group import Group queryString = "dbs:(%s)" % " ".join(user.readable()) if filter: queryString = queryString + " AND " + core.dictToQuery(query) print queryString try: if all: rows = lucene.search(db, "shifts", q=queryString, include_docs=True, sort="\modified") else: rows = lucene.search(db, "shifts", q=queryString, include_docs=True, sort="\modified", skip=start, limit=limit) except Exception, err: print err return []
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 shifts(cls, user, byHref=None, byDomain=None, byFollowing=False, byGroups=False, bySpace=None, start=0, limit=25, filter=False, query=None, all=False): from server.models.ssuser import SSUser db = core.connect("shiftspace/shared") lucene = core.lucene() queryString = "" # TODO: validate all fields - David if byHref or byDomain: if byHref: queryString = "hrefExact:\"%s_HREF_EXACT\"" % byHref elif byDomain: queryString = "domain:\"%s\"" % byDomain if bySpace: queryString = queryString + " spaceName:" + bySpace queryString = queryString + " AND ((draft:false AND private:false)" if user: queryString = queryString + " OR createdBy:%s" % user.id dbs = user.readable() dbs.append(SSUser.db(user.id)) dbsStr = " ".join(dbs) queryString = queryString + ((" OR (draft:false%s)" % ( (len(dbs) > 0 and (" AND dbs:(%s)" % dbsStr)) or ""))) queryString = queryString + ")" elif byFollowing: from server.models.follow import Follow # FIXME: impossible to make this scale in a simple way for many followers w/o p2p - David 12/2/09 # when p2p we can tag the shift as a follow shift when we get it results = Follow.following_by_created(core.connect()) following = " ".join(core.values(results[[user.id]:[user.id, {}]])) queryString = "(draft:false AND private:false AND createdBy:(%s)) OR dbs:(%s)" % ( following, SSUser.db(user.id)) elif byGroups: from server.models.group import Group queryString = "dbs:(%s)" % " ".join(user.readable()) if filter: queryString = queryString + " AND " + core.dictToQuery(query) print queryString try: if all: rows = lucene.search(db, "shifts", q=queryString, include_docs=True, sort="\modified") else: rows = lucene.search(db, "shifts", q=queryString, include_docs=True, sort="\modified", skip=start, limit=limit) except Exception, err: print err return []