def like(req): r = { 'err':'invalid' } if req.get_method() == "POST": blog_id = req.get_form_var('bid', '') single = req.get_form_var('single', '0') single = single == '1' blog = Blog.get(blog_id) if blog and req.user and not blog.is_liked(req.user.id): blog.like(req.user.id) blog = Blog.get(blog.id) r = { 'err':'ok', 'inner_html': stf("/blog/utils.html", "blog_ui_inline", b=blog, req=req, single=single), } for t in blog.topics: html = str(stf("/blog/utils.html", "blog_ui_inline", b=blog, single=False, req=req)) data = { 'blog_id': blog.id, 'inner_html': html } publish_channel_msg('me-topic-%s' % t.id, data) return json.dumps(r)
def comment(req): r = { 'err':'invalid' } if req.get_method() == "POST": blog_id = req.get_form_var('bid', '') blog = Blog.get(blog_id) content = req.get_form_var('content', '').strip() single = req.get_form_var('single', '0') single = single == '1' upload_file = req.get_form_var("update_file", None) #print 'j comment', blog_id, content if blog and req.user and content: filename = '' ftype = '' if upload_file: filename = upload_file.tmp_filename ftype = upload_file.content_type blog.comment(req.user.id, content, filename, ftype) blog = Blog.get(blog.id) r = { 'err':'ok', 'inner_html': stf("/blog/utils.html", "blog_ui_inline", b=blog, req=req, single=single), } for t in blog.topics: html = str(stf("/blog/utils.html", "blog_ui_inline", b=blog, single=False, req=req)) data = { 'blog_id': blog.id, 'inner_html': html } publish_channel_msg('me-topic-%s' % t.id, data) return json.dumps(r)
def remove(self, req): blog = self.blog if blog.btype == blog.TYPE_THREAD: return req.redirect("/thread/%s/remove" % blog.id) if req.user and req.user.id == self.blog.user_id: Blog.remove(self.blog.id, req.user.id) return req.redirect("/update")
def update_photo_data(self, data): success = False old_id = self.photo_id try: new_id = old_id + 1 doubanfs.set("/me/card/%s/photo/%s/%s" % (self.id, new_id, Cate.ORIGIN), data) from webapp.models.utils import scale d = scale(data, Cate.LARGE, DEFAULT_CONFIG) doubanfs.set("/me/card/%s/photo/%s/%s" % (self.id, new_id, Cate.LARGE), d) print "update photo success photo_id=%s" % new_id store.execute("update me_card set `photo`=%s where `user_id`=%s", (new_id, self.id)) store.commit() success = True except: print "doubanfs write fail!!! %s" % self.id self.photo_id = old_id store.execute("update me_card set `photo`=`photo`-1 where `user_id`=%s", self.id) store.commit() doubanfs.delete("/me/card/%s/photo/%s/%s" % (self.id, new_id, Cate.LARGE)) doubanfs.delete("/me/card/%s/photo/%s/%s" % (self.id, new_id, Cate.ORIGIN)) print 'rollback photo to old_id', old_id if success: Notify.new(self.id, self.id, Notify.TYPE_CHANGE_PHOTO, extra={'photo_id':new_id}) print "send change photo blog" from webapp.models.blog import Blog Blog.new(self.id, Blog.TYPE_BLOG, Blog.BLOG_ICON, extra={'photo_id':new_id}) doubanmc.delete(self.MC_KEY % self.id) doubanmc.delete(self.MC_KEY % self.uid)
def send_blog(cls, card_id, author_id, ntype, extra={}, ctime=None): from webapp.models.blog import Blog TYPES = [cls.TYPE_LIKE, cls.TYPE_TAG, cls.TYPE_REQUEST_PHOTO, cls.TYPE_REQUEST_CHANGE_PHOTO, cls.TYPE_BADAGE, cls.TYPE_AWARD_VOTED, cls.TYPE_QUESTION, cls.TYPE_PHOTO_LIKE, cls.TYPE_PHOTO_TAG, cls.TYPE_REC, cls.TYPE_CREATE_GROUP, cls.TYPE_TAG_ADD_GROUP, cls.TYPE_JOIN_GROUP, cls.TYPE_ADD_GROUP, cls.TYPE_NEW_THREAD, ] #print 'send_blog', ntype, TYPES, ntype in TYPES if ntype in TYPES: extra = extra extra['author_id'] = author_id #print 'do send_blog', ntype Blog.new(card_id, Blog.TYPE_NOTIFY, ntype, extra=extra, ctime=ctime)
def update(self, req): req.nav = '/update' start = req.get_form_var('start') limit = req.get_form_var('count', 20) cate = req.get_form_var('cate', 'b') topic_num, topics = Topic.gets() start = start and str(start).isdigit() and int(start) or 0 limit = limit and str(limit).isdigit() and int(limit) or 0 error = None prefix = "/update?cate=%s&" % cate print 'update', error, req.get_method(), req.get_method() == "POST" if req.get_method() == "POST": text = req.get_form_var("update_text", '').strip() upload_file = req.get_form_var("update_file", None) print 'post', text, upload_file if not text and not upload_file: error = "no_data" if error is None: filename = '' ftype = '' if upload_file: filename = upload_file.tmp_filename ftype = upload_file.content_type bid = Blog.new(req.user.id, Blog.TYPE_BLOG, content=text, filename=filename, ftype=ftype) blog = Blog.get(bid) for t in blog.topics: html = str(stf("/blog/utils.html", "blog_ui", b=blog, req=req)) data = { 'html': html } #publish_channel_msg('me-topic-%s' % t.id, data) return req.redirect('%sstart=%s' % (prefix, start)) total, blogs = Blog.gets(start=start, limit=limit, blog_type=cate) if req.get_form_var("output", None) == 'json': fireworks = req.get_form_var("fireworks", None) req.response.set_content_type('application/json; charset=utf-8') d = { 'total':total, 'start':start, 'count':limit, } if fireworks: d['blogs'] = [b.fireworks_dict() for b in blogs] else: d['blogs'] = [b.json_dict(req.user) for b in blogs] return json.dumps(d) return st('/update.html', **locals())
def _q_lookup(req, id): blog = Blog.get(id) if blog: if blog.btype == blog.TYPE_THREAD: return req.redirect("/thread/%s/" % id) return BlogUI(req, blog) return TraversalError("no such blog")
def update_photo(self, filename): data = open(filename).read() if len(data) > MAX_SIZE: return "too_large" store.execute("update me_event set `photo`=`photo`+1 where `id`=%s", self.id) store.commit() self.photo_id = self.photo_id + 1 doubanfs.set("/me/evc%s-%s-%s" % (self.id, self.photo_id, Cate.ORIGIN), data) o = scale(data, Cate.LARGE, DEFAULT_CONFIG) doubanfs.set("/me/evc%s-%s-%s" % (self.id, self.photo_id, Cate.LARGE), o) for c in CATES[:2]: d = scale(o, c, DEFAULT_CONFIG) doubanfs.set("/me/evc%s-%s-%s" % (self.id, self.photo_id, c), d) if self.photo_id == 1: from webapp.models.blog import Blog Blog.new(self.author_id, Blog.TYPE_BLOG, Blog.BLOG_EVENT, extra={'photo_id':self.photo_id, 'event_id':self.id})
def _q_index(self, req): print 'Enter into index.' if req.card and not req.card.is_basic: return req.redirect('/mine') req.nav = '/' card = req.card photo_num, photo_cards = Card.gets(cate='photo', limit=20) all_badages = Badage.gets() num, blogs = Blog.gets(limit=30) num, photo_blogs = Blog.get_photo_blogs(limit=40) photo_blogs = [b for b in photo_blogs if b.n_unlike == 0] num, event_photos = EventPhoto.gets(limit=20) new_photos = sorted(photo_blogs + event_photos + photo_cards, key=attrgetter('sort_time'), reverse=True) print 'Index...' return st('/index.html', **locals())
def unlike(req): r = { 'err':'invalid' } if req.get_method() == "POST": blog_id = req.get_form_var('bid', '') single = req.get_form_var('single', '0') single = single == '1' blog = Blog.get(blog_id) if blog and req.user and not blog.is_unliked(req.user.id): blog.unlike(req.user.id) blog = Blog.get(blog.id) r = { 'err':'ok', 'inner_html': stf("/blog/utils.html", "blog_ui_inline", b=blog, req=req, single=single), } return json.dumps(r)
def mine(self, req): req.nav = '/mine' card = None card = req.card if card and not card.is_basic: return make(req) n, blogs = Blog.gets(card.id, blog_type='b') award = Award.get_actived() return st('/home.html', **locals())
def _q_index(self, req): card = self.card if req.get_form_var("output", None) == 'json': req.response.set_content_type('application/json; charset=utf-8') return json.dumps(card.json_dict(req.user)) elif req.get_form_var("html_widget", None): return stf("/card/utils.html", "card_widget", card=card, req=req) n, blogs = Blog.gets(card.id, blog_type='b') return st('/card/card.html', **locals())
def blogs(self, req): start = req.get_form_var('start') start = start and str(start).isdigit() and int(start) or 0 limit = 20 error = None card = self.card prefix = "%sblogs?" % card.path total, blogs = Blog.gets(card_id=self.card.id, start=start, limit=limit, blog_type='b') return st('/blog/blogs.html', **locals())
def __init__(self, id, title, group_id, author_id, flag, rtime): self.id = str(id) self.title = title self.group_id = str(group_id) self.author_id = str(author_id) self.flag = flag self.rtime = rtime from webapp.models.blog import Blog self.blog = Blog.get(self.id)
def uncomment(req): r = { 'err':'invalid' } if req.get_method() == "POST": blog_id = req.get_form_var('bid', '') comment_id = req.get_form_var('comment_id', '') single = req.get_form_var('single', '0') single = single == '1' blog = Blog.get(blog_id) #print 'j uncomment', comment_id if req.user and blog: blog.uncomment(req.user.id, comment_id) blog = Blog.get(blog.id) r = { 'err':'ok', 'inner_html': stf("/blog/utils.html", "blog_ui_inline", b=blog, req=req, single=single), } return json.dumps(r)
def new(cls, user_id, group_id, title, content, filename, ftype): from webapp.models.blog import Blog id = Blog.new(user_id, Blog.TYPE_THREAD, content=content, filename=filename, ftype=ftype) if id: store.execute("insert into me_thread(id, title, group_id, author_id)" " values(%s,%s,%s,%s)", (id, title, group_id, user_id)) store.execute("update me_group set `n_thread`=`n_thread`+1, rtime=rtime where id=%s", group_id) store.commit() Notify.new(user_id, user_id, Notify.TYPE_NEW_THREAD, extra={"thread_id":id}) return id
def new(cls, question_id, author_id, content, filename='', ftype=''): q = Question.get(question_id) if q and q.user_id == author_id: blog_id = '0' store.execute("insert into me_answer(question_id, author_id, blog_id)" " values(%s,%s,%s)", (question_id, author_id, blog_id)) id = store.get_cursor(table="me_answer").lastrowid from webapp.models.blog import Blog blog_id = Blog.new(author_id, Blog.TYPE_BLOG, '', content, filename, ftype, extra={'question_id':q.id}) store.execute("update me_answer set blog_id=%s, rtime=rtime where id=%s", (blog_id, id)) store.commit() Notify.new(q.author_id, q.user_id, Notify.TYPE_ANSWER, extra={"question_id":q.id, "blog_id":blog_id}) return id
def _q_lookup(req, name): if not req.user: raise TraversalError("need login") if not ("-" in name and name.lower()[-4:] in [".mp3"]): raise TraversalError("no such audio") n = name[:-4] ftype = name[-3:] target_id, audio_id, cate = n.split("-") if target_id.startswith("b"): blog = Blog.get(target_id.replace("b", "")) if blog: data = blog.audio_data(cate) if not data: raise TraversalError("no such audio") resp = req.response resp.set_content_type("audio/mp3") resp.set_header("Cache-Control", "max-age=%d" % (365 * 24 * 60 * 60)) resp.set_header("Expires", "Wed, 01 Jan 2020 00:00:00 GMT") if "pragma" in resp.headers: del resp.headers["pragma"] if cate == Cate.ORIGIN: resp.set_content_type("application/force-download") resp.set_header("Content-Disposition", 'attachment; filename="%s.%s"' % (name, ftype)) return data
def create_fake_data(): tag1 = Tag('前端') tag1.save() tag2 = Tag('后端') tag2.save() tag3 = Tag('JavaScript') tag3.save() tag4 = Tag('Python') tag4.save() tag5 = Tag('面试') tag5.save() tag6 = Tag('SQL') tag6.save() tag7 = Tag('读书笔记') tag7.save() blog1 = Blog('xilixjd', 'MySQL必知必会读书笔记', blog1_content) blog1.time = time.time()*1000 blog1.tags.extend([tag2, tag6, tag7]) blog1.save() # time.sleep(3) blog2 = Blog('xilixjd', '单页web应用 JavaScript 从前端到后端', blog2_content) blog2.time = time.time()*1000 blog2.tags.extend([Tag.query.filter_by(title='前端').first(), Tag.query.filter_by(title='后端').first(), Tag.query.filter_by(title='JavaScript').first(), tag7]) blog2.save() # time.sleep(3) blog3 = Blog('xilixjd', 'JavaScript 语言精粹', blog3_content) blog3.time = time.time()*1000 blog3.tags.extend([tag1, tag3, tag7]) blog3.save() # time.sleep(3) blog4 = Blog('xilixjd', 'JavaScript高级程序设计', blog4_content) blog4.time = time.time()*1000 blog4.tags.extend([tag1, tag3, tag7]) blog4.save() # time.sleep(3) blog5 = Blog('xilixjd', '超长干货超多的前端面试汇总', blog5_content) blog5.time = time.time()*1000 blog5.tags.extend([Tag.query.filter_by(title='前端').first(), Tag.query.filter_by(title='面试').first(), Tag.query.filter_by(title='JavaScript').first()]) blog5.save() blog6 = Blog('xilixjd', 'clean-code-javascript', blog6_content) blog6.time = time.time()*1000 blog6.tags.extend([Tag.query.filter_by(title='前端').first(), Tag.query.filter_by(title='JavaScript').first()]) blog6.save()
def send_irc(self): from webapp.models.blog import Blog if self.card and self.card and self.card.email: message = '' if self.ntype == self.TYPE_ADD_GROUP: from webapp.models.group import Group gid = self.extra.get('group_id', 0) group = Group.get(gid) if group: message = "#me: 厂工 %s 把你拉进了小组 %s 查看:%s" % (self.author.name, group.name, group.url) elif self.ntype == self.TYPE_ANSWER: from webapp.models.question import Question qid = self.extra.get('question_id', 0) bid = self.extra.get('blog_id', 0) question = Question.get(qid) blog = Blog.get(bid) if question and blog: message = "#me: 厂工 %s 回答了你的问题 查看:%s" % (self.author.name, blog.url) elif self.ntype == self.TYPE_QUESTION: from webapp.models.question import Question qid = self.extra.get('question_id', 0) question = Question.get(qid) if question: if question.is_anonymous: message = "#me: 某厂工问了你一个问题 查看:%s#answers" % (self.card.url) else: message = "#me: 厂工 %s 问了你一个问题 查看:%s#answers" % (self.author.name, self.card.url) elif self.ntype == self.TYPE_AWARD_VOTED: from webapp.models.badage import Badage, Award aid = self.extra.get('badage_id', 0) bid = self.extra.get('blog_id', 0) blog = Blog.get(bid) award = Award.get(aid) if award: if blog: message = "#me: 厂工 %s 在评选%s大奖时,投了你一票哦~ 查看:%s" % (self.author.name, award.fullname, blog.url) else: message = "#me: 厂工 %s 在评选%s大奖时,投了你一票哦~ " % (self.author.name, award.fullname) elif self.ntype == self.TYPE_PHOTO_LIKE: from webapp.models.event import Event, EventPhoto pid = self.extra.get('photo_id', 0) photo = EventPhoto.get(pid) if photo: message = "#me: 厂工 %s 给你上传的照片 + 1了哦~ 查看:%s" % (self.author.name, photo.photo_url) elif self.ntype == self.TYPE_PHOTO_TAG: from webapp.models.event import Event, EventPhoto pid = self.extra.get('photo_id', 0) photo = EventPhoto.get(pid) if photo: message = "#me: 厂工 %s 在上传的照片圈出了你哦~ 查看:%s" % (self.author.name, photo.photo_url) elif self.ntype == self.TYPE_BLOG_LIKE: bid = self.extra.get('blog_id', 0) blog = Blog.get(bid) if blog: message = "#me: 厂工 %s 赞了你的%s~ 查看:%s" % (self.author.name, blog.type_name, blog.url) elif self.ntype == self.TYPE_BLOG_UNLIKE: bid = self.extra.get('blog_id', 0) blog = Blog.get(bid) if blog: message = "#me: 你的%s被人踩了哦~ 查看:%s" % (blog.type_name, blog.url) elif self.ntype == self.TYPE_BLOG_COMMENT: cid = self.extra.get('comment_id', 0) bid = self.extra.get('blog_id', 0) blog = Blog.get(bid) if blog: message = "#me: 厂工 %s 回复了你的%s哦~ 查看:%s#comment_%s" % (self.author.name, blog.type_name, blog.url, cid) elif self.ntype == self.TYPE_BLOG_REPLY: cid = self.extra.get('comment_id', 0) bid = self.extra.get('blog_id', 0) blog = Blog.get(bid) if blog: message = "#me: 你回复的%s有了新回复哦~ 查看:%s#comment_%s" % (blog.type_name, blog.url, cid) elif self.ntype == self.TYPE_BLOG_MENTION: bid = self.extra.get('blog_id', 0) blog = Blog.get(bid) if blog: message = "#me: 厂工 %s 的%s提到了你哦~ 查看:%s" % (self.author.name, blog.type_name, blog.url) elif self.ntype == self.TYPE_BLOG_COMMENT_MENTION: cid = self.extra.get('comment_id', 0) bid = self.extra.get('blog_id', 0) blog = Blog.get(bid) if blog: message = "#me: 厂工 %s 在 %s 的%s回复里提到了你哦~ 查看:%s" % (self.author.name, blog.owner.name, blog.type_name, blog.url) elif self.ntype == self.TYPE_LIKE: message = "#me: 厂工 %s ( %s )收藏了你的卡片哦~ 你说他/她/它是不是对你感兴趣?(//▽//)" % (self.author.name, self.author_card.url) elif self.ntype == self.TYPE_COMMENT: cid = self.extra.get('comment_id', 0) message = "#me: 厂工 %s 给你的卡片留言了哦~ 查看:%s#comment_%s" % (self.author.name, self.card.url, cid) elif self.ntype == self.TYPE_MENTION: cid = self.extra.get('comment_id', 0) card_id = self.extra.get('card_id', 0) if card_id == self.card_id: message = "#me: 厂工 %s 在给你的卡片留言中提到了你~ 查看:%s#comment_%s" % (self.author.name, self.card.url, cid) else: from webapp.models.card import Card card = Card.get(card_id) message = "#me: 厂工 %s 在给 %s 的卡片留言中提到了你了哦~ 查看:%s#comment_%s" % (self.author.name, card.screen_name, card.url, cid) elif self.ntype == self.TYPE_PHOTO_COMMENT: cid = self.extra.get('comment_id', 0) pid = self.extra.get('photo_id', 0) from webapp.models.event import Event, EventPhoto photo = EventPhoto.get(pid) if photo: message = "#me: 厂工 %s 给你上传的照片留言了哦~ 查看:%s%s#comment_%s" % (self.author.name, SITE, photo.path, cid) elif self.ntype == self.TYPE_PHOTO_COMMENT_MENTION: cid = self.extra.get('comment_id', 0) pid = self.extra.get('photo_id', 0) from webapp.models.event import Event, EventPhoto photo = EventPhoto.get(pid) if photo: message = "#me: 厂工 %s 照片里提到你了哦~ 查看:%s%s#comment_%s" % (self.author.name, SITE, photo.path, cid) elif self.ntype == self.TYPE_TAG: tags = self.extra.get('tags').encode('utf-8') message = "#me: 你的卡片有了新的标签(%s),你猜谁给你打的? ≖‿≖✧ 查看:%s" % (tags, self.card.url) elif self.ntype == self.TYPE_REQUEST_PHOTO: message = "#me: 厂工 %s ( %s ) 求你的真相照片哦~ 传一张吧,拜托了!m(_ _)m 上传:%s/make" % (self.author.name, self.author_card.url, SITE) elif self.ntype == self.TYPE_REQUEST_CHANGE_PHOTO: message = "#me: 厂工 %s ( %s )和大家都觉得你的真相照片不真,简直是@#^&*! 换一张吧,拜托了!m(_ _)m 上传:%s/make" % (self.author.name, self.author_card.url, SITE) elif self.ntype == self.TYPE_BADAGE: from webapp.models.card import Badage badage = Badage.get(self.author_id) if badage: message = "#me: 你得到了一枚%s徽章哦~ 点击查看:%s/badage/%s" % (badage.name, SITE, badage.name) iu = self.card.email.replace('@douban.com','') print 'send_irc', iu, message try: irc_send_message(iu, message) except: print 'sent irc error!!!'
def blog(self): from webapp.models.blog import Blog return Blog.get(self.blog_id)
def _q_lookup(req, name): if not ('-' in name and name.lower()[-4:] in ['.jpg','.png','.gif']): raise TraversalError("no such photo") ftype = name[-3:] n = name[:-4] if '$' in name: n = name[:name.rindex('$')] target_id, photo_id, cate = n.split('-') print 'p target_id=%s photo_id=%s cate=%s' % (target_id, photo_id, cate) data = None scale_type = None width = None height = None if cate.startswith("r_"): try: r, scale_type, sizes = cate.split('_') width, height = sizes.split('x') width = int(width) height = int(height) print 'do resize x=%s, y=%s' % (width, height) except: print 'do resize fail !!!! ', name cate = Cate.LARGE if target_id.startswith('evc'): event = Event.get(target_id.replace('evc','')) if event: data = event.photo_data(cate) elif target_id.startswith('evp'): photo = EventPhoto.get(photo_id) if photo: data = photo.photo_data(cate) elif target_id.startswith('bc'): blog_comment = BlogComment.get(target_id.replace('bc', '')) if blog_comment: data = blog_comment.photo_data(cate) elif target_id.startswith('b'): blog = Blog.get(target_id.replace('b', '')) if blog: data = blog.photo_data(cate) elif target_id.startswith('g'): group = Group.get(target_id.replace('g', '')) if group: data = group.photo_data(cate) else: if target_id.startswith('_u_'): target_id = target_id.replace('_u_', '') card = Card.get(target_id) if not card: card = Card.get_by_ldap(target_id) if card: data = card.photo_data(photo_id, cate) if not data: print "no such photo" raise TraversalError("no such photo") scale_type = SCALES.get(scale_type, None) if scale_type: configs = {} configs[Cate.RESIZE] = { 'width': width, 'height': height, 'scale': scale_type, 'gif_scale': scale_type, } data = scale(data, Cate.RESIZE, configs) resp = req.response resp.set_content_type('image/jpeg') resp.set_header('Cache-Control', 'max-age=%d' % (365*24*60*60)) resp.set_header('Expires', 'Wed, 01 Jan 2020 00:00:00 GMT') if 'pragma' in resp.headers: del resp.headers['pragma'] if cate == Cate.ORIGIN: resp.set_content_type('application/force-download') resp.set_header('Content-Disposition', 'attachment; filename="%s.%s"' % (name, ftype)); return data