Example #1
0
	def get(self, curs):

		blog_page = memcache.get('main_blog_page')
		
		if blog_page is not None:
			logging.info('cache hit ----------------')
			return blog_page
		else:
			curs1 = Cursor(urlsafe = curs)
			query_forward = BlogModel.query().order(-BlogModel.date_created)
			query_reverse = BlogModel.query().order(BlogModel.date_created)
			
			posts, next_curs, next_more = query_forward.fetch_page(POSTS_PER_PAGE, start_cursor=curs1)
			rev_posts, prev_curs, prev_more = query_reverse.fetch_page(POSTS_PER_PAGE, start_cursor=curs1)
			
			
			if next_curs and next_more:
				next_curs = next_curs.urlsafe()
			else:
				next_curs = ''	

			if curs == None:
				prev_curs = ''	
			elif prev_curs and prev_more:
				prev_curs = prev_curs.urlsafe()
			else:
				prev_curs = 'MainPage'	

			blog_page = render_template('blog/blog_main.html', posts=posts, next_curs=next_curs, prev_curs = prev_curs)
			if not memcache.add('main_blog_page', blog_page, 86400):
				logging.error('Memcache set failed.')
			logging.info('cache miss ----*********---------')	
			return blog_page
Example #2
0
	def get(self,post_key):
		if post_key:
			self.blog_model = BlogModel.get_by_id(post_key)
		if not self.blog_model:
			error_msg  = 'The blog id ' + str(post_key) +' is not valid. Please enter a valid id or return to home page.'
			flash(error_msg)
			return redirect(url_for('error_msg'))
	
		return render_template('blog/blog_single_post.html', post = self.blog_model)
Example #3
0
def manage_blogs(request, blog_id=None):
    if request.method == 'POST':
        form = BlogForm(request.POST)
        if form.is_valid():
            niche = NicheModel.objects.get(id=int(request.POST['niche']))
            newblog = BlogModel(address=request.POST['address'],
                                user=request.POST['user'],
                                password=request.POST['password'],
                                niche=niche)
            newblog.save()
            return HttpResponseRedirect('/manage_blogs/')
    else:
        initial_data = None
        if blog_id:
            try:
                edit_blog = BlogModel.objects.get(pk=blog_id)
                initial_data = edit_blog.get_data()
            except Exception:
                pass
        form = BlogForm(initial_data)
        blogs = BlogModel.objects.all()
        return render_to_response('manage_blogs.html',
            {'blogs': blogs, 'form': form, 'blog_id': blog_id}, context_instance=RequestContext(request))
Example #4
0
    def post(self):
        parser = reqparse.RequestParser();
        parser.add_argument('title', type=str, required=True, help='Title cannot be blank!')
        parser.add_argument('content', type=str, required=True, help='Content cannot be blank!')
        parser.add_argument('author', type=str, required=True, help='Author cannot be blank!')
        args = parser.parse_args()

        blog = BlogModel(
            title=args['title'],
            content=args['content'],
            author=args['anthor'],
            creation_date=datetime.now(utc)
        )
        blog_manager.add_blog(blog)
        return blog, status.HTTP_201_CREATED
Example #5
0
 def get(self, blog_id):
     post = BlogModel.get_by_id(int(blog_id))  # type: BlogModel
     if post:
         user = getUserFromRequest(self.request)  # type: UserModel
         userSignedIn = user is not None  # type: bool
         kwargs = {
             'post': post,
             'userSignedIn': userSignedIn,
             'current_user': user
         }
         if user and post.author.key().id() == user.key().id():
             kwargs['showEdit'] = True
         self.response.write(templater.render_a_post(**kwargs))
     else:
         self.response.write("blog entry not found")
Example #6
0
 def post(self):
     title = self.request.get("title")
     content = self.request.get("content")
     if title and content:
         post = BlogModel(title=title, content=content)
         user = getUserFromRequest(self.request)
         if user:
             post.author = user
             post.put()
             key = int(post.key().id())
             self.redirect("/blog/%d" % key)
         else:
             self.redirect('/signup')
     else:
         self.redirect("/blog/new")
