Esempio n. 1
0
    def save(did, user, email, content):
    
        diary_detail = Diary.get_detail(did)

        # init comments_count in single-diary
        comments_count = diary_detail.get('comments_count')
        if comments_count is None:
            comments_count = 0

        comment = {
                "_id": Kid.kid(), 
                "did": int(did),
                "user": user,
                "email": email,
                "content": content,
                "publish_time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
                }

        db.diaries.update({'_id': int(did)}, {'$inc': {'comments_count': 1}, 
                                         '$push': {'comment': comment}})

        # Save in Comments Collection for Admin 
        diary_title = diary_detail.get('title')
        comment['diary_title'] = diary_title
        db.comments.save(comment)
        return
Esempio n. 2
0
    def add(title, content, c_name, c_id, tags):
        summary = content[0:80] + '...'
        html = markdown.markdown(content)

        diary = {
                "_id": Kid.kid(),
                "title": title,
                "category": c_name,
                "category_id": int(c_id),
                "tags": tags,
                "content": content,
                "html": html,
                "summary": markdown.markdown(summary),
                "publish_time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
                }
        db.diaries.save(diary)

        # save category
        Category.update_diary(c_id, diary.get('_id'), title, diary.get('publish_time'))

        if tags is not None:
            # save tags
            for tag in tags:
                Tag.add(tag, diary)
        return 
Esempio n. 3
0
    def add_diary(_id, did, title, dtime):

        diary = {
                "_id": Kid.kid(),
                "did": int(did),
                "title": title,
                "publish_time": dtime
              }

        return db.tags.update({'_id': int(_id)}, {'$inc': {'diaries_num': 1}, '$push': {'diaries': diary}})
Esempio n. 4
0
 def search_add_keyword(name):
     if Calculate.search_find_by_keyword(name) is None:
         keyword = {
                    "_id": Kid.kid(),
                    "name": name,
                    "count": 1,
                    "publish_time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
                   }
         return db.cal_search_words.save(keyword)
     else:
         return db.cal_search_words.update({'name': name}, {'$inc': {'count': 1}})
Esempio n. 5
0
    def add(title, desc):
        gallaries = db.gallaries

        galllary = {
            "_id": Kid.kid(),
            "title": title,
            "photo_count": 0,
            "description": desc,
            "publish_time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
        }
        gallaries.save(galllary)
        return
Esempio n. 6
0
    def new(cat):
      categories = db.categories

      category = {
                  "_id": Kid.kid(),
                  "name": cat,
                  "diaries_num": 0,
                  "publish_time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
                }

      categories.save(category)
      return category.get('_id')
Esempio n. 7
0
    def new(title, content, property):
        html = markdown.markdown(content)

        detail = {
                "_id": Kid.kid(),
                "title": title,
                "property": property,
                "content": content,
                "html": html,
                "publish_time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
                }

        db.pages.save(detail)
        return
Esempio n. 8
0
    def save_photo(gid, url, title):
        content = {
            "_id": Kid.kid(),
            "title": title,
            "gid": int(gid),
            "url": url,
            "publish_time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
        }

        # save gallary index
        index = Gallary.get_detail(gid).get("index")
        if index is None:
            db.gallaries.update({"_id": int(gid)}, {"$set": {"index": url}})

        # i update gallary detail
        db.gallaries.update({"_id": int(gid)}, {"$inc": {"photo_count": 1}, "$push": {"content": content}})
        return
Esempio n. 9
0
    def update_diary(_id, did, title, dtime, last_id=None):

        """ first determine whether there is an exist diary
            if there is, delete it from old category then create it in new category,
            else, create it
        """
        
        if last_id is not None:
            Category.del_diary(did)

        diary = {
                "_id": Kid.kid(),
                "did": int(did),
                "title": title,
                "publish_time": dtime
              }

        db.categories.update({'_id': int(_id)}, {'$inc': {'diaries_num': 1}, '$push': {'diaries': diary}})
        return
Esempio n. 10
0
    def reply(did, cid, content):

        comment = {
                "_id": Kid.kid(), 
                "did": int(did),
                "parent_id": int(cid),
                "user": "******",
                "content": content,
                "publish_time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
                }

        db.diaries.update({'_id': int(did)}, {'$inc': {'comments_count': 1}, 
                                         '$push': {'comment': comment}})

        # Save in Comments Collection for Admin 
        diary_detail = Diary.get_detail(did)
        diary_title = diary_detail.get('title')
        comment['diary_title'] = diary_title
        db.comments.save(comment)
        return
