def post(self): ''' Create a new user (if user exists, error response is returned) from sent data (user object at JSON format). ''' if UserManager.getUser(): self.return_failure("User already exists.") else: data = self.get_body_as_dict(expectedFields=["name"]) if data: user = User() user.name = data['name'] user.save() user.key = user._id user.save() self.return_json(user.toJson(), 201) else: self.return_failure( "Data are not correct. User has not been created.", 400)
class NewebeClient(HTTPClient): """ Tornado client wrapper to write requests to Newebe faster. """ def login(self, password): """ Grab authentication cookie from login request. """ response = self.post("login/json/", body='{"password":"******"}' % password) assert response.headers["Set-Cookie"].startswith("password="******"Set-Cookie"] def set_default_user(self, url=ROOT_URL): """ Set to DB default user. This is useful for automatic login. """ self.root_url = url self.user = UserManager.getUser() if self.user: self.user.delete() self.user = User( name="John Doe", password=hashlib.sha224("password").hexdigest(), key="key", authorKey="authorKey", url=url, description="my description", ) self.user.save() def set_default_user_2(self, url=ROOT_URL): """ Set to DB default user. This is useful for automatic login. """ self.root_url = url User._db = db2 self.user = UserManager.getUser() if self.user: self.user.delete() self.user = User( name="Dan Frazer", password=hashlib.sha224("password").hexdigest(), key="key2", authorKey="authorKey2", url=url, description="my description", ) self.user.save() User._db = db def get(self, url): """ Perform a GET request. """ if hasattr(self, "root_url") and self.root_url: url = self.root_url + url request = HTTPRequest(url, validate_cert=False) if hasattr(self, "cookie") and self.cookie: request.headers["Cookie"] = self.cookie return HTTPClient.fetch(self, request) def post(self, url, body): """ Perform a POST request. """ if hasattr(self, "root_url") and self.root_url: url = self.root_url + url request = HTTPRequest(url, method="POST", body=body, validate_cert=False) if hasattr(self, "cookie") and self.cookie: request.headers["Cookie"] = self.cookie return HTTPClient.fetch(self, request) def put(self, url, body): """ Perform a PUT request. """ if hasattr(self, "root_url") and self.root_url: url = self.root_url + url request = HTTPRequest(url, method="PUT", body=body, validate_cert=False) if hasattr(self, "cookie") and self.cookie: request.headers["Cookie"] = self.cookie return HTTPClient.fetch(self, request) def delete(self, url): """ Perform a DELETE request. """ if hasattr(self, "root_url") and self.root_url: url = self.root_url + url request = HTTPRequest(url, method="DELETE", validate_cert=False) if self.cookie: request.headers["Cookie"] = self.cookie return HTTPClient.fetch(self, request) def fetch_document_from_url(self, url): """ Retrieve newebe document from a givent url """ response = self.get(url) assert response.code == 200 assert response.headers["Content-Type"] == "application/json" return json_decode(response.body) def fetch_documents_from_url(self, url): """ Retrieve newebe document list from a givent url """ response = self.get(url) assert response.code == 200 assert_in("application/json", response.headers["Content-Type"]) world.data = json_decode(response.body) return world.data["rows"] def fetch_document(self, path): """ Retrieve document from path located on localhost server. """ return self.fetch_document_from_url(path) def fetch_documents(self, path): """ Retrieve document list from path located on localhost server. """ return self.fetch_documents_from_url(path)