def update(self, fields, updateDbs=True): from server.models.ssuser import SSUser if fields.get("content"): self.content = fields.get("content") if fields.get("summary"): self.summary = self.content["summary"] = fields.get("summary") if fields.get("broken"): self.broken = fields.get("broken") if fields.get("dbs"): self.dbs = list(set(self.dbs.extend(fields.get("dbs")))) self.modified = datetime.now() # update the correct user db if self.publishData.private: db = SSUser.privateDb(self.createdBy) else: db = SSUser.publicDb(self.createdBy) self.store(core.connect(db)) core.replicate(db, "shiftspace/shared") # update followers and groups if updateDbs: for db in self.publishData.dbs: dbtype, dbid = db.split("/") if dbtype == "group": from server.models.group import Group Group.read(dbid).updateShift(self) return Shift.joinData(self, self.createdBy)
def create(cls, shiftJson): from server.models.ssuser import SSUser newShift = Shift(**utils.clean(shiftJson)) createdBy = newShift.createdBy db = core.connect(SSUser.privateDb(createdBy)) newShift.domain = utils.domain(newShift.href) newShift.store(db) core.replicate(SSUser.privateDb(createdBy), "shiftspace/shared") return Shift.joinData(newShift, newShift.createdBy)
def delete(self): from server.models.ssuser import SSUser db = core.connect(SSUser.privateDb(self.createdBy)) if db.get(self.id): del db[self.id] else: db = core.connect(SSUser.publicDb(self.createdBy)) if db.get(self.id): del db[self.id] core.replicate(db.name, "shiftspace/shared")
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 create(cls, shiftJson): from server.models.ssuser import SSUser cleaned = utils.clean(shiftJson) sanitized = sanitizeShift(cleaned) newShift = Shift(**sanitized) createdBy = newShift.createdBy db = core.connect(SSUser.privateDb(createdBy)) newShift.domain = utils.domain(newShift.href) newShift.store(db) core.replicate(SSUser.privateDb(createdBy), "shiftspace/shared") return Shift.joinData(newShift, newShift.createdBy)
def markRead(self, value=True): from server.models.ssuser import SSUser db = core.connect(SSUser.messagesDb(self.toId)) if value: if not self.isRead(): db[Message.makeReadId(self.id, self.toId)] = { "type": "message-read", "msgId": self.id, "toId": self.toId, } else: del db[Message.makeReadId(self.id, self.toId)] core.replicate(SSUser.messagesDb(self.toId), "shiftspace/shared") return self
def create(cls, fromId, toId, title, text, meta="generic", content=None): from server.models.ssuser import SSUser db = core.connect(SSUser.messagesDb(toId)) json = { "fromId": fromId, "toId": toId, "title": text, "text": text, "meta": meta, "content": content or {} } newMessage = Message(**utils.clean(json)) newMessage.store(db) core.replicate(SSUser.messagesDb(toId), "shiftspace/shared") return newMessage
def delete(self): """ Delete a single comment. """ from server.models.ssuser import SSUser db = core.connect(SSUser.privateDb(self.createdBy)) try: del db[self.id] except: pass db = core.connect(Comment.db(self.shiftId)) try: del db[self.id] except: pass core.replicate(Comment.db(self.shiftId), "shiftspace/shared")
def publish(self, publishData=None): from server.models.ssuser import SSUser if publishData == None: return self db = core.connect(SSUser.privateDb(self.createdBy)) dbs = [] author = SSUser.read(self.createdBy) oldPublishData = dict(self.items())["publishData"] allowed = [] # get the private status isPrivate = True if publishData and publishData.get("private") != None: isPrivate = publishData.get("private") else: isPrivate = self.isPrivate() # get the dbs being published to publishDbs = (publishData and publishData.get("dbs")) or [] # get the list of dbs the user is actually allowed to publish to allowed = [] if (publishData and isPrivate and len(publishDbs) > 0): from server.models.group import Group allowedGroups = author.writeable() allowed = list(set(allowedGroups).intersection(set(publishDbs))) # upate the private setting, the shift is no longer draft self.publishData.private = isPrivate self.publishData.draft = False # publish or update a copy to group/x, group/y, ... newGroupDbs = [s for s in allowed if s.split("/")[0] == "group"] oldGroupDbs = [s for s in oldPublishData.get("dbs") if s.split("/")[0] == "group"] newGroupDbs = list(set(newGroupDbs).difference(set(oldGroupDbs))) if newGroupDbs and len(newGroupDbs) > 0: dbs.extend(list(set(newGroupDbs))) # publish to any user we haven't published to before newUserDbs = [s for s in publishDbs if s.split("/")[0] == "user"] if newUserDbs and len(newUserDbs) > 0: userDbs = list(set(newUserDbs)) dbs.extend(userDbs) self.shareWith([s.split("/")[1] for s in userDbs]) self.publishData.dbs = dbs # store the human readable version targets = publishData.get("targets") if targets: self.publishData.targets = targets # update/add to group dbs self.updateInGroups(oldGroupDbs) self.addToGroups(newGroupDbs) # if public shift # create in user/public, delete from user/private # replicate shiftspace/public to shiftspace/shared if not isPrivate: publicdb = SSUser.publicDb(self.createdBy) if Shift.load(core.connect(publicdb), self.id): self.updateIn(publicdb) else: # TODO: copyTo should probably just be store - David self.copyTo(publicdb) privatedb = core.connect(SSUser.privateDb(self.createdBy)) del privatedb[self.id] # we need to delete the private copy out of shiftspace/shared shared = core.connect("shiftspace/shared") del shared[self.id] # TODO: check that we have to force it in order to have it ready for replication - David db = core.connect(publicdb) core.replicate(publicdb, "shiftspace/public") core.replicate(publicdb, "shiftspace/shared") else: privatedb = SSUser.privateDb(self.createdBy) self.store(core.connect(privatedb)) core.replicate(privatedb, "shiftspace/shared") return Shift.joinData(self, self.createdBy)
def publish(self, publishData=None): from server.models.ssuser import SSUser if publishData == None: return self db = core.connect(SSUser.privateDb(self.createdBy)) dbs = [] author = SSUser.read(self.createdBy) oldPublishData = dict(self.items())["publishData"] allowed = [] # get the private status isPrivate = True if publishData and publishData.get("private") != None: isPrivate = publishData.get("private") else: isPrivate = self.isPrivate() # get the dbs being published to publishDbs = (publishData and publishData.get("dbs")) or [] # get the list of dbs the user is actually allowed to publish to allowed = [] if (publishData and isPrivate and len(publishDbs) > 0): from server.models.group import Group allowedGroups = author.writeable() allowed = list(set(allowedGroups).intersection(set(publishDbs))) # upate the private setting, the shift is no longer draft self.publishData.private = isPrivate self.publishData.draft = False # publish or update a copy to group/x, group/y, ... newGroupDbs = [s for s in allowed if s.split("/")[0] == "group"] oldGroupDbs = [ s for s in oldPublishData.get("dbs") if s.split("/")[0] == "group" ] newGroupDbs = list(set(newGroupDbs).difference(set(oldGroupDbs))) if newGroupDbs and len(newGroupDbs) > 0: dbs.extend(list(set(newGroupDbs))) # publish to any user we haven't published to before newUserDbs = [s for s in publishDbs if s.split("/")[0] == "user"] if newUserDbs and len(newUserDbs) > 0: userDbs = list(set(newUserDbs)) dbs.extend(userDbs) self.shareWith([s.split("/")[1] for s in userDbs]) self.publishData.dbs = dbs # store the human readable version targets = publishData.get("targets") if targets: self.publishData.targets = targets # update/add to group dbs self.updateInGroups(oldGroupDbs) self.addToGroups(newGroupDbs) # if public shift # create in user/public, delete from user/private # replicate shiftspace/public to shiftspace/shared if not isPrivate: publicdb = SSUser.publicDb(self.createdBy) if Shift.load(core.connect(publicdb), self.id): self.updateIn(publicdb) else: # TODO: copyTo should probably just be store - David self.copyTo(publicdb) privatedb = core.connect(SSUser.privateDb(self.createdBy)) del privatedb[self.id] # we need to delete the private copy out of shiftspace/shared shared = core.connect("shiftspace/shared") del shared[self.id] # TODO: check that we have to force it in order to have it ready for replication - David db = core.connect(publicdb) core.replicate(publicdb, "shiftspace/public") core.replicate(publicdb, "shiftspace/shared") else: privatedb = SSUser.privateDb(self.createdBy) self.store(core.connect(privatedb)) core.replicate(privatedb, "shiftspace/shared") return Shift.joinData(self, self.createdBy)