def categories(): print("******* PRINT JSON *************") print(request.json) print() if not request.is_json: return json.dumps({'Result': 'Not Json'}) data = request.get_json() print(data) try: test_data = validate_category(data['DataStructure']) if test_data != []: return json.dumps({ 'Result': 'Metadata Error', 'DataErrors': test_data }) create_category(db, data['CategoryName'], data) return json.dumps({'Result': 'Category Created'}) except KeyError as e: return json.dumps({'Result': 'No %s' % str(e)}) except IntegrityError: return json.dumps({'Result': "CategoryName already exists"}) except Exception as e: return json.dumps({'Result': str(type(e)) + ":" + str(e)}) return json.dumps(['unexpected end'])
def do_POST(self): self._set_headers(201) content_len = int(self.headers.get('content-length', 0)) post_body = self.rfile.read(content_len) # Convert JSON string to a Python dictionary post_body = json.loads(post_body) # Parse the URL (resource, id) = self.parse_url(self.path) # Initialize new resource. Decided to recycle variable. new_resource = None if resource == "register": new_resource = register_user(post_body) if resource == "login": new_resource = login_user(post_body) if resource == "categories": new_resource = create_category(post_body) if resource == "comments": new_resource = create_comment(post_body) if resource == "posts": new_resource = create_post(post_body) if resource == "tags": new_resource = create_tag(post_body) if resource == "postTags": new_resource = create_post_tag(post_body) self.wfile.write(f"{new_resource}".encode())
def do_POST(self): self._set_headers(201) content_len = int(self.headers.get('content-length', 0)) post_body = self.rfile.read(content_len) post_body = json.loads(post_body) (resource, id) = self.parse_url(self.path) new_item = None if resource == "login": new_item = check_user(post_body) elif resource == "register": new_item = create_user(post_body) elif resource == "posts": new_item = create_post(post_body) elif resource == "comments": new_item = create_comment(post_body) elif resource == "tags": new_item = create_tag(post_body) # elif resource == "reactions": # new_item = create_reaction(post_body) elif resource == "subscriptions": new_item = create_subscription(post_body) elif resource == "categories": new_item = create_category(post_body) self.wfile.write(f"{new_item}".encode())
def do_POST(self): self._set_headers(201) content_len = int(self.headers.get('content-length', 0)) post_body = self.rfile.read(content_len) post_body = json.loads(post_body) (resource, id) = self.parse_url(self.path) new_item = None if resource == "register": new_item = register_user(post_body) if resource == "tags": new_item = create_tag(post_body) if resource == "login": new_item = get_users_by_login(post_body['email'], post_body['password']) if resource == "categories": new_item = create_category(post_body) if resource == "posts": new_item = add_post(post_body) self.wfile.write(new_item.encode())
def do_POST(self): self._set_headers(201) content_len = int(self.headers.get('content-length', 0)) post_body = self.rfile.read(content_len) post_body = json.loads(post_body) (resource, id) = self.parse_url(self.path) new_resource = None # Add a new items to the list. if resource == "users": new_resource = create_user(post_body) if resource == "categories": new_resource = create_category(post_body) if resource == "tags": new_resource = create_tag(post_body) if resource == "posts": new_resource = create_post(post_body) if resource == "tagPosts": new_resource = create_tagPost(post_body) if resource == "reactions": new_resource = create_reaction(post_body) if resource == "subscriptions": new_resource = create_subscription(post_body) if resource == "reactionPosts": new_resource = create_reactionPost(post_body) # Encode the new object and send in response self.wfile.write(f"{new_resource}".encode())
def do_POST(self): self._set_headers(201) content_len = int(self.headers.get('content-length', 0)) post_body = self.rfile.read(content_len) post_body = json.loads(post_body) (resource, id) = self.parse_url(self.path) new_creation = None if resource == "postreactions": new_creation = add_reaction(post_body) elif resource == "reactions": new_creation = create_reaction(post_body) elif resource == "posts": new_creation = create_post(post_body) elif resource == "comments": new_creation = create_comment(post_body) elif resource == "users": new_creation = register_new_user(post_body) elif resource == "login": new_creation = existing_user_check(post_body) elif resource == "tags": new_creation = create_tag(post_body) elif resource == "categories": new_creation = create_category(post_body) elif resource == "subscriptions": new_creation = subscribing_to_post(post_body) elif resource == "subscribed": new_creation = check_subscribed(post_body) self.wfile.write(new_creation.encode())
def do_POST(self): # Set response code to 'Created' self._set_headers(201) content_len = int(self.headers.get('content-length', 0)) post_body = self.rfile.read(content_len) post_body = json.loads(post_body) resource, id = self.parse_url(self.path) new_item = None if resource == "login": new_item = get_auth_user(post_body) if resource == "register": new_item = register_user(post_body) if resource == "posts": new_item = create_post(post_body) if resource == "categories": new_item = create_category(post_body) if resource == "comments": new_item = create_comment(post_body) if resource == "tags": new_item = create_tag(post_body) if resource == "postTags": new_item = create_post_tag(post_body) if resource == "subscriptions": new_item = create_subscription(post_body) if resource == "postReaction": new_item = create_postReaction(post_body) self.wfile.write(f"{new_item}".encode())
def do_POST(self): # Set response code to 'Created' self._set_headers(201) content_len = int(self.headers.get('content-length', 0)) post_body = self.rfile.read(content_len) # Convert JSON string to a Python dictionary post_body = json.loads(post_body) # Parse the URL (resource, id) = self.parse_url(self.path) # Initialize new item new_item = None if resource == "login": new_item = get_user_by_email(post_body) if resource == "register": new_item = create_user(post_body) if resource == "posts": if 'tag_id' in post_body: # Not updating post object so no need to put this call in PUT # For now, POST request in Postman only new_item = add_tag_to_post(post_body) else: new_item = create_post(post_body) # pass if resource == "categories": new_item = create_category(post_body) if resource == "tags": new_item = create_tag(post_body) if resource == "comments": new_item = create_comment(post_body) self.wfile.write(f"{new_item}".encode())
def do_POST(self): self._set_headers(201) content_len = int(self.headers.get('content-length', 0)) post_body = self.rfile.read(content_len) post_body = json.loads(post_body) (resource, id) = self.parse_url(self.path) new_comment = None new_category = None new_post = None new_tag = None new_user = None new_login = None if resource == "comments": new_comment = create_comment(post_body) self.wfile.write(f"{new_comment}".encode()) if resource == "categories": new_category = create_category(post_body) self.wfile.write(f"{new_category}".encode()) if resource == "posts": new_post = create_post(post_body) self.wfile.write(f"{new_post}".encode()) if resource == "tags": new_tag = create_tag(post_body) self.wfile.write(f"{new_tag}".encode()) if resource == "register": new_user = create_user(post_body) self.wfile.write(new_user.encode()) if resource == "login": new_login = get_user_by_email_and_password(post_body) self.wfile.write(f"{new_login}".encode())