Example #7
0
    def get(self, friend_id):
        length = 0
        friend = UserModel.get_by_id(long(friend_id))
        if friend:
            posts = BlogModel.all()
            posts.filter('author = ', friend)
            posts.order('-modified_time')
            length = posts.count()
            user = getUserFromRequest(self.request)
            if user:
                self.response.write(
                    templater.render_friendprofile(userSignedIn=True,
                                                   posts=posts,
                                                   friend_id=friend_id,
                                                   user=user,
                                                   length=length,
                                                   friend=friend))
            else:
                self.redirect("/")

        else:
            self.response.write("Friend entry not found")
Example #8
0
 def post(self, blog_id):
     title = self.request.get("title")
     content = self.request.get("content")
     post = BlogModel.get_by_id(long(blog_id))  # type:BlogModel
     user = getUserFromRequest(self.request)  # type: UserModel
     if title and content and post.author.key().id() == user.key().id():
         # user is authorised to make changes
         if self.request.get('edit'):
             # update the post contents
             post.content = content
             post.title = title
             post.put()
             self.redirect("/blog/%s" % (blog_id))
         elif self.request.get('delete'):
             # delete post is triggered and got confirmation from user
             post.delete()
         # redirect to user home. If we return user entries, it will list old posts as well
         self.redirect('/welcome')
     elif user:
         # redirect to user blog entries
         self.redirect('/blog/user')
     else:
         self.redirect("/signin")
Example #9
0
 def delete(self):
     return BlogModel.delete_all()
Example #10
0
 def get(self):
     return BlogModel.return_all()
Example #11
0
 def get(self, user_id):
     try:
         return BlogModel.find_by_user_id(user_id)
     except:
         return {'message': 'Something went wrong'}, 500
Example #12
0
    def post(self, blog_id):
        """
        This method is used to handle comment post/edit/delete on a post
        """
        user = getUserFromRequest(self.request)  # type: UserModel
        if user:
            post = BlogModel.get_by_id(long(blog_id))  # type: BlogModel
            if not post:
                # no post found. so redirect to default page.
                self.redirect('/blog/all')
                return
            logging.info(self.request)
            commentBtnAction = self.request.get('commentBtn')  # type:str
            # update/create a new comment
            if commentBtnAction:
                # check if the comment body is valid
                content = self.request.get('content')
                if content:
                    if commentBtnAction == 'comment':
                        # create comment
                        cmnt = CommentModel(author=user,
                                            post=post,
                                            content=content)
                        cmnt.put()
                        # making the page to refresh after putting content; because of db eng consistency problems
                        time.sleep(0.1)
                    elif commentBtnAction.isdigit():
                        # update comment by the id
                        cmnt = CommentModel.get_by_id(
                            long(commentBtnAction))  # type: CommentModel
                        # confirm that the comment is updated by its author
                        if cmnt and user.key() == cmnt.author.key():
                            cmnt.content = content
                            cmnt.put()
                            time.sleep(0.1)
            # delete comment
            elif self.request.get('deleteComment'):
                commentId = self.request.get('deleteComment')  # type:str
                if commentId and commentId.isdigit():
                    cmnt = CommentModel.get_by_id(
                        long(commentId))  # type: CommentModel
                    # confirm that the comment is deleted by its author
                    if cmnt and user.key() == cmnt.author.key():
                        logging.info('cmnt %s' % cmnt)
                        cmnt.delete()
                        time.sleep(0.1)
            # like or dislike posts
            elif user.key().id() != post.author.key().id():
                # author of the post can't like his own
                if self.request.get('likeComment'):
                    # add the like count if the user already didn't liked
                    if user.key().id() not in post.likes:
                        post.likes.append(user.key().id())
                        post.likescount = int(post.likescount) + 1
                        # persist changes
                        post.put()
                        time.sleep(0.1)
                elif self.request.get('dislikeComment'):
                    # reduce the like count and pop user id from list
                    if user.key().id() in post.likes:
                        post.likes.remove(user.key().id())
                        post.likescount = int(post.likescount) - 1
                        # persist changes
                        post.put()
                        time.sleep(0.1)

            self.redirect('/blog/%s' % blog_id)
        else:
            self.redirect('/signin')