def post(self, urlname): node = Node.get(urlname=urlname) if not node: return self.redirect_next_url() user = self.current_user kwargs = self.request.arguments try: selected = kwargs.get("parent_name") print(selected) except: selected = [n.name for n in node.parent_nodes] kwargs = { "name": [node.name], "urlname": [node.urlname], "description": [node.description], "style": [node.style], } form = NodeEditForm.init(Node.get_node_choices(), selected, node=node, **kwargs) if form.validate(): node = form.save(user, node=node) result = {"status": "success", "message": "节点修改成功", "node_url": node.url} if self.is_ajax: return self.write(result) self.flash_message(**result) return self.redirect(node.url) if self.is_ajax: return self.write(form.result) return self.render("node/edit.html", form=form, node=node)
def post(self, urlname): node = Node.get(urlname=urlname) if not node: return self.redirect_next_url() user = self.current_user kwargs = self.request.arguments try: selected = kwargs.get('parent_name') print(selected) except: selected = [n.name for n in node.parent_nodes] kwargs = { 'name': [node.name], 'urlname': [node.urlname], 'description': [node.description], 'style': [node.style] } form = NodeEditForm.init(Node.get_node_choices(), selected, node=node, **kwargs) if form.validate(): node = form.save(user, node=node) result = { 'status': 'success', 'message': '节点修改成功', 'node_url': node.url } if self.is_ajax: return self.write(result) self.flash_message(**result) return self.redirect(node.url) if self.is_ajax: return self.write(form.result) return self.render("node/edit.html", form=form, node=node)
def get(self): node_id = int(self.get_argument("node_id", 0)) node = Node.get(id=node_id) if node: selected = [node.name] else: selected = [] form = NodeForm.init(Node.get_node_choices(), selected) return self.render("node/create.html", form=form)
def get(self): node_id = int(self.get_argument('node_id', 0)) node = Node.get(id=node_id) if node: selected = [node.name] else: selected = [] form = NodeForm.init(Node.get_node_choices(), selected) return self.render("node/create.html", form=form)
def get(self, urlname): node = Node.get(urlname=urlname) if node: selected = [n.name for n in node.parent_nodes] else: return self.redirect_next_url() kwargs = {'name': [node.name], 'urlname': [node.urlname], 'description': [node.description], 'style': [node.style]} form = NodeEditForm.init(Node.get_node_choices(), selected, node=node, **kwargs) return self.render("node/edit.html", form=form, node=node)
def get(self): node_id = force_int(self.get_argument('node_id', 0), 0) node = Node.get(id=node_id) if node: selected = node.name else: selected = None choices = Node.get_node_choices() form = TopicForm.init(choices=choices, selected=selected) return self.render("topic/create.html", form=form, node=node)
def get(self): page = force_int(self.get_argument('page', 1), 1) page_count = 0 nodes = [] category = self.get_argument('category', None) hot_nodes = Node.get_nodes(category='hot', limit=8) new_nodes = Node.get_nodes(category='new', limit=8) url = '/nodes' if category == 'all': nodes = Node.get_nodes(category='all', page=page) node_count = orm.count(Node.get_nodes(page=None)) page_count = (node_count + config.node_paged - 1) // config.node_paged url = '/nodes?category=' + category return self.render("node/show.html", hot_nodes=hot_nodes, new_nodes=new_nodes, nodes=nodes, category=category, page=page, page_count=page_count, url=url)
def save(self, user, role=None): data = self.data try: parent_name = data.pop('parent_name') except: parent_name = None data.update({'user_id': user.id}) node = Node(**data).save() if not parent_name: if not NodeNode.get(parent_id=1, child_id=node.id): NodeNode(parent_id=1, child_id=node.id).save() else: for name in parent_name: parent = Node.get(name=name) if parent: if not NodeNode.get(parent_id=parent.id, child_id=node.id): NodeNode(parent_id=parent.id, child_id=node.id).save() return node
def save(self, user, topic=None): data = self.data node_name = data.pop('node_name', None) if not node_name: logging.info('no node_name in form data, data: %s', data) if not self.node: self.node = Node.get(name=node_name) if not self.node: logging.info('node is None in form instance, self: %s', self) content = unicode(data.get('content')) data.update({ 'user_id': user.id, 'node_id': self.node.id, 'content': strip_xss_tags(content) }) if topic: category = 'edit' pre_node_id = topic.node_id pre_title = topic.title pre_content = topic.content cur_node_id = data.get('node_id') cur_title = data.get('title') cur_content = data.get('content') changed = 0 if pre_node_id != cur_node_id: topic.node.topic_count -= 1 self.node.topic_count += 1 diff_content = '主题节点从' + '<a class="node" href="' +\ topic.node.url + '">' + topic.node.name +\ '</a>移动到<a class="node" href="' + self.node.url + '">' +\ self.node.name + '</a>' changed = 1 if pre_title != cur_title or pre_content != cur_content: content1 = '<p><h2>' + pre_title + '</h2></p>' + pre_content content2 = '<p><h2>' + cur_title + '</h2></p>' + cur_content diff_content = ghdiff.diff(content1, content2, css=None) changed = 1 if changed == 1: topic.node_id = cur_node_id topic.title = cur_title topic.content = cur_content History(user_id=user.id, content=diff_content, topic_id=topic.id).save() else: return topic else: category = 'create' topic = Topic(**data) return topic.save(category=category)
def get(self, topic_id): topic = Topic.get(id=topic_id) if topic and\ (topic.author == self.current_user or self.current_user.is_admin): selected = topic.node.name else: return self.redirect_next_url() choices = Node.get_node_choices() kwargs = {'node_name': [selected], 'title': [topic.title], 'content': [topic.content]} form = TopicForm.init(choices=choices, selected=selected, **kwargs) return self.render("topic/create.html", form=form, node=topic.node)
def post(self, node_id): category = self.get_argument('category', None) node = Node.get(id=node_id) if not node: return self.redirect_next_url() if not self.request.files or 'myimage' not in self.request.files: self.write({"status": "error", "message": "对不起,请选择图片"}) return image_type_list = [ 'image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/bmp', 'image/x-png' ] icon_type_list = [ 'image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/bmp', 'image/x-png', 'image/ico .ico', 'image/x-icon' ] send_file = self.request.files['myimage'][0] if category != 'icon' and send_file[ 'content_type'] not in image_type_list: self.write({ "status": "error", "message": "对不起,仅支持 jpg, jpeg, bmp, gif, png\ 格式的图片" }) return if category == 'icon' and send_file[ 'content_type'] not in icon_type_list: self.write({ "status": "error", "message": "对不起,仅支持 ico, jpg, jpeg, bmp, gif, png\ 格式的图片" }) return if len(send_file['body']) > 6 * 1024 * 1024: self.write({"status": "error", "message": "对不起,请上传6M以下的图片"}) return tmp_file = tempfile.NamedTemporaryFile(delete=True) tmp_file.write(send_file['body']) tmp_file.seek(0) try: image_one = Image.open(tmp_file.name) except IOError, error: logging.info(error) logging.info('+' * 30 + '\n') logging.info(self.request.headers) tmp_file.close() self.write({"status": "error", "message": "对不起,此文件不是图片"}) return
def get(self): page = force_int(self.get_argument("page", 1), 1) page_count = 0 nodes = [] category = self.get_argument("category", None) hot_nodes = Node.get_nodes(category="hot", limit=8) new_nodes = Node.get_nodes(category="new", limit=8) url = "/nodes" if category == "all": nodes = Node.get_nodes(category="all", page=page) node_count = orm.count(Node.get_nodes(page=None)) page_count = (node_count + config.node_paged - 1) // config.node_paged url = "/nodes?category=" + category return self.render( "node/show.html", hot_nodes=hot_nodes, new_nodes=new_nodes, nodes=nodes, category=category, page=page, page_count=page_count, url=url, )
def save(self, user, topic=None): data = self.data node_name = data.pop('node_name', None) if not node_name: logging.info('no node_name in form data, data: %s', data) if not self.node: self.node = Node.get(name=node_name) if not self.node: logging.info('node is None in form instance, self: %s', self) content = str(data.get('content')) data.update({'user_id': user.id, 'node_id': self.node.id, 'content': strip_xss_tags(content)}) if topic: category = 'edit' pre_node_id = topic.node_id pre_title = topic.title pre_content = topic.content cur_node_id = data.get('node_id') cur_title = data.get('title') cur_content = data.get('content') changed = 0 if pre_node_id != cur_node_id: topic.node.topic_count -= 1 self.node.topic_count += 1 diff_content = '主题节点从' + '<a class="node" href="' +\ topic.node.url + '">' + topic.node.name +\ '</a>移动到<a class="node" href="' + self.node.url + '">' +\ self.node.name + '</a>' changed = 1 if pre_title != cur_title or pre_content != cur_content: content1 = '<p><h2>' + pre_title + '</h2></p>' + pre_content content2 = '<p><h2>' + cur_title + '</h2></p>' + cur_content diff_content = ghdiff.diff(content1, content2, css=None) changed = 1 if changed == 1: topic.node_id = cur_node_id topic.title = cur_title topic.content = cur_content History(user_id=user.id, content=diff_content, topic_id=topic.id).save() else: return topic else: category = 'create' topic = Topic(**data) return topic.save(category=category)
def post(self): node_id = force_int(self.get_argument('node_id', 0), 0) node = Node.get(id=node_id) user = self.current_user form = TopicForm(self.request.arguments) if form.validate(): topic = form.save(user=user) topic.put_notifier() result = {'status': 'success', 'message': '主题创建成功', 'topic_url': topic.url} if self.is_ajax: return self.write(result) self.flash_message(**result) return self.redirect(topic.url) if self.is_ajax: return self.write(form.result) return self.render("topic/create.html", form=form, node=node)
def post(self, node_id): category = self.get_argument('category', None) node = Node.get(id=node_id) if not node: return self.redirect_next_url() if not self.request.files or 'myimage' not in self.request.files: self.write({"status": "error", "message": "对不起,请选择图片"}) return image_type_list = ['image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/bmp', 'image/x-png'] icon_type_list = ['image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/bmp', 'image/x-png', 'image/ico .ico', 'image/x-icon'] send_file = self.request.files['myimage'][0] if category != 'icon' and send_file['content_type'] not in image_type_list: self.write({"status": "error", "message": "对不起,仅支持 jpg, jpeg, bmp, gif, png\ 格式的图片"}) return if category == 'icon' and send_file['content_type'] not in icon_type_list: self.write({"status": "error", "message": "对不起,仅支持 ico, jpg, jpeg, bmp, gif, png\ 格式的图片"}) return if len(send_file['body']) > 6 * 1024 * 1024: self.write({"status": "error", "message": "对不起,请上传6M以下的图片"}) return tmp_file = tempfile.NamedTemporaryFile(delete=True) tmp_file.write(send_file['body']) tmp_file.seek(0) try: image_one = Image.open(tmp_file.name) except IOError, error: logging.info(error) logging.info('+' * 30 + '\n') logging.info(self.request.headers) tmp_file.close() self.write({"status": "error", "message": "对不起,此文件不是图片"}) return
def save(self, user, role=None, node=None): data = self.data try: parent_name = data.pop('parent_name') except: parent_name = None data.update({'user_id': user.id}) nns = NodeNode.select(lambda rv: rv.child_id == node.id) for nn in nns: nn.delete() if not parent_name: if not NodeNode.get(parent_id=1, child_id=node.id): NodeNode(parent_id=1, child_id=node.id).save() else: for name in parent_name: parent = Node.get(name=unicode(name)) if parent: if not NodeNode.get(parent_id=parent.id, child_id=node.id): NodeNode(parent_id=parent.id, child_id=node.id).save() node = node.update(data) return node
def post(self, node_id): category = self.get_argument('category', None) node = Node.get(id=node_id) if not node: return self.redirect_next_url() if not self.request.files or 'myimage' not in self.request.files: self.write({"status": "error", "message": "对不起,请选择图片"}) return image_type_list = [ 'image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/bmp', 'image/x-png' ] icon_type_list = [ 'image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/bmp', 'image/x-png', 'image/ico .ico', 'image/x-icon' ] send_file = self.request.files['myimage'][0] if category != 'icon' and send_file[ 'content_type'] not in image_type_list: self.write({ "status": "error", "message": "对不起,仅支持 jpg, jpeg, bmp, gif, png\ 格式的图片" }) return if category == 'icon' and send_file[ 'content_type'] not in icon_type_list: self.write({ "status": "error", "message": "对不起,仅支持 ico, jpg, jpeg, bmp, gif, png\ 格式的图片" }) return if len(send_file['body']) > 6 * 1024 * 1024: self.write({"status": "error", "message": "对不起,请上传6M以下的图片"}) return tmp_file = tempfile.NamedTemporaryFile(delete=True) tmp_file.write(send_file['body']) tmp_file.seek(0) try: image_one = Image.open(tmp_file.name) except IOError as error: logging.info(error) logging.info('+' * 30 + '\n') logging.info(self.request.headers) tmp_file.close() self.write({"status": "error", "message": "对不起,此文件不是图片"}) return width = image_one.size[0] height = image_one.size[1] if width < 20 or height < 20 or width > 30000 or height > 30000: tmp_file.close() self.write({ "status": "error", "message": "对不起,请上传长宽在20px~30000px之间的图片!" }) return user = self.current_user upload_path = sys.path[0] + "/static/upload/" + get_year() + '/' +\ get_month() + "/" if not os.path.exists(upload_path): try: os.system('mkdir -p %s' % upload_path) except: pass timestamp = str(int(time.time())) + \ ''.join(random.sample('ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba', 6)) + '_' + str(user.id) image_format = send_file['filename'].split('.').pop().lower() tmp_name = upload_path + timestamp + '.' + image_format image_one.save(tmp_name) tmp_file.close() path = '/' +\ '/'.join(tmp_name.split('/')[tmp_name.split('/').index("static"):]) del_path = None if category == 'head': del_path = node.head_img node.head_img = path msg = '节点头部背景设置成功' data = {'path': path, 'category': 'head'} elif category == 'icon': image_two = image_one.resize((75, 75), Image.ANTIALIAS) tmp_name2 = upload_path + timestamp + 'x75.' + image_format image_two.save(tmp_name2) try: os.system('rm -f %s%s' % (sys.path[0], path)) except: pass path = '/' +\ '/'.join(tmp_name2.split('/')[tmp_name2.split('/').index("static"):]) del_path = node.icon_img node.icon_img = path msg = '节点图标设置成功' data = {'path': path, 'category': 'icon'} elif category == 'background': del_path = node.background_img node.background_img = path msg = '节点背景设置成功' data = {'path': path, 'category': 'background'} else: msg = '图片上传成功' data = {'path': path} if del_path: try: os.system('rm -f %s%s' % (sys.path[0], del_path)) except: pass return self.send_success_result(msg=msg, **data)
def init_node(): from collipa.models import Node if not Node.get(id=1): Node(name=u'根节点', urlname='root', description=u'一切的根源').save()
def validate_node_name(self, field): node_name = str(self.node_name.data) node = Node.get(name=node_name) if not node: raise ValidationError('不存在此节点') self.node = node
def get(self, urlname, category="all"): node = Node.get(urlname=urlname) if not node: raise tornado.web.HTTPError(404) page = force_int(self.get_argument("page", 1), 1) action = self.get_argument("action", None) tag = self.get_argument("tag", None) if tag: if tag == "description": result = { "status": "success", "message": "简介传输成功", "node_description": node.description, "node_topic_count": node.topic_count, "node_follow_count": node.follow_count, } return self.write(result) if tag == "relationship": parent_nodes = node.parent_nodes child_nodes = node.child_nodes sibling_nodes = node.sibling_nodes parent_json = [] children_json = [] sibling_json = [] for p in parent_nodes: parent_json.append( dict( id=p.id, name=p.name, url=p.url, description=p.description, summary=p.summary, urlname=p.urlname, icon=p.icon, ) ) for c in child_nodes: children_json.append( dict( id=c.id, name=c.name, url=c.url, description=c.description, summary=c.summary, urlname=c.urlname, icon=c.icon, ) ) for s in sibling_nodes: sibling_json.append( dict( id=s.id, name=s.name, url=s.url, description=s.description, summary=s.summary, urlname=s.urlname, icon=s.icon, ) ) result = { "status": "success", "parent_nodes": parent_json, "child_nodes": children_json, "sibling_nodes": sibling_json, } return self.write(result) user = self.current_user if action and user: if action == "follow": result = user.follow(node_id=node.id) if self.is_ajax: return self.write(result) self.flash_message(**result) return self.redirect_next_url() topic_count = orm.count(node.get_topics(page=None, category=category)) page_count = (topic_count + config.reply_paged - 1) // config.reply_paged url = node.url + "?category=" + category topics = node.get_topics(page=page, category=category) return self.render( "node/index.html", node=node, topics=topics, category=category, page=page, page_count=page_count, url=url )
def post(self, node_id): category = self.get_argument("category", None) node = Node.get(id=node_id) if not node: return self.redirect_next_url() if not self.request.files or "myimage" not in self.request.files: self.write({"status": "error", "message": "对不起,请选择图片"}) return image_type_list = ["image/gif", "image/jpeg", "image/pjpeg", "image/png", "image/bmp", "image/x-png"] icon_type_list = [ "image/gif", "image/jpeg", "image/pjpeg", "image/png", "image/bmp", "image/x-png", "image/ico .ico", "image/x-icon", ] send_file = self.request.files["myimage"][0] if category != "icon" and send_file["content_type"] not in image_type_list: self.write( { "status": "error", "message": "对不起,仅支持 jpg, jpeg, bmp, gif, png\ 格式的图片", } ) return if category == "icon" and send_file["content_type"] not in icon_type_list: self.write( { "status": "error", "message": "对不起,仅支持 ico, jpg, jpeg, bmp, gif, png\ 格式的图片", } ) return if len(send_file["body"]) > 6 * 1024 * 1024: self.write({"status": "error", "message": "对不起,请上传6M以下的图片"}) return tmp_file = tempfile.NamedTemporaryFile(delete=True) tmp_file.write(send_file["body"]) tmp_file.seek(0) try: image_one = Image.open(tmp_file.name) except IOError as error: logging.info(error) logging.info("+" * 30 + "\n") logging.info(self.request.headers) tmp_file.close() self.write({"status": "error", "message": "对不起,此文件不是图片"}) return width = image_one.size[0] height = image_one.size[1] if width < 20 or height < 20 or width > 30000 or height > 30000: tmp_file.close() self.write({"status": "error", "message": "对不起,请上传长宽在20px~30000px之间的图片!"}) return user = self.current_user upload_path = sys.path[0] + "/static/upload/" + get_year() + "/" + get_month() + "/" if not os.path.exists(upload_path): try: os.system("mkdir -p %s" % upload_path) except: pass timestamp = ( str(int(time.time())) + "".join(random.sample("ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba", 6)) + "_" + str(user.id) ) image_format = send_file["filename"].split(".").pop().lower() tmp_name = upload_path + timestamp + "." + image_format image_one.save(tmp_name) tmp_file.close() path = "/" + "/".join(tmp_name.split("/")[tmp_name.split("/").index("static") :]) del_path = None if category == "head": del_path = node.head_img node.head_img = path msg = "节点头部背景设置成功" data = {"path": path, "category": "head"} elif category == "icon": image_two = image_one.resize((75, 75), Image.ANTIALIAS) tmp_name2 = upload_path + timestamp + "x75." + image_format image_two.save(tmp_name2) try: os.system("rm -f %s%s" % (sys.path[0], path)) except: pass path = "/" + "/".join(tmp_name2.split("/")[tmp_name2.split("/").index("static") :]) del_path = node.icon_img node.icon_img = path msg = "节点图标设置成功" data = {"path": path, "category": "icon"} elif category == "background": del_path = node.background_img node.background_img = path msg = "节点背景设置成功" data = {"path": path, "category": "background"} else: msg = "图片上传成功" data = {"path": path} if del_path: try: os.system("rm -f %s%s" % (sys.path[0], del_path)) except: pass return self.send_success_result(msg=msg, **data)
def get(self, urlname, category='all'): node = Node.get(urlname=urlname) if not node: raise tornado.web.HTTPError(404) page = force_int(self.get_argument('page', 1), 1) action = self.get_argument('action', None) tag = self.get_argument('tag', None) if tag: if tag == 'description': result = { 'status': 'success', 'message': '简介传输成功', 'node_description': node.description, 'node_topic_count': node.topic_count, 'node_follow_count': node.follow_count, } return self.write(result) if tag == 'relationship': parent_nodes = node.parent_nodes child_nodes = node.child_nodes sibling_nodes = node.sibling_nodes parent_json = [] children_json = [] sibling_json = [] for p in parent_nodes: parent_json.append(dict(id=p.id, name=p.name, url=p.url, description=p.description, summary=p.summary, urlname=p.urlname, icon=p.icon)) for c in child_nodes: children_json.append(dict(id=c.id, name=c.name, url=c.url, description=c.description, summary=c.summary, urlname=c.urlname, icon=c.icon)) for s in sibling_nodes: sibling_json.append(dict(id=s.id, name=s.name, url=s.url, description=s.description, summary=s.summary, urlname=s.urlname, icon=s.icon)) result = { 'status': 'success', 'parent_nodes': parent_json, 'child_nodes': children_json, 'sibling_nodes': sibling_json } return self.write(result) user = self.current_user if action and user: if action == 'follow': result = user.follow(node_id=node.id) if self.is_ajax: return self.write(result) self.flash_message(**result) return self.redirect_next_url() topic_count = orm.count(node.get_topics(page=None, category=category)) page_count = (topic_count + config.reply_paged - 1) // config.reply_paged url = node.url + '?category=' + category topics = node.get_topics(page=page, category=category) return self.render("node/index.html", node=node, topics=topics, category=category, page=page, page_count=page_count, url=url)
def validate_node_name(self, field): node_name = unicode(self.node_name.data) node = Node.get(name=node_name) if not node: raise ValidationError('不存在此节点') self.node = node
def validate_urlname(self, field): data = field.data.lower() node = Node.get(urlname=data) if node and node != self.node: raise ValidationError('此节点地址已存在')
def get(self, urlname, category='all'): node = Node.get(urlname=urlname) if not node: raise tornado.web.HTTPError(404) page = force_int(self.get_argument('page', 1), 1) action = self.get_argument('action', None) tag = self.get_argument('tag', None) if tag: if tag == 'description': result = { 'status': 'success', 'message': '简介传输成功', 'node_description': node.description, 'node_topic_count': node.topic_count, 'node_follow_count': node.follow_count, } return self.write(result) if tag == 'relationship': parent_nodes = node.parent_nodes child_nodes = node.child_nodes sibling_nodes = node.sibling_nodes parent_json = [] children_json = [] sibling_json = [] for p in parent_nodes: parent_json.append( dict(id=p.id, name=p.name, url=p.url, description=p.description, summary=p.summary, urlname=p.urlname, icon=p.icon)) for c in child_nodes: children_json.append( dict(id=c.id, name=c.name, url=c.url, description=c.description, summary=c.summary, urlname=c.urlname, icon=c.icon)) for s in sibling_nodes: sibling_json.append( dict(id=s.id, name=s.name, url=s.url, description=s.description, summary=s.summary, urlname=s.urlname, icon=s.icon)) result = { 'status': 'success', 'parent_nodes': parent_json, 'child_nodes': children_json, 'sibling_nodes': sibling_json } return self.write(result) user = self.current_user if action and user: if action == 'follow': result = user.follow(node_id=node.id) if self.is_ajax: return self.write(result) self.flash_message(**result) return self.redirect_next_url() topic_count = orm.count(node.get_topics(page=None, category=category)) page_count = (topic_count + config.reply_paged - 1) // config.reply_paged url = node.url + '?category=' + category topics = node.get_topics(page=page, category=category) return self.render("node/index.html", node=node, topics=topics, category=category, page=page, page_count=page_count, url=url)
def validate_urlname(self, field): data = field.data.lower() if Node.get(urlname=data): raise ValidationError('此节点地址已存在')