示例#1
0
文件: forms.py 项目: qlqwl/Niflheim
    def save(self):
        if self.obj is None:
            post = Post()
        else:
            post = self.obj

        # make slug
        title = self.title.data
        if (self.slug.data is None) or (self.slug.data.strip()
                                        == "") or (self.slug.data.strip()
                                                   == u""):
            slug = pretty_slug(title)
        else:
            slug = slugify(self.slug.data.strip())

        # category
        if (self.category.data.strip() is "") or (self.category.data.strip() is
                                                  u""):
            category = None
        else:
            category = ndb.Key(urlsafe=self.category.data.strip())

        # tag
        tag = []
        for word in self.tag.data.split(","):
            word = word.strip()
            if not word == "":
                t = Tag(id=pretty_slug(word).lower())
                t.title = word
                key = t.put()
                tag.append(key)

        t = datetime.now()
        archive = Archive(id=t.strftime("%Y_%m"))
        archive.time = date(year=t.year, month=t.month, day=1)
        archive_key = archive.put()

        post.title = title
        post.slug = slug
        post.text = self.text.data

        post.category = category
        post.tag = tag
        post.archive = archive_key

        post.order = self.order.data
        post.status = self.status.data
        post.allowComment = self.allowComment.data
        post.allowPing = self.allowPing.data
        post.allowFeed = self.allowFeed.data

        post.put()
        app.config["LATEST"] = datetime.now()