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, 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 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, 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 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 _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 get(cls, id): r = store.execute("select `user_id`, `uid`, `email`, `skype`, `name`, `alias`, `phone`, `photo`," " `flag`, `join_time`, `rtime`, `ctime`" " from me_card where `user_id`=%s", id) card = None if r and r[0]: card = cls(*r[0]) else: r = store.execute("select `user_id`, `uid`, `email`, `skype`, `name`, `alias`, `phone`, `photo`," " `flag`, `join_time`, `rtime`, `ctime`" " from me_card where `uid`=%s", id) if r and r[0]: card = cls(*r[0]) if card: try: employee = Employee.dget(card.id) if employee: #print 'get info by dae service', employee.fullname, employee.douban_mail, employee.entry_date if employee.fullname: card.name = employee.fullname if employee.douban_mail: card.email = employee.douban_mail if employee.entry_date: card.join_time = datetime.strptime(employee.entry_date, '%Y-%m-%d') except: print "dae service EmployeeClient error user_id %s" % (card and card.id or '0') print 'card', id, card.id, card.uid, card.email, card.name return card
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 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 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 gets_by_tag(cls, tag): r = store.execute("select id from me_tag where name=%s", tag) if r and r[0]: tag_id = r[0][0] if tag_id: rs = store.execute("select distinct(user_id) from me_user_tag where tag_id=%s", tag_id) return sorted([cls.get(r[0]) for r in rs if str(r[0]) not in CHART_BLACK_LIST_UIDS], key=attrgetter('score'), reverse=True) return []
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 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 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 gets_by_card(cls, card_id, start=0, limit=10): r = store.execute("select count(1) user_id from me_like where liker_id=%s", card_id) n = r and r[0][0] rs = store.execute("select user_id from me_like where liker_id=%s" " order by rtime desc limit %s, %s", (card_id, start, limit)) cids = [] if rs: cids = [str(r[0]) for r in rs] return n, [cls.get(i) for i in cids]
def basic_result(cls): ret = {} r = store.execute("select count(1) user_id from me_card where flag=%s", Card.FLAG_NORMAL) ret['n_card'] = r and r[0][0] r = store.execute("select count(1) user_id from me_card where email !='' and flag=%s", Card.FLAG_NORMAL) ret['n_basic_card'] = r and r[0][0] r = store.execute("select count(1) user_id from me_card where photo > 0 and flag=%s", Card.FLAG_NORMAL) ret['n_photo_card'] = r and r[0][0] return ret
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 get(cls, id): r = store.execute("select id, uid, name, member_name, intro, photo, user_id, flag, n_tag," " n_member, n_thread, ctime, rtime from me_group where id=%s and flag=%s", (id, cls.FLAG_NORMAL)) if r and r[0]: return cls(*r[0]) else: r = store.execute("select id, uid, name, member_name, intro, photo, user_id, flag, n_tag," " n_member, n_thread, ctime, rtime from me_group where uid=%s and flag=%s", (id, cls.FLAG_NORMAL)) if r and r[0]: return cls(*r[0])
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, 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 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 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 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, 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 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, 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 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 gossip_result(cls): ret = {} r = store.execute("select user_id from me_card where join_time > '2006-01-09'" " and flag=%s order by join_time limit 1", Card.FLAG_NORMAL) if r and r[0]: ret['first_employee'] = Card.get(r[0][0]) r = store.execute("select user_id from me_card where user_id > 1000001" " and flag=%s order by user_id limit 1", Card.FLAG_NORMAL) if r and r[0]: ret['first_register'] = Card.get(r[0][0]) r = store.execute("select user_id from (select user_id, count(user_id) as c from me_like" " group by user_id) as s order by s.c desc limit 1") if r and r[0]: ret['likest_card'] = Card.get(r[0][0]) r = store.execute("select user_id from (select user_id, count(user_id) as c from me_question" " group by user_id) as s order by s.c desc limit 1") if r and r[0]: ret['question_card'] = Card.get(r[0][0]) r = store.execute("select user_id from (select user_id, count(user_id) as c from me_comment" " group by user_id) as s order by s.c desc limit 1") if r and r[0]: ret['commentest_card'] = Card.get(r[0][0]) r = store.execute("select user_id from (select user_id, count(tag_id) as c from me_user_tag" " group by user_id) as s order by s.c desc limit 1") if r and r[0]: ret['taggest_card'] = Card.get(r[0][0]) rs = store.execute("select s.name, s.c from (select name, count(tag_id) as c from me_tag as t, me_user_tag as ut" " where t.id=ut.tag_id group by t.id) as s order by s.c desc limit 5") ret['hotest_tags'] = rs r = store.execute("select user_id from (select user_id, count(user_id) as c from me_user_badage" " group by user_id) as s order by s.c desc limit 1") if r and r[0]: ret['badagest_card'] = Card.get(r[0][0]) r = store.execute("select user_id, s.c from (select user_id, count(user_id) as c from me_notify" " where ntype=%s group by user_id) as s order by s.c desc limit 1", Notify.TYPE_REQUEST_PHOTO) if r and r[0]: ret['most_request_photo_card'] = (Card.get(r[0][0]), r[0][1]) r = store.execute("select user_id, s.c from (select user_id, count(user_id) as c from me_notify" " where ntype=%s group by user_id) as s order by s.c desc limit 1", Notify.TYPE_REQUEST_CHANGE_PHOTO) if r and r[0]: ret['most_request_change_photo_card'] = (Card.get(r[0][0]), r[0][1]) return ret