def populate(self, data): if not isinstance(data, dict): raise SchemaTypeError() for k, t in self.structure.iteritems(): # from form which all val are unicode if k in data and type(data[k]) is unicode and data[k]: if t is unicode: self[k] = force_unicode(data[k]) if k in self.sanitized_fields: self[k] = sanitize(self[k]) elif t is list: self[k] = listify(data[k], ',') elif t is datetime.datetime: self[k] = datetime.datetime.strptime(data[k], '%d/%m/%Y') elif t is bool: if data[k] == 'False': self[k] = False else: self[k] = bool(int(data[k])) elif t is int: self[k] = int(re.sub('[.,]*', '', data[k])) else: self[k] = data[k] elif k in data: self[k] = data[k] elif t is bool: self[k] = False self.update_html(data)
def post(self): next = self.get_argument("next", None) text = self.get_argument("text") content_id = self.get_argument("content_id") comment = { "id": unicode(uuid.uuid4()), "user": DBRef(User.collection_name, self.current_user._id), "created_at": datetime.datetime.utcnow(), "text": sanitize(text), } parent = self.get_argument("parent", None) if parent is None: comment.update({"responses": []}) html = self.render_string( "statics/item-comment", user=self.current_user, comment=comment, commenters={self.current_user._id: self.current_user}, item={"_id": content_id}, ) append_to = "#commentsbox" else: html = "<p>%s - %s</p>" % (comment["text"], self.current_user["username"]) append_to = "#resp-" + parent js_add_comment = """ function add_resp(id, parent,comment) { spec = {_id:id} c = db.contents.findOne(spec); comments = c['comments']; if (parent) { idx = null; for(var i=0; i< comments.length; i++) { if (comments[i]['id'] == parent) {idx=i; break;} } if (idx != null) { comments[idx]['responses'].push(comment); c['comments'] = comments db.contents.save(c); } } else { db.contents.update(spec, {$push: {"comments": comment}}); } } """ Content.collection.database.eval(js_add_comment, content_id, parent, comment) if self.is_xhr(): data = {"append": True, "html": html, "target": append_to} return self.json_response(None, "OK", data) else: self.redirect(self.get_argument("next"))
def post(self): next = self.get_argument("next", None) text = self.get_argument("text") content_id = self.get_argument("content_id") comment = {"id": unicode(uuid.uuid4()), 'user': DBRef(User.collection_name, self.current_user._id), 'created_at': datetime.datetime.utcnow(), 'text': sanitize(text), } parent = self.get_argument("parent", None) if parent is None: comment.update({"responses": []}) html = self.render_string("statics/item-comment", user=self.current_user, comment=comment, commenters={self.current_user._id: self.current_user}, item={'_id': content_id} ) append_to = "#commentsbox" else: html = "<p>%s - %s</p>" % (comment['text'], self.current_user['username']) append_to = "#resp-" + parent js_add_comment = """ function add_resp(id, parent,comment) { spec = {_id:id} c = db.contents.findOne(spec); comments = c['comments']; if (parent) { idx = null; for(var i=0; i< comments.length; i++) { if (comments[i]['id'] == parent) {idx=i; break;} } if (idx != null) { comments[idx]['responses'].push(comment); c['comments'] = comments db.contents.save(c); } } else { db.contents.update(spec, {$push: {"comments": comment}}); } } """ Content.collection.database.eval(js_add_comment, content_id, parent, comment) if self.is_xhr(): data = {'append':True, 'html': html, 'target': append_to} return self.json_response(None, "OK", data) else: self.redirect(self.get_argument('next'))
def post(self, ch): js_insert = """ function insertNewMessage(ch, newMsg) { c = db.chats.findOne({_id:ch}); id = c? (c['messages'].length + 1) : 1; newMsg['id'] = id; db.chats.update({_id:ch}, {$push: {messages:newMsg}}, true); return id; } """ message = { "from": self.current_user["username"], "avatar": self.current_user["avatar"], "body": sanitize(self.get_argument("body")), "date": datetime.datetime.isoformat(datetime.datetime.utcnow()) } id = Chat.collection.database.eval(js_insert, ch, message) message["id"] = id self.new_messages(ch, [message]) self.write(message)