def __init__(self, remoteAddress, method, baseUri, path, fileext, params, headers, body): self.remoteAddress = remoteAddress self.method = method self.baseUri = baseUri self.path = path self.fileext = fileext self.params = params self.origParams = params.copy() self.headers = headers self.cookies = SimpleCookie(headers.get("Cookie", "")) self.body = body self.user = None if "session" in self.cookies: self.session = Session.findOne( {"id": self.cookies["session"].value}) if self.session: self.user = User.findOne({ "id": self.session.user, "passwordHash": self.session.passwordHash }) if self.user: self.session.lastAddress = self.remoteAddress self.session.lastUse = datetime.utcnow() self.session.save() self.user.isDisabled = False self.user.save() else: self.session.erase()
def createOne(cls, req, res): password = req.body.pop("password", "") user = User.findOne(req.body) if not user or not checkPassword(password, user.passwordHash): res.status = 400 render(req, res, "session/new.html.bepy") return user.isDisabled = False user.save() session = Session.create({ "user": user.id, "passwordHash": user.passwordHash, "lastAddress": req.remoteAddress, "lastUse": datetime.utcnow() }) res.cookies["session"] = session.id res.cookies["session"]["httponly"] = True res.cookies["session"]["max-age"] = 365 * 24 * 60 * 60 # 1 year should be permanent enough res.status = 201 res.headers["Location"] = "/session/{0}".format(session.id) render(req, res, "redirect-home.html.bepy") return session
def __init__(self, remoteAddress, method, baseUri, path, fileext, params, headers, body): self.remoteAddress = remoteAddress self.method = method self.baseUri = baseUri self.path = path self.fileext = fileext self.params = params self.origParams = params.copy() self.headers = headers self.cookies = SimpleCookie(headers.get("Cookie", "")) self.body = body self.user = None if "session" in self.cookies: self.session = Session.findOne({"id": self.cookies["session"].value}) if self.session: self.user = User.findOne({ "id": self.session.user, "passwordHash": self.session.passwordHash }) if self.user: self.session.lastAddress = self.remoteAddress self.session.lastUse = datetime.utcnow() self.session.save() self.user.isDisabled = False self.user.save() else: self.session.erase()
def createOne(cls, req, res): password = req.body.pop("password", "") user = User.findOne(req.body) if not user or not checkPassword(password, user.passwordHash): res.status = 400 render(req, res, "session/new.html.bepy") return user.isDisabled = False user.save() session = Session.create({ "user": user.id, "passwordHash": user.passwordHash, "lastAddress": req.remoteAddress, "lastUse": datetime.utcnow() }) res.cookies["session"] = session.id res.cookies["session"]["httponly"] = True res.cookies["session"][ "max-age"] = 365 * 24 * 60 * 60 # 1 year should be permanent enough res.status = 201 res.headers["Location"] = "/session/{0}".format(session.id) render(req, res, "redirect-home.html.bepy") return session
def createOne(cls, req, res): if not req.user: res.status = 403 return if not "content" in req.body or len(req.body["content"]) < 1: res.status = 400 return req.body["user"] = req.user.id req.body["timestamp"] = datetime.utcnow() bleat = super(BleatController, cls).createOne(req, res) # Find all mentioned users that want notifications usersToNotify = {} for userId in bleat.mentions: user = User.findOne({"id": userId}) if user.notifyOnMention and not user.isDisabled: usersToNotify[user.id] = user # Find out who we're replying to and send a notification email if needed if bleat.inReplyTo: inReplyTo = Bleat.findOne({"id": bleat.inReplyTo}) inReplyToUser = User.findOne({"id": inReplyTo.user}) if inReplyToUser.notifyOnReply and not inReplyToUser.isDisabled: if inReplyToUser.id in usersToNotify: del usersToNotify[ inReplyToUser. id] # Don't send two emails to a single person sendEmail( inReplyToUser.email, "Bitter Bleat Reply Notification", u"{0} has responded to you in their bleat:\n{1}\n\n{2}". format(req.user.name or req.user.username, bleat.content, u"{0}/bleat/{1}".format(req.baseUri, bleat.id))) # Send the mentioned notifications for user in usersToNotify.values(): sendEmail( user.email, "Bitter Mention Notification", u"{0} has mentioned you in their bleat:\n{1}\n\n{2}".format( req.user.name or req.user.username, bleat.content, u"{0}/bleat/{1}".format(req.baseUri, bleat.id))) return bleat
def create(cls, properties): attachments = properties.pop("attachments", []) bleat = super(Bleat, cls).create(properties) # Find all mentioned users mentions = set() startIndex = 0 while startIndex < len(bleat.content): startIndex = bleat.content.find("@", startIndex) if startIndex < 0: break startIndex += 1 # Could binary search, but usernames generally aren't very long username = "" endIndex = startIndex + 1 try: for endIndex in xrange(startIndex + 1, len(bleat.content) + 1): username = canonicaliseUsername( bleat.content[startIndex:endIndex]) except ValueError: pass if username: user = User.findOne({"canonicalUsername": username}) if user: mentions.add(user.id) startIndex = endIndex if mentions: db.execute( "insert into bleat_mention (bleat, user) values {0}".format( ", ".join(["(?, ?)"] * len(mentions))), list( itertools.chain.from_iterable( itertools.product((bleat.id, ), mentions)))) setattr(bleat, "mentions", mentions) if attachments: db.execute( "insert into bleat_attachment (bleat, file) values {0}".format( ", ".join(["(?, ?)"] * len(attachments))), list( itertools.chain.from_iterable( itertools.product((bleat.id, ), attachments)))) setattr(bleat, "attachments", attachments) return bleat