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(self, name, icon_filename='', zoomout=2): try: now = datetime.now() if name and name != self.name: store.execute("update demo_app set name=%s, rtime=%s" " where id=%s", (name, now, self.id)) store.commit() self.name = name if icon_filename: icon_ver = self.icon_ver + 1 data = open(icon_filename).read() screen = self.screen with Image(blob=data) as img: img.resize(screen.icon_width, screen.icon_height, "catrom") filestore.save_image(img, "app-icon-%s-%s.jpg" % (self.id, icon_ver)) img.resize(screen.icon_width/zoomout, screen.icon_height/zoomout, "catrom") filestore.save_image(img, "app-icon-%s-%s.jpg" % (self.id, icon_ver), "s") store.execute("update demo_app set icon_ver=%s, rtime=%s" " where id=%s", (icon_ver, now, self.id)) store.commit() self.icon_ver = icon_ver except IntegrityError: #traceback.print_exc() store.rollback()
def _join(self, user_id): if not self.is_joined(user_id): store.execute("insert into me_group_member(group_id, user_id)" " values(%s, %s)", (self.id, user_id)) store.execute("update me_group set n_member=n_member+1, rtime=rtime" " where id=%s", self.id) store.commit()
def quit(self, user_id): if self.is_joined(user_id): store.execute("delete from me_group_member where group_id=%s" " and user_id=%s", (self.id, user_id)) store.execute("update me_group set n_member=n_member-1, rtime=rtime" " where id=%s", self.id) store.commit()
def update(self, name, photo_filename=''): try: now = datetime.now() if name and name != self.name: store.execute("update demo_page set name=%s, rtime=%s" " where id=%s", (name, now, self.id)) store.commit() if photo_filename: photo_ver = self.photo_ver + 1 data = open(photo_filename).read() rect = self.rect zoomout = self.app.zoomout with Image(blob=data) as img: img.resize(rect.width, rect.height, "catrom") filestore.save_image(img, "page-photo-%s-%s.jpg" % (self.id, photo_ver)) img.resize(rect.width/zoomout, rect.height/zoomout, "catrom") filestore.save_image(img, "page-photo-%s-%s.jpg" % (self.id, photo_ver), "s") store.execute("update demo_page set photo_ver=%s, rtime=%s" " where id=%s", (photo_ver, now, self.id)) store.commit() except IntegrityError: #traceback.print_exc() store.rollback()
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 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 transfer_user(from_id, to_id): from_user = User(id=from_id) to_user = User(id=to_id) if from_user and to_user: from_id = from_user.id to_id = to_user.id print "from", from_id, "to", to_id # +----------------+ # | me_card | print "trans me_card" store.execute( "update me_card set user_id=%s, uid=%s, rtime=rtime" " where user_id=%s", (to_id, to_user.uid, from_id) ) # | me_comment | print "trans me_comment" store.execute("update me_comment set user_id=%s, rtime=rtime" " where user_id=%s", (to_id, from_id)) store.execute("update me_comment set author_id=%s, rtime=rtime" " where author_id=%s", (to_id, from_id)) # | me_like | print "trans me_like" store.execute("update me_like set user_id=%s, rtime=rtime" " where user_id=%s", (to_id, from_id)) store.execute("update me_like set liker_id=%s, rtime=rtime" " where liker_id=%s", (to_id, from_id)) # | me_notify | print "trans me_notify" store.execute("update me_notify set user_id=%s, rtime=rtime" " where user_id=%s", (to_id, from_id)) store.execute("update me_notify set author_id=%s, rtime=rtime" " where author_id=%s", (to_id, from_id)) # | me_profile | print "trans me_profile" store.execute("update me_profile set user_id=%s, rtime=rtime" " where user_id=%s", (to_id, from_id)) # | me_tag | # | me_user_tag | print "trans me_user_tag" store.execute("update me_user_tag set user_id=%s, rtime=rtime" " where user_id=%s", (to_id, from_id)) store.execute("update me_user_tag set tagger_id=%s, rtime=rtime" " where tagger_id=%s", (to_id, from_id)) # +----------------+ store.commit()
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_extra(self, extra): self.extra['expire'] = expire try: extra = json.dumps(self.extra) except: extra = "{}" store.execute("update me_badage set extrea=%s where id=%s", (extra, self.id)) store.commit()
def hide(cls, card_id, admin_id): if admin_id in ADMINS: store.execute("update me_card set flag=%s where user_id=%s", (cls.FLAG_HIDE, card_id)) store.commit() card = cls.get(card_id) if card: doubanmc.delete(cls.MC_KEY % card.id) doubanmc.delete(cls.MC_KEY % card.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 update(cls, card_id, sex, love, zodiac, astro, birthday, marriage, province, hometown, weibo, instagram, blog, code, github, resume, intro): now = datetime.now() store.execute( "replace into me_profile(user_id, sex, love, zodiac, astro, birthday, marriage, province, hometown," "weibo, instagram, blog, code, github, resume, intro) values(%s,%s,%s,%s,%s,%s,%s,%s,%s," "%s,%s,%s,%s,%s,%s,%s)", (card_id, sex, love, zodiac, astro, birthday, marriage, province, hometown, weibo, instagram, blog, code, github, resume, intro)) store.commit()
def update(self, name, os, width, height, icon_width, icon_height, virtual_keys): try: virtual_keys = virtual_keys and 'Y' or 'N' store.execute("update demo_screen set name=%s, os=%s, width=%s, height=%s," " icon_width=%s, icon_height=%s, virtual_keys=%s where id=%s", (name, os, width, height, icon_width, icon_height, virtual_keys, self.id)) store.commit() except IntegrityError: store.rollback()
def rotate(self, direction): if direction in ['left', 'right']: data = self.photo_data(Cate.ORIGIN) with Image(blob=data) as img: img.rotate(direction == "left" and 270 or 90) data = img.make_blob() EventPhoto.update_photo(self.id, self.event_id, data) store.execute("update me_event_photo set rtime=%s where id=%s", (datetime.now(), self.id)) store.commit()
def update(self, name, content, online_date, user_ids=[], extra={}): user_ids = ' '.join(user_ids) try: extra = json.dumps(extra) except: extra = "{}" store.execute("update me_event set name=%s, content=%s," " online_date=%s, user_ids=%s, extra=%s where id=%s", (name, content, online_date, user_ids, extra, self.id)) store.commit()
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 get(cls, card_id): r = store.execute("select user_id, sex, love, zodiac, astro, birthday, marriage, province, hometown," " weibo, instagram, blog, code, github, resume, intro from me_profile" " where user_id=%s", card_id) if r and r[0]: return cls(*r[0]) else: store.execute("insert into me_profile(user_id) values(%s)", card_id) store.commit() return cls.get(card_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 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 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, 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 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, badage_name, sponsor, num, expire_days): bd = Badage.get_by_name(badage_name) if bd: now = datetime.now() expire_time = now + timedelta(days=expire_days) award = cls.get(bd.id) if award: expire_time = award.rtime + timedelta(days=expire_days) now = award.rtime store.execute("replace into me_award(badage_id, sponsor, num, expire_time, rtime)" " values(%s,%s,%s,%s,%s)", (bd.id, sponsor, num, expire_time, now)) store.commit()
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 update_photo(self, filename): data = open(filename).read() if len(data) > MAX_SIZE: return "too_large" store.execute("update me_group set `photo`=`photo`+1 where `id`=%s", self.id) store.commit() self.photo_id = self.photo_id + 1 doubanfs.set("/me/g%s-%s-%s" % (self.id, self.photo_id, Cate.ORIGIN), data) o = scale(data, Cate.LARGE, DEFAULT_CONFIG) doubanfs.set("/me/g%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/g%s-%s-%s" % (self.id, self.photo_id, c), d)
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 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, 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