def new(cls, photo_id, user_id, author_id, left, top, width, height): store.execute("insert into me_photo_tag(photo_id,user_id,author_id,`left`,top,width,height)" " values(%s,%s,%s,%s,%s,%s,%s)", (photo_id, user_id, author_id, left, top, width, height)) store.commit() id = store.get_cursor(table="me_photo_tag").lastrowid Notify.new(user_id, author_id, Notify.TYPE_PHOTO_TAG, extra={"photo_id":photo_id, "card_id":user_id}) return id
def new(cls, app, name, rect, photo_filename='', parent_id='', page_type=None): screen = app.screen page_type = page_type or cls.TYPE_NORMAL try: photo_ver = photo_filename and 1 or 0 store.execute("insert into demo_page(name, parent_page_id, app_id, photo_ver, rect_id, `type`)" " values(%s,%s,%s,%s,%s,%s)", (name, parent_id or '0', app.id, photo_ver, rect.id, page_type)) store.commit() id = store.get_cursor(table="demo_page").lastrowid if photo_filename: data = open(photo_filename).read() with Image(blob=data) as img: if page_type == cls.TYPE_ITEM: rect.height = rect.width*img.height/img.width rect.save() img.resize(rect.width, rect.height, "catrom") filestore.save_image(img, "page-photo-%s-%s.jpg" % (id, photo_ver)) img.resize(rect.width/app.zoomout, rect.height/app.zoomout, "catrom") filestore.save_image(img, "page-photo-%s-%s.jpg" % (id, photo_ver), "s") else: rect.height = 100 #default height rect.save return id except IntegrityError: #traceback.print_exc() store.rollback()
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 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 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 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, event_id, author_id, filename): #print 'upload', event_id, author_id, filename data = open(filename).read() if len(data) > MAX_SIZE: return "too_large" store.execute("insert into me_event_photo(event_id,author_id) values(%s,%s)", (event_id, author_id)) store.commit() pid = store.get_cursor(table="me_event_photo").lastrowid cls.update_photo(pid, event_id, data) #print 'new photo', pid return pid
def new(cls, name, os, width, height, icon_width, icon_height, virtual_keys): try: virtual_keys = virtual_keys and 'Y' or 'N' store.execute("insert into demo_screen(name, os, width, height, icon_width," " icon_height, virtual_keys) values(%s,%s,%s,%s,%s,%s,%s)", (name, os, width, height, icon_width, icon_height, virtual_keys)) store.commit() id = store.get_cursor(table="demo_screen").lastrowid return id except IntegrityError: store.rollback()
def new(cls, page_id, rect, type, dismiss=None, to_page_id=0, resp_rect=None): rect_id = rect and rect.id or 0 resp_rect_id = resp_rect and resp_rect.id or 0 try: dismiss = dismiss == 'Y' and 'Y' or 'N' store.execute("insert into demo_action(page_id, to_page_id, rect_id, resp_rect_id, `type`, `dismiss`)" " values(%s,%s,%s,%s,%s,%s)", (page_id, to_page_id, rect_id, resp_rect_id, type, dismiss)) store.commit() id = store.get_cursor(table="demo_action").lastrowid return id except IntegrityError: store.rollback()
def new(cls, author_id, name, content, online_date, user_ids=[], extra={}): user_ids = ' '.join(user_ids) try: extra = json.dumps(extra) except: extra = "{}" store.execute("insert into me_event(author_id, name, content," " online_date, user_ids, extra) values(%s,%s,%s,%s,%s,%s)", (author_id, name, content, online_date, user_ids, extra)) store.commit() id = store.get_cursor(table="me_event").lastrowid return id
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 register(cls, email, password): passhash = pwd_hash(email, password) try: name = email.split('@')[0] store.execute("insert into demo_user(email, passwd, name) " " values(%s, %s, %s)", (email, passhash, name)) store.commit() id = store.get_cursor(table="demo_user").lastrowid u = cls.get(id) session = create_session(email) u.update_session(session) return u except IntegrityError: store.rollback()
def new(cls, card_id, author_id, ntype, extra={}): #print 'Notify', card_id, author_id, ntype, extra if card_id == author_id: return cls.send_blog(card_id, author_id, ntype, extra) try: extra = json.dumps(extra) except: extra = "{}" store.execute("insert into me_notify(user_id, author_id, ntype, extra)" " values(%s,%s,%s,%s)", (card_id, author_id, ntype, extra)) store.commit() nid = store.get_cursor(table="me_notify").lastrowid if nid: n = cls.get(nid) n.send_irc() n.to_blog()
def bind(cls, name, user_id, blog_id): if '#' in name: return name = name.lower() r = store.execute("select id from me_topic where name=%s", name) topic_id = None if r and r[0]: topic_id = r[0][0] else: store.execute("insert into me_topic(name) values(%s)", name) topic_id = store.get_cursor(table="me_topic").lastrowid if topic_id: store.execute("replace into me_topic_blog(user_id, topic_id, blog_id)" " values(%s,%s,%s)", (user_id, topic_id, blog_id)) store.commit() return topic_id
def new(cls, user_id, name, icon_filename, screen_id=DEFAULT_SCREEN.id, zoomout=2): try: icon_ver = 1 store.execute("insert into demo_app(user_id, name, screen_id, icon_ver, zoomout)" " values(%s,%s,%s,%s,%s)", (user_id, name, screen_id, icon_ver, zoomout)) store.commit() id = store.get_cursor(table="demo_app").lastrowid screen = Screen.get(screen_id) data = open(icon_filename).read() with Image(blob=data) as img: img.resize(screen.icon_width, screen.icon_height, "catrom") filestore.save_image(img, "app-icon-%s-%s.jpg" % (id, icon_ver)) img.resize(screen.icon_width/zoomout, screen.icon_height/zoomout, "catrom") filestore.save_image(img, "app-icon-%s-%s.jpg" % (id, icon_ver), "s") return id except IntegrityError: #traceback.print_exc() store.rollback()
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 new(cls, x, y, w, h): store.execute("insert into demo_rect(x, y, width, height)" " values(%s, %s, %s, %s)", (x, y, w, h)) store.commit() id = store.get_cursor(table="demo_rect").lastrowid return id