Example #1
0
 def __init__(self, request, response):
     self.initialize(request, response)
     if Auth.is_logged_in(request):
         if Auth.check_cookie(request.cookies.get("user_id")):
             self.current_user = Auth.get_current_user(
                 request.cookies.get("user_id")
             )
         else:
             Auth.invalidate_cookie(response)
             self.redirect("/")
Example #2
0
    def post(self):

        username = self.request.get("username")
        password = self.request.get("password")

        try:
            user = Auth.login(username, password)
            # sets the auth cookie
            self.response.headers.add_header(
                'Set-Cookie', 'user_id=' + Auth.make_secure_cookie(user))
            self.redirect("/welcome")
        except Exception as e:
            self.render("login.html", error=e)
Example #3
0
    def post(self):

        username = self.request.get("username")
        password = self.request.get("password")
        password_verify = self.request.get("password_verify")
        email = self.request.get("email")

        try:
            user = Auth.signup(username, password, password_verify, email)
            # sets the auth cookie
            self.response.headers.add_header(
                'Set-Cookie', 'user_id=' + Auth.make_secure_cookie(user))
            self.redirect("/welcome")

        except Exception as e:
            self.render("signup.html", username=username, email=email, error=e)
    def post(self, post_id):

        if not Auth.is_logged_in(self.request):
            self.redirect("/login")
        else:
            current_user = self.current_user

            post = Post.by_id(int(post_id))

            # verify if post exists
            if post is not None:

                # verify if this post matches user logged in
                if post.author.key().id() == current_user.key().id():

                    title = self.request.get("title")
                    text = self.request.get("content")

                    if title and text:
                        post = Post.by_id(int(post_id))
                        post.title = title
                        post.content = text
                        post.put()
                        self.redirect("/post/" + str(post.key().id()))
                    else:
                        error = "Title and Text must be provided" \
                                " to submit an article!"
                        self.render("newpost.html", error=error)

                else:  # redirect the user to the view
                    self.redirect("/post/" + str(post.key().id()))

            else:  # redirect the user to the view
                self.redirect("/not-found")
Example #5
0
    def get(self, comment_id):

        if not Auth.is_logged_in(self.request):  # checks if user is logged in
            self.response.content_type = 'application/json'
            response_obj = {
                'type': 'error',
                'message': 'You must be logged in to do that!'
            }
            self.response.write(json.encode(response_obj))

        else:

            comment = Comment.by_id(int(comment_id))

            # check if comment belongs to current user
            if comment.author.key().id() == self.current_user.key().id():
                self.response.content_type = 'application/json'
                response_obj = {
                    'type': 'success',
                    'comment': {
                        "id": comment.key().id(),
                        "text": comment.text,
                    }
                }
                self.response.write(json.encode(response_obj))
            else:
                self.response.content_type = 'application/json'
                response_obj = {
                    'type': 'error',
                    'message': "You can only edit your own comments. ;D"
                }
                self.response.write(json.encode(response_obj))
Example #6
0
    def get(self):
        if Auth.is_logged_in(self.request):

            posts = Post.by_author(self.current_user)

            self.render("welcome.html", posts=posts)
        else:
            self.redirect("/login")
Example #7
0
    def post(self):

        if not Auth.is_logged_in(self.request):
            self.response.content_type = 'application/json'
            response_obj = {
                'type': 'error',
                'message': 'You must be logged in to comment on posts!'
            }
            self.response.write(json.encode(response_obj))
        else:
            current_user = self.current_user
            post = Post.by_id(int(self.request.get("pid")))
            text = self.request.get("text")
            comment_id = self.request.get("comment_id")

            if text:

                is_edit = False

                # if is a edit to comment
                if comment_id:
                    comment = Comment.by_id(int(comment_id))
                    comment.text = text
                    is_edit = True
                else:
                    comment = Comment(author=current_user,
                                      post=post,
                                      text=text)

                comment.put()

                self.response.content_type = 'application/json'
                response_obj = {
                    'type': 'success',
                    'message': 'Comment created!',
                    'editing': is_edit,
                    'comment': {
                        "id": comment.key().id(),
                        "author": current_user.username,
                        "text": text,
                        "time": comment.created.strftime("%d. %B %Y")
                    }
                }
                self.response.write(json.encode(response_obj))
            else:
                self.response.content_type = 'application/json'
                response_obj = {
                    'type': 'error',
                    'message': 'Comment not created!'
                }
                self.response.write(json.encode(response_obj))
Example #8
0
    def post(self):

        if not Auth.is_logged_in(self.request):
            self.response.content_type = 'application/json'
            response_obj = {
                'type': 'error',
                'message': 'You must be logged in to like posts!'
            }
            self.response.write(json.encode(response_obj))
        else:

            current_user = self.current_user
            post = Post.by_id(int(self.request.get("pid")))

            # checks if user already liked this post, if returns > 0
            user_liked = Like.check_user_liked(current_user, post)

            # checks if this post belongs to the current_user
            if post.author.key().id == current_user.key().id:
                self.response.content_type = 'application/json'
                response_obj = {
                    'type': 'error',
                    'message': 'You can`t like you own posts :('
                }
                self.response.write(json.encode(response_obj))
            else:
                if user_liked > 0:
                    Like.unlike(current_user, post)
                    self.response.content_type = 'application/json'
                    response_obj = {
                        'type': 'success',
                        'message': 'Post unliked!',
                        'action': 'unlike'
                    }
                    self.response.write(json.encode(response_obj))
                else:
                    like = Like(author=current_user, post=post)
                    like.put()

                    self.response.content_type = 'application/json'
                    response_obj = {
                        'type': 'success',
                        'message': 'Post liked!',
                        'action': 'like'
                    }
                    self.response.write(json.encode(response_obj))
async def frontend(request):
    if request.content_length != 0:
        result = None
        try:
            db = request.app['db']
            jsn = await request.json()
            if jsn['type'] == 'auth':  # запрос авторизации
                auth = Auth(jsn, db)
                return await auth.check()
            elif jsn['type'] == 'save':
                save_settings = SaveSettings(jsn, db)
                return await save_settings.save()
        except Exception as e:
            # await (MetricsFactory(db)).inc(MetricsConst.Frontend.error)
            return web.json_response(str(e), status=400)
    else:  # пустой запрос
        # await (MetricsFactory(db)).inc(MetricsConst.Frontend.error)
        return web.json_response({"error": {
            "message": "Empty request"
        }},
                                 status=204)
Example #10
0
 def get(self):
     if Auth.is_logged_in(self.request):
         self.render("profile.html")
     else:
         self.redirect("/login")