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, userId, groupId, otherId, level): from server.models.ssuser import SSUser from server.models.group import Group db = core.connect() if not groupId: raise MissingGroupError if not userId: raise MissingCreatorError if Permission.readByUserAndGroup(otherId, groupId): raise PermissionAlreadyExistsError adminable = [row.value for row in Permission.by_adminable(db, key=userId).rows] allowed = groupId in adminable if not allowed: theUser = SSUser.read(userId) allowed = theUser.isAdmin() if not allowed: theGroup = Group.read(groupId) allowed = theUser.isOwnerOf(theGroup) if not allowed: raise CreateEventPermissionError json = { "createdBy": userId, "userId": otherId, "groupId": groupId, "level": level } newPermission = Permission(**utils.clean(json)) newPermission.store(db) return newPermission
def create(cls, userJson): """ Create a new user document. Also creates the three databases (user/public, user/private, user/inbox) to allow for peer-to-peer distribution. Parameters: userJson - a dictionary of fields and their values. """ from server.models.shift import Shift from server.models.message import Message server = core.server() db = core.connect() if userJson.get("passwordVerify"): del userJson["passwordVerify"] if userJson.get("password"): userJson['password'] = utils.md5hash(userJson['password']) if userJson.get("email"): hashedEmail = utils.md5hash(userJson["email"]) userJson["gravatar"] = "http://www.gravatar.com/avatar/%s?s=32" % hashedEmail userJson["gravatarLarge"] = "http://www.gravatar.com/avatar/%s?s=60" % hashedEmail newUser = SSUser(**utils.clean(userJson)) newUser.store(db) # user's public shifts, will be replicated to shiftspace and user/feed server.create(SSUser.publicDb(newUser.id)) # all of the user's shifts as well as subscribed content server.create(SSUser.privateDb(newUser.id)) # all of the user's messages server.create(SSUser.messagesDb(newUser.id)) # sync views Message.by_created.sync(server[SSUser.messagesDb(newUser.id)]) return newUser
def create(cls, userId, groupId, otherId, level): from server.models.ssuser import SSUser from server.models.group import Group db = core.connect() if not groupId: raise MissingGroupError if not userId: raise MissingCreatorError if Permission.readByUserAndGroup(otherId, groupId): raise PermissionAlreadyExistsError adminable = [ row.value for row in Permission.by_adminable(db, key=userId).rows ] allowed = groupId in adminable if not allowed: theUser = SSUser.read(userId) allowed = theUser.isAdmin() if not allowed: theGroup = Group.read(groupId) allowed = theUser.isOwnerOf(theGroup) if not allowed: raise CreateEventPermissionError json = { "createdBy": userId, "userId": otherId, "groupId": groupId, "level": level } newPermission = Permission(**utils.clean(json)) newPermission.store(db) return newPermission
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 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 update(self, fields): fields = utils.clean(fields) db = core.connect() if fields.get("bio"): self.bio = fields.get("bio") if fields.get("url"): self.url = fields.get("url") if fields.get("displayName"): self.displayName = fields.get("displayName") if fields.get("fullName"): self.fullName = fields.get("fullName") self.store(db) 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 create(cls, groupJson): from server.models.permission import Permission from server.models.ssuser import SSUser userId = groupJson["createdBy"] # create the group metadata newGroup = Group(**utils.clean(groupJson)) newGroup.source.server = core.serverName() newGroup.source.database = Group.db(newGroup.id) # save the group metadata to the master db newGroup.store(core.connect()) # create the root permission for this group Permission.create(userId, newGroup.id, userId, level=4) # create the group db server = core.sharedServer() server.create(Group.db(newGroup.id)) # copy the group metadata to the db newGroup.copyTo(Group.db(newGroup.id)) return newGroup
def create(cls, userJson): """ Create a new user document. Also creates the three databases (user/public, user/private, user/inbox) to allow for peer-to-peer distribution. Parameters: userJson - a dictionary of fields and their values. """ from server.models.shift import Shift from server.models.message import Message server = core.sharedServer() db = core.connect() if userJson.get("passwordVerify"): del userJson["passwordVerify"] if userJson.get("password"): userJson['password'] = utils.md5hash(userJson['password']) if userJson.get("email"): hashedEmail = utils.md5hash(userJson["email"]) userJson[ "gravatar"] = "http://www.gravatar.com/avatar/%s?s=32" % hashedEmail userJson[ "gravatarLarge"] = "http://www.gravatar.com/avatar/%s?s=60" % hashedEmail newUser = SSUser(**utils.clean(userJson)) newUser.store(db) # user's public shifts, will be replicated to shiftspace and user/feed server.create(SSUser.publicDb(newUser.id)) # all of the user's shifts as well as subscribed content server.create(SSUser.privateDb(newUser.id)) # all of the user's messages server.create(SSUser.messagesDb(newUser.id)) # sync views Message.by_created.sync(server[SSUser.messagesDb(newUser.id)]) return newUser