Exemple #1
0
    def post(self):
        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, including whether the gist should be public or not
        name = self.form.appname.data.strip()
        command = self.form.appcommand.data.strip()
        description = self.form.appdescription.data.strip()
        if self.form.apppublic.data.strip() == 'public':
            public = True
        else:
            public = False

        # check to see if there is an app in this account with this command name, or used globally
        if command in config.reserved_commands or models.App.get_by_user_and_command(
                user_info.key, command):
            self.add_message(
                _("The command '%s' is already in use.  Try picking another command name for your app."
                  % command), 'warning')
            return self.get()

        # if the gist is going to be public, fork an existing gist to include thumbnail
        if public:
            # fork our base gist in config.py to the user's github account
            gist = github.fork_gist(social_user.access_token,
                                    config.gist_template_id)
            if not gist:
                # if we can't fork or something went wrong let's just force a creation of a private gist
                public = False
            else:
                gist_id = gist['id']

        # either way, we build values for templates now
        template_val = {
            "name": name,
            "command": command,
            "description": description,
            "github_user": social_user.uid,
        }

        # ...and build a dict of files we want to push into the gist
        files = {
            config.gist_manifest_name:
            self.jinja2.render_template("app/gist_manifest_stub.txt",
                                        **template_val),
            config.gist_javascript_name:
            self.jinja2.render_template("app/gist_javascript_stub.txt",
                                        **template_val),
            config.gist_index_html_name:
            self.jinja2.render_template("app/gist_index_html_stub.txt",
                                        **template_val),
            config.gist_tinyprobe_html_name:
            self.jinja2.render_template("app/gist_tinyprobe_html_stub.txt",
                                        **template_val),
            config.gist_markdown_name:
            self.jinja2.render_template("app/gist_markdown_stub.txt",
                                        **template_val)
        }

        # now we loop through the files and add them to the other JSON values for github
        file_data = dict((filename, {
            'content': text
        }) for filename, text in files.items())

        # patch the gist if app/gist is public
        if public:
            data = json.dumps({
                'description': "%s for TinyProbe" % name,
                'files': file_data
            })
            patch = github.patch_user_gist(social_user.access_token, gist_id,
                                           data)
            thumb_url = gist['files'][config.gist_thumbnail_name]['raw_url']

            if not patch:
                self.add_message(
                    _('App was not created.  No gist returned from Github.'),
                    'warning')
                return self.get()
        else:
            # if app/gist is to be private, we just make a new gist
            data = json.dumps({
                'description': "%s for TinyProbe" % name,
                'public': public,
                'files': file_data
            })
            gist = github.put_user_gist(social_user.access_token, data)

            # private gists get a holder thumbnail we host
            if gist:
                gist_id = gist['id']
                thumb_url = config.gist_thumbnail_default_url
            else:
                self.add_message(
                    _('App was not created.  No gist returned from Github.'),
                    'warning')
                return self.get()

        # make sure it's not already in the database (unlikely)
        if not models.App.get_by_user_and_gist_id(user_info.key, gist_id):
            # save the app in our database
            app = models.App(
                name=name,
                command=command,
                description=description,
                thumb_url=thumb_url,
                gist_id=gist_id,
                owner=user_info.key,
                author=user_info.key,
                github_author=social_user.uid,
                public=public,
            )

            app.put()

            self.add_message(_('App %s successfully created!' % name),
                             'success')
            params = {"app_id": app.key.id()}
            return self.redirect_to('apps-detail', **params)
        else:
            # something went wrong with the App.put_user_app call in models.py
            self.add_message(
                _("An app gist was created, but we didn't get it in the database!"
                  ), 'warning')
            return self.get()
Exemple #2
0
    def post(self):
        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_article_manifest_name: self.jinja2.render_template("blog/gist_manifest_stub.txt", **template_val),
            config.gist_article_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 TinyProbe" % title, '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)
        articles = models.Article.get_by_user_and_gist_id(user_info.key, gist_id)

        if not articles:
            # 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')
        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()
Exemple #3
0
    def post(self):
        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, including whether the gist should be public or not
        name = self.form.appname.data.strip()
        command = self.form.appcommand.data.strip()
        description = self.form.appdescription.data.strip()
        if self.form.apppublic.data.strip() == 'public':
            public = True
        else:
            public = False

        # check to see if there is an app in this account with this command name, or used globally 
        if command in config.reserved_commands or models.App.get_by_user_and_command(user_info.key, command):
            self.add_message(_("The command '%s' is already in use.  Try picking another command name for your app." % command), 'warning')
            return self.get()
        
        # if the gist is going to be public, fork an existing gist to include thumbnail
        if public:
            # fork our base gist in config.py to the user's github account
            gist = github.fork_gist(social_user.access_token, config.gist_template_id)
            if not gist:
                # if we can't fork or something went wrong let's just force a creation of a private gist
                public = False
            else:
                gist_id = gist['id']

        # either way, we build values for templates now
        template_val = {
            "name": name,
            "command": command,
            "description": description,
            "github_user": social_user.uid,
        }

        # ...and build a dict of files we want to push into the gist
        files = {
            config.gist_manifest_name: self.jinja2.render_template("app/gist_manifest_stub.txt", **template_val),
            config.gist_javascript_name: self.jinja2.render_template("app/gist_javascript_stub.txt", **template_val),
            config.gist_index_html_name: self.jinja2.render_template("app/gist_index_html_stub.txt", **template_val),
            config.gist_tinyprobe_html_name: self.jinja2.render_template("app/gist_tinyprobe_html_stub.txt", **template_val),
            config.gist_markdown_name: self.jinja2.render_template("app/gist_markdown_stub.txt", **template_val)
        }

        # now we loop through the files and add them to the other JSON values for github
        file_data = dict((filename, {'content': text}) for filename, text in files.items())
        
        # patch the gist if app/gist is public
        if public:
            data = json.dumps({'description': "%s for TinyProbe" % name, 'files': file_data})
            patch = github.patch_user_gist(social_user.access_token, gist_id, data)
            thumb_url = gist['files'][config.gist_thumbnail_name]['raw_url']

            if not patch:
                self.add_message(_('App was not created.  No gist returned from Github.'), 'warning')
                return self.get()  
        else:
            # if app/gist is to be private, we just make a new gist
            data = json.dumps({'description': "%s for TinyProbe" % name, 'public': public, 'files': file_data})
            gist = github.put_user_gist(social_user.access_token, data)
            
            # private gists get a holder thumbnail we host
            if gist:
                gist_id = gist['id']
                thumb_url = config.gist_thumbnail_default_url
            else:
                self.add_message(_('App was not created.  No gist returned from Github.'), 'warning')
                return self.get()

        # make sure it's not already in the database (unlikely)
        if not models.App.get_by_user_and_gist_id(user_info.key, gist_id):
            # save the app in our database            
            app = models.App(
                name = name,
                command = command,
                description = description,
                thumb_url = thumb_url,
                gist_id = gist_id,
                owner = user_info.key,
                author = user_info.key,
                github_author = social_user.uid,
                public = public,
            )

            app.put()

            self.add_message(_('App %s successfully created!' % name), 'success')
            params = {"app_id": app.key.id()}
            return self.redirect_to('apps-detail', **params)
        else:
            # something went wrong with the App.put_user_app call in models.py
            self.add_message(_("An app gist was created, but we didn't get it in the database!"), 'warning')
            return self.get()
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()