def page(context, request): #get mongoDB posts blog_data = BlogData(request, 'page') page = blog_data.get_post_by_url(context.__name__) if page is None: raise HTTPNotFound('notfound').exception return {'cur_page': page[u'shortname'], 'page_title': page[u'title'], 'url': page[u'url'], 'body': page[u'body'] }
def page(context, request): #get mongoDB posts blog_data = BlogData(request, 'page') page = blog_data.get_post_by_url(context.__name__) if page is None: raise HTTPNotFound('notfound').exception return { 'cur_page': page[u'shortname'], 'page_title': page[u'title'], 'url': page[u'url'], 'body': page[u'body'] }
def inject_renderer_globals(event): request = event['request'] #set the static site info settings = request.registry.settings #get the recent entries blog_data = BlogData(request) recent_titles = blog_data.get_recent_posts(10, 1, True) event['recent_titles'] = recent_titles # Build static info from the configuration file event['static_info'] = {'blog_title': settings['site_name'], 'site_motto': settings['site_tag_line']} event['google_analytics'] = request.registry.settings['google_analytics']
def my_view(request): #get mongoDB posts blog_data = BlogData(request) posts = blog_data.get_recent_posts(10, 1) entries = [] for post in posts: postDate = post[u'postDate'].strftime("%B %d %Y") entry = {'title': post[u'title'], 'url': post[u'url'], 'date': postDate, 'author': post[u'author'], 'blog_body': strip_tags(post[u'postText'][:1000]) + '...', 'tags': post[u'tags']} entries.append(entry) return {'cur_page': 'home', 'page_title': 'Welcome to Brett\'s Blog', 'entries': entries}
def pages(context, request): print 'pages' blog_data = BlogData(request, 'page') #now let's handle the list of entries if 'listPages' in request.params: pages = blog_data.get_all_pages(50, 1) entries = [] for page in pages: entry = {'title': page[u'title'], 'url': page[u'url'], 'order': page[u'order'], 'shortname': page[u'shortname'], 'active': page[u'active']} entries.append(entry) return {'entries': entries}
def post(context, request): #get mongoDB posts blog_data = BlogData(request) post = blog_data.get_post_by_url(context.__name__) if post is None: raise HTTPNotFound('notfound').exception postDate = post[u'postDate'].strftime("%B %d %Y") return {'cur_page': '', 'page_title': post[u'title'], 'title': post[u'title'], 'category': post[u'category'], 'author': post[u'author'], 'url': post[u'url'], 'date': postDate, 'blog_body': post[u'postText'], 'tags': post[u'tags']}
def tag_view(context, request): #get mongoDB posts blog_data = BlogData(request) posts = blog_data.get_recent_posts_by_tag(context.__name__, 10, 1) entries = [] for post in posts: postDate = post[u'postDate'].strftime("%B %d %Y") entry = {'title': post[u'title'], 'url': post[u'url'], 'date': postDate, 'author': post[u'author'], 'blog_body': strip_tags(post[u'postText'][:1000]) + '...', 'tags': post[u'tags']} entries.append(entry) if not entries: raise HTTPNotFound('notfound').exception return {'cur_page': 'home', 'page_title': 'Category: ' + context.__name__, 'entries': entries}
def post(context, request): #get mongoDB posts blog_data = BlogData(request) post = blog_data.get_post_by_url(context.__name__) if post is None: raise HTTPNotFound('notfound').exception postDate = post[u'postDate'].strftime("%B %d %Y") return { 'cur_page': '', 'page_title': post[u'title'], 'title': post[u'title'], 'category': post[u'category'], 'author': post[u'author'], 'url': post[u'url'], 'date': postDate, 'blog_body': post[u'postText'], 'tags': post[u'tags'] }
def pages(context, request): print 'pages' blog_data = BlogData(request, 'page') #now let's handle the list of entries if 'listPages' in request.params: pages = blog_data.get_all_pages(50, 1) entries = [] for page in pages: entry = { 'title': page[u'title'], 'url': page[u'url'], 'order': page[u'order'], 'shortname': page[u'shortname'], 'active': page[u'active'] } entries.append(entry) return {'entries': entries}
def my_view(request): #get mongoDB posts blog_data = BlogData(request) posts = blog_data.get_recent_posts(10, 1) entries = [] for post in posts: postDate = post[u'postDate'].strftime("%B %d %Y") entry = { 'title': post[u'title'], 'url': post[u'url'], 'date': postDate, 'author': post[u'author'], 'blog_body': strip_tags(post[u'postText'][:1000]) + '...', 'tags': post[u'tags'] } entries.append(entry) return { 'cur_page': 'home', 'page_title': 'Welcome to Brett\'s Blog', 'entries': entries }
def tag_view(context, request): #get mongoDB posts blog_data = BlogData(request) posts = blog_data.get_recent_posts_by_tag(context.__name__, 10, 1) entries = [] for post in posts: postDate = post[u'postDate'].strftime("%B %d %Y") entry = { 'title': post[u'title'], 'url': post[u'url'], 'date': postDate, 'author': post[u'author'], 'blog_body': strip_tags(post[u'postText'][:1000]) + '...', 'tags': post[u'tags'] } entries.append(entry) if not entries: raise HTTPNotFound('notfound').exception return { 'cur_page': 'home', 'page_title': 'Category: ' + context.__name__, 'entries': entries }
def new_post(context, request): post_data = dict() blog_data = BlogData(request) #This handles the New entry post if 'postBlog' in request.params: if request.params['action_type'] == 'add': for key, value in request.params.items(): if key == 'tags': value = value.split(', ') if key != 'postBlog' or key != 'action_type': post_data[key] = value post_data['author'] = 'Brett Dangerfield' post_data['postDate'] = datetime.datetime.utcnow() #check to see if the post is active if 'active' in post_data.keys(): post_data['active'] = True else: post_data['active'] = False #insert the post into mongo collection = request.db['blogPosts'] try: collection.insert(post_data, safe=True) return {'success': True, 'message': 'Post Saved'} except pymongo.errors.DuplicateKeyError: return {'success': False, 'message': 'Duplicate URL'} #we are editing an existing post elif request.params['action_type'] == 'edit': for key, value in request.params.items(): if key == 'tags': value = value.split(', ') if key != 'postBlog' or key != 'action_type': post_data[key] = value post_data['author'] = 'Brett Dangerfield' post_data['postDate'] = datetime.datetime.utcnow() #check to see if the post is active if 'active' in post_data.keys(): post_data['active'] = True else: post_data['active'] = False #insert the post into mongo collection = request.db['blogPosts'] try: collection.update({'url': post_data['url']}, post_data) return {'success': True, 'message': 'Post Saved'} except pymongo.errors.DuplicateKeyError: return {'success': False, 'message': 'Duplicate URL'} #now let's handle the list of entries elif 'listPosts' in request.params: posts = blog_data.get_all_posts(50, 1) entries = [] for post in posts: postDate = post[u'postDate'].strftime("%B %d %Y") tags = post[u'tags'] if u'tags' in post else '' entry = {'title': post[u'title'], 'url': post[u'url'], 'date': postDate, 'category': post[u'category'], 'tags': tags, 'active': post[u'active']} entries.append(entry) return {'entries': entries} #get a single post for editing elif 'editPost' in request.params: post = blog_data.get_post_by_url(request.params['id']) return {'title': post[u'title'], 'category': post[u'category'], 'author': post[u'author'], 'url': post[u'url'], 'author': post[u'author'], 'blog_body': post[u'postText'], 'active': post[u'active'], 'tags': post[u'tags']}
def delete_post(context, request): blog_data = BlogData(request) blog_data.delete_post(request.params['id']) return {'result': True}
def new_post(context, request): post_data = dict() blog_data = BlogData(request) #This handles the New entry post if 'postBlog' in request.params: if request.params['action_type'] == 'add': for key, value in request.params.items(): if key == 'tags': value = value.split(', ') if key != 'postBlog' or key != 'action_type': post_data[key] = value post_data['author'] = 'Brett Dangerfield' post_data['postDate'] = datetime.datetime.utcnow() #check to see if the post is active if 'active' in post_data.keys(): post_data['active'] = True else: post_data['active'] = False #insert the post into mongo collection = request.db['blogPosts'] try: collection.insert(post_data, safe=True) return {'success': True, 'message': 'Post Saved'} except pymongo.errors.DuplicateKeyError: return {'success': False, 'message': 'Duplicate URL'} #we are editing an existing post elif request.params['action_type'] == 'edit': for key, value in request.params.items(): if key == 'tags': value = value.split(', ') if key != 'postBlog' or key != 'action_type': post_data[key] = value post_data['author'] = 'Brett Dangerfield' post_data['postDate'] = datetime.datetime.utcnow() #check to see if the post is active if 'active' in post_data.keys(): post_data['active'] = True else: post_data['active'] = False #insert the post into mongo collection = request.db['blogPosts'] try: collection.update({'url': post_data['url']}, post_data) return {'success': True, 'message': 'Post Saved'} except pymongo.errors.DuplicateKeyError: return {'success': False, 'message': 'Duplicate URL'} #now let's handle the list of entries elif 'listPosts' in request.params: posts = blog_data.get_all_posts(50, 1) entries = [] for post in posts: postDate = post[u'postDate'].strftime("%B %d %Y") tags = post[u'tags'] if u'tags' in post else '' entry = { 'title': post[u'title'], 'url': post[u'url'], 'date': postDate, 'category': post[u'category'], 'tags': tags, 'active': post[u'active'] } entries.append(entry) return {'entries': entries} #get a single post for editing elif 'editPost' in request.params: post = blog_data.get_post_by_url(request.params['id']) return { 'title': post[u'title'], 'category': post[u'category'], 'author': post[u'author'], 'url': post[u'url'], 'author': post[u'author'], 'blog_body': post[u'postText'], 'active': post[u'active'], 'tags': post[u'tags'] }