Beispiel #1
0
 def delete(self):
     data = request.get_json()
     title = data['title']
     # Check NULL condition of all fields
     if title == "":
         response = app.response_class(response=json.dumps(
             {"message": "Bad Request"}, indent=4),
                                       status=400,
                                       content_type='application/json')
         return response
     user_details = users_db.get_user_details(
         request.authorization.username)
     user_details = user_details[0]
     article_id = articles_db.get_article_details(user_details[0], title)
     if not article_id:
         # Article not found
         response = app.response_class(response=json.dumps(
             {"message": "Articles Not Found"}, indent=4),
                                       status=404,
                                       content_type='application/json')
         return response
     article_id = article_id[0]
     article_user_id = article_id[1]
     if article_user_id == user_details[0]:
         # Delete an article
         articles_db.delete_article(article_id[0])
         response = app.response_class(response=json.dumps(
             {"message": "OK"}, indent=4),
                                       status=200,
                                       content_type='application/json')
         return response
Beispiel #2
0
 def put(self):
     data = request.get_json()
     title = data['title']
     author = data['author']
     text = data['text']
     # Check NULL condition of all fields
     if text == "" or author == "" or title == "" or text == "":
         response = app.response_class(response=json.dumps(
             {"message": "Bad Request"}, indent=4),
                                       status=400,
                                       content_type='application/json')
         return response
     user_details = users_db.get_user_details(
         request.authorization.username)
     user_details = user_details[0]
     article_id = articles_db.get_article_details(user_details[0], title)
     if not article_id:
         # Check if article exists or not
         response = app.response_class(response=json.dumps(
             {"message": "Not Found"}, indent=4),
                                       status=404,
                                       content_type='application/json')
         return response
     article_id = article_id[0]
     articles_db.edit_article(title, author, text, article_id[0])
     response = app.response_class(response=json.dumps({"message": "OK"},
                                                       indent=4),
                                   status=200,
                                   content_type='application/json')
     return response
Beispiel #3
0
 def post(self):
     data = request.get_json()
     text = data['text']
     author = data['author']
     title = data['title']
     url = data['url']
     # Check NULL condition of all fields
     if text == "" or author == "" or title == "" or url == "":
         response = app.response_class(response=json.dumps(
             {"message": "Bad Request"}, indent=4),
                                       status=400,
                                       content_type='application/json')
         return response
     user_id = users_db.get_user_details(request.authorization.username)
     user_id = user_id[0]
     # Post an article
     articles_db.post_article(user_id[0], text, author, title, url)
     # Get article id
     article_id = articles_db.get_article(title)
     article_id = article_id[0]
     response = Response("article is created",
                         status=201,
                         mimetype='application/json')
     response.headers['location'] = "http://127.0.0.1:5100/articles/" + str(
         article_id[5])
     return response
Beispiel #4
0
 def post(self):
     data = request.get_json()
     username = data['username']
     password = data['password']
     display_name = data['display_name']
     # Check NULL condition of all fields
     if username == "" or password == "" or display_name == "":
         response = app.response_class(response=json.dumps(
             {"message": "Bad Request"}, indent=4),
                                       status=400,
                                       content_type='application/json')
         return response
     user_details = users_db.get_user_details(username)
     if user_details:
         #User already exists
         response = app.response_class(response=json.dumps(
             {"message": "Conflict"}, indent=4),
                                       status=409,
                                       content_type='application/json')
         return response
     else:
         # Create a new user
         users_db.create_user(username, password, display_name)
         response = app.response_class(response=json.dumps(
             {"message": "Created"}, indent=4),
                                       status=201,
                                       content_type='application/json')
         return response
Beispiel #5
0
def verify(username, password):
    passwd = users_db.get_user_details(username)
    if passwd:
        passwd = passwd[0]
        passwd = passwd[2]
        if sha256_crypt.verify(password, passwd):
            return True
        else:
            return False
    else:
        return False
Beispiel #6
0
def verify(username, password):
    global author
    passwd = users_db.get_user_details(username)
    if passwd:
        passwd = passwd[0]
        passwd = passwd[2]
        if sha256_crypt.verify(password, passwd):
            return True
        else:
            author = "Anonymous Coward"
            return False
    else:
        author = "Anonymous Coward"
        return False
Beispiel #7
0
def authorization():
    auth = request.authorization
    if auth:
        user_details = users_db.get_user_details(auth.username)
        if user_details:
            user_details = user_details[0]
            password = users_db.encode_password(auth.password)
            auth = request.authorization
            if auth and auth.username == user_details[1] and user_details[
                    2] == password:
                return True
            else:
                return False
    else:
        return False
Beispiel #8
0
 def get(self):
     username = request.authorization.username
     password = request.authorization.password
     passwd = users_db.get_user_details(username)
     if passwd:
         passwd = passwd[0]
         passwd = passwd[2]
         if sha256_crypt.verify(password, passwd):
             response = app.response_class(response = json.dumps({"message": "OK"}, indent = 4),
                                           status = 200,
                                           content_type = 'application/json')
             return response
         else:
             response = app.response_class(response = json.dumps({"message": "UNAUTHORIZED"}, indent = 4),
                                           status = 401,
                                           content_type = 'application/json')
             return response
     else:
         response = app.response_class(response = json.dumps({"message": "UNAUTHORIZED"}, indent = 4),
                                       status = 401,
                                       content_type = 'application/json')
         return response