Ejemplo n.º 1
0
    def get(self, article_id = None):
        # 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
Ejemplo n.º 2
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
Ejemplo n.º 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()
Ejemplo n.º 4
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()