Example #1
0
File: event.py Project: leonsim/me
 def update_photo(cls, pid, event_id, data):
     doubanfs.set("/me/evp-%s-%s-%s" % (event_id, pid, Cate.ORIGIN), data)
     o = scale(data, Cate.LARGE, DEFAULT_CONFIG)
     doubanfs.set("/me/evp-%s-%s-%s" % (event_id, pid, Cate.LARGE), o)
     for c in CATES:
         d = scale(o, c, DEFAULT_CONFIG)
         doubanfs.set("/me/evp-%s-%s-%s" % (event_id, pid, c), d)
Example #2
0
File: group.py Project: fordream/me
 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)
Example #3
0
File: card.py Project: fordream/me
 def recreate_photo(self):
     try:
         data = doubanfs.get("/me/card/%s/photo/%s/%s" % (self.id, self.photo_id, Cate.ORIGIN))
         d = scale(data, Cate.LARGE, DEFAULT_CONFIG)
         doubanfs.set("/me/card/%s/photo/%s/%s" % (self.id, self.photo_id, Cate.LARGE), d)
     except:
         print "doubanfs write fail!!! %s" % self.id
Example #4
0
File: card.py Project: fordream/me
    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)
Example #5
0
File: event.py Project: leonsim/me
 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})
Example #6
0
File: blog.py Project: leonsim/me
    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})
Example #7
0
File: blog.py Project: leonsim/me
    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
Example #8
0
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