Beispiel #1
0
def make_entry(rec):
    """docstring for make_entry"""
    body = rec.get('body')
    body_html = markdown2.markdown(body)
    rec.update({'body_html': body_html})
    slug = rec.get('slug')
    title = rec.get('title')
    excerpt = rec.get('excerpt')
    markdown = rec.get('markdown') or 'markdown'
    tags = rec.get('tags') or []
    if len(tags) == 0:
        tags = ['general']
    tags = [db.Category(utils.slugify(tag)) for tag in tags if tag]
    
    static = rec.get('static')
    
    if not slug:
        utils.slugify(title)
        
    if not excerpt:
        soup = BeautifulSoup.BeautifulSoup(body_html)
        paras = soup.findAll('p')
        if paras:
            excerpt = paras[0].string
    return Entry(author=users.get_current_user(),
    title=title,
    slug=slug,
    body=body,
    body_html=body_html,
    markdown=markdown,
    excerpt=excerpt,
    tags= tags,
    static=static,
    )
Beispiel #2
0
    def post(self,slug=None):
        title = self.request.get("title")
        body = self.request.get("body")
        markdown = self.request.get("markup")
        st  = self.request.get("static")
        cm  = self.request.get("comments")
        if st == '1': 
            static = True
        else: 
            static = False

        if cm  == '1': 
            comments = True
        else: 
            comments = False
        
        tags = self.request.get("tags")
        tags = tags.split(' ')
        if len(tags) == 0:
            tags = ['general']
        tags = [db.Category(utils.slugify(tag)) for tag in tags if tag]

        body_html = to_html(body, markdown)

        soup = BeautifulSoup.BeautifulSoup(body_html)
        paras = soup.findAll('p')
        
        if paras:
            excerpt = paras[0].string
        else: 
            excerpt = None
        
        entry = db.Query(Entry).filter("slug =", slug).get()
        if not entry:
            entry = Entry(
                author=users.get_current_user(),
                title=title,
                slug=utils.slugify(title),
                body=body,
                body_html=body_html,
                markdown=markdown,
                excerpt=excerpt,
                tags=tags,
                static=static,
                comments=comments,
            )
        else:
            entry.title = title
            entry.body = body
            entry.body_html = body_html
            entry.excerpt = excerpt
            entry.static = static
            entry.tags = tags
            entry.comments = comments
        entry.put()
        self.redirect(entry.url())
Beispiel #3
0
    def post(self, slug=None):
        title = self.request.get("title")
        body = self.request.get("body")
        markdown = self.request.get("markup")
        st = self.request.get("static")
        cm = self.request.get("comments")
        if st == '1': static = True
        else: static = False
        if cm == '1': comments = True
        else: comments = False

        tags = self.request.get("tags")
        tags = tags.split(' ')
        if len(tags) == 0:
            tags = ['general']
        tags = [db.Category(utils.slugify(tag)) for tag in tags if tag]

        body_html = to_html(body, markdown)

        soup = BeautifulSoup.BeautifulSoup(body_html)
        paras = soup.findAll('p')

        if paras:
            excerpt = paras[0].string
        else:
            excerpt = ''

        entry = db.Query(Entry).filter("slug =", slug).get()
        if not entry:
            entry = Entry(
                author=users.get_current_user(),
                title=title,
                slug=utils.slugify(title),
                body=body,
                body_html=body_html,
                markdown=markdown,
                excerpt=excerpt,
                tags=tags,
                static=static,
                comments=comments,
            )
        else:
            entry.title = title
            entry.body = body
            entry.body_html = body_html
            entry.excerpt = excerpt
            entry.static = static
            entry.tags = tags
            entry.comments = comments
        entry.put()
        self.redirect(entry.url())
Beispiel #4
0
	def clean_title(self):
		try:
			s = Story.objects.get(slug = slugify(self.cleaned_data['title']))
			raise ValidationError(_("Post with identical title already exists, try modifying your story title."))
		except ObjectDoesNotExist:
			pass
		return self.cleaned_data['title']
