Exemple #1
0
    def get(self, username=None, article_id=None):

        if not isinstance(article_id, (int, long)):
            return

        # pull the github token out of the social user db and then fork it
        user_info = models.User.get_by_id(long(self.user_id))
        social_user = models.SocialUser.get_by_user_and_provider(
            user_info.key, 'github')
        article = models.Article.get_by_id(long(article_id))

        # lame because we don't do anything if we fail here
        if article:
            gist = github.fork_gist(social_user.access_token, article.gist_id)
            # we have a new article on our hands after the fork - fetch the data and insert
            if gist:
                # prep the slug
                slug = utils.slugify(gist['title'])

                # stuff into entry
                article = models.Article(
                    title=gist['title'],
                    summary=gist['summary'],
                    created=datetime.datetime.fromtimestamp(gist['published']),
                    gist_id=gist['gist_id'],
                    owner=user_info.key,
                    slug=slug,
                    article_type=gist['article_type'],
                )

                # update db
                article.put()
        return
Exemple #2
0
    def get(self):
        # pull the github token out of the social user db and grab gists from github
        if self.request.get('job_token') != config.job_token:
            logging.info("Hacker attack on jobs!")
            return
        else:
            user_info = models.User.get_by_id(long(self.request.get('user')))
            social_user = models.SocialUser.get_by_user_and_provider(
                user_info.key, 'github')
            gists = github.get_user_gists(social_user.uid,
                                          social_user.access_token)

            # update with the gists
            for gist in gists:
                article = models.Article.get_by_user_and_gist_id(
                    user_info.key, gist['gist_id'])

                if article:
                    # update existing article with new data
                    article.title = gist['title']
                    article.summary = gist['summary']
                    article.gist_id = gist['gist_id']
                    article.article_type = gist['article_type']
                    article.updated = datetime.datetime.fromtimestamp(
                        gist['published'])
                else:
                    # we have a new article on our hands - insert
                    # prep the slug
                    slug = utils.slugify(gist['title'])
                    article = models.Article(
                        title=gist['title'],
                        summary=gist['summary'],
                        created=datetime.datetime.fromtimestamp(
                            gist['published']),
                        gist_id=gist['gist_id'],
                        owner=user_info.key,
                        slug=slug,
                        article_type=gist['article_type'],
                    )

                # update
                article.put()

                # flush memcache copy just in case we had it
                github.flush_gist_content(article.gist_id)

            # use the channel to tell the browser we are done
            channel_token = self.request.get('channel_token')
            channel.send_message(channel_token, 'reload')
            return
Exemple #3
0
    def post(self):
        if not self.form.validate():
            self.add_message("The form did not validate.", 'error')
            return self.get()

        # who's blogging this shizzle?
        user_info = models.User.get_by_id(long(self.user_id))

        # load values out of the form
        title = self.form.title.data.strip()
        summary = self.form.summary.data.strip()
        filename = self.form.filename.data.strip()
        article_type = self.form.article_type.data.strip()

        # when written?
        published_epoch_gmt = int(datetime.datetime.now().strftime("%s"))

        # prep the slug
        slug = utils.slugify(title)

        # save the article in our database
        article = models.Article(
            title=title,
            summary=summary,
            created=datetime.datetime.fromtimestamp(published_epoch_gmt),
            filename=filename,
            owner=user_info.key,
            slug=slug,
            article_type=article_type,
        )
        article.put()

        # log to alert
        self.add_message(_('Article %s successfully created!' % title),
                         'success')

        # give it a few seconds to update db, then redirect
        time.sleep(2)
        return self.redirect_to('blog-article-list')
Exemple #4
0
    def post(self, username=None):
        if not self.form.validate():
            return self.get()

        # pull the github token out of the social user db
        user_info = models.User.get_by_id(long(self.user_id))
        social_user = models.SocialUser.get_by_user_and_provider(
            user_info.key, 'github')

        # load values out of the form
        title = self.form.title.data.strip()
        summary = self.form.summary.data.strip()
        article_type = self.form.article_type.data.strip()

        # when written?
        published_epoch_gmt = int(datetime.datetime.now().strftime("%s"))

        # push the sample article to the user's gist on github (published in this context means date published)
        template_val = {
            "username": social_user.uid,
            "title": title,
            "summary": summary,
            "published": published_epoch_gmt,
            "type": article_type,
        }

        # build a dict of files we want to push into the gist
        files = {
            config.gist_manifest_name:
            self.jinja2.render_template("blog/gist_manifest_stub.txt",
                                        **template_val),
            config.gist_markdown_name:
            self.jinja2.render_template("blog/gist_markdown_stub.txt",
                                        **template_val)
        }

        # loop through them and add them to the other JSON values for github
        file_data = dict((filename, {
            'content': text
        }) for filename, text in files.items())
        data = json.dumps({
            'description': "%s for StackGeek" % title,
            'public': True,
            'files': file_data
        })

        # stuff it to github and then grab our gist_id
        gist = github.put_user_gist(social_user.access_token, data)
        gist_id = gist['id']

        # prep the slug
        slug = utils.slugify(title)

        # make sure it's not already in the database (unlikely)
        if not models.Article.get_by_user_and_gist_id(user_info.key, gist_id):
            # save the article in our database
            article = models.Article(
                title=title,
                summary=summary,
                created=datetime.datetime.fromtimestamp(published_epoch_gmt),
                gist_id=gist_id,
                owner=user_info.key,
                slug=slug,
                article_type=article_type,
            )
            article.put()

            self.add_message(_('Article "%s" successfully created!' % title),
                             'success')
            return self.redirect_to('blog-article-list', username=username)
        else:
            # put_user_article call in models.py
            self.add_message(
                _('Article was not created.  Something went horribly wrong somewhere!'
                  % name), 'warning')
            return self.get()