Esempio n. 11
0
    def get(self, *args):
        # OAth2 for sina_weibo
        APP_KEY =  conf['weibo_app_key']
        APP_SECRET = conf['weibo_app_secret']
        CALLBACK_URL = conf['url'] + '/wback'

        client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL)

        code = self.get_argument('code')
        r = client.request_access_token(code)
        access_token = r.access_token
        expires_in = r.expires_in
        client.set_access_token(access_token, expires_in)

        _id = client.account.get_uid.get().get('uid')
        auth = client.users.show.get(uid=_id)
        
        if auth:
            accounts = conf['db'].accounts
            account = {
                    "_id": Kid.kid(),
                    "id": auth.get('id'),
                    "name": auth.get('screen_name'),
                    "avatar": auth.get('profile_image_url'),
                    "loc_name": auth.get('location'),
                    "desc": auth.get('description'),
                    }

            try:
                exist = Account.get_by_id(auth.get('id'))

                if exist is None:
                    accounts.save(account)
                else:
                    accounts.update({'id': int(auth.get('id'))}, {'$set': account})

                    self.set_secure_cookie("user", account.get('name'))
            except Exception as e:
                print str(e)

            self.redirect('/')
Esempio n. 12
0
    def add(tag, diary):

        exist_tag = Tag.find_by_name(tag)

        """ first determine whether there is an exist tag
            if there is, update from old tag then add diary in it,
            else, create a new tag
        """

        if exist_tag is None:
            new_tag = {
                       "_id": Kid.kid(),
                       "name": tag,
                       "diaries_num": 0,
                       "publish_time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
                      }

            db.tags.save(new_tag)
            tid = new_tag.get('_id')
        else:
            tid = exist_tag.get('_id')

        Tag.add_diary(tid, diary.get('_id'), diary.get('title'), diary.get('publish_time'))
        return
Esempio n. 13
0
    def get(self, *args):

        KEY = "08e0dfb50c90b0991aa29f1f25b211cb"
        SECRET = "378ed0dadf96de47"
        CALLBACK = conf["url"] + "/dback"
        SCOPE = "douban_basic_common,community_basic_user,community_basic_note"

        client = DoubanClient(KEY, SECRET, CALLBACK, SCOPE)
        code = self.get_argument("code")

        if code:
            client.auth_with_code(code)
            auth = client.user.me
            if auth:
                accounts = conf["db"].accounts
                account = {
                    "_id": Kid.kid(),
                    "id": auth.get("id"),
                    "name": auth.get("name"),
                    "avatar": auth.get("avatar"),
                    "loc_name": auth.get("loc_name"),
                    "desc": auth.get("desc"),
                }

                try:
                    exist = Account.get_by_id(auth.get("id"))

                    if exist is None:
                        accounts.save(account)
                    else:
                        accounts.update({"id": auth.get("id")}, {"$set": account})

                except Exception as e:
                    print str(e)

                diary = client.note.list(auth.get("id"), 0, 20)
                diaries = conf["db"].diaries
                diary_notes = diary.get("notes")
                for i in diary_notes:

                    # re-summary content from douban
                    summary = i.get("content")[0:80] + "..."

                    diary = {
                        "_id": Kid.kid(),
                        "update_time": i.get("update_time"),
                        "publish_time": i.get("publish_time"),
                        "photos": i.get("photos"),
                        "comments_count": i.get("comments_count"),
                        "liked_count": i.get("liked_count"),
                        "recs_count": i.get("recs_count"),
                        "id": i.get("id"),
                        "alt": i.get("alt"),
                        "can_reply": i.get("can_reply"),
                        "title": i.get("title"),
                        "privacy": i.get("privacy"),
                        "summary": summary,
                        "content": client.note.get(i.get("id"), format="html_full").get("content"),
                    }
                    try:
                        exist = Diary.get_by_id(i.get("id"))

                        if exist is None:
                            diaries.save(diary)
                        else:
                            diaries.update({"id": i.get("id")}, {"$set": diary})

                    except Exception as e:
                        print str(e)

                self.redirect("/")
        else:
            print "douban_callback code missing"