def put_detail(self, collection, pk): """Updates whole document.""" collection = self.get_collection(collection) if '_id' in request.json: # we are ignoring id fields of bundle, # because _id field is immutable del request.json['_id'] collection.update({"_id": ObjectId(pk)}, request.json) response.status = 202 return jsonify(request.json)
def authenticate(self): collection = self.connection['users'] username = request.json.get('username') password = request.json.get('password') hasher = hashlib.md5() hasher.update(password) user = collection.find_one({ 'username': username, 'password': hasher.hexdigest(), }) or abort(400, 'Wrong username or password') access_token = str(uuid.uuid4()) self.connection['access_tokens'].insert({ 'access_token': access_token, 'user_id': user.get('id')}) user.pop('password') user.update({'access_token': access_token}) return jsonify(user)
def authenticate(self): collection = self.connection['users'] username = request.json.get('username') password = request.json.get('password') hasher = hashlib.md5() hasher.update(password) user = collection.find_one({ 'username': username, 'password': hasher.hexdigest(), }) or abort(400, 'Wrong username or password') access_token = str(uuid.uuid4()) self.connection['access_tokens'].insert({ 'access_token': access_token, 'user_id': user.get('id') }) user.pop('password') user.update({'access_token': access_token}) return jsonify(user)
def get_list(self, collection): """Returns paginated objects.""" collection = self.get_collection(collection) limit = int_or_default(request.query.limit, 20) offset = int_or_default(request.query.offset, 0) query = self.get_query() cursor = collection.find(query) meta = { "limit": limit, "offset": offset, "total_count": cursor.count(), } objects = cursor.skip(offset).limit(limit) objects = map(self.get_bundler(collection), objects) return jsonify({"meta": meta, "objects": objects})
def register(self): collection = self.connection['users'] password = request.json.get('password') username = request.json.get('username') email = request.json.get('email') if not username or not password: abort(400, 'Please give an username and password') if collection.find_one({'username': username}): abort(400, 'A user with that username already exists') if collection.find_one({'email': email}): abort(400, 'A user with that email already exists') hasher = hashlib.md5() hasher.update(password) response.status = 201 return jsonify({"_id": collection.insert({ 'username': request.json.get('username'), 'email': request.json.get('email'), 'password': hasher.hexdigest() })})
def register(self): collection = self.connection['users'] password = request.json.get('password') username = request.json.get('username') email = request.json.get('email') if not username or not password: abort(400, 'Please give an username and password') if collection.find_one({'username': username}): abort(400, 'A user with that username already exists') if collection.find_one({'email': email}): abort(400, 'A user with that email already exists') hasher = hashlib.md5() hasher.update(password) response.status = 201 return jsonify({ "_id": collection.insert({ 'username': request.json.get('username'), 'email': request.json.get('email'), 'password': hasher.hexdigest() }) })
def error(self, error, message=None): """Returns the error response.""" return jsonify({"error": error.status_code, "message": error.body or message})
def post_list(self, collection): """Creates new document""" collection = self.get_collection(collection) inserted = collection.insert(request.json) response.status = 201 return jsonify({"_id": inserted})
def get_detail(self, collection, pk): """Returns a single document.""" cursor = self.get_collection(collection) data = cursor.find_one({"_id": ObjectId(pk)}) or abort(404) return jsonify(self.get_bundler(cursor)(data))
def error(self, error, message=None): """Returns the error response.""" return jsonify({ "error": error.status_code, "message": error.body or message })