Beispiel #5
0
    def post(self):
        title = self.request.get('subject')
        post = self.request.get('content')
        slug = self.request.get('slug')
        cid = self.request.get('cid')

        error = dict()
        cid = int(cid)
        if title and post:
            if not slug or not valid_slug(slug):
                slug = slugify(str(title))
            slug_in_db = Articles.all().filter('slug =', slug).get()

            if not slug_in_db or cid == slug_in_db.key().id():
                edit = Articles.get_by_id(cid)
                edit.title = title
                edit.post = post
                edit.slug = slug
                edit.put()

                # Remove from memcache
                memcache.flush_all()
                self.redirect('/articles/' + slug)

            else:
                error['slug'] = 'Sorry, this slug is taken. Please enter a unique one'
                form = {'title': title, 'post': post, 'slug': slug, 'cid': cid, 'error': error, 'logged_in': True}
                self.render_form(form)
        else:
            if not title:
                error['subject'] = 'Please enter a title'
            if not post:
                error['post'] = 'Please enter some content'
            form = {'title': title, 'post': post, 'slug': slug, 'cid': cid, 'error': error, 'logged_in': True}
            self.render_form(form)
Beispiel #6
0
    def post(self):
        title = self.request.get('subject')
        post = self.request.get('content')
        slug = self.request.get('slug')

        error = dict()

        if title and post:
            if not slug:
                slug = slugify(str(title))
            slug_in_db = Articles.ll().filter('slug =', slug).get()

            if not slug_in_db:
                submission = Articles(title=title, post=post, slug=slug)
                submission.put()

                memcache.flush_all()

            else:
                error['slug'] = 'Sorry, this slug is taken. Please enter a unique one'
                self.render_post(title, post, slug, error)
        else:
            if not title:
                error['subject'] = 'Please enter a title'
            if not post:
                error['post'] = 'Please enter some content'
            self.render_post(title, post, slug, error)
Beispiel #7
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
Beispiel #8
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
Beispiel #9
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
Beispiel #10
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_article_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_raw_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
Beispiel #11
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')
Beispiel #12
0
    def post(self, lang, slug=None):
        author = users.get_current_user()
        title = self.request.get("title")
        slug = utils.slugify(title)
        markdown = self.request.get("markdown")
        body_html = to_html(markdown)
        soup = BeautifulSoup.BeautifulSoup(body_html)
        paras = soup.findAll("p")
        language = self.request.get("language")

        if paras:
            strParas = [i.encode("utf8") for i in paras[:2]]
            excerpt = "\n".join(strParas) + "\n...\n"
            excerpt = excerpt.decode("utf8")
        else:
            excerpt = ""

        entry = db.Query(Entry).filter("slug =", slug).get()
        if not entry:
            entry = Entry(
                author=author,
                title=title,
                slug=slug,
                body_html=body_html,
                markdown=markdown,
                excerpt=excerpt,
                language=language,
            )
        else:
            entry.title = title
            entry.markdown = markdown
            entry.body_html = body_html
            entry.excerpt = excerpt
        entry.put()
        refreshFeed(language)  # refresh Feed upon save
        memcache.delete(slug, namespace="entry")
        memcache.delete(lang + "_main")  # main page must be refreshed
        self.redirect(entry.url())
Beispiel #13
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")
Beispiel #14
0
def submit(request):
	"""Submit new post view"""
	topic_list = Topic.objects.get_tree()
	selected_topic = ''
	if request.method == 'POST':
		form = StoryForm(request.POST)
		if form.is_valid():
			s = Story(title = form.cleaned_data['title'], text = form.cleaned_data['text'], user = request.user, topic = form.cleaned_data['topic'], url = form.cleaned_data['url'])
			s.slug = slugify(form.cleaned_data['title'])
			s.save()
			ds = DuggedStory(user = request.user, story = s)
			ds.save()
			urlredirect = reverse('stories_slug', args=[s.topic.urlname, s.slug])
			return redirect_to(request, urlredirect)
		else:
			if form.data['topic']:
				try:
					selected_topic = int(form.data['topic'])
				except:
					pass
	else:
		form = StoryForm()
	return render_to_response('stories/submit.html', {'form': form, 'user': request.user, 'topic_list': topic_list, 'selected_topic': selected_topic})
Beispiel #15
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()
Beispiel #16
0
	def __unicode__(self):
		return slugify(self.title)
Beispiel #17
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()
Beispiel #18
0
 def test_slugify(self):
     s=slugify(self.title)
     self.assertTrue(s == "blaldqsdjq_d54_ceuuds_haha_ajskq")
Beispiel #19
0
 def test_slugify(self):
     s = slugify(self.title)
     self.assertTrue(s == "blaldqsdjq_d54_ceuuds_haha_ajskq")