示例#1
0
def agregarPost():
	if 'username' in login_session:
		if request.method == 'GET':
			return render_template('add-post.html')
		else:
			if request.method == 'POST':
				registro = session.query(User).filter_by(username = login_session['username']).one()
				post=Blog(
						titulo = request.form['titulo'],
						contenido = request.form['contenido'],
						fecha_creacion = datetime.datetime.now(),
						id_user = registro.id)
				
				if 'foto' in request.files: #verifico que haya una img cargada
					file = request.files['foto']
					if file and allowed_file(file.filename): #verifico que tenga el formato permitido
						filename = secure_filename(file.filename)
						file.save(os.path.join(UPLOAD_FOLDER, filename)) #lo guarda en la carpeta de img de posts
						post.foto = filename

				session.add(post)
				session.commit()
				flash('Post creado correctamente', 'success')
				return redirect(url_for('index'))
	else:
		return redirect(url_for('index'))
示例#2
0
    def delete(self, post_id):
        blog = Blog.get_by_id(int(post_id))
        comment = Comment.get_by_id(int(self.request.get("id")))
        cookie_val = self.request.cookies.get("user_id")

        if not self.blog_exists(blog):
            message = "Invalid. The blog page does not exist."
            self.send_response(404, message)
            return
        if not self.comment_exists(comment):
            message = "Invalid. The comment does not exist."
            self.send_response(400, message)
            return
        if not self.is_signed_in(cookie_val):
            message = "Invalid. Must be signed in to edit comment."
            self.send_response(401, message)
            return
        if not self.is_author(cookie_val, comment):
            message = "Invalid. Must be its author to edit this."
            self.send_response(403, message)
            return

        comment.delete()

        message = json.dumps(
            {"success": "The comment has been deleted successfully."})
        self.send_response(200, message)
示例#3
0
    def post(self, post_id):
        title = self.request.get("title")
        content = self.request.get("content")
        cookie_val = self.request.cookies.get("user_id")
        blog = Blog.get_by_id(int(post_id))

        if not blog:
            self.response.set_status(404)
            self.redirect("/blog/not_found")
            return
        if not self.is_signed_in(cookie_val):
            self.response.set_status(401)
            self.redirect("/blog/login")
            return
        if not self.is_author(cookie_val, blog):
            self.response.set_status(403)
            self.redirect("/blog/not_authorized")
            return
        if not title and content:
            error = ("Either title or texts are empty. Please fill both in "
                     "before trying again.")
            self.render("updateBlog.html",
                        title=title,
                        content=content,
                        error=error,
                        signed_in=True)
            return

        blog.title = title
        blog.content = content
        blog_id = blog.put().id()

        self.response.set_status(200)
        self.redirect("/blog/%s" % blog_id)
示例#4
0
    def post(self, post_id):
        cookie_val = self.request.cookies.get("user_id")
        blog = Blog.get_by_id(int(post_id))

        if not self.is_signed_in(cookie_val):
            message = "Not signed in."
            self.send_response(401, message)
            return
        if not self.blog_exists(blog):
            message = "Post doesn't exist."
            self.send_response(404, message)
            return
        if self.is_author(cookie_val, blog):
            message = "Post cannot be liked by creator."
            self.send_response(400, message)
            return

        user_id = cookie_val.split("|")[0]

        # Check if user already liked the post.
        # Remove if id already in a list.
        if (user_id in blog.liked_by):
            self.remove_like(blog, user_id)
        else:
            self.add_like(blog, user_id)
示例#5
0
    def get(self):
        cookie_val = self.request.cookies.get("user_id")

        query = Blog.gql("ORDER BY date_created DESC")
        blogs = query.fetch(limit=10)

        # Determine whether to insert 'Login' or 'Logout' button.
        if (self.is_signed_in(cookie_val)):
            self.render("mainPage.html", blogs=blogs, signed_in=True)
        # Include 'login' button since user is not signed in.
        else:
            self.render("mainPage.html", blogs=blogs)
示例#6
0
    def get(self, post_id):
        cookie_val = self.request.cookies.get("user_id")
        blog = Blog.get_by_id(int(post_id))
        comments = (Comment.all().filter("blog =",
                                         blog.key()).order("-date_created"))

        # Determine whether to insert 'Login' or 'Logout' button.
        if (self.is_signed_in(cookie_val)):
            self.render("readBlog.html",
                        blog=blog,
                        signed_in=True,
                        comments=comments)
        # Insert login button
        else:
            self.render("readBlog.html", blog=blog, comments=comments)
示例#7
0
    def post(self):
        title = self.request.get("title")
        content = self.request.get("content")
        cookie_val = self.request.cookies.get("user_id")

        if not self.is_signed_in(cookie_val):
            self.redirect("/blog/login")
            return
        if not title and content:
            error = ("Either title or content is missing. Please make sure "
                     "both are filled in, and try again.")
            self.render("createBlog.html", error=error)
            return

        # Here, cookie is non-empty.  Its value can be retrieved safely.
        user = User.get_by_id(int(cookie_val.split("|")[0]))

        blog_entry = Blog(title=title,
                          content=content,
                          author=user,
                          number_of_likes=0)
        blog_id = blog_entry.put().id()

        self.redirect("/blog/%s" % blog_id)
示例#8
0
    def get(self, post_id):
        cookie_val = self.request.cookies.get("user_id")
        blog = Blog.get_by_id(int(post_id))

        if not blog:
            self.response.set_status(404)
            self.redirect("/blog/not_found")
            return
        if not self.is_signed_in(cookie_val):
            self.response.set_status(401)
            self.redirect("/blog/login")
            return
        if not self.is_author(cookie_val, blog):
            self.response.set_status(403)
            self.redirect("/blog/not_authorized")
            return

        self.render("deleteBlog.html", blog=blog, signed_in=True)
示例#9
0
def add_blog():
	if request.method == 'POST':
		title = request.form['title']
		text = request.form['text']
		username = session['username']

		user = User.query.filter_by( name=username ).first()

		blog = Blog( title = title, text=text, author = user )
		db.session.add( blog )
		db.session.commit()
		return redirect(url_for('show_blog'))

	elif request.method == 'GET':
		username = session['username']

		return render_template('addblog.html', username= username)

	return redirect( url_for( 'show_blog' ) )
示例#10
0
    def post(self, post_id):
        blog = Blog.get_by_id(int(post_id))
        cookie_val = self.request.cookies.get("user_id")

        if not blog:
            self.response.set_status(404)
            self.redirect("/blog/not_found")
            return
        if not self.is_signed_in(cookie_val):
            self.response.set_status(401)
            self.redirect("/blog/login")
            return
        if not self.is_author(cookie_val, blog):
            self.response.set_status(403)
            self.redirect("/blog/not_authorized")
            return

        blog.delete()

        self.response.set_status(200)
        self.redirect("/blog")
示例#11
0
    def get(self, post_id):
        cookie_val = self.request.cookies.get("user_id")
        blog = Blog.get_by_id(int(post_id))

        # Check if user is authorized to modify the blog post.
        #
        # Also, determine whether to insert login or logout button.
        if not blog:
            self.response.set_status(404)
            self.redirect("/blog/not_found")
            return
        if not self.is_signed_in(cookie_val):
            self.response.set_status(401)
            self.redirect("/blog/login")
            return
        if not self.is_author(cookie_val, blog):
            self.response.set_status(403)
            self.redirect("/blog/not_authorized")
            return

        self.render("updateBlog.html", blog=blog, signed_in=True)
示例#12
0
    def post(self, post_id):
        data = json.loads(self.request.body)
        cookie_val = self.request.cookies.get("user_id")
        blog = Blog.get_by_id(int(post_id))
        title = data["title"]
        content = data["content"]
        user = User.get_by_id(int(cookie_val.split("|")[0]))

        if not self.blog_exists(blog):
            message = "Invalid. The requested page doesn't exist."
            self.send_response(404, message)
            return
        if not self.is_signed_in(cookie_val):
            message = "Invalid. Only signed in User can post comments"
            self.send_response(401, message)
            return
        if not (title and content):
            message = "Invalid. Title and texts must not be empty."
            self.send_response(400, message)
            return

        comment = Comment(title=title, content=content, blog=blog, author=user)
        comment_id = comment.put().id()

        message = json.dumps({
            "success":
            "Comment successfully added to database.",
            "id":
            comment_id,
            "title":
            title,
            "content":
            content,
            "author":
            user.username,
            "date_created":
            comment.date_created.strftime("%B %d %Y %I:%M%p")
        })
        self.send_response(200, message)
示例#13
0
    def put(self, post_id):
        data = json.loads(self.request.body)
        comment = Comment.get_by_id(int(data["id"]))
        blog = Blog.get_by_id(int(post_id))
        cookie_val = self.request.cookies.get("user_id")
        new_title = data["title"]
        new_content = data["content"]

        if not (new_content and new_title):
            message = "Invalid. Both title and comment must not be empty."
            self.send_response(400, message)
            return
        if not self.blog_exists(blog):
            self.send_response(404)
            return
        if not self.comment_exists(comment):
            message = "Invalid. The comment does not exist."
            self.send_response(400, message)
            return
        if not self.is_signed_in(cookie_val):
            message = "Invalid. Must be signed in to edit comments."
            self.send_response(400, message)
            return
        if not self.is_author(cookie_val, comment):
            message = "Invalid. Only its author is allowed to edit."
            self.send_response(400, message)
            return

        comment.title = new_title
        comment.content = new_content
        comment.put()

        message = json.dumps(
            {"success": "The comment has been updated successfully."})
        self.send_response(200, message)
        self.response.set_status(200)
               mail='*****@*****.**',
               sub='best sales in surf',
               msg='qsdjqksldqsdkqsdlkqsdqsd')

session.add(cnt1)
session.add(cnt2)
session.add(cnt3)
session.add(cnt4)
print("Contacts Done :)")

##########################################################"""

post1 = Blog(
    title='Best day with surf hotel fammily',
    body=
    'body="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essenti',
    tags='qqskldqlksdjklqsdj',
    img_off='klqjdlkqjskdqsldk',
    categorie='Psycologue')
post2 = Blog(
    title='Surf time in imsouan',
    body=
    'body="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essenti',
    tags='qqskldqlksdjklqsdj',
    img_off='klqjdlkqjskdqsldk',
    categorie='Psycologue')
post3 = Blog(
    title='Tamraght summer vlog',
    body=
    'body="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essenti',
    tags='qqskldqlksdjklqsdj',