def tag(cls, card_id, tagger_id, tags=[]): rs = store.execute("select tag_id from me_user_tag where user_id=%s and tagger_id=%s" " order by tag_id", (card_id, tagger_id)) old_tag_ids = [str(r[0]) for r in rs] #print 'old tag ids', old_tag_ids store.execute("delete from me_user_tag where user_id=%s and tagger_id=%s", (card_id, tagger_id)) for t in tags: if t: r = store.execute("select id from me_tag where name=%s", t) if r and r[0]: tag_id = r[0][0] else: store.execute("insert into me_tag(name) values(%s)", t) tag_id = store.get_cursor(table="me_tag").lastrowid if tag_id: store.execute("replace into me_user_tag(user_id, tagger_id, tag_id)" " values(%s,%s,%s)", (card_id, tagger_id, tag_id)) store.commit() rs = store.execute("select tag_id from me_user_tag where user_id=%s and tagger_id=%s" " order by tag_id", (card_id, tagger_id)) new_tag_ids = [str(r[0]) for r in rs] diff_tag_ids = list(set(new_tag_ids) - set(old_tag_ids)) #print 'new tag ids', new_tag_ids if diff_tag_ids and card_id != tagger_id: diff_tags = [cls.get(i).name for i in diff_tag_ids] Notify.new(card_id, tagger_id, Notify.TYPE_TAG, extra={"tags":' '.join(diff_tags)})
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 new(cls, user_id, author_id, content, anonymous): if user_id != author_id: flag = anonymous and cls.FLAG_ANONYMOUS or cls.FLAG_NORMAL store.execute("insert into me_question(user_id, author_id, content, flag)" " values(%s,%s,%s,%s)", (user_id, author_id, content, flag)) id = store.get_cursor(table="me_question").lastrowid store.commit() Notify.new(user_id, author_id, Notify.TYPE_QUESTION, extra={"question_id":id}) return id
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 add(cls, card_id, badage_id, admin_id): if admin_id in ADMINS: r = store.execute("select 1 from me_user_badage where user_id=%s and" " badage_id=%s", (card_id, badage_id)) if not r: store.execute("insert into me_user_badage(user_id, badage_id)" " values(%s,%s)", (card_id, badage_id)) store.commit() Notify.new(card_id, badage_id, Notify.TYPE_BADAGE) return True return False
def comment(self, author_id, content): store.execute("insert into me_comment(`user_id`,`author_id`,`content`)" " values(%s,%s,%s)", (self.id, author_id, content)); store.commit() cid = store.get_cursor(table="me_comment").lastrowid Notify.new(self.id, author_id, Notify.TYPE_COMMENT, extra={"comment_id":cid}) if '@' in content: from webapp.models.utils import mention_text ret = mention_text(content) for b, e, card_id, kind in ret['postions']: Notify.new(card_id, author_id, Notify.TYPE_MENTION, extra={"card_id":self.id, "comment_id":cid})
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 new(cls, uid, user_id, name, member_name='', intro='', tags=[], filename=''): g = cls.get(uid) if not g: now = datetime.now() store.execute("insert into me_group(uid, user_id, name, member_name, intro, ctime, rtime)" " values(%s,%s,%s,%s,%s,%s,%s)", (uid, user_id, name, member_name, intro, now, now)) store.commit() id = store.get_cursor(table="me_group").lastrowid g = cls.get(id) g.update_photo(filename) Notify.new(user_id, user_id, Notify.TYPE_CREATE_GROUP, extra={"group_id":g.id}) g._join(user_id) g.update_tags(tags) return id
def new(cls, user_id, btype, action='', content='', filename='', ftype='', extra={}, ctime=None): #print 'Blog new', user_id, btype, action, content, filename, ftype, extra, ctime try: extra = json.dumps(extra) except: extra = "{}" now = ctime or datetime.now() store.execute("insert into me_blog(`user_id`,`btype`,`action`, `content`, `extra`, `ctime`)" " values(%s,%s,%s,%s,%s,%s)", (user_id, btype, action or cls.BLOG_TEXT, content, extra, now)) id = store.get_cursor(table="me_blog").lastrowid if filename and ftype: ftype = CONTENT_TYPE_DICT.get(ftype, None) if not ftype: return "invalid_type" data = open(filename).read() if len(data) > MAX_SIZE: return "too_large" if ftype in ['jpg', 'png', 'gif']: store.execute("insert into me_blog_photo(blog_id,author_id,ftype) values(%s,%s,%s)", (id, user_id, ftype)) photo_id = store.get_cursor(table="me_blog_photo").lastrowid store.execute("update me_blog set photo_id=%s,action=%s where id=%s", (photo_id, cls.BLOG_PHOTO, id)) doubanfs.set("/me/bp%s-%s-%s" % (id, photo_id, Cate.ORIGIN), data) o = scale(data, Cate.LARGE, DEFAULT_CONFIG) doubanfs.set("/me/bp%s-%s-%s" % (id, photo_id, Cate.LARGE), o) elif ftype in ['mp3']: store.execute("insert into me_blog_audio(blog_id,author_id,ftype) values(%s,%s,%s)", (id, user_id, ftype)) audio_id = store.get_cursor(table="me_blog_audio").lastrowid store.execute("update me_blog set audio_id=%s,action=%s where id=%s", (audio_id, cls.BLOG_AUDIO, id)) doubanfs.set("/me/ba%s-%s-%s" % (id, audio_id, Cate.ORIGIN), data) store.commit() if '@' in content or '#' in content: from webapp.models.utils import mention_text ret = mention_text(content, True) for b, e, rid, kind in ret['postions']: if kind == 'card': card_id = rid Notify.new(card_id, user_id, Notify.TYPE_BLOG_MENTION, extra={"card_id":user_id, "blog_id":id}) elif kind == 'topic': name = rid topic_id = Topic.bind(name, user_id, id) if btype in [cls.TYPE_BLOG, cls.TYPE_NOTIFY]: blog = Blog.get(id) #send_fireworks(blog.fireworks_dict()) return id
def comment(self, author_id, content, filename='', ftype=''): print 'comment', filename, ftype store.execute("insert into me_blog_comment(`blog_id`,`author_id`,`content`)" " values(%s,%s,%s)", (self.id, author_id, content)); cid = store.get_cursor(table="me_blog_comment").lastrowid store.execute("update me_blog set `n_comment`=`n_comment`+1 where id=%s", self.id) store.commit() print 'comment_id', cid if cid and filename and ftype: ftype = CONTENT_TYPE_DICT.get(ftype, None) if ftype in ['jpg', 'png', 'gif']: data = open(filename).read() store.execute("update me_blog_comment set photo_id=photo_id+1," "rtime=rtime where id=%s", cid) store.commit() doubanfs.set("/me/bcp%s-%s-%s" % (cid, 1, Cate.ORIGIN), data) o = scale(data, Cate.LARGE, DEFAULT_CONFIG) doubanfs.set("/me/bcp%s-%s-%s" % (cid, 1, Cate.LARGE), o) Notify.new(self.user_id, author_id, Notify.TYPE_BLOG_COMMENT, extra={"comment_id":cid, "blog_id":self.id}) if '@' in content or '#' in content: from webapp.models.utils import mention_text ret = mention_text(content, True) for b, e, card_id, kind in ret['postions']: if kind == 'card': Notify.new(card_id, author_id, Notify.TYPE_BLOG_COMMENT_MENTION, extra={"card_id":self.user_id, "comment_id":cid, "blog_id":self.id}) aids = [cm.author_id for cm in self.comments if cm.author_id not in [self.user_id, author_id]] for aid in set(aids): Notify.new(aid, author_id, Notify.TYPE_BLOG_REPLY, extra={"comment_id":cid, "blog_id":self.id})
def update_tags(self, tags): rs = store.execute("select tag_id from me_group_tag where group_id=%s and tagger_id=%s" " order by tag_id", (self.id, self.user_id)) old_tag_ids = [str(r[0]) for r in rs] #print 'old tag ids', old_tag_ids store.execute("delete from me_group_tag where group_id=%s and tagger_id=%s", (self.id, self.user_id)) for t in tags: if t: r = store.execute("select id from me_tag where name=%s", t) if r and r[0]: tag_id = r[0][0] else: store.execute("insert into me_tag(name) values(%s)", t) tag_id = store.get_cursor(table="me_tag").lastrowid if tag_id: store.execute("replace into me_group_tag(group_id, tagger_id, tag_id)" " values(%s,%s,%s)", (self.id, self.user_id, tag_id)) store.commit() for t in self.tag_names: ds = Card.gets_by_tag(t) [self._join(d.id) for d in ds] Notify.new(self.user_id, self.user_id, Notify.TYPE_TAG_ADD_GROUP, extra={"group_id":self.id, "tag":t})
def like(self, liker_id): store.execute("replace into me_like(user_id, liker_id) values(%s,%s)", (self.id, liker_id)) store.commit() Notify.new(self.id, liker_id, Notify.TYPE_LIKE)
def join(self, user_id): self._join(user_id) Notify.new(user_id, user_id, Notify.TYPE_JOIN_GROUP, extra={"group_id":self.id})
def add_member(self, user_id, by_id): self._join(user_id) Notify.new(user_id, by_id, Notify.TYPE_ADD_GROUP, extra={"group_id":self.id})
def new(cls, user_id, rtype, rec_id): rec_cls = cls.RTYPE_DICT.get(rtype, None) if rec_cls: obj = rec_cls.get(rec_id) if obj: return Notify.new(user_id, user_id, Notify.TYPE_REC, extra={'type':rtype, 'id':rec_id})
def unlike(self, unliker_id): if not self.is_unliked(unliker_id): store.execute("replace into me_blog_unlike(blog_id, unliker_id) values(%s,%s)", (self.id, unliker_id)) store.execute("update me_blog set `n_unlike`=`n_unlike`+1 where id=%s", self.id) store.commit() Notify.new(self.user_id, unliker_id, Notify.TYPE_BLOG_UNLIKE, extra={'blog_id':self.id})
def notifications(self): return Notify.gets(self.id)
def vote_by_user(self, card_id, voter_id): if card_id != voter_id: store.execute("replace into me_award_vote(badage_id, user_id, voter_id)" " values(%s,%s,%s)", (self.badage_id, card_id, voter_id)) store.commit() Notify.new(card_id, voter_id, Notify.TYPE_AWARD_VOTED, extra={"badage_id":self.badage